blob: 2fb99d8751482e628f750372c67eeb6b56cbd4e1 [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_D(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
12
reed@android.com8a1c16f2008-12-17 15:59:43 +000013typedef int64_t SkDScalar;
14
15static SkScalar SkDScalar_toScalar(SkDScalar value) {
16 SkDScalar result = (value + (1 << 15)) >> 16;
bsalomon@google.com8e06dab2011-10-07 20:03:39 +000017 SkDEBUGCODE(int top = static_cast<int>(result >> 31);)
reed@android.com8a1c16f2008-12-17 15:59:43 +000018 SkASSERT(top == 0 || top == -1);
19 return (SkScalar)result;
20}
21
22static SkDScalar SkDScalar_setMul(SkScalar a, SkScalar b) {
caryclark@google.com9da0cea32012-06-06 12:11:33 +000023 return (SkDScalar) ((SkDScalar) a * (SkDScalar) b);
reed@android.com8a1c16f2008-12-17 15:59:43 +000024}
25
26static void computeOuterProduct(SkMatrix* matrix,
27 const SkPoint pts0[3], const SkPoint& ave0,
28 const SkPoint pts1[3], const SkPoint& ave1) {
29 SkDScalar tmp[4];
reed@android.com4516f472009-06-29 16:25:36 +000030 sk_bzero(tmp, sizeof(tmp));
rmistry@google.comd6176b02012-08-23 18:14:13 +000031
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 for (int i = 0; i < 3; i++) {
33 SkScalar x0 = pts0[i].fX - ave0.fX;
34 SkScalar y0 = pts0[i].fY - ave0.fY;
35 SkScalar x1 = pts1[i].fX - ave1.fX;
36 SkScalar y1 = pts1[i].fY - ave1.fY;
37 tmp[0] += SkDScalar_setMul(x0, x1);
38 tmp[1] += SkDScalar_setMul(x0, y1);
39 tmp[2] += SkDScalar_setMul(y0, x1);
40 tmp[3] += SkDScalar_setMul(y0, y1);
41 }
42 matrix->reset();
43 matrix->setScaleX(SkDScalar_toScalar(tmp[0]));
44 matrix->setSkewY( SkDScalar_toScalar(tmp[1]));
45 matrix->setSkewX( SkDScalar_toScalar(tmp[2]));
46 matrix->setScaleY(SkDScalar_toScalar(tmp[3]));
47}
48
49static SkScalar dot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) {
50 return SkDScalar_toScalar(SkDScalar_setMul(ax, bx) +
51 SkDScalar_setMul(ay, by));
52}
53
robertphillips@google.com8637a362012-04-10 18:32:35 +000054bool SkSetPoly3To3_D(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 const SkPoint& srcAve = src[0];
56 const SkPoint& dstAve = dst[0];
rmistry@google.comd6176b02012-08-23 18:14:13 +000057
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 SkMatrix srcOP, dstOP;
rmistry@google.comd6176b02012-08-23 18:14:13 +000059
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 computeOuterProduct(&srcOP, src, srcAve, src, srcAve);
61
62 if (!srcOP.invert(&srcOP)) {
63 return false;
64 }
65
66 computeOuterProduct(&dstOP, src, srcAve, dst, dstAve);
67
68 matrix->setConcat(dstOP, srcOP);
69 matrix->setTranslateX(dstAve.fX - dot(srcAve.fX, srcAve.fY,
70 matrix->getScaleX(), matrix->getSkewX()));
71 matrix->setTranslateY(dstAve.fY - dot(srcAve.fX, srcAve.fY,
72 matrix->getSkewY(), matrix->getScaleY()));
73 return true;
74}