File: TransformationMatrix.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (499 lines) | stat: -rw-r--r-- 18,775 bytes parent folder | download
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 * Copyright (C) 2005, 2006 Apple Computer, Inc.  All rights reserved.
 *
 * 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 TransformationMatrix_h
#define TransformationMatrix_h

#include "SkMatrix44.h"
#include "platform/geometry/FloatPoint.h"
#include "platform/geometry/FloatPoint3D.h"
#include "wtf/Alignment.h"
#include "wtf/Allocator.h"
#include "wtf/CPU.h"
#include "wtf/Compiler.h"
#include "wtf/PtrUtil.h"
#include <memory>
#include <string.h>  // for memcpy

namespace blink {

class AffineTransform;
class IntRect;
class LayoutRect;
class FloatRect;
class FloatQuad;
class FloatBox;
struct Rotation;
#if CPU(X86_64)
#define TRANSFORMATION_MATRIX_USE_X86_64_SSE2
#endif

// TransformationMatrix must not be allocated on Oilpan's heap since
// Oilpan doesn't (yet) have an ability to allocate the TransformationMatrix
// with 16-byte alignment. PartitionAlloc has the ability.
class PLATFORM_EXPORT TransformationMatrix {
  USING_FAST_MALLOC(TransformationMatrix);

 public:
#if defined(TRANSFORMATION_MATRIX_USE_X86_64_SSE2)
  typedef WTF_ALIGNED(double, Matrix4[4][4], 16);
#else
  typedef double Matrix4[4][4];
#endif

  static std::unique_ptr<TransformationMatrix> create() {
    return WTF::makeUnique<TransformationMatrix>();
  }
  static std::unique_ptr<TransformationMatrix> create(
      const TransformationMatrix& t) {
    return WTF::makeUnique<TransformationMatrix>(t);
  }
  static std::unique_ptr<TransformationMatrix> create(double a,
                                                      double b,
                                                      double c,
                                                      double d,
                                                      double e,
                                                      double f) {
    return WTF::makeUnique<TransformationMatrix>(a, b, c, d, e, f);
  }
  static std::unique_ptr<TransformationMatrix> create(double m11,
                                                      double m12,
                                                      double m13,
                                                      double m14,
                                                      double m21,
                                                      double m22,
                                                      double m23,
                                                      double m24,
                                                      double m31,
                                                      double m32,
                                                      double m33,
                                                      double m34,
                                                      double m41,
                                                      double m42,
                                                      double m43,
                                                      double m44) {
    return WTF::wrapUnique(
        new TransformationMatrix(m11, m12, m13, m14, m21, m22, m23, m24, m31,
                                 m32, m33, m34, m41, m42, m43, m44));
  }

  TransformationMatrix() {
    checkAlignment();
    makeIdentity();
  }
  TransformationMatrix(const AffineTransform&);
  TransformationMatrix(const TransformationMatrix& t) {
    checkAlignment();
    *this = t;
  }
  TransformationMatrix(double a,
                       double b,
                       double c,
                       double d,
                       double e,
                       double f) {
    checkAlignment();
    setMatrix(a, b, c, d, e, f);
  }
  TransformationMatrix(double m11,
                       double m12,
                       double m13,
                       double m14,
                       double m21,
                       double m22,
                       double m23,
                       double m24,
                       double m31,
                       double m32,
                       double m33,
                       double m34,
                       double m41,
                       double m42,
                       double m43,
                       double m44) {
    checkAlignment();
    setMatrix(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41,
              m42, m43, m44);
  }
  TransformationMatrix(const SkMatrix44& matrix) {
    setMatrix(
        matrix.get(0, 0), matrix.get(1, 0), matrix.get(2, 0), matrix.get(3, 0),
        matrix.get(0, 1), matrix.get(1, 1), matrix.get(2, 1), matrix.get(3, 1),
        matrix.get(0, 2), matrix.get(1, 2), matrix.get(2, 2), matrix.get(3, 2),
        matrix.get(0, 3), matrix.get(1, 3), matrix.get(2, 3), matrix.get(3, 3));
  }

  void setMatrix(double a, double b, double c, double d, double e, double f) {
    m_matrix[0][0] = a;
    m_matrix[0][1] = b;
    m_matrix[0][2] = 0;
    m_matrix[0][3] = 0;
    m_matrix[1][0] = c;
    m_matrix[1][1] = d;
    m_matrix[1][2] = 0;
    m_matrix[1][3] = 0;
    m_matrix[2][0] = 0;
    m_matrix[2][1] = 0;
    m_matrix[2][2] = 1;
    m_matrix[2][3] = 0;
    m_matrix[3][0] = e;
    m_matrix[3][1] = f;
    m_matrix[3][2] = 0;
    m_matrix[3][3] = 1;
  }

