blob: 9a1a6a128065ba758883c922754037365c2e2ce3 [file] [log] [blame]
caryclark@google.com9166dcb2013-04-08 11:50:00 +00001/*
2 * Copyright 2012 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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "src/pathops/SkIntersections.h"
8#include "src/pathops/SkPathOpsRect.h"
9#include "src/pathops/SkReduceOrder.h"
10#include "tests/PathOpsCubicIntersectionTestData.h"
11#include "tests/PathOpsQuadIntersectionTestData.h"
12#include "tests/PathOpsTestCommon.h"
13#include "tests/Test.h"
caryclark@google.com9166dcb2013-04-08 11:50:00 +000014
John Stiles97659732021-08-12 10:48:09 -040015using namespace PathOpsCubicIntersectionTestData;
16
caryclark@google.com02352d12013-11-25 15:05:05 +000017#if 0 // disable test until stroke reduction is supported
caryclark@google.com9166dcb2013-04-08 11:50:00 +000018static bool controls_inside(const SkDCubic& cubic) {
19 return between(cubic[0].fX, cubic[1].fX, cubic[3].fX)
20 && between(cubic[0].fX, cubic[2].fX, cubic[3].fX)
21 && between(cubic[0].fY, cubic[1].fY, cubic[3].fY)
22 && between(cubic[0].fY, cubic[2].fY, cubic[3].fY);
23}
24
25static bool tiny(const SkDCubic& cubic) {
26 int index, minX, maxX, minY, maxY;
27 minX = maxX = minY = maxY = 0;
28 for (index = 1; index < 4; ++index) {
29 if (cubic[minX].fX > cubic[index].fX) {
30 minX = index;
31 }
32 if (cubic[minY].fY > cubic[index].fY) {
33 minY = index;
34 }
35 if (cubic[maxX].fX < cubic[index].fX) {
36 maxX = index;
37 }
38 if (cubic[maxY].fY < cubic[index].fY) {
39 maxY = index;
40 }
41 }
42 return approximately_equal(cubic[maxX].fX, cubic[minX].fX)
43 && approximately_equal(cubic[maxY].fY, cubic[minY].fY);
44}
45
46static void find_tight_bounds(const SkDCubic& cubic, SkDRect& bounds) {
47 SkDCubicPair cubicPair = cubic.chopAt(0.5);
48 if (!tiny(cubicPair.first()) && !controls_inside(cubicPair.first())) {
49 find_tight_bounds(cubicPair.first(), bounds);
50 } else {
51 bounds.add(cubicPair.first()[0]);
52 bounds.add(cubicPair.first()[3]);
53 }
54 if (!tiny(cubicPair.second()) && !controls_inside(cubicPair.second())) {
55 find_tight_bounds(cubicPair.second(), bounds);
56 } else {
57 bounds.add(cubicPair.second()[0]);
58 bounds.add(cubicPair.second()[3]);
59 }
60}
caryclark@google.com111417a2013-11-25 14:36:58 +000061#endif
caryclark@google.com9166dcb2013-04-08 11:50:00 +000062
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +000063DEF_TEST(PathOpsReduceOrderCubic, reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000064 size_t index;
65 SkReduceOrder reducer;
66 int order;
67 enum {
68 RunAll,
69 RunPointDegenerates,
70 RunNotPointDegenerates,
71 RunLines,
72 RunNotLines,
73 RunModEpsilonLines,
74 RunLessEpsilonLines,
75 RunNegEpsilonLines,
76 RunQuadraticLines,
77 RunQuadraticPoints,
78 RunQuadraticModLines,
79 RunComputedLines,
80 RunNone
81 } run = RunAll;
82 int firstTestIndex = 0;
83#if 0
84 run = RunComputedLines;
85 firstTestIndex = 18;
86#endif
87 int firstPointDegeneratesTest = run == RunAll ? 0 : run == RunPointDegenerates
88 ? firstTestIndex : SK_MaxS32;
89 int firstNotPointDegeneratesTest = run == RunAll ? 0 : run == RunNotPointDegenerates
90 ? firstTestIndex : SK_MaxS32;
91 int firstLinesTest = run == RunAll ? 0 : run == RunLines ? firstTestIndex : SK_MaxS32;
92 int firstNotLinesTest = run == RunAll ? 0 : run == RunNotLines ? firstTestIndex : SK_MaxS32;
93 int firstModEpsilonTest = run == RunAll ? 0 : run == RunModEpsilonLines
94 ? firstTestIndex : SK_MaxS32;
95 int firstLessEpsilonTest = run == RunAll ? 0 : run == RunLessEpsilonLines
96 ? firstTestIndex : SK_MaxS32;
97 int firstNegEpsilonTest = run == RunAll ? 0 : run == RunNegEpsilonLines
98 ? firstTestIndex : SK_MaxS32;
99 int firstQuadraticPointTest = run == RunAll ? 0 : run == RunQuadraticPoints
100 ? firstTestIndex : SK_MaxS32;
101 int firstQuadraticLineTest = run == RunAll ? 0 : run == RunQuadraticLines
102 ? firstTestIndex : SK_MaxS32;
103 int firstQuadraticModLineTest = run == RunAll ? 0 : run == RunQuadraticModLines
104 ? firstTestIndex : SK_MaxS32;
caryclark@google.com927b7022013-11-25 14:18:21 +0000105#if 0
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000106 int firstComputedLinesTest = run == RunAll ? 0 : run == RunComputedLines
107 ? firstTestIndex : SK_MaxS32;
caryclark@google.com927b7022013-11-25 14:18:21 +0000108#endif
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000109 for (index = firstPointDegeneratesTest; index < pointDegenerates_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -0700110 const CubicPts& c = pointDegenerates[index];
111 SkDCubic cubic;
112 cubic.debugSet(c.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000113 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000114 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000115 if (order != 1) {
116 SkDebugf("[%d] pointDegenerates order=%d\n", static_cast<int>(index), order);
117 REPORTER_ASSERT(reporter, 0);
118 }
119 }
120 for (index = firstNotPointDegeneratesTest; index < notPointDegenerates_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -0700121 const CubicPts& c = notPointDegenerates[index];
122 SkDCubic cubic;
123 cubic.debugSet(c.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000124 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000125 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000126 if (order == 1) {
127 SkDebugf("[%d] notPointDegenerates order=%d\n", static_cast<int>(index), order);
caryclark@google.com927b7022013-11-25 14:18:21 +0000128 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000129 REPORTER_ASSERT(reporter, 0);
130 }
131 }
132 for (index = firstLinesTest; index < lines_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -0700133 const CubicPts& c = lines[index];
134 SkDCubic cubic;
135 cubic.debugSet(c.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000136 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000137 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000138 if (order != 2) {
139 SkDebugf("[%d] lines order=%d\n", static_cast<int>(index), order);
140 REPORTER_ASSERT(reporter, 0);
141 }
142 }
143 for (index = firstNotLinesTest; index < notLines_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -0700144 const CubicPts& c = notLines[index];
145 SkDCubic cubic;
146 cubic.debugSet(c.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000147 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000148 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000149 if (order == 2) {
150 SkDebugf("[%d] notLines order=%d\n", static_cast<int>(index), order);
151 REPORTER_ASSERT(reporter, 0);
152 }
153 }
154 for (index = firstModEpsilonTest; index < modEpsilonLines_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -0700155 const CubicPts& c = modEpsilonLines[index];
156 SkDCubic cubic;
157 cubic.debugSet(c.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000158 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000159 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000160 if (order == 2) {
161 SkDebugf("[%d] line mod by epsilon order=%d\n", static_cast<int>(index), order);
162 REPORTER_ASSERT(reporter, 0);
163 }
164 }
165 for (index = firstLessEpsilonTest; index < lessEpsilonLines_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -0700166 const CubicPts& c = lessEpsilonLines[index];
167 SkDCubic cubic;
168 cubic.debugSet(c.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000169 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000170 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000171 if (order != 2) {
172 SkDebugf("[%d] line less by epsilon/2 order=%d\n", static_cast<int>(index), order);
caryclark@google.com927b7022013-11-25 14:18:21 +0000173 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000174 REPORTER_ASSERT(reporter, 0);
175 }
176 }
177 for (index = firstNegEpsilonTest; index < negEpsilonLines_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -0700178 const CubicPts& c = negEpsilonLines[index];
179 SkDCubic cubic;
180 cubic.debugSet(c.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000181 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000182 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000183 if (order != 2) {
184 SkDebugf("[%d] line neg by epsilon/2 order=%d\n", static_cast<int>(index), order);
185 REPORTER_ASSERT(reporter, 0);
186 }
187 }
188 for (index = firstQuadraticPointTest; index < quadraticPoints_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -0700189 const QuadPts& q = quadraticPoints[index];
190 SkDQuad quad;
191 quad.debugSet(q.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000192 SkASSERT(ValidQuad(quad));
caryclark624637c2015-05-11 07:21:27 -0700193 SkDCubic cubic = quad.debugToCubic();
caryclark@google.com927b7022013-11-25 14:18:21 +0000194 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000195 if (order != 1) {
196 SkDebugf("[%d] point quad order=%d\n", static_cast<int>(index), order);
197 REPORTER_ASSERT(reporter, 0);
198 }
199 }
200 for (index = firstQuadraticLineTest; index < quadraticLines_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -0700201 const QuadPts& q = quadraticLines[index];
202 SkDQuad quad;
203 quad.debugSet(q.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000204 SkASSERT(ValidQuad(quad));
caryclark624637c2015-05-11 07:21:27 -0700205 SkDCubic cubic = quad.debugToCubic();
caryclark@google.com927b7022013-11-25 14:18:21 +0000206 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000207 if (order != 2) {
208 SkDebugf("[%d] line quad order=%d\n", static_cast<int>(index), order);
209 REPORTER_ASSERT(reporter, 0);
210 }
211 }
212 for (index = firstQuadraticModLineTest; index < quadraticModEpsilonLines_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -0700213 const QuadPts& q = quadraticModEpsilonLines[index];
214 SkDQuad quad;
215 quad.debugSet(q.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000216 SkASSERT(ValidQuad(quad));
caryclark624637c2015-05-11 07:21:27 -0700217 SkDCubic cubic = quad.debugToCubic();
caryclark@google.com927b7022013-11-25 14:18:21 +0000218 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000219 if (order != 3) {
220 SkDebugf("[%d] line mod quad order=%d\n", static_cast<int>(index), order);
221 REPORTER_ASSERT(reporter, 0);
222 }
223 }
224
caryclark@google.com927b7022013-11-25 14:18:21 +0000225#if 0 // disable test until stroke reduction is supported
226// test if computed line end points are valid
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000227 for (index = firstComputedLinesTest; index < lines_count; ++index) {
228 const SkDCubic& cubic = lines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000229 SkASSERT(ValidCubic(cubic));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000230 bool controlsInside = controls_inside(cubic);
231 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics,
232 SkReduceOrder::kStroke_Style);
233 if (order == 2 && reducer.fLine[0] == reducer.fLine[1]) {
234 SkDebugf("[%d] line computed ends match order=%d\n", static_cast<int>(index), order);
235 REPORTER_ASSERT(reporter, 0);
236 }
237 if (controlsInside) {
238 if ( (reducer.fLine[0].fX != cubic[0].fX && reducer.fLine[0].fX != cubic[3].fX)
239 || (reducer.fLine[0].fY != cubic[0].fY && reducer.fLine[0].fY != cubic[3].fY)
240 || (reducer.fLine[1].fX != cubic[0].fX && reducer.fLine[1].fX != cubic[3].fX)
241 || (reducer.fLine[1].fY != cubic[0].fY && reducer.fLine[1].fY != cubic[3].fY)) {
242 SkDebugf("[%d] line computed ends order=%d\n", static_cast<int>(index), order);
243 REPORTER_ASSERT(reporter, 0);
244 }
245 } else {
246 // binary search for extrema, compare against actual results
247 // while a control point is outside of bounding box formed by end points, split
248 SkDRect bounds = {DBL_MAX, DBL_MAX, -DBL_MAX, -DBL_MAX};
249 find_tight_bounds(cubic, bounds);
250 if ( (!AlmostEqualUlps(reducer.fLine[0].fX, bounds.fLeft)
251 && !AlmostEqualUlps(reducer.fLine[0].fX, bounds.fRight))
252 || (!AlmostEqualUlps(reducer.fLine[0].fY, bounds.fTop)
253 && !AlmostEqualUlps(reducer.fLine[0].fY, bounds.fBottom))
254 || (!AlmostEqualUlps(reducer.fLine[1].fX, bounds.fLeft)
255 && !AlmostEqualUlps(reducer.fLine[1].fX, bounds.fRight))
256 || (!AlmostEqualUlps(reducer.fLine[1].fY, bounds.fTop)
257 && !AlmostEqualUlps(reducer.fLine[1].fY, bounds.fBottom))) {
258 SkDebugf("[%d] line computed tight bounds order=%d\n", static_cast<int>(index), order);
259 REPORTER_ASSERT(reporter, 0);
260 }
261 }
262 }
caryclark@google.com927b7022013-11-25 14:18:21 +0000263#endif
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000264}