Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
PathOpsTestCommon.cpp File Reference
#include "include/core/SkPath.h"
#include "include/core/SkPathTypes.h"
#include "include/core/SkPoint.h"
#include "include/core/SkTypes.h"
#include "src/base/SkTSort.h"
#include "src/core/SkPathPriv.h"
#include "src/pathops/SkPathOpsBounds.h"
#include "src/pathops/SkPathOpsConic.h"
#include "src/pathops/SkPathOpsCubic.h"
#include "src/pathops/SkPathOpsLine.h"
#include "src/pathops/SkPathOpsQuad.h"
#include "src/pathops/SkPathOpsRect.h"
#include "src/pathops/SkPathOpsTSect.h"
#include "src/pathops/SkPathOpsTypes.h"
#include "src/pathops/SkReduceOrder.h"
#include "tests/PathOpsTestCommon.h"
#include <cmath>
#include <cstring>
#include <utility>

Go to the source code of this file.

Functions

static double calc_t_div (const SkDCubic &cubic, double precision, double start)
 
static bool add_simple_ts (const SkDCubic &cubic, double precision, TArray< double, true > *ts)
 
static void addTs (const SkDCubic &cubic, double precision, double start, double end, TArray< double, true > *ts)
 
static void toQuadraticTs (const SkDCubic *cubic, double precision, TArray< double, true > *ts)
 
void CubicToQuads (const SkDCubic &cubic, double precision, TArray< SkDQuad, true > &quads)
 
void CubicPathToQuads (const SkPath &cubicPath, SkPath *quadPath)
 
void CubicPathToSimple (const SkPath &cubicPath, SkPath *simplePath)
 
bool ValidBounds (const SkPathOpsBounds &bounds)
 
bool ValidConic (const SkDConic &conic)
 
bool ValidCubic (const SkDCubic &cubic)
 
bool ValidLine (const SkDLine &line)
 
bool ValidPoint (const SkDPoint &pt)
 
bool ValidPoints (const SkPoint *pts, int count)
 
bool ValidQuad (const SkDQuad &quad)
 
bool ValidVector (const SkDVector &v)
 

Function Documentation

◆ add_simple_ts()

static bool add_simple_ts ( const SkDCubic cubic,
double  precision,
TArray< double, true > *  ts 
)
static

Definition at line 54 of file PathOpsTestCommon.cpp.

54 {
55 double tDiv = calc_t_div(cubic, precision, 0);
56 if (tDiv >= 1) {
57 return true;
58 }
59 if (tDiv >= 0.5) {
60 ts->push_back(0.5);
61 return true;
62 }
63 return false;
64}
static double calc_t_div(const SkDCubic &cubic, double precision, double start)

◆ addTs()

static void addTs ( const SkDCubic cubic,
double  precision,
double  start,
double  end,
TArray< double, true > *  ts 
)
static

Definition at line 66 of file PathOpsTestCommon.cpp.

67 {
68 double tDiv = calc_t_div(cubic, precision, 0);
69 double parts = ceil(1.0 / tDiv);
70 for (double index = 0; index < parts; ++index) {
71 double newT = start + (index / parts) * (end - start);
72 if (newT > 0 && newT < 1) {
73 ts->push_back(newT);
74 }
75 }
76}
glong glong end
SIN Vec< N, float > ceil(const Vec< N, float > &x)
Definition SkVx.h:702

◆ calc_t_div()

static double calc_t_div ( const SkDCubic cubic,
double  precision,
double  start 
)
static

Definition at line 31 of file PathOpsTestCommon.cpp.

31 {
32 const double adjust = sqrt(3.) / 36;
33 SkDCubic sub;
34 const SkDCubic* cPtr;
35 if (start == 0) {
36 cPtr = &cubic;
37 } else {
38 // OPTIMIZE: special-case half-split ?
39 sub = cubic.subDivide(start, 1);
40 cPtr = &sub;
41 }
42 const SkDCubic& c = *cPtr;
43 double dx = c[3].fX - 3 * (c[2].fX - c[1].fX) - c[0].fX;
44 double dy = c[3].fY - 3 * (c[2].fY - c[1].fY) - c[0].fY;
45 double dist = sqrt(dx * dx + dy * dy);
46 double tDiv3 = precision / (adjust * dist);
47 double t = std::cbrt(tDiv3);
48 if (start > 0) {
49 t = start + (1 - start) * t;
50 }
51 return t;
52}
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition SkRecords.h:208
AI float cubic(float precision, const SkPoint pts[], const VectorXform &vectorXform=VectorXform())
SIN Vec< N, float > sqrt(const Vec< N, float > &x)
Definition SkVx.h:706