  void setMatrix(double m11,
                 double m12,
                 double m13,
                 double m14,
                 double m21,
                 double m22,
                 double m23,
                 double m24,
                 double m31,
                 double m32,
                 double m33,
                 double m34,
                 double m41,
                 double m42,
                 double m43,
                 double m44) {
    m_matrix[0][0] = m11;
    m_matrix[0][1] = m12;
    m_matrix[0][2] = m13;
    m_matrix[0][3] = m14;
    m_matrix[1][0] = m21;
    m_matrix[1][1] = m22;
    m_matrix[1][2] = m23;
    m_matrix[1][3] = m24;
    m_matrix[2][0] = m31;
    m_matrix[2][1] = m32;
    m_matrix[2][2] = m33;
    m_matrix[2][3] = m34;
    m_matrix[3][0] = m41;
    m_matrix[3][1] = m42;
    m_matrix[3][2] = m43;
    m_matrix[3][3] = m44;
  }

  TransformationMatrix& operator=(const TransformationMatrix& t) {
    setMatrix(t.m_matrix);
    return *this;
  }

  TransformationMatrix& makeIdentity() {
    setMatrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
    return *this;
  }

  bool isIdentity() const {
    return m_matrix[0][0] == 1 && m_matrix[0][1] == 0 && m_matrix[0][2] == 0 &&
           m_matrix[0][3] == 0 && m_matrix[1][0] == 0 && m_matrix[1][1] == 1 &&
           m_matrix[1][2] == 0 && m_matrix[1][3] == 0 && m_matrix[2][0] == 0 &&
           m_matrix[2][1] == 0 && m_matrix[2][2] == 1 && m_matrix[2][3] == 0 &&
           m_matrix[3][0] == 0 && m_matrix[3][1] == 0 && m_matrix[3][2] == 0 &&
           m_matrix[3][3] == 1;
  }

  // Map a 3D point through the transform, returning a 3D point.
  FloatPoint3D mapPoint(const FloatPoint3D&) const;

  // Map a 2D point through the transform, returning a 2D point.
  // Note that this ignores the z component, effectively projecting the point
  // into the z=0 plane.
  FloatPoint mapPoint(const FloatPoint&) const;

  // If the matrix has 3D components, the z component of the result is
  // dropped, effectively projecting the rect into the z=0 plane
  FloatRect mapRect(const FloatRect&) 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;
  LayoutRect mapRect(const LayoutRect&) const;

  // If the matrix has 3D components, the z component of the result is
  // dropped, effectively projecting the quad into the z=0 plane
  FloatQuad mapQuad(const FloatQuad&) const;

  // Map a point on the z=0 plane into a point on
  // the plane with with the transform applied, by extending
  // a ray perpendicular to the source plane and computing
  // the local x,y position of the point where that ray intersects
  // with the destination plane.
  FloatPoint projectPoint(const FloatPoint&, bool* clamped = 0) const;
  // Projects the four corners of the quad
  FloatQuad projectQuad(const FloatQuad&, bool* clamped = 0) const;
  // Projects the four corners of the quad and takes a bounding box,
  // while sanitizing values created when the w component is negative.
  LayoutRect clampedBoundsOfProjectedQuad(const FloatQuad&) const;

  void transformBox(FloatBox&) const;

  double m11() const { return m_matrix[0][0]; }
  void setM11(double f) { m_matrix[0][0] = f; }
  double m12() const { return m_matrix[0][1]; }
  void setM12(double f) { m_matrix[0][1] = f; }
  double m13() const { return m_matrix[0][2]; }
  void setM13(double f) { m_matrix[0][2] = f; }
  double m14() const { return m_matrix[0][3]; }
  void setM14(double f) { m_matrix[0][3] = f; }
  double m21() const { return m_matrix[1][0]; }
  void setM21(double f) { m_matrix[1][0] = f; }
  double m22() const { return m_matrix[1][1]; }
  void setM22(double f) { m_matrix[1][1] = f; }
  double m23() const { return m_matrix[1][2]; }
  void setM23(double f) { m_matrix[1][2] = f; }
  double m24() const { return m_matrix[1][3]; }
  void setM24(double f) { m_matrix[1][3] = f; }
  double m31() const { return m_matrix[2][0]; }
  void setM31(double f) { m_matrix[2][0] = f; }
  double m32() const { return m_matrix[2][1]; }
  void setM32(double f) { m_matrix[2][1] = f; }
  double m33() const { return m_matrix[2][2]; }
  void setM33(double f) { m_matrix[2][2] = f; }
  double m34() const { return m_matrix[2][3]; }
  void setM34(double f) { m_matrix[2][3] = f; }
  double m41() const { return m_matrix[3][0]; }
  void setM41(double f) { m_matrix[3][0] = f; }
  double m42() const { return m_matrix[3][1]; }
  void setM42(double f) { m_matrix[3][1] = f; }
  double m43() const { return m_matrix[3][2]; }
  void setM43(double f) { m_matrix[3][2] = f; }
  double m44() const { return m_matrix[3][3]; }
  void setM44(double f) { m_matrix[3][3] = f; }

