2#ifndef _SMatrix_DEFINED_
3#define _SMatrix_DEFINED_
6#include <interface/SRender-i.h>
7#include <interface/SMatrix-i.h>
9#define SToBool(cond) ((cond) != 0)
132 STDMETHOD_(IxForm *, Data)(THIS) SCONST OVERRIDE;
137 STDMETHOD_(
void, reset)(THIS) OVERRIDE;
142 STDMETHOD_(
void, setIdentity)(THIS) OVERRIDE;
148 STDMETHOD_(BOOL, isIdentity)(THIS) SCONST OVERRIDE;
155 STDMETHOD_(
void, setTranslate)(THIS_
float dx,
float dy) OVERRIDE;
162 STDMETHOD_(
void, setScale)(THIS_
float sx,
float sy) OVERRIDE;
171 STDMETHOD_(
void, setScale2)(THIS_
float sx,
float sy,
float px,
float py) OVERRIDE;
177 STDMETHOD_(
void, setRotate)(THIS_
float degrees) OVERRIDE;
185 STDMETHOD_(
void, setRotate2)(THIS_
float degrees,
float px,
float py) OVERRIDE;
192 STDMETHOD_(
void, setSkew)(THIS_
float kx,
float ky) OVERRIDE;
201 STDMETHOD_(
void, setSkew2)(THIS_
float kx,
float ky,
float px,
float py) OVERRIDE;
227 if (fTypeMask & kUnknown_Mask)
229 fTypeMask = this->computeTypeMask();
251 if (fTypeMask & kUnknown_Mask)
253 fTypeMask = this->computeTypeMask();
255 return (fTypeMask & kRectStaysRect_Mask) != 0;
282 bool isSimilarity(
float tol = SK_ScalarNearlyZero)
const;
290 bool preservesRightAngles(
float tol = SK_ScalarNearlyZero)
const;
313 SASSERT((
unsigned)index < 9);
322 float get(
int index)
const
324 SASSERT((
unsigned)index < 9);
335 SASSERT((
unsigned)index < 9);
336 this->setTypeMask(kUnknown_Mask);
345 void set(
int index,
float value)
347 SASSERT((
unsigned)index < 9);
349 this->setTypeMask(kUnknown_Mask);
357 void setMatrix(
const float data[9],
int matType = kUnknown_Mask);
374 bool setIDiv(
int divx,
int divy);
383 void setSinCos(
float sinValue,
float cosValue,
float px,
float py);
390 void setSinCos(
float sinValue,
float cosValue);
404 void preTranslate(
float dx,
float dy);
411 void preTranslate(
int dx,
int dy);
420 void preScale(
float sx,
float sy,
float px,
float py);
427 void preScale(
float sx,
float sy);
435 void preRotate(
float degrees,
float px,
float py);
441 void preRotate(
float degrees);
450 void preSkew(
float kx,
float ky,
float px,
float py);
457 void preSkew(
float kx,
float ky);
463 void preConcat(
const SMatrix &other);
470 void postTranslate(
float dx,
float dy);
477 void postTranslate(
int dx,
int dy);
486 void postScale(
float sx,
float sy,
float px,
float py);
493 void postScale(
float sx,
float sy);
501 bool postIDiv(
int divx,
int divy);
509 void postRotate(
float degrees,
float px,
float py);
515 void postRotate(
float degrees);
524 void postSkew(
float kx,
float ky,
float px,
float py);
531 void postSkew(
float kx,
float ky);
537 void postConcat(
const SMatrix &other);
581 bool setRectToRect(
const SRect &src,
const SRect &dst, ScaleToFit stf);
591 bool setPolyToPoly(
const SPoint src[],
const SPoint dst[],
int count);
611 return this->invertNonIdentity(inverse);
618 static void SetAffineIdentity(
float affine[6]);
627 bool asAffine(
float affine[6])
const;
637 void mapPoints(SPoint dst[],
const SPoint src[],
int count)
const;
660 SASSERT(stride >=
sizeof(SPoint));
661 SASSERT(0 == stride %
sizeof(
float));
662 for (
int i = 0; i < count; ++i)
665 pts = (SPoint *)((intptr_t)pts + stride);
678 SASSERT(stride >=
sizeof(SPoint));
679 SASSERT(0 == stride %
sizeof(
float));
680 for (
int i = 0; i < count; ++i)
683 src = (SPoint *)((intptr_t)src + stride);
684 dst = (SPoint *)((intptr_t)dst + stride);
697 void mapHomogeneousPoints(
float dst[],
const float src[],
int count)
const;
705 void mapXY(
float x,
float y, SPoint *result)
const
719 void mapVectors(SVector2D dst[],
const SVector2D src[],
int count)
const;
741 bool mapRect(SRect *dst,
const SRect &src)
const;
752 return this->
mapRect(rect, *rect);
776 float mapRadius(
float radius)
const;
791 SASSERT((mask & ~kAllMasks) == 0);
792 return gMapXYProcs[mask & kAllMasks];
817 SASSERT((mask & ~kAllMasks) == 0);
818 return gMapPtsProcs[mask & kAllMasks];
849 kMaxFlattenSize = 9 *
sizeof(float) +
sizeof(uint32_t)
857 float getMinScale()
const;
864 float getMaxScale()
const;
873 bool getMinMaxScales(
float scaleFactors[2])
const;
879 static const SMatrix &I();
886 static const SMatrix &InvalidMatrix();
907 this->setTypeMask(kUnknown_Mask);
919 kRectStaysRect_Mask = 0x10,
924 kOnlyPerspectiveValid_Mask = 0x40,
926 kUnknown_Mask = 0x80,
928 kORableMasks = kTranslate_Mask | kScale_Mask | kAffine_Mask | kPerspective_Mask,
930 kAllMasks = kTranslate_Mask | kScale_Mask | kAffine_Mask | kPerspective_Mask | kRectStaysRect_Mask
933 mutable uint32_t fTypeMask;
935 uint8_t computeTypeMask()
const;
936 uint8_t computePerspectiveTypeMask()
const;
938 void setTypeMask(
int mask);
940 void orTypeMask(
int mask);
942 void clearTypeMask(
int mask)
945 SASSERT((mask & kAllMasks) == mask);
946 fTypeMask = fTypeMask & ~mask;
949 TypeMask getPerspectiveTypeMaskOnly()
const
951 if ((fTypeMask & kUnknown_Mask) && !(fTypeMask & kOnlyPerspectiveValid_Mask))
953 fTypeMask = this->computePerspectiveTypeMask();
955 return (TypeMask)(fTypeMask & 0xF);
961 bool isTriviallyIdentity()
const
963 if (fTypeMask & kUnknown_Mask)
967 return ((fTypeMask & 0xF) == 0);
970 bool invertNonIdentity(SMatrix *inverse)
const;
972 static bool Poly2Proc(
const SPoint[], SMatrix *,
const SPoint &scale);
973 static bool Poly3Proc(
const SPoint[], SMatrix *,
const SPoint &scale);
974 static bool Poly4Proc(
const SPoint[], SMatrix *,
const SPoint &scale);
976 static void Identity_xy(
const SMatrix &,
float,
float, SPoint *);
977 static void Trans_xy(
const SMatrix &,
float,
float, SPoint *);
978 static void Scale_xy(
const SMatrix &,
float,
float, SPoint *);
979 static void ScaleTrans_xy(
const SMatrix &,
float,
float, SPoint *);
980 static void Rot_xy(
const SMatrix &,
float,
float, SPoint *);
981 static void RotTrans_xy(
const SMatrix &,
float,
float, SPoint *);
982 static void Persp_xy(
const SMatrix &,
float,
float, SPoint *);
984 static const MapXYProc gMapXYProcs[];
986 static void Identity_pts(
const SMatrix &, SPoint[],
const SPoint[],
int);
987 static void Trans_pts(
const SMatrix &, SPoint dst[],
const SPoint[],
int);
988 static void Scale_pts(
const SMatrix &, SPoint dst[],
const SPoint[],
int);
989 static void ScaleTrans_pts(
const SMatrix &, SPoint dst[],
const SPoint[],
int count);
990 static void Rot_pts(
const SMatrix &, SPoint dst[],
const SPoint[],
int);
991 static void RotTrans_pts(
const SMatrix &, SPoint dst[],
const SPoint[],
int count);
992 static void Persp_pts(
const SMatrix &, SPoint dst[],
const SPoint[],
int);
993 static const MapPtsProc gMapPtsProcs[];
995 friend class SkPerspIter;
998 static float SFloatSinCos(
float radians,
float *cosValue);
The SMatrix class holds a 3x3 matrix for transforming coordinates. SMatrix does not have a constructo...
float get(int index) const
Accesses the matrix element at the specified index.
void postRotate(float degrees, float px, float py)
Post-concats the matrix with the specified rotation.
void mapPointsWithStride(SPoint dst[], SPoint src[], size_t stride, int count) const
Like mapPoints but with custom byte stride between the points.
bool invert(SMatrix *inverse) const
If this matrix can be inverted, return true and if inverse is not null, set inverse to be the inverse...
void postTranslate(float dx, float dy)
Post-concats the matrix with the specified translation.
float & operator[](int index)
Accesses the matrix element at the specified index.
void mapRectToQuad(SPoint dst[4], const SRect &rect) const
Apply this matrix to the src rectangle, and write the four transformed points into dst....
void mapPoints(SPoint dst[], const SPoint src[], int count) const
Apply this matrix to the array of points specified by src, and write the transformed points into the ...
bool hasPerspective() const
Checks if the matrix contains perspective elements.
SMatrix & rotate(float deg)
Post-concats the matrix with a rotation.
static MapPtsProc GetMapPtsProc(TypeMask mask)
Get the appropriate MapPtsProc for the given type mask.
bool mapRect(SRect *dst, const SRect &src) const
Apply this matrix to the src rectangle, and write the transformed rectangle into dst....
void reset() OVERRIDE
Resets the matrix to the identity matrix.
void mapVectors(SVector2D vecs[], int count) const
Apply this matrix to the array of vectors specified by src, and write the transformed vectors into th...
bool isScaleTranslate() const
Checks if the matrix contains only scale and translation.
bool mapRect(SRect *rect) const
Apply this matrix to the rectangle, and write the transformed rectangle back into it....
SMatrix & shear(float sh, float sv)
Post-concats the matrix with a shear.
TypeMask getType() const
Returns a bitfield describing the transformations the matrix may perform. The bitfield is computed co...
friend bool operator!=(const SMatrix &a, const SMatrix &b)
Inequality operator.
MapPtsProc getMapPtsProc() const
Get the appropriate MapPtsProc for this matrix.
void setConcat(const SMatrix &a, const SMatrix &b)
Sets the matrix to the concatenation of the two specified matrices.
bool cheapEqualTo(const SMatrix &m) const
Efficient comparison of two matrices. It distinguishes between zero and negative zero....
bool rectStaysRect() const
Returns true if the matrix will map a rectangle to another rectangle. This can be true if the matrix ...
ScaleToFit
Enum for scale-to-fit options.
bool preservesAxisAlignment() const
Alias for rectStaysRect().
void setTranslate(const SVector2D &v)
Sets the matrix to a translation.
SMatrix & scale(float sx, float sy)
Post-concats the matrix with a scale.
BOOL isIdentity() SCONST OVERRIDE
Checks if the matrix is the identity matrix.
void postSkew(float kx, float ky, float px, float py)
Post-concats the matrix with the specified skew.
void mapPointsWithStride(SPoint pts[], size_t stride, int count) const
Like mapPoints but with custom byte stride between the points. Stride should be a multiple of sizeof(...
void setTranslate(float dx, float dy) OVERRIDE
Sets the matrix to a translation.
SMatrix & translate(float dx, float dy)
Post-concats the matrix with a translation.
static MapXYProc GetMapXYProc(TypeMask mask)
Get the appropriate MapXYProc for the given type mask.
MapXYProc getMapXYProc() const
Get the appropriate MapXYProc for this matrix.
void postScale(float sx, float sy, float px, float py)
Post-concats the matrix with the specified scale.
float operator[](int index) const
Accesses the matrix element at the specified index.
TypeMask
Enum of bit fields for the mask returned by getType(). Use this to identify the complexity of the mat...
@ kAffine_Mask
Set if the matrix skews or rotates.
@ kPerspective_Mask
Set if the matrix is in perspective.
@ kScale_Mask
Set if the matrix has X or Y scale.
@ kIdentity_Mask
Set if the matrix is identity.
@ kTranslate_Mask
Set if the matrix has translation.
void(* MapXYProc)(const SMatrix &mat, float x, float y, SPoint *result)
Function pointer type for mapping a single point (x, y) to a transformed point.
void mapVectors(SVector2D dst[], const SVector2D src[], int count) const
Apply this matrix to the array of vectors specified by src, and write the transformed vectors into th...
void(* MapPtsProc)(const SMatrix &mat, SPoint dst[], const SPoint src[], int count)
Function pointer type for mapping an array of points.
SMatrix()
Default constructor, initializes the matrix to identity.
void dirtyMatrixTypeCache()
Testing routine; the matrix's type cache should never need to be manually invalidated during normal u...
void set(int index, float value)
Sets the matrix element at the specified index.
static SMatrix Concat(const SMatrix &a, const SMatrix &b)
Return the concatenation of two matrices, a * b.
void mapPoints(SPoint pts[], int count) const
Apply this matrix to the array of points, overwriting it with the transformed values....
void mapXY(float x, float y, SPoint *result) const
Apply this matrix to the point (x, y) and store the result in *result.