◆ CubicPathToQuads()

void CubicPathToQuads ( const SkPath cubicPath,
SkPath quadPath 
)

Definition at line 178 of file PathOpsTestCommon.cpp.

178 {
179 quadPath->reset();
182 for (auto [verb, pts, w] : SkPathPriv::Iterate(cubicPath)) {
183 switch (verb) {
185 quadPath->moveTo(pts[0].fX, pts[0].fY);
186 continue;
188 quadPath->lineTo(pts[1].fX, pts[1].fY);
189 break;
191 quadPath->quadTo(pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
192 break;
194 quads.clear();
195 cubic.set(pts);
196 CubicToQuads(cubic, cubic.calcPrecision(), quads);
197 for (int index = 0; index < quads.size(); ++index) {
198 SkPoint qPts[2] = {
199 quads[index][1].asSkPoint(),
200 quads[index][2].asSkPoint()
201 };
202 quadPath->quadTo(qPts[0].fX, qPts[0].fY, qPts[1].fX, qPts[1].fY);
203 }
204 break;
206 quadPath->close();
207 break;
208 default:
209 SkDEBUGFAIL("bad verb");
210 return;
211 }
212 }
213}
void CubicToQuads(const SkDCubic &cubic, double precision, TArray< SkDQuad, true > &quads)
#define SkDEBUGFAIL(message)
Definition SkAssert.h:118
@ kClose
SkPath::RawIter returns 0 points.
@ kCubic
SkPath::RawIter returns 4 points.
@ kQuad
SkPath::RawIter returns 3 points.
@ kMove
SkPath::RawIter returns 1 point.
@ kLine
SkPath::RawIter returns 2 points.
SkPath & moveTo(SkScalar x, SkScalar y)
Definition SkPath.cpp:678
SkPath & lineTo(SkScalar x, SkScalar y)
Definition SkPath.cpp:718
SkPath & reset()
Definition SkPath.cpp:360
SkPath & quadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2)
Definition SkPath.cpp:736
SkPath & close()
Definition SkPath.cpp:813
int size() const
Definition SkTArray.h:416
SkScalar w

◆ CubicPathToSimple()

void CubicPathToSimple ( const SkPath cubicPath,
SkPath simplePath 
)

Definition at line 215 of file PathOpsTestCommon.cpp.

215 {
216 simplePath->reset();
218 for (auto [verb, pts, w] : SkPathPriv::Iterate(cubicPath)) {
219 switch (verb) {
221 simplePath->moveTo(pts[0].fX, pts[0].fY);
222 continue;
224 simplePath->lineTo(pts[1].fX, pts[1].fY);
225 break;
227 simplePath->quadTo(pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
228 break;
229 case SkPathVerb::kCubic: {
230 cubic.set(pts);
231 double tInflects[2];
232 int inflections = cubic.findInflections(tInflects);
233 if (inflections > 1 && tInflects[0] > tInflects[1]) {
234 using std::swap;
235 swap(tInflects[0], tInflects[1]);
236 }
237 double lo = 0;
238 for (int index = 0; index <= inflections; ++index) {
239 double hi = index < inflections ? tInflects[index] : 1;
240 SkDCubic part = cubic.subDivide(lo, hi);
241 SkPoint cPts[3];
242 cPts[0] = part[1].asSkPoint();
243 cPts[1] = part[2].asSkPoint();
244 cPts[2] = part[3].asSkPoint();
245 simplePath->cubicTo(cPts[0].fX, cPts[0].fY, cPts[1].fX, cPts[1].fY,
246 cPts[2].fX, cPts[2].fY);
247 lo = hi;
248 }
249 break;
250 }
252 simplePath->close();
253 break;
254 default:
255 SkDEBUGFAIL("bad verb");
256 return;
257 }
258 }
259}
SkPath & cubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar x3, SkScalar y3)
Definition SkPath.cpp:789
static void swap(TArray< T, M > &a, TArray< T, M > &b)
Definition SkTArray.h:737

