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