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 */ 8 #include "SkMatrix.h" 9 10 // FIXME: needs to be in a header 11 bool SkSetPoly3To3_D(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]); 12 13 typedef int64_t SkDScalar; 14 15 static SkScalar SkDScalar_toScalar(SkDScalar value) { 16 SkDScalar result = (value + (1 << 15)) >> 16; 17 SkDEBUGCODE(int top = static_cast<int>(result >> 31);) 18 SkASSERT(top == 0 || top == -1); 19 return (SkScalar)result; 20 } 21 22 static SkDScalar SkDScalar_setMul(SkScalar a, SkScalar b) { 23 return (SkDScalar) ((SkDScalar) a * (SkDScalar) b); 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]; 30 sk_bzero(tmp, sizeof(tmp)); 31 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 54 bool SkSetPoly3To3_D(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]) { 55 const SkPoint& srcAve = src[0]; 56 const SkPoint& dstAve = dst[0]; 57 58 SkMatrix srcOP, dstOP; 59 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 } 75