◆ CubicToQuads()

void CubicToQuads ( const SkDCubic cubic,
double  precision,
TArray< SkDQuad, true > &  quads 
)

Definition at line 148 of file PathOpsTestCommon.cpp.

148 {
150 toQuadraticTs(&cubic, precision, &ts);
151 if (ts.empty()) {
152 SkDQuad quad = cubic.toQuad();
153 quads.push_back(quad);
154 return;
155 }
156 double tStart = 0;
157 for (int i1 = 0; i1 <= ts.size(); ++i1) {
158 const double tEnd = i1 < ts.size() ? ts[i1] : 1;
160 bounds.setBounds(cubic);
161 SkDCubic part = cubic.subDivide(tStart, tEnd);
162 SkDQuad quad = part.toQuad();
163 if (quad[1].fX < bounds.fLeft) {
164 quad[1].fX = bounds.fLeft;
165 } else if (quad[1].fX > bounds.fRight) {
166 quad[1].fX = bounds.fRight;
167 }
168 if (quad[1].fY < bounds.fTop) {
169 quad[1].fY = bounds.fTop;
170 } else if (quad[1].fY > bounds.fBottom) {
171 quad[1].fY = bounds.fBottom;
172 }
173 quads.push_back(quad);
174 tStart = tEnd;
175 }
176}
static void toQuadraticTs(const SkDCubic *cubic, double precision, TArray< double, true > *ts)
bool empty() const
Definition SkTArray.h:194
Optional< SkRect > bounds
Definition SkRecords.h:189
SkDQuad toQuad() const

◆ toQuadraticTs()

static void toQuadraticTs ( const SkDCubic cubic,
double  precision,
TArray< double, true > *  ts 
)
static

Definition at line 78 of file PathOpsTestCommon.cpp.

78 {
79 SkReduceOrder reducer;
80 int order = reducer.reduce(*cubic, SkReduceOrder::kAllow_Quadratics);
81 if (order < 3) {
82 return;
83 }
84 double inflectT[5];
85 int inflections = cubic->findInflections(inflectT);
86 SkASSERT(inflections <= 2);
87 if (!cubic->endsAreExtremaInXOrY()) {
88 inflections += cubic->findMaxCurvature(&inflectT[inflections]);
89 SkASSERT(inflections <= 5);
90 }
91 SkTQSort<double>(inflectT, inflectT + inflections);
92 // OPTIMIZATION: is this filtering common enough that it needs to be pulled out into its
93 // own subroutine?
94 while (inflections && approximately_less_than_zero(inflectT[0])) {
95 memmove(inflectT, &inflectT[1], sizeof(inflectT[0]) * --inflections);
96 }
97 int start = 0;
98 int next = 1;
99 while (next < inflections) {
100 if (!approximately_equal(inflectT[start], inflectT[next])) {
101 ++start;
102 ++next;
103 continue;
104 }
105 memmove(&inflectT[start], &inflectT[next], sizeof(inflectT[0]) * (--inflections - start));
106 }
107
108 while (inflections && approximately_greater_than_one(inflectT[inflections - 1])) {
109 --inflections;
110 }
111 SkDCubicPair pair;
112 if (inflections == 1) {
113 pair = cubic->chopAt(inflectT[0]);
114 int orderP1 = reducer.reduce(pair.first(), SkReduceOrder::kNo_Quadratics);
115 if (orderP1 < 2) {
116 --inflections;
117 } else {
118 int orderP2 = reducer.reduce(pair.second(), SkReduceOrder::kNo_Quadratics);
119 if (orderP2 < 2) {
120 --inflections;
121 }
122 }
123 }
124 if (inflections == 0 && add_simple_ts(*cubic, precision, ts)) {
125 return;
126 }
127 if (inflections == 1) {
128 pair = cubic->chopAt(inflectT[0]);
129 addTs(pair.first(), precision, 0, inflectT[0], ts);
130 addTs(pair.second(), precision, inflectT[0], 1, ts);
131 return;
132 }
133 if (inflections > 1) {
134 SkDCubic part = cubic->subDivide(0, inflectT[0]);
135 addTs(part, precision, 0, inflectT[0], ts);
136 int last = inflections - 1;
137 for (int idx = 0; idx < last; ++idx) {
138 part = cubic->subDivide(inflectT[idx], inflectT[idx + 1]);
139 addTs(part, precision, inflectT[idx], inflectT[idx + 1], ts);
140 }
141 part = cubic->subDivide(inflectT[last], 1);
142 addTs(part, precision, inflectT[last], 1, ts);
143 return;
144 }
145 addTs(*cubic, precision, 0, 1, ts);
146}
static float next(float f)
static bool add_simple_ts(const SkDCubic &cubic, double precision, TArray< double, true > *ts)
static void addTs(const SkDCubic &cubic, double precision, double start, double end, TArray< double, true > *ts)
#define SkASSERT(cond)
Definition SkAssert.h:116
bool approximately_less_than_zero(double x)
bool approximately_equal(double x, double y)
bool approximately_greater_than_one(double x)
SkDCubic second() const
SkDCubic first() const
int reduce(const SkDCubic &cubic, Quadratics)

