epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkMatrix.h" |
| 9 | |
| 10 | #ifdef SK_SCALAR_IS_FIXED |
| 11 | typedef int64_t SkDScalar; |
| 12 | |
| 13 | static SkScalar SkDScalar_toScalar(SkDScalar value) { |
| 14 | SkDScalar result = (value + (1 << 15)) >> 16; |
| 15 | int top = result >> 31; |
| 16 | SkASSERT(top == 0 || top == -1); |
| 17 | return (SkScalar)result; |
| 18 | } |
epoger@google.com | 5256440 | 2011-07-21 14:18:48 +0000 | [diff] [blame] | 19 | static SkScalar divide(SkDScalar numer, SkDScalar denom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | denom >>= 16; |
| 21 | return numer / denom; |
| 22 | } |
| 23 | #else |
| 24 | typedef double SkDScalar; |
| 25 | |
| 26 | static SkScalar SkDScalar_toScalar(SkDScalar value) { |
| 27 | return static_cast<float>(value); |
| 28 | } |
epoger@google.com | 5256440 | 2011-07-21 14:18:48 +0000 | [diff] [blame] | 29 | static SkScalar divide(SkDScalar numer, SkDScalar denom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | return static_cast<float>(numer / denom); |
| 31 | } |
| 32 | #endif |
| 33 | |
| 34 | static SkDScalar SkDScalar_setMul(SkScalar a, SkScalar b) { |
| 35 | return (SkDScalar)a * b; |
| 36 | } |
| 37 | |
| 38 | static void computeOuterProduct(SkScalar op[4], |
| 39 | const SkPoint pts0[3], const SkPoint& ave0, |
| 40 | const SkPoint pts1[3], const SkPoint& ave1) { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 41 | sk_bzero(op, 4 * sizeof(op[0])); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | for (int i = 0; i < 3; i++) { |
| 43 | SkScalar x0 = pts0[i].fX - ave0.fX; |
| 44 | SkScalar y0 = pts0[i].fY - ave0.fY; |
| 45 | SkScalar x1 = pts1[i].fX - ave1.fX; |
| 46 | SkScalar y1 = pts1[i].fY - ave1.fY; |
| 47 | op[0] += SkScalarMul(x0, x1); |
| 48 | op[1] += SkScalarMul(x0, y1); |
| 49 | op[2] += SkScalarMul(y0, x1); |
| 50 | op[3] += SkScalarMul(y0, y1); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static SkDScalar ddot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) { |
| 55 | return SkDScalar_setMul(ax, bx) + SkDScalar_setMul(ay, by); |
| 56 | } |
| 57 | |
| 58 | static SkScalar dot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) { |
| 59 | return SkDScalar_toScalar(ddot(ax, ay, bx, by)); |
| 60 | } |
| 61 | |
| 62 | bool SkSetPoly3To3(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]) { |
| 63 | const SkPoint& srcAve = src[0]; |
| 64 | const SkPoint& dstAve = dst[0]; |
| 65 | |
| 66 | SkScalar srcOP[4], dstOP[4]; |
| 67 | |
| 68 | computeOuterProduct(srcOP, src, srcAve, src, srcAve); |
| 69 | computeOuterProduct(dstOP, src, srcAve, dst, dstAve); |
| 70 | |
| 71 | SkDScalar det = SkDScalar_setMul(srcOP[0], srcOP[3]) - |
| 72 | SkDScalar_setMul(srcOP[1], srcOP[2]); |
| 73 | |
| 74 | SkDScalar M[4]; |
| 75 | |
| 76 | const SkScalar srcOP0 = srcOP[3]; |
| 77 | const SkScalar srcOP1 = -srcOP[1]; |
| 78 | const SkScalar srcOP2 = -srcOP[2]; |
| 79 | const SkScalar srcOP3 = srcOP[0]; |
| 80 | |
| 81 | M[0] = ddot(srcOP0, srcOP1, dstOP[0], dstOP[2]); |
| 82 | M[1] = ddot(srcOP2, srcOP3, dstOP[0], dstOP[2]); |
| 83 | M[2] = ddot(srcOP0, srcOP1, dstOP[1], dstOP[3]); |
| 84 | M[3] = ddot(srcOP2, srcOP3, dstOP[1], dstOP[3]); |
| 85 | |
| 86 | matrix->reset(); |
epoger@google.com | 5256440 | 2011-07-21 14:18:48 +0000 | [diff] [blame] | 87 | matrix->setScaleX(divide(M[0], det)); |
| 88 | matrix->setSkewX( divide(M[1], det)); |
| 89 | matrix->setSkewY (divide(M[2], det)); |
| 90 | matrix->setScaleY(divide(M[3], det)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | matrix->setTranslateX(dstAve.fX - dot(srcAve.fX, srcAve.fY, |
| 92 | matrix->getScaleX(), matrix->getSkewX())); |
| 93 | matrix->setTranslateY(dstAve.fY - dot(srcAve.fX, srcAve.fY, |
| 94 | matrix->getSkewY(), matrix->getScaleY())); |
| 95 | return true; |
| 96 | } |
| 97 | |