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