1
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 */
8 #include "SkMatrix.h"
9 #include "SkMalloc.h"
10
11 // FIXME: needs to be in a header
12 bool SkSetPoly3To3_A(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
13
14 typedef double SkDScalar;
15
SkDScalar_toScalar(SkDScalar value)16 static SkScalar SkDScalar_toScalar(SkDScalar value) {
17 return static_cast<float>(value);
18 }
divide(SkDScalar numer,SkDScalar denom)19 static SkScalar divide(SkDScalar numer, SkDScalar denom) {
20 return static_cast<float>(numer / denom);
21 }
22
SkDScalar_setMul(SkScalar a,SkScalar b)23 static SkDScalar SkDScalar_setMul(SkScalar a, SkScalar b) {
24 return (SkDScalar) ((SkDScalar) a * b);
25 }
26
computeOuterProduct(SkScalar op[4],const SkPoint pts0[3],const SkPoint & ave0,const SkPoint pts1[3],const SkPoint & ave1)27 static void computeOuterProduct(SkScalar op[4],
28 const SkPoint pts0[3], const SkPoint& ave0,
29 const SkPoint pts1[3], const SkPoint& ave1) {
30 sk_bzero(op, 4 * sizeof(op[0]));
31 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;
36 op[0] += x0 * x1;
37 op[1] += x0 * y1;
38 op[2] += y0 * x1;
39 op[3] += y0 * y1;
40 }
41 }
42
ddot(SkScalar ax,SkScalar ay,SkScalar bx,SkScalar by)43 static SkDScalar ddot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) {
44 return SkDScalar_setMul(ax, bx) + SkDScalar_setMul(ay, by);
45 }
46
dot(SkScalar ax,SkScalar ay,SkScalar bx,SkScalar by)47 static SkScalar dot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) {
48 return SkDScalar_toScalar(ddot(ax, ay, bx, by));
49 }
50
SkSetPoly3To3_A(SkMatrix * matrix,const SkPoint src[3],const SkPoint dst[3])51 bool SkSetPoly3To3_A(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]) {
52 const SkPoint& srcAve = src[0];
53 const SkPoint& dstAve = dst[0];
54
55 SkScalar srcOP[4], dstOP[4];
56
57 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];
64
65 const SkScalar srcOP0 = srcOP[3];
66 const SkScalar srcOP1 = -srcOP[1];
67 const SkScalar srcOP2 = -srcOP[2];
68 const SkScalar srcOP3 = srcOP[0];
69
70 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]);
74
75 matrix->reset();
76 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));
80 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 }
86