Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 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 #ifndef SkCurveMeasure_DEFINED | |
| 9 #define SkCurveMeasure_DEFINED | |
| 10 | |
| 11 #include "SkPoint.h" | |
| 12 #include "SkNx.h" | |
| 13 | |
| 14 static SkScalar weights8[8] = {0.3626837833783620, 0.3626837833783620, 0.3137066 458778873, 0.3137066458778873, 0.2223810344533745, 0.2223810344533745, 0.1012285 362903763, 0.1012285362903763}; | |
|
reed1
2016/07/27 19:59:43
can you move these into private section of ArcLeng
Harry Stern
2016/07/27 20:39:07
I can, but the Sk8f weights and absc would have to
| |
| 15 static SkScalar absc8[8] = {-0.1834346424956498, 0.1834346424956498, -0.52553240 99163290, 0.5255324099163290, -0.7966664774136267, 0.7966664774136267, -0.960289 8564975363, 0.9602898564975363}; | |
| 16 | |
| 17 static Sk8f weights = Sk8f::Load(weights8); | |
| 18 static Sk8f absc = 0.5f*(Sk8f::Load(absc8) + 1.0f); | |
| 19 | |
| 20 | |
| 21 // We use Gaussian quadrature (https://en.wikipedia.org/wiki/Gaussian_quadrature ) | |
| 22 // to approximate the arc length integral here, because it is amenable to SIMD. | |
| 23 class ArcLengthIntegrator { | |
| 24 public: | |
| 25 virtual ~ArcLengthIntegrator(); | |
| 26 | |
| 27 float computeLength(float t) { | |
| 28 float length = 0.0; | |
| 29 | |
| 30 Sk8f lengths = evaluate_derivative_length(absc*t); | |
| 31 lengths = weights*lengths; | |
| 32 // is it faster or more accurate to sum and then multiply or vice versa? | |
| 33 lengths = lengths*(t*0.5f); | |
| 34 | |
| 35 for (size_t i = 0; i < 8; i++) { | |
| 36 length += lengths[i]; | |
| 37 } | |
| 38 return length; | |
| 39 } | |
| 40 | |
| 41 private: | |
| 42 Sk8f evaluate_derivative_length(Sk8f ts) { | |
| 43 Sk8f x = this->evaluate_derivative_x(ts); | |
| 44 Sk8f y = this->evaluate_derivative_y(ts); | |
| 45 | |
| 46 x = x * x; | |
| 47 y = y * y; | |
| 48 | |
| 49 return (x + y).sqrt(); | |
| 50 } | |
| 51 | |
| 52 virtual Sk8f evaluate_derivative_x(Sk8f &ts) = 0; | |
| 53 virtual Sk8f evaluate_derivative_y(Sk8f &ts) = 0; | |
| 54 }; | |
| 55 ArcLengthIntegrator::~ArcLengthIntegrator() { } | |
| 56 | |
| 57 class QuadArcLengthIntegrator : public ArcLengthIntegrator { | |
| 58 public: | |
| 59 QuadArcLengthIntegrator(const SkPoint pts[3]) { | |
| 60 float Ax = pts[0].x(); | |
| 61 float Bx = pts[1].x(); | |
| 62 float Cx = pts[2].x(); | |
| 63 float Ay = pts[0].y(); | |
| 64 float By = pts[1].y(); | |
| 65 float Cy = pts[2].y(); | |
| 66 | |
| 67 A2BC_x = Sk8f(2.0f*(Ax - 2*Bx + Cx)); | |
| 68 A2BC_y = Sk8f(2.0f*(Ay - 2*By + Cy)); | |
| 69 | |
| 70 AB_x = Sk8f(2.0f*(Bx - Ax)); | |
| 71 AB_y = Sk8f(2.0f*(By - Ay)); | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 Sk8f evaluate_derivative_x(Sk8f &ts) override { | |
|
reed1
2016/07/27 19:59:43
const Sk8f& ts
Harry Stern
2016/07/27 20:39:07
Done.
| |
| 76 return A2BC_x*ts + AB_x; | |
| 77 } | |
| 78 Sk8f evaluate_derivative_y(Sk8f &ts) override { | |
| 79 return A2BC_y*ts + AB_y; | |
| 80 } | |
| 81 | |
| 82 Sk8f A2BC_x, A2BC_y; | |
| 83 Sk8f AB_x, AB_y; | |
| 84 }; | |
| 85 | |
| 86 class SkCurveMeasure { | |
| 87 public: | |
| 88 // I guess this should have a parameter that uses the SegType enum in SkPath Measure | |
| 89 // and then we can switch on that to template the evaluator and integrator | |
| 90 SkCurveMeasure(SkPoint pts[3]); | |
| 91 | |
| 92 SkScalar getTime(SkScalar targetLength); | |
| 93 void getPosTan(SkScalar distance, SkPoint* pos, SkVector* tan); | |
| 94 SkScalar getLength(); | |
| 95 private: | |
| 96 ~SkCurveMeasure(); | |
| 97 // maybe template on an evaluator class | |
| 98 SkPoint evaluateQuad(SkScalar t); | |
| 99 SkVector evaluateQuadDerivative(SkScalar t); | |
| 100 //SkPoint evaluate_cubic(SkScalar t); | |
| 101 //SkVector evaluate_cubic_derivative(SkScalar t); | |
| 102 //SkPoint evaluate_conic(SkScalar t); | |
| 103 //SkVector evaluate_conic_derivative(SkScalar t); | |
| 104 | |
| 105 const SkScalar kTolerance = 0.0001f; | |
| 106 const int kNewtonIters = 5; | |
| 107 const int kBisectIters = 5; | |
| 108 const int kPieces = 15; | |
| 109 | |
| 110 SkPoint fPts[3]; | |
| 111 SkScalar fLength = -1.0f; | |
| 112 ArcLengthIntegrator* fIntegrator; | |
| 113 | |
| 114 // for debug purposes | |
| 115 int fIters; | |
| 116 }; | |
| 117 | |
| 118 #endif // SkCurveMeasure_DEFINED | |
| OLD | NEW |