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_D(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]); |
| 12 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | typedef int64_t SkDScalar; |
| 14 | |
| 15 | static SkScalar SkDScalar_toScalar(SkDScalar value) { |
| 16 | SkDScalar result = (value + (1 << 15)) >> 16; |
bsalomon@google.com | 8e06dab | 2011-10-07 20:03:39 +0000 | [diff] [blame] | 17 | SkDEBUGCODE(int top = static_cast<int>(result >> 31);) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | SkASSERT(top == 0 || top == -1); |
| 19 | return (SkScalar)result; |
| 20 | } |
| 21 | |
| 22 | static SkDScalar SkDScalar_setMul(SkScalar a, SkScalar b) { |
caryclark@google.com | 9da0cea3 | 2012-06-06 12:11:33 +0000 | [diff] [blame] | 23 | return (SkDScalar) ((SkDScalar) a * (SkDScalar) b); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static 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.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 30 | sk_bzero(tmp, sizeof(tmp)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 31 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | 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 | |
| 49 | static 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.com | 8637a36 | 2012-04-10 18:32:35 +0000 | [diff] [blame] | 54 | bool SkSetPoly3To3_D(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | const SkPoint& srcAve = src[0]; |
| 56 | const SkPoint& dstAve = dst[0]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 57 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | SkMatrix srcOP, dstOP; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 59 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | 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 | } |