2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkMatrix.h"
|
2017-03-20 15:19:23 +00:00
|
|
|
#include "SkMemory.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-06-06 12:11:33 +00:00
|
|
|
// FIXME: needs to be in a header
|
|
|
|
bool SkSetPoly3To3_D(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
typedef int64_t SkDScalar;
|
|
|
|
|
|
|
|
static SkScalar SkDScalar_toScalar(SkDScalar value) {
|
|
|
|
SkDScalar result = (value + (1 << 15)) >> 16;
|
2011-10-07 20:03:39 +00:00
|
|
|
SkDEBUGCODE(int top = static_cast<int>(result >> 31);)
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(top == 0 || top == -1);
|
|
|
|
return (SkScalar)result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkDScalar SkDScalar_setMul(SkScalar a, SkScalar b) {
|
2012-06-06 12:11:33 +00:00
|
|
|
return (SkDScalar) ((SkDScalar) a * (SkDScalar) b);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void computeOuterProduct(SkMatrix* matrix,
|
|
|
|
const SkPoint pts0[3], const SkPoint& ave0,
|
|
|
|
const SkPoint pts1[3], const SkPoint& ave1) {
|
|
|
|
SkDScalar tmp[4];
|
2009-06-29 16:25:36 +00:00
|
|
|
sk_bzero(tmp, sizeof(tmp));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
SkScalar x0 = pts0[i].fX - ave0.fX;
|
|
|
|
SkScalar y0 = pts0[i].fY - ave0.fY;
|
|
|
|
SkScalar x1 = pts1[i].fX - ave1.fX;
|
|
|
|
SkScalar y1 = pts1[i].fY - ave1.fY;
|
|
|
|
tmp[0] += SkDScalar_setMul(x0, x1);
|
|
|
|
tmp[1] += SkDScalar_setMul(x0, y1);
|
|
|
|
tmp[2] += SkDScalar_setMul(y0, x1);
|
|
|
|
tmp[3] += SkDScalar_setMul(y0, y1);
|
|
|
|
}
|
|
|
|
matrix->reset();
|
|
|
|
matrix->setScaleX(SkDScalar_toScalar(tmp[0]));
|
|
|
|
matrix->setSkewY( SkDScalar_toScalar(tmp[1]));
|
|
|
|
matrix->setSkewX( SkDScalar_toScalar(tmp[2]));
|
|
|
|
matrix->setScaleY(SkDScalar_toScalar(tmp[3]));
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar dot(SkScalar ax, SkScalar ay, SkScalar bx, SkScalar by) {
|
|
|
|
return SkDScalar_toScalar(SkDScalar_setMul(ax, bx) +
|
|
|
|
SkDScalar_setMul(ay, by));
|
|
|
|
}
|
|
|
|
|
2012-04-10 18:32:35 +00:00
|
|
|
bool SkSetPoly3To3_D(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]) {
|
2008-12-17 15:59:43 +00:00
|
|
|
const SkPoint& srcAve = src[0];
|
|
|
|
const SkPoint& dstAve = dst[0];
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMatrix srcOP, dstOP;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
computeOuterProduct(&srcOP, src, srcAve, src, srcAve);
|
|
|
|
|
|
|
|
if (!srcOP.invert(&srcOP)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
computeOuterProduct(&dstOP, src, srcAve, dst, dstAve);
|
|
|
|
|
|
|
|
matrix->setConcat(dstOP, srcOP);
|
|
|
|
matrix->setTranslateX(dstAve.fX - dot(srcAve.fX, srcAve.fY,
|
|
|
|
matrix->getScaleX(), matrix->getSkewX()));
|
|
|
|
matrix->setTranslateY(dstAve.fY - dot(srcAve.fX, srcAve.fY,
|
|
|
|
matrix->getSkewY(), matrix->getScaleY()));
|
|
|
|
return true;
|
|
|
|
}
|