◆ ValidBounds()

bool ValidBounds ( const SkPathOpsBounds bounds)

Definition at line 261 of file PathOpsTestCommon.cpp.

261 {
262 if (SkIsNaN(bounds.fLeft)) {
263 return false;
264 }
265 if (SkIsNaN(bounds.fTop)) {
266 return false;
267 }
268 if (SkIsNaN(bounds.fRight)) {
269 return false;
270 }
271 return !SkIsNaN(bounds.fBottom);
272}
static bool SkIsNaN(T x)

◆ ValidConic()

bool ValidConic ( const SkDConic conic)

Definition at line 274 of file PathOpsTestCommon.cpp.

274 {
275 for (int index = 0; index < SkDConic::kPointCount; ++index) {
276 if (!ValidPoint(conic[index])) {
277 return false;
278 }
279 }
280 if (SkDoubleIsNaN(conic.fWeight)) {
281 return false;
282 }
283 return true;
284}
bool ValidPoint(const SkDPoint &pt)
static bool SkDoubleIsNaN(double x)
AI float conic(float tolerance, const SkPoint pts[], float w, const VectorXform &vectorXform=VectorXform())
static const int kPointCount

◆ ValidCubic()

bool ValidCubic ( const SkDCubic cubic)

Definition at line 286 of file PathOpsTestCommon.cpp.

286 {
287 for (int index = 0; index < 4; ++index) {
288 if (!ValidPoint(cubic[index])) {
289 return false;
290 }
291 }
292 return true;
293}

◆ ValidLine()

bool ValidLine ( const SkDLine line)

Definition at line 295 of file PathOpsTestCommon.cpp.

295 {
296 for (int index = 0; index < 2; ++index) {
297 if (!ValidPoint(line[index])) {
298 return false;
299 }
300 }
301 return true;
302}

◆ ValidPoint()

bool ValidPoint ( const SkDPoint pt)

Definition at line 304 of file PathOpsTestCommon.cpp.

304 {
305 if (SkDoubleIsNaN(pt.fX)) {
306 return false;
307 }
308 return !SkDoubleIsNaN(pt.fY);
309}

◆ ValidPoints()

bool ValidPoints ( const SkPoint pts,
int  count 
)

Definition at line 311 of file PathOpsTestCommon.cpp.

311 {
312 for (int index = 0; index < count; ++index) {
313 if (SkIsNaN(pts[index].fX)) {
314 return false;
315 }
316 if (SkIsNaN(pts[index].fY)) {
317 return false;
318 }
319 }
320 return true;
321}
int count

◆ ValidQuad()

bool ValidQuad ( const SkDQuad quad)

Definition at line 323 of file PathOpsTestCommon.cpp.

323 {
324 for (int index = 0; index < 3; ++index) {
325 if (!ValidPoint(quad[index])) {
326 return false;
327 }
328 }
329 return true;
330}

◆ ValidVector()

bool ValidVector ( const SkDVector v)

Definition at line 332 of file PathOpsTestCommon.cpp.

332 {
333 if (SkDoubleIsNaN(v.fX)) {
334 return false;
335 }
336 return !SkDoubleIsNaN(v.fY);
337}