OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkPatchUtils.h" |
| 9 |
| 10 #include "SkGeometry.h" |
| 11 |
| 12 // size in pixels of each partition per axis, adjust this knob |
| 13 static const int kPartitionSize = 30; |
| 14 |
| 15 /** |
| 16 * Calculate the approximate arc length given a bezier curve's control points. |
| 17 */ |
| 18 static SkScalar approx_arc_length(SkPoint* points, int count) { |
| 19 if (count < 2) { |
| 20 return 0; |
| 21 } |
| 22 SkScalar arcLength = 0; |
| 23 for (int i = 0; i < count - 1; i++) { |
| 24 arcLength += SkPoint::Distance(points[i], points[i + 1]); |
| 25 } |
| 26 return arcLength; |
| 27 } |
| 28 |
| 29 SkISize SkPatchUtils::GetLevelOfDetail(const SkPatch& patch, const SkMatrix* mat
rix) { |
| 30 |
| 31 SkPoint mapPts[12]; |
| 32 matrix->mapPoints(mapPts, patch.getControlPoints(), 12); |
| 33 |
| 34 // Approximate length of each cubic. |
| 35 SkPoint pts[4]; |
| 36 patch.getTopPoints(pts); |
| 37 matrix->mapPoints(pts, 4); |
| 38 SkScalar topLength = approx_arc_length(pts, 4); |
| 39 |
| 40 patch.getBottomPoints(pts); |
| 41 matrix->mapPoints(pts, 4); |
| 42 SkScalar bottomLength = approx_arc_length(pts, 4); |
| 43 |
| 44 patch.getLeftPoints(pts); |
| 45 matrix->mapPoints(pts, 4); |
| 46 SkScalar leftLength = approx_arc_length(pts, 4); |
| 47 |
| 48 patch.getRightPoints(pts); |
| 49 matrix->mapPoints(pts, 4); |
| 50 SkScalar rightLength = approx_arc_length(pts, 4); |
| 51 |
| 52 // Level of detail per axis, based on the larger side between top and bottom
or left and right |
| 53 int lodX = static_cast<int>(SkMaxScalar(topLength, bottomLength) / kPartitio
nSize); |
| 54 int lodY = static_cast<int>(SkMaxScalar(leftLength, rightLength) / kPartitio
nSize); |
| 55 |
| 56 return SkISize::Make(SkMax32(4, lodX), SkMax32(4, lodY)); |
| 57 } |
OLD | NEW |