blob: 19adf40887e74b85cf1b27aa87727e38df15cfcb [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkMatrix.h"
Herb Derbyb549cc32017-03-27 13:35:15 -04009#include "SkMalloc.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010
caryclark@google.com9da0cea32012-06-06 12:11:33 +000011// FIXME: needs to be in a header
12bool SkSetPoly3To3(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
13
reed@android.com8a1c16f2008-12-17 15:59:43 +000014static void computeOuterProduct(SkScalar op[4],
15 const SkPoint pts0[3], const SkPoint& ave0,
16 const SkPoint pts1[3], const SkPoint& ave1) {
reed@android.com4516f472009-06-29 16:25:36 +000017 sk_bzero(op, 4 * sizeof(op[0]));
reed@android.com8a1c16f2008-12-17 15:59:43 +000018 for (int i = 0; i < 3; i++) {
19 SkScalar x0 = pts0[i].fX - ave0.fX;
20 SkScalar y0 = pts0[i].fY - ave0.fY;
21 SkScalar x1 = pts1[i].fX - ave1.fX;
22 SkScalar y1 = pts1[i].fY - ave1.fY;
Mike Reeddf85c382017-02-14 10:59:19 -050023 op[0] += x0 * x1;
24 op[1] += x0 * y1;
25 op[2] += y0 * x1;
26 op[3] += y0 * y1;
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 }
28}
29
30static SkScalar dot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) {
Mike Reeddf85c382017-02-14 10:59:19 -050031 return ax * bx + ay * by;
reed@android.com8a1c16f2008-12-17 15:59:43 +000032}
33
34bool SkSetPoly3To3(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]) {
35 const SkPoint& srcAve = src[0];
36 const SkPoint& dstAve = dst[0];
rmistry@google.comd6176b02012-08-23 18:14:13 +000037
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 SkScalar srcOP[4], dstOP[4];
rmistry@google.comd6176b02012-08-23 18:14:13 +000039
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 computeOuterProduct(srcOP, src, srcAve, src, srcAve);
41 computeOuterProduct(dstOP, src, srcAve, dst, dstAve);
42
Mike Reeddf85c382017-02-14 10:59:19 -050043 SkScalar det = srcOP[0] * srcOP[3] - srcOP[1] * srcOP[2];
reed@android.com8a1c16f2008-12-17 15:59:43 +000044
45 // need SkScalarNearlyZeroSquared for this (to match Chrome's fix)
46 if (SkScalarNearlyZero(det)) {
47 return false;
48 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000049
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 SkScalar invDet = SkScalarInvert(det);
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 // now compute invDet * [srcOP]T * [dstOP]
rmistry@google.comd6176b02012-08-23 18:14:13 +000053
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 // scale and transpose
Mike Reeddf85c382017-02-14 10:59:19 -050055 const SkScalar srcOP0 = srcOP[3] * invDet;
56 const SkScalar srcOP1 = -srcOP[1] * invDet;
57 const SkScalar srcOP2 = -srcOP[2] * invDet;
58 const SkScalar srcOP3 = srcOP[0] * invDet;
reed@android.com8a1c16f2008-12-17 15:59:43 +000059
60 matrix->reset();
61 matrix->setScaleX(dot(srcOP0, srcOP1, dstOP[0], dstOP[2]));
62 matrix->setSkewX( dot(srcOP2, srcOP3, dstOP[0], dstOP[2]));
63 matrix->setSkewY (dot(srcOP0, srcOP1, dstOP[1], dstOP[3]));
64 matrix->setScaleY(dot(srcOP2, srcOP3, dstOP[1], dstOP[3]));
65 matrix->setTranslateX(dstAve.fX - dot(srcAve.fX, srcAve.fY,
66 matrix->getScaleX(), matrix->getSkewX()));
67 matrix->setTranslateY(dstAve.fY - dot(srcAve.fX, srcAve.fY,
68 matrix->getSkewY(), matrix->getScaleY()));
69 return true;
70}