blob: c85814568c3c515585b7a36a5d81f77524e2f096 [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"
9
caryclark@google.com9da0cea32012-06-06 12:11:33 +000010// FIXME: needs to be in a header
11bool SkSetPoly3To3_A(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
12
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#ifdef SK_SCALAR_IS_FIXED
14 typedef int64_t SkDScalar;
15
16 static SkScalar SkDScalar_toScalar(SkDScalar value) {
17 SkDScalar result = (value + (1 << 15)) >> 16;
18 int top = result >> 31;
19 SkASSERT(top == 0 || top == -1);
20 return (SkScalar)result;
21 }
epoger@google.com52564402011-07-21 14:18:48 +000022 static SkScalar divide(SkDScalar numer, SkDScalar denom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 denom >>= 16;
24 return numer / denom;
25 }
26#else
27 typedef double SkDScalar;
28
29 static SkScalar SkDScalar_toScalar(SkDScalar value) {
30 return static_cast<float>(value);
31 }
epoger@google.com52564402011-07-21 14:18:48 +000032 static SkScalar divide(SkDScalar numer, SkDScalar denom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 return static_cast<float>(numer / denom);
34 }
35#endif
36
37static SkDScalar SkDScalar_setMul(SkScalar a, SkScalar b) {
robertphillips@google.com8637a362012-04-10 18:32:35 +000038 return (SkDScalar) ((SkDScalar) a * b);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039}
40
41static void computeOuterProduct(SkScalar op[4],
42 const SkPoint pts0[3], const SkPoint& ave0,
43 const SkPoint pts1[3], const SkPoint& ave1) {
reed@android.com4516f472009-06-29 16:25:36 +000044 sk_bzero(op, 4 * sizeof(op[0]));
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 for (int i = 0; i < 3; i++) {
46 SkScalar x0 = pts0[i].fX - ave0.fX;
47 SkScalar y0 = pts0[i].fY - ave0.fY;
48 SkScalar x1 = pts1[i].fX - ave1.fX;
49 SkScalar y1 = pts1[i].fY - ave1.fY;
50 op[0] += SkScalarMul(x0, x1);
51 op[1] += SkScalarMul(x0, y1);
52 op[2] += SkScalarMul(y0, x1);
53 op[3] += SkScalarMul(y0, y1);
54 }
55}
56
57static SkDScalar ddot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) {
58 return SkDScalar_setMul(ax, bx) + SkDScalar_setMul(ay, by);
59}
60
61static SkScalar dot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) {
62 return SkDScalar_toScalar(ddot(ax, ay, bx, by));
63}
64
robertphillips@google.com8637a362012-04-10 18:32:35 +000065bool SkSetPoly3To3_A(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 const SkPoint& srcAve = src[0];
67 const SkPoint& dstAve = dst[0];
rmistry@google.comd6176b02012-08-23 18:14:13 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 SkScalar srcOP[4], dstOP[4];
rmistry@google.comd6176b02012-08-23 18:14:13 +000070
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 computeOuterProduct(srcOP, src, srcAve, src, srcAve);
72 computeOuterProduct(dstOP, src, srcAve, dst, dstAve);
73
74 SkDScalar det = SkDScalar_setMul(srcOP[0], srcOP[3]) -
75 SkDScalar_setMul(srcOP[1], srcOP[2]);
76
77 SkDScalar M[4];
rmistry@google.comd6176b02012-08-23 18:14:13 +000078
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 const SkScalar srcOP0 = srcOP[3];
80 const SkScalar srcOP1 = -srcOP[1];
81 const SkScalar srcOP2 = -srcOP[2];
82 const SkScalar srcOP3 = srcOP[0];
rmistry@google.comd6176b02012-08-23 18:14:13 +000083
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 M[0] = ddot(srcOP0, srcOP1, dstOP[0], dstOP[2]);
85 M[1] = ddot(srcOP2, srcOP3, dstOP[0], dstOP[2]);
86 M[2] = ddot(srcOP0, srcOP1, dstOP[1], dstOP[3]);
87 M[3] = ddot(srcOP2, srcOP3, dstOP[1], dstOP[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 matrix->reset();
epoger@google.com52564402011-07-21 14:18:48 +000090 matrix->setScaleX(divide(M[0], det));
91 matrix->setSkewX( divide(M[1], det));
92 matrix->setSkewY (divide(M[2], det));
93 matrix->setScaleY(divide(M[3], det));
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 matrix->setTranslateX(dstAve.fX - dot(srcAve.fX, srcAve.fY,
95 matrix->getScaleX(), matrix->getSkewX()));
96 matrix->setTranslateY(dstAve.fY - dot(srcAve.fX, srcAve.fY,
97 matrix->getSkewY(), matrix->getScaleY()));
98 return true;
99}
100