Skip to content

Commit

Permalink
Update Matrix APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Sep 8, 2024
1 parent a319642 commit ada0424
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 142 deletions.
117 changes: 42 additions & 75 deletions include/lunasvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,98 +297,65 @@ class LUNASVG_API Matrix {
Matrix(const Transform& transform);

/**
* @brief Multiplies this matrix with another matrix.
* @brief Pre-multiplies this matrix with another matrix.
* @param matrix The matrix to multiply with.
* @return A reference to this matrix after multiplication.
* @return A new matrix that is the result of the multiplication.
*/
Matrix& multiply(const Matrix& matrix);
Matrix operator*(const Matrix& matrix) const;

/**
* @brief Rotates this matrix by the specified angle.
* @param angle The rotation angle in degrees.
* @return A reference to this matrix after rotation.
*/
Matrix& rotate(float angle);

/**
* @brief Rotates this matrix by the specified angle around a point.
* @param angle The rotation angle in degrees.
* @param cx The x-coordinate of the center of rotation.
* @param cy The y-coordinate of the center of rotation.
* @return A reference to this matrix after rotation.
* @brief Pre-multiplies this matrix with another matrix in-place.
* @param matrix The matrix to multiply with.
* @return A reference to this matrix after multiplication.
*/
Matrix& rotate(float angle, float cx, float cy);
Matrix& operator*=(const Matrix& matrix);

/**
* @brief Scales this matrix by the specified factors.
* @param sx The horizontal scaling factor.
* @param sy The vertical scaling factor.
* @return A reference to this matrix after scaling.
*/
Matrix& scale(float sx, float sy);

/**
* @brief Shears this matrix by the specified factors.
* @param shx The horizontal shearing factor.
* @param shy The vertical shearing factor.
* @return A reference to this matrix after shearing.
* @brief Pre-multiplies this matrix with another matrix.
* @param matrix The matrix to multiply with.
* @return A reference to this matrix after multiplication.
*/
Matrix& shear(float shx, float shy);
Matrix& multiply(const Matrix& matrix);

/**
* @brief Translates this matrix by the specified offsets.
* @brief Pre-translates this matrix by the specified offsets.
* @param tx The horizontal translation offset.
* @param ty The vertical translation offset.
* @return A reference to this matrix after translation.
*/
Matrix& translate(float tx, float ty);

/**
* @brief Post-multiplies this matrix with another matrix.
* @param matrix The matrix to post-multiply with.
* @return A reference to this matrix after post-multiplication.
* @brief Pre-scales this matrix by the specified factors.
* @param sx The horizontal scaling factor.
* @param sy The vertical scaling factor.
* @return A reference to this matrix after scaling.
*/
Matrix& postMultiply(const Matrix& matrix);
Matrix& scale(float sx, float sy);

/**
* @brief Post-rotates this matrix by the specified angle.
* @brief Pre-rotates this matrix by the specified angle.
* @param angle The rotation angle in degrees.
* @return A reference to this matrix after post-rotation.
* @return A reference to this matrix after rotation.
*/
Matrix& postRotate(float angle);
Matrix& rotate(float angle);

/**
* @brief Post-rotates this matrix by the specified angle around a point.
* @brief Pre-rotates this matrix by the specified angle around a point.
* @param angle The rotation angle in degrees.
* @param cx The x-coordinate of the center of rotation.
* @param cy The y-coordinate of the center of rotation.
* @return A reference to this matrix after post-rotation.
*/
Matrix& postRotate(float angle, float cx, float cy);

/**
* @brief Post-scales this matrix by the specified factors.
* @param sx The horizontal scaling factor.
* @param sy The vertical scaling factor.
* @return A reference to this matrix after post-scaling.
* @return A reference to this matrix after rotation.
*/
Matrix& postScale(float sx, float sy);
Matrix& rotate(float angle, float cx, float cy);

/**
* @brief Post-shears this matrix by the specified factors.
* @brief Pre-shears this matrix by the specified factors.
* @param shx The horizontal shearing factor.
* @param shy The vertical shearing factor.
* @return A reference to this matrix after post-shearing.
*/
Matrix& postShear(float shx, float shy);

/**
* @brief Post-translates this matrix by the specified offsets.
* @param tx The horizontal translation offset.
* @param ty The vertical translation offset.
* @return A reference to this matrix after post-translation.
* @return A reference to this matrix after shearing.
*/
Matrix& postTranslate(float tx, float ty);
Matrix& shear(float shx, float shy);

/**
* @brief Inverts this matrix.
Expand All @@ -407,6 +374,22 @@ class LUNASVG_API Matrix {
*/
void reset();

/**
* @brief Creates a translation matrix with the specified offsets.
* @param tx The horizontal translation offset.
* @param ty The vertical translation offset.
* @return A new translation matrix.
*/
static Matrix translated(float tx, float ty);

/**
* @brief Creates a scaling matrix with the specified factors.
* @param sx The horizontal scaling factor.
* @param sy The vertical scaling factor.
* @return A new scaling matrix.
*/
static Matrix scaled(float sx, float sy);

/**
* @brief Creates a rotation matrix with the specified angle.
* @param angle The rotation angle in degrees.
Expand All @@ -423,14 +406,6 @@ class LUNASVG_API Matrix {
*/
static Matrix rotated(float angle, float cx, float cy);

/**
* @brief Creates a scaling matrix with the specified factors.
* @param sx The horizontal scaling factor.
* @param sy The vertical scaling factor.
* @return A new scaling matrix.
*/
static Matrix scaled(float sx, float sy);

/**
* @brief Creates a shearing matrix with the specified factors.
* @param shx The horizontal shearing factor.
Expand All @@ -439,14 +414,6 @@ class LUNASVG_API Matrix {
*/
static Matrix sheared(float shx, float shy);

/**
* @brief Creates a translation matrix with the specified offsets.
* @param tx The horizontal translation offset.
* @param ty The vertical translation offset.
* @return A new translation matrix.
*/
static Matrix translated(float tx, float ty);

float a{1}; ///< The horizontal scaling factor.
float b{0}; ///< The vertical shearing factor.
float c{0}; ///< The horizontal shearing factor.
Expand Down
38 changes: 19 additions & 19 deletions source/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@ Transform& Transform::operator*=(const Transform& transform)

Transform& Transform::multiply(const Transform& transform)
{
return (*this = *this * transform);
return (*this *= transform);
}

Transform& Transform::translate(float tx, float ty)
{
return multiply(translated(tx, ty));
}

Transform& Transform::scale(float sx, float sy)
{
return multiply(scaled(sx, sy));
}

Transform& Transform::rotate(float angle)
Expand All @@ -63,24 +73,24 @@ Transform& Transform::rotate(float angle, float cx, float cy)
return multiply(rotated(angle, cx, cy));
}

