blob: 41eb1789d560934049774071710ca2b77d6d5bfe [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_A(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
13
reed@google.com8f4d2302013-12-17 16:44:46 +000014typedef double SkDScalar;
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
reed@google.com8f4d2302013-12-17 16:44:46 +000016static SkScalar SkDScalar_toScalar(SkDScalar value) {
17 return static_cast<float>(value);
18}
19static SkScalar divide(SkDScalar numer, SkDScalar denom) {
20 return static_cast<float>(numer / denom);
21}
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
23static SkDScalar SkDScalar_setMul(SkScalar a, SkScalar b) {
robertphillips@google.com8637a362012-04-10 18:32:35 +000024 return (SkDScalar) ((SkDScalar) a * b);
reed@android.com8a1c16f2008-12-17 15:59:43 +000025}
26
27static void computeOuterProduct(SkScalar op[4],
28 const SkPoint pts0[3], const SkPoint& ave0,
29 const SkPoint pts1[3], const SkPoint& ave1) {
reed@android.com4516f472009-06-29 16:25:36 +000030 sk_bzero(op, 4 * sizeof(op[0]));
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 for (int i = 0; i < 3; i++) {
32 SkScalar x0 = pts0[i].fX - ave0.fX;
33 SkScalar y0 = pts0[i].fY - ave0.fY;
34 SkScalar x1 = pts1[i].fX - ave1.fX;
35 SkScalar y1 = pts1[i].fY - ave1.fY;
Mike Reeddf85c382017-02-14 10:59:19 -050036 op[0] += x0 * x1;
37 op[1] += x0 * y1;
38 op[2] += y0 * x1;
39 op[3] += y0 * y1;
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 }
41}
42
43static SkDScalar ddot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) {
44 return SkDScalar_setMul(ax, bx) + SkDScalar_setMul(ay, by);
45}
46
47static SkScalar dot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) {
48 return SkDScalar_toScalar(ddot(ax, ay, bx, by));
49}
50
robertphillips@google.com8637a362012-04-10 18:32:35 +000051bool SkSetPoly3To3_A(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 const SkPoint& srcAve = src[0];
53 const SkPoint& dstAve = dst[0];
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 SkScalar srcOP[4], dstOP[4];
rmistry@google.comd6176b02012-08-23 18:14:13 +000056
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 computeOuterProduct(srcOP, src, srcAve, src, srcAve);
58 computeOuterProduct(dstOP, src, srcAve, dst, dstAve);
59
60 SkDScalar det = SkDScalar_setMul(srcOP[0], srcOP[3]) -
61 SkDScalar_setMul(srcOP[1], srcOP[2]);
62
63 SkDScalar M[4];
rmistry@google.comd6176b02012-08-23 18:14:13 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 const SkScalar srcOP0 = srcOP[3];
66 const SkScalar srcOP1 = -srcOP[1];
67 const SkScalar srcOP2 = -srcOP[2];
68 const SkScalar srcOP3 = srcOP[0];
rmistry@google.comd6176b02012-08-23 18:14:13 +000069
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 M[0] = ddot(srcOP0, srcOP1, dstOP[0], dstOP[2]);
71 M[1] = ddot(srcOP2, srcOP3, dstOP[0], dstOP[2]);
72 M[2] = ddot(srcOP0, srcOP1, dstOP[1], dstOP[3]);
73 M[3] = ddot(srcOP2, srcOP3, dstOP[1], dstOP[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 matrix->reset();
epoger@google.com52564402011-07-21 14:18:48 +000076 matrix->setScaleX(divide(M[0], det));
77 matrix->setSkewX( divide(M[1], det));
78 matrix->setSkewY (divide(M[2], det));
79 matrix->setScaleY(divide(M[3], det));
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 matrix->setTranslateX(dstAve.fX - dot(srcAve.fX, srcAve.fY,
81 matrix->getScaleX(), matrix->getSkewX()));
82 matrix->setTranslateY(dstAve.fY - dot(srcAve.fX, srcAve.fY,
83 matrix->getSkewY(), matrix->getScaleY()));
84 return true;
85}