blob: 750db3074f1809ddd611b2c2737852a877b80295 [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(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
12
reed@android.com8a1c16f2008-12-17 15:59:43 +000013static void computeOuterProduct(SkScalar op[4],
14 const SkPoint pts0[3], const SkPoint& ave0,
15 const SkPoint pts1[3], const SkPoint& ave1) {
reed@android.com4516f472009-06-29 16:25:36 +000016 sk_bzero(op, 4 * sizeof(op[0]));
reed@android.com8a1c16f2008-12-17 15:59:43 +000017 for (int i = 0; i < 3; i++) {
18 SkScalar x0 = pts0[i].fX - ave0.fX;
19 SkScalar y0 = pts0[i].fY - ave0.fY;
20 SkScalar x1 = pts1[i].fX - ave1.fX;
21 SkScalar y1 = pts1[i].fY - ave1.fY;
22 op[0] += SkScalarMul(x0, x1);
23 op[1] += SkScalarMul(x0, y1);
24 op[2] += SkScalarMul(y0, x1);
25 op[3] += SkScalarMul(y0, y1);
26 }
27}
28
29static SkScalar dot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) {
30 return SkScalarMul(ax, bx) + SkScalarMul(ay, by);
31}
32
33bool SkSetPoly3To3(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]) {
34 const SkPoint& srcAve = src[0];
35 const SkPoint& dstAve = dst[0];
rmistry@google.comd6176b02012-08-23 18:14:13 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 SkScalar srcOP[4], dstOP[4];
rmistry@google.comd6176b02012-08-23 18:14:13 +000038
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 computeOuterProduct(srcOP, src, srcAve, src, srcAve);
40 computeOuterProduct(dstOP, src, srcAve, dst, dstAve);
41
42 SkScalar det = SkScalarMul(srcOP[0], srcOP[3]) - SkScalarMul(srcOP[1], srcOP[2]);
43
44 // need SkScalarNearlyZeroSquared for this (to match Chrome's fix)
45 if (SkScalarNearlyZero(det)) {
46 return false;
47 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000048
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 SkScalar invDet = SkScalarInvert(det);
rmistry@google.comd6176b02012-08-23 18:14:13 +000050
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 // now compute invDet * [srcOP]T * [dstOP]
rmistry@google.comd6176b02012-08-23 18:14:13 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 // scale and transpose
54 const SkScalar srcOP0 = SkScalarMul( srcOP[3], invDet);
55 const SkScalar srcOP1 = SkScalarMul(-srcOP[1], invDet);
56 const SkScalar srcOP2 = SkScalarMul(-srcOP[2], invDet);
57 const SkScalar srcOP3 = SkScalarMul( srcOP[0], invDet);
58
59 matrix->reset();
60 matrix->setScaleX(dot(srcOP0, srcOP1, dstOP[0], dstOP[2]));
61 matrix->setSkewX( dot(srcOP2, srcOP3, dstOP[0], dstOP[2]));
62 matrix->setSkewY (dot(srcOP0, srcOP1, dstOP[1], dstOP[3]));
63 matrix->setScaleY(dot(srcOP2, srcOP3, dstOP[1], dstOP[3]));
64 matrix->setTranslateX(dstAve.fX - dot(srcAve.fX, srcAve.fY,
65 matrix->getScaleX(), matrix->getSkewX()));
66 matrix->setTranslateY(dstAve.fY - dot(srcAve.fX, srcAve.fY,
67 matrix->getSkewY(), matrix->getScaleY()));
68 return true;
69}