Transform& Transform::scale(float sx, float sy)
Transform& Transform::shear(float shx, float shy)
{
return multiply(scaled(sx, sy));
return multiply(sheared(shx, shy));
}

Transform& Transform::shear(float shx, float shy)
Transform& Transform::postMultiply(const Transform& transform)
{
return multiply(sheared(shx, shy));
return (*this = transform * *this);
}

Transform& Transform::translate(float tx, float ty)
Transform& Transform::postTranslate(float tx, float ty)
{
return multiply(translated(tx, ty));
return postMultiply(translated(tx, ty));
}

Transform& Transform::postMultiply(const Transform& transform)
Transform& Transform::postScale(float sx, float sy)
{
return (*this = transform * *this);
return postMultiply(scaled(sx, sy));
}

Transform& Transform::postRotate(float angle)
Expand All @@ -93,21 +103,11 @@ Transform& Transform::postRotate(float angle, float cx, float cy)
return postMultiply(rotated(angle, cx, cy));
}

Transform& Transform::postScale(float sx, float sy)
{
return postMultiply(scaled(sx, sy));
}

Transform& Transform::postShear(float shx, float shy)
{
return postMultiply(sheared(shx, shy));
}

Transform& Transform::postTranslate(float tx, float ty)
{
return postMultiply(translated(tx, ty));
}

Transform Transform::inverse() const
{
plutovg_matrix_t inverse;
Expand Down
12 changes: 6 additions & 6 deletions source/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,18 @@ class Transform {
Transform& operator*=(const Transform& transform);

Transform& multiply(const Transform& transform);
Transform& translate(float tx, float ty);
Transform& scale(float sx, float sy);
Transform& rotate(float angle);
Transform& rotate(float angle, float cx, float cy);
Transform& scale(float sx, float sy);
Transform& shear(float shx, float shy);
Transform& translate(float tx, float ty);

Transform& postMultiply(const Transform& transform);
Transform& postTranslate(float tx, float ty);
Transform& postScale(float sx, float sy);
Transform& postRotate(float angle);
Transform& postRotate(float angle, float cx, float cy);
Transform& postScale(float sx, float sy);
Transform& postShear(float shx, float shy);
Transform& postTranslate(float tx, float ty);

Transform inverse() const;
Transform& invert();
Expand All @@ -308,11 +308,11 @@ class Transform {

bool parse(const char* data, size_t length);

static Transform translated(float tx, float ty);
static Transform scaled(float sx, float sy);
static Transform rotated(float angle);
static Transform rotated(float angle, float cx, float cy);
static Transform scaled(float sx, float sy);
static Transform sheared(float shx, float shy);
static Transform translated(float tx, float ty);

static const Transform Identity;

Expand Down
Loading

0 comments on commit ada0424

Please sign in to comment.