1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/*
* Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved.
* 2010 Dirk Schulze <krit@webkit.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef AffineTransform_h
#define AffineTransform_h
#include "platform/transforms/TransformationMatrix.h"
#include "wtf/Allocator.h"
#include <string.h> // for memcpy
namespace blink {
class FloatPoint;
class FloatQuad;
class FloatRect;
class IntPoint;
class IntRect;
class TransformationMatrix;
#define IDENTITY_TRANSFORM \
{ 1, 0, 0, 1, 0, 0 }
class PLATFORM_EXPORT AffineTransform {
DISALLOW_NEW();
public:
typedef double Transform[6];
AffineTransform();
AffineTransform(double a, double b, double c, double d, double e, double f);
AffineTransform(const Transform transform) { setMatrix(transform); }
void setMatrix(double a, double b, double c, double d, double e, double f);
void setTransform(const AffineTransform& other) {
setMatrix(other.m_transform);
}
void map(double x, double y, double& x2, double& y2) const;
// Rounds the mapped point to the nearest integer value.
IntPoint mapPoint(const IntPoint&) const;
FloatPoint mapPoint(const FloatPoint&) const;
IntSize mapSize(const IntSize&) const;
FloatSize mapSize(const FloatSize&) const;
// Rounds the resulting mapped rectangle out. This is helpful for bounding
// box computations but may not be what is wanted in other contexts.
IntRect mapRect(const IntRect&) const;
FloatRect mapRect(const FloatRect&) const;
FloatQuad mapQuad(const FloatQuad&) const;
bool isIdentity() const;
double a() const { return m_transform[0]; }
void setA(double a) { m_transform[0] = a; }
double b() const { return m_transform[1]; }
void setB(double b) { m_transform[1] = b; }
double c() const { return m_transform[2]; }
void setC(double c) { m_transform[2] = c; }
double d() const { return m_transform[3]; }
void setD(double d) { m_transform[3] = d; }
double e() const { return m_transform[4]; }
void setE(double e) { m_transform[4] = e; }
double f() const { return m_transform[5]; }
void setF(double f) { m_transform[5] = f; }
void makeIdentity();
// this' = this * other
AffineTransform& multiply(const AffineTransform& other);
// this' = other * this
AffineTransform& preMultiply(const AffineTransform& other);
AffineTransform& scale(double);
AffineTransform& scale(double sx, double sy);
AffineTransform& scaleNonUniform(double sx, double sy);
AffineTransform& rotate(double a);
AffineTransform& rotateRadians(double a);
AffineTransform& rotateFromVector(double x, double y);
AffineTransform& translate(double tx, double ty);
AffineTransform& shear(double sx, double sy);
AffineTransform& flipX();
AffineTransform& flipY();
AffineTransform& skew(double angleX, double angleY);
AffineTransform& skewX(double angle);
AffineTransform& skewY(double angle);
double xScaleSquared() const;
double xScale() const;
double yScaleSquared() const;
double yScale() const;
double det() const;
bool isInvertible() const;
AffineTransform inverse() const;
TransformationMatrix toTransformationMatrix() const;
bool isIdentityOrTranslation() const {
return m_transform[0] == 1 && m_transform[1] == 0 && m_transform[2] == 0 &&
m_transform[3] == 1;
}
bool isIdentityOrTranslationOrFlipped() const {
return m_transform[0] == 1 && m_transform[1] == 0 && m_transform[2] == 0 &&
(m_transform[3] == 1 || m_transform[3] == -1);
}
bool preservesAxisAlignment() const {
return (m_transform[1] == 0 && m_transform[2] == 0) ||
(m_transform[0] == 0 && m_transform[3] == 0);
}
bool operator==(const AffineTransform& m2) const {
return (m_transform[0] == m2.m_transform[0] &&
m_transform[1] == m2.m_transform[1] &&
m_transform[2] == m2.m_transform[2] &&
m_transform[3] == m2.m_transform[3] &&
m_transform[4] == m2.m_transform[4] &&
m_transform[5] == m2.m_transform[5]);
}
bool operator!=(const AffineTransform& other) const {
return !(*this == other);
}
// *this = *this * t (i.e., a multRight)
AffineTransform& operator*=(const AffineTransform& t) { return multiply(t); }
// result = *this * t (i.e., a multRight)
AffineTransform operator*(const AffineTransform& t) const {
AffineTransform result = *this;
result *= t;
return result;
}
static AffineTransform translation(double x, double y) {
return AffineTransform(1, 0, 0, 1, x, y);
}
// decompose the matrix into its component parts
typedef struct {
double scaleX, scaleY;
double angle;
double remainderA, remainderB, remainderC, remainderD;
double translateX, translateY;
} DecomposedType;
bool decompose(DecomposedType&) const;
void recompose(const DecomposedType&);
void copyTransformTo(Transform m) {
memcpy(m, m_transform, sizeof(Transform));
}
// If |asMatrix| is true, the transform is returned as a matrix in row-major
// order. Otherwise, the transform's decomposition is returned which shows
// the translation, scale, etc.
String toString(bool asMatrix = false) const;
private:
void setMatrix(const Transform m) {
if (m && m != m_transform)
memcpy(m_transform, m, sizeof(Transform));
}
Transform m_transform;
};
// Redeclared here to avoid ODR issues.
// See platform/testing/TransformPrinters.h.
void PrintTo(const AffineTransform&, std::ostream*);
} // namespace blink
#endif
|