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 | 4408cca | 2009-10-27 02:24:03 +0000 | [diff] [blame] | 8 | #include "SkBoundaryPatch.h" |
| 9 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 10 | SK_DEFINE_INST_COUNT(SkBoundary) |
| 11 | |
reed@android.com | 2ee7c64 | 2009-10-28 14:25:34 +0000 | [diff] [blame] | 12 | SkBoundaryPatch::SkBoundaryPatch() : fBoundary(NULL) {} |
reed@android.com | 4408cca | 2009-10-27 02:24:03 +0000 | [diff] [blame] | 13 | |
| 14 | SkBoundaryPatch::~SkBoundaryPatch() { |
reed@android.com | 2ee7c64 | 2009-10-28 14:25:34 +0000 | [diff] [blame] | 15 | SkSafeUnref(fBoundary); |
reed@android.com | 4408cca | 2009-10-27 02:24:03 +0000 | [diff] [blame] | 16 | } |
| 17 | |
reed@android.com | 2ee7c64 | 2009-10-28 14:25:34 +0000 | [diff] [blame] | 18 | SkBoundary* SkBoundaryPatch::setBoundary(SkBoundary* b) { |
| 19 | SkRefCnt_SafeAssign(fBoundary, b); |
| 20 | return b; |
reed@android.com | 4408cca | 2009-10-27 02:24:03 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | static SkPoint SkMakePoint(SkScalar x, SkScalar y) { |
| 24 | SkPoint pt; |
| 25 | pt.set(x, y); |
| 26 | return pt; |
| 27 | } |
| 28 | |
| 29 | static SkPoint SkPointInterp(const SkPoint& a, const SkPoint& b, SkScalar t) { |
| 30 | return SkMakePoint(SkScalarInterp(a.fX, b.fX, t), |
| 31 | SkScalarInterp(a.fY, b.fY, t)); |
| 32 | } |
| 33 | |
reed@android.com | 2ee7c64 | 2009-10-28 14:25:34 +0000 | [diff] [blame] | 34 | SkPoint SkBoundaryPatch::eval(SkScalar unitU, SkScalar unitV) { |
| 35 | SkBoundary* b = fBoundary; |
| 36 | SkPoint u = SkPointInterp(b->eval(SkBoundary::kLeft, SK_Scalar1 - unitV), |
| 37 | b->eval(SkBoundary::kRight, unitV), |
| 38 | unitU); |
| 39 | SkPoint v = SkPointInterp(b->eval(SkBoundary::kTop, unitU), |
| 40 | b->eval(SkBoundary::kBottom, SK_Scalar1 - unitU), |
| 41 | unitV); |
reed@android.com | 4408cca | 2009-10-27 02:24:03 +0000 | [diff] [blame] | 42 | return SkMakePoint(SkScalarAve(u.fX, v.fX), |
| 43 | SkScalarAve(u.fY, v.fY)); |
| 44 | } |
| 45 | |
reed@android.com | 2ee7c64 | 2009-10-28 14:25:34 +0000 | [diff] [blame] | 46 | bool SkBoundaryPatch::evalPatch(SkPoint verts[], int rows, int cols) { |
| 47 | if (rows < 2 || cols < 2) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | const SkScalar invR = SkScalarInvert(SkIntToScalar(rows - 1)); |
| 52 | const SkScalar invC = SkScalarInvert(SkIntToScalar(cols - 1)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 53 | |
reed@android.com | 2ee7c64 | 2009-10-28 14:25:34 +0000 | [diff] [blame] | 54 | for (int y = 0; y < cols; y++) { |
| 55 | SkScalar yy = y * invC; |
| 56 | for (int x = 0; x < rows; x++) { |
| 57 | *verts++ = this->eval(x * invR, yy); |
| 58 | } |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
reed@android.com | 4408cca | 2009-10-27 02:24:03 +0000 | [diff] [blame] | 63 | //////////////////////////////////////////////////////////////////////// |
| 64 | |
| 65 | #include "SkGeometry.h" |
| 66 | |
reed@android.com | 2ee7c64 | 2009-10-28 14:25:34 +0000 | [diff] [blame] | 67 | SkPoint SkLineBoundary::eval(Edge e, SkScalar t) { |
| 68 | SkASSERT((unsigned)e < 4); |
| 69 | return SkPointInterp(fPts[e], fPts[(e + 1) & 3], t); |
reed@android.com | 4408cca | 2009-10-27 02:24:03 +0000 | [diff] [blame] | 70 | } |
| 71 | |
reed@android.com | 2ee7c64 | 2009-10-28 14:25:34 +0000 | [diff] [blame] | 72 | SkPoint SkCubicBoundary::eval(Edge e, SkScalar t) { |
| 73 | SkASSERT((unsigned)e < 4); |
| 74 | |
| 75 | // ensure our 4th cubic wraps to the start of the first |
| 76 | fPts[12] = fPts[0]; |
| 77 | |
reed@android.com | 4408cca | 2009-10-27 02:24:03 +0000 | [diff] [blame] | 78 | SkPoint loc; |
reed@android.com | 2ee7c64 | 2009-10-28 14:25:34 +0000 | [diff] [blame] | 79 | SkEvalCubicAt(&fPts[e * 3], t, &loc, NULL, NULL); |
reed@android.com | 4408cca | 2009-10-27 02:24:03 +0000 | [diff] [blame] | 80 | return loc; |
| 81 | } |