  double a() const { return m_matrix[0][0]; }
  void setA(double a) { m_matrix[0][0] = a; }

  double b() const { return m_matrix[0][1]; }
  void setB(double b) { m_matrix[0][1] = b; }

  double c() const { return m_matrix[1][0]; }
  void setC(double c) { m_matrix[1][0] = c; }

  double d() const { return m_matrix[1][1]; }
  void setD(double d) { m_matrix[1][1] = d; }

  double e() const { return m_matrix[3][0]; }
  void setE(double e) { m_matrix[3][0] = e; }

  double f() const { return m_matrix[3][1]; }
  void setF(double f) { m_matrix[3][1] = f; }

  // *this = *this * mat.
  TransformationMatrix& multiply(const TransformationMatrix&);

  TransformationMatrix& scale(double);
  TransformationMatrix& scaleNonUniform(double sx, double sy);
  TransformationMatrix& scale3d(double sx, double sy, double sz);

  TransformationMatrix& rotate(double d) { return rotate3d(0, 0, d); }
  // Angles are in degrees.
  TransformationMatrix& rotate3d(double rx, double ry, double rz);
  TransformationMatrix& rotate3d(const Rotation&);

  // The vector (x,y,z) is normalized if it's not already. A vector of
  // (0,0,0) uses a vector of (0,0,1).
  TransformationMatrix& rotate3d(double x, double y, double z, double angle);

  TransformationMatrix& translate(double tx, double ty);
  TransformationMatrix& translate3d(double tx, double ty, double tz);

  // translation added with a post-multiply
  TransformationMatrix& translateRight(double tx, double ty);
  TransformationMatrix& translateRight3d(double tx, double ty, double tz);

  TransformationMatrix& skew(double angleX, double angleY);
  TransformationMatrix& skewX(double angle) { return skew(angle, 0); }
  TransformationMatrix& skewY(double angle) { return skew(0, angle); }

  TransformationMatrix& applyPerspective(double p);

  // Changes the transform to apply as if the origin were at (x, y, z).
  TransformationMatrix& applyTransformOrigin(double x, double y, double z);
  TransformationMatrix& applyTransformOrigin(const FloatPoint3D& origin) {
    return applyTransformOrigin(origin.x(), origin.y(), origin.z());
  }

  // Changes the transform to:
  //
  //     scale3d(z, z, z) * mat * scale3d(1/z, 1/z, 1/z)
  //
  // Useful for mapping zoomed points to their zoomed transformed result:
  //
  //     new_mat * (scale3d(z, z, z) * x) == scale3d(z, z, z) * (mat * x)
  //
  TransformationMatrix& zoom(double zoomFactor);

  bool isInvertible() const;

  // This method returns the identity matrix if it is not invertible.
  // Use isInvertible() before calling this if you need to know.
  TransformationMatrix inverse() const;

  // decompose the matrix into its component parts
  typedef struct {
    double scaleX, scaleY, scaleZ;
    double skewXY, skewXZ, skewYZ;
    double quaternionX, quaternionY, quaternionZ, quaternionW;
    double translateX, translateY, translateZ;
    double perspectiveX, perspectiveY, perspectiveZ, perspectiveW;
  } DecomposedType;

  WARN_UNUSED_RESULT bool decompose(DecomposedType&) const;
  void recompose(const DecomposedType&);

  void blend(const TransformationMatrix& from, double progress);

  bool isAffine() const {
    return m13() == 0 && m14() == 0 && m23() == 0 && m24() == 0 && m31() == 0 &&
           m32() == 0 && m33() == 1 && m34() == 0 && m43() == 0 && m44() == 1;
  }

  // Throw away the non-affine parts of the matrix (lossy!)
  void makeAffine();

  AffineTransform toAffineTransform() const;

  // Flatten into a 2-D transformation (non-invertable).
  // Same as gfx::Transform::FlattenTo2d(); see the docs for that function for
  // details and discussion.
  void flattenTo2d();

  bool operator==(const TransformationMatrix& m2) const {
    return m_matrix[0][0] == m2.m_matrix[0][0] &&
           m_matrix[0][1] == m2.m_matrix[0][1] &&
           m_matrix[0][2] == m2.m_matrix[0][2] &&
           m_matrix[0][3] == m2.m_matrix[0][3] &&
           m_matrix[1][0] == m2.m_matrix[1][0] &&
           m_matrix[1][1] == m2.m_matrix[1][1] &&
           m_matrix[1][2] == m2.m_matrix[1][2] &&
           m_matrix[1][3] == m2.m_matrix[1][3] &&
           m_matrix[2][0] == m2.m_matrix[2][0] &&
           m_matrix[2][1] == m2.m_matrix[2][1] &&
           m_matrix[2][2] == m2.m_matrix[2][2] &&
           m_matrix[2][3] == m2.m_matrix[2][3] &&
           m_matrix[3][0] == m2.m_matrix[3][0] &&
           m_matrix[3][1] == m2.m_matrix[3][1] &&
           m_matrix[3][2] == m2.m_matrix[3][2] &&
           m_matrix[3][3] == m2.m_matrix[3][3];
  }

  bool operator!=(const TransformationMatrix& other) const {
    return !(*this == other);
  }

  // *this = *this * t
  TransformationMatrix& operator*=(const TransformationMatrix& t) {
    return multiply(t);
  }

  // result = *this * t
  TransformationMatrix operator*(const TransformationMatrix& t) const {
    TransformationMatrix result = *this;
    result.multiply(t);
    return result;
  }

  bool isIdentityOrTranslation() const {
    return m_matrix[0][0] == 1 && m_matrix[0][1] == 0 && m_matrix[0][2] == 0 &&
           m_matrix[0][3] == 0 && m_matrix[1][0] == 0 && m_matrix[1][1] == 1 &&
           m_matrix[1][2] == 0 && m_matrix[1][3] == 0 && m_matrix[2][0] == 0 &&
           m_matrix[2][1] == 0 && m_matrix[2][2] == 1 && m_matrix[2][3] == 0 &&
           m_matrix[3][3] == 1;
  }

  bool isIdentityOr2DTranslation() const {
    return isIdentityOrTranslation() && m_matrix[3][2] == 0;
  }

  bool isIntegerTranslation() const;

  // If this transformation is identity or 2D translation, returns the
  // translation.
  FloatSize to2DTranslation() const;

  typedef float FloatMatrix4[16];
  void toColumnMajorFloatArray(FloatMatrix4& result) const;

  static SkMatrix44 toSkMatrix44(const TransformationMatrix&);

  // If |asMatrix|, return the matrix in row-major order. Otherwise, return
  // the transform's decomposition which shows the translation, scale, etc.
  String toString(bool asMatrix = false) const;

 private:
  // multiply passed 2D point by matrix (assume z=0)
  void multVecMatrix(double x, double y, double& dstX, double& dstY) const;
  FloatPoint internalMapPoint(const FloatPoint& sourcePoint) const {
    double resultX;
    double resultY;
    multVecMatrix(sourcePoint.x(), sourcePoint.y(), resultX, resultY);
    return FloatPoint(static_cast<float>(resultX), static_cast<float>(resultY));
  }

  // multiply passed 3D point by matrix
  void multVecMatrix(double x,
                     double y,
                     double z,
                     double& dstX,
                     double& dstY,
                     double& dstZ) const;
  FloatPoint3D internalMapPoint(const FloatPoint3D& sourcePoint) const {
    double resultX;
    double resultY;
    double resultZ;
    multVecMatrix(sourcePoint.x(), sourcePoint.y(), sourcePoint.z(), resultX,
                  resultY, resultZ);
    return FloatPoint3D(static_cast<float>(resultX),
                        static_cast<float>(resultY),
                        static_cast<float>(resultZ));
  }

  void setMatrix(const Matrix4 m) {
    if (m && m != m_matrix)
      memcpy(m_matrix, m, sizeof(Matrix4));
  }

  void checkAlignment() {
#if defined(TRANSFORMATION_MATRIX_USE_X86_64_SSE2)
    // m_matrix can cause this class to require higher than usual alignment.
    // Make sure the allocator handles this.
    ASSERT((reinterpret_cast<uintptr_t>(this) &
            (WTF_ALIGN_OF(TransformationMatrix) - 1)) == 0);
#endif
  }

  Matrix4 m_matrix;
};

// Redeclared here to avoid ODR issues.
// See platform/testing/TransformPrinters.h.
void PrintTo(const TransformationMatrix&, std::ostream*);

}  // namespace blink

#endif  // TransformationMatrix_h