blob: 090e22ef86f995b49444f5051f549d64197b62ee [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 */
7#include "PathOpsCubicIntersectionTestData.h"
8#include "PathOpsQuadIntersectionTestData.h"
caryclark@google.com8d0a5242013-07-16 16:11:16 +00009#include "PathOpsTestCommon.h"
caryclark@google.com9166dcb2013-04-08 11:50:00 +000010#include "SkIntersections.h"
11#include "SkPathOpsRect.h"
12#include "SkReduceOrder.h"
13#include "Test.h"
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +000014#include "TestClassDef.h"
caryclark@google.com9166dcb2013-04-08 11:50:00 +000015
caryclark@google.com02352d12013-11-25 15:05:05 +000016#if 0 // disable test until stroke reduction is supported
caryclark@google.com9166dcb2013-04-08 11:50:00 +000017static bool controls_inside(const SkDCubic& cubic) {
18 return between(cubic[0].fX, cubic[1].fX, cubic[3].fX)
19 && between(cubic[0].fX, cubic[2].fX, cubic[3].fX)
20 && between(cubic[0].fY, cubic[1].fY, cubic[3].fY)
21 && between(cubic[0].fY, cubic[2].fY, cubic[3].fY);
22}
23
24static bool tiny(const SkDCubic& cubic) {
25 int index, minX, maxX, minY, maxY;
26 minX = maxX = minY = maxY = 0;
27 for (index = 1; index < 4; ++index) {
28 if (cubic[minX].fX > cubic[index].fX) {
29 minX = index;
30 }
31 if (cubic[minY].fY > cubic[index].fY) {
32 minY = index;
33 }
34 if (cubic[maxX].fX < cubic[index].fX) {
35 maxX = index;
36 }
37 if (cubic[maxY].fY < cubic[index].fY) {
38 maxY = index;
39 }
40 }
41 return approximately_equal(cubic[maxX].fX, cubic[minX].fX)
42 && approximately_equal(cubic[maxY].fY, cubic[minY].fY);
43}
44
45static void find_tight_bounds(const SkDCubic& cubic, SkDRect& bounds) {
46 SkDCubicPair cubicPair = cubic.chopAt(0.5);
47 if (!tiny(cubicPair.first()) && !controls_inside(cubicPair.first())) {
48 find_tight_bounds(cubicPair.first(), bounds);
49 } else {
50 bounds.add(cubicPair.first()[0]);
51 bounds.add(cubicPair.first()[3]);
52 }
53 if (!tiny(cubicPair.second()) && !controls_inside(cubicPair.second())) {
54 find_tight_bounds(cubicPair.second(), bounds);
55 } else {
56 bounds.add(cubicPair.second()[0]);
57 bounds.add(cubicPair.second()[3]);
58 }
59}
caryclark@google.com111417a2013-11-25 14:36:58 +000060#endif
caryclark@google.com9166dcb2013-04-08 11:50:00 +000061
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +000062DEF_TEST(PathOpsReduceOrderCubic, reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000063 size_t index;
64 SkReduceOrder reducer;
65 int order;
66 enum {
67 RunAll,
68 RunPointDegenerates,
69 RunNotPointDegenerates,
70 RunLines,
71 RunNotLines,
72 RunModEpsilonLines,
73 RunLessEpsilonLines,
74 RunNegEpsilonLines,
75 RunQuadraticLines,
76 RunQuadraticPoints,
77 RunQuadraticModLines,
78 RunComputedLines,
79 RunNone
80 } run = RunAll;
81 int firstTestIndex = 0;
82#if 0
83 run = RunComputedLines;
84 firstTestIndex = 18;
85#endif
86 int firstPointDegeneratesTest = run == RunAll ? 0 : run == RunPointDegenerates
87 ? firstTestIndex : SK_MaxS32;
88 int firstNotPointDegeneratesTest = run == RunAll ? 0 : run == RunNotPointDegenerates
89 ? firstTestIndex : SK_MaxS32;
90 int firstLinesTest = run == RunAll ? 0 : run == RunLines ? firstTestIndex : SK_MaxS32;
91 int firstNotLinesTest = run == RunAll ? 0 : run == RunNotLines ? firstTestIndex : SK_MaxS32;
92 int firstModEpsilonTest = run == RunAll ? 0 : run == RunModEpsilonLines
93 ? firstTestIndex : SK_MaxS32;
94 int firstLessEpsilonTest = run == RunAll ? 0 : run == RunLessEpsilonLines
95 ? firstTestIndex : SK_MaxS32;
96 int firstNegEpsilonTest = run == RunAll ? 0 : run == RunNegEpsilonLines
97 ? firstTestIndex : SK_MaxS32;
98 int firstQuadraticPointTest = run == RunAll ? 0 : run == RunQuadraticPoints
99 ? firstTestIndex : SK_MaxS32;
100 int firstQuadraticLineTest = run == RunAll ? 0 : run == RunQuadraticLines
101 ? firstTestIndex : SK_MaxS32;
102 int firstQuadraticModLineTest = run == RunAll ? 0 : run == RunQuadraticModLines
103 ? firstTestIndex : SK_MaxS32;
caryclark@google.com927b7022013-11-25 14:18:21 +0000104#if 0
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000105 int firstComputedLinesTest = run == RunAll ? 0 : run == RunComputedLines
106 ? firstTestIndex : SK_MaxS32;
caryclark@google.com927b7022013-11-25 14:18:21 +0000107#endif
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000108 for (index = firstPointDegeneratesTest; index < pointDegenerates_count; ++index) {
109 const SkDCubic& cubic = pointDegenerates[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000110 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000111 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000112 if (order != 1) {
113 SkDebugf("[%d] pointDegenerates order=%d\n", static_cast<int>(index), order);
114 REPORTER_ASSERT(reporter, 0);
115 }
116 }
117 for (index = firstNotPointDegeneratesTest; index < notPointDegenerates_count; ++index) {
118 const SkDCubic& cubic = notPointDegenerates[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000119 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000120 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000121 if (order == 1) {
122 SkDebugf("[%d] notPointDegenerates order=%d\n", static_cast<int>(index), order);
caryclark@google.com927b7022013-11-25 14:18:21 +0000123 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000124 REPORTER_ASSERT(reporter, 0);
125 }
126 }
127 for (index = firstLinesTest; index < lines_count; ++index) {
128 const SkDCubic& cubic = lines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000129 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000130 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000131 if (order != 2) {
132 SkDebugf("[%d] lines order=%d\n", static_cast<int>(index), order);
133 REPORTER_ASSERT(reporter, 0);
134 }
135 }
136 for (index = firstNotLinesTest; index < notLines_count; ++index) {
137 const SkDCubic& cubic = notLines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000138 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000139 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000140 if (order == 2) {
141 SkDebugf("[%d] notLines order=%d\n", static_cast<int>(index), order);
142 REPORTER_ASSERT(reporter, 0);
143 }
144 }
145 for (index = firstModEpsilonTest; index < modEpsilonLines_count; ++index) {
146 const SkDCubic& cubic = modEpsilonLines[index];
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] line mod by epsilon order=%d\n", static_cast<int>(index), order);
151 REPORTER_ASSERT(reporter, 0);
152 }
153 }
154 for (index = firstLessEpsilonTest; index < lessEpsilonLines_count; ++index) {
155 const SkDCubic& cubic = lessEpsilonLines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000156 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000157 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000158 if (order != 2) {
159 SkDebugf("[%d] line less by epsilon/2 order=%d\n", static_cast<int>(index), order);
caryclark@google.com927b7022013-11-25 14:18:21 +0000160 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000161 REPORTER_ASSERT(reporter, 0);
162 }
163 }
164 for (index = firstNegEpsilonTest; index < negEpsilonLines_count; ++index) {
165 const SkDCubic& cubic = negEpsilonLines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000166 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000167 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000168 if (order != 2) {
169 SkDebugf("[%d] line neg by epsilon/2 order=%d\n", static_cast<int>(index), order);
170 REPORTER_ASSERT(reporter, 0);
171 }
172 }
173 for (index = firstQuadraticPointTest; index < quadraticPoints_count; ++index) {
174 const SkDQuad& quad = quadraticPoints[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000175 SkASSERT(ValidQuad(quad));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000176 SkDCubic cubic = quad.toCubic();
caryclark@google.com927b7022013-11-25 14:18:21 +0000177 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000178 if (order != 1) {
179 SkDebugf("[%d] point quad order=%d\n", static_cast<int>(index), order);
180 REPORTER_ASSERT(reporter, 0);
181 }
182 }
183 for (index = firstQuadraticLineTest; index < quadraticLines_count; ++index) {
184 const SkDQuad& quad = quadraticLines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000185 SkASSERT(ValidQuad(quad));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000186 SkDCubic cubic = quad.toCubic();
caryclark@google.com927b7022013-11-25 14:18:21 +0000187 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000188 if (order != 2) {
189 SkDebugf("[%d] line quad order=%d\n", static_cast<int>(index), order);
190 REPORTER_ASSERT(reporter, 0);
191 }
192 }
193 for (index = firstQuadraticModLineTest; index < quadraticModEpsilonLines_count; ++index) {
194 const SkDQuad& quad = quadraticModEpsilonLines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000195 SkASSERT(ValidQuad(quad));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000196 SkDCubic cubic = quad.toCubic();
caryclark@google.com927b7022013-11-25 14:18:21 +0000197 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000198 if (order != 3) {
199 SkDebugf("[%d] line mod quad order=%d\n", static_cast<int>(index), order);
200 REPORTER_ASSERT(reporter, 0);
201 }
202 }
203
caryclark@google.com927b7022013-11-25 14:18:21 +0000204#if 0 // disable test until stroke reduction is supported
205// test if computed line end points are valid
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000206 for (index = firstComputedLinesTest; index < lines_count; ++index) {
207 const SkDCubic& cubic = lines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000208 SkASSERT(ValidCubic(cubic));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000209 bool controlsInside = controls_inside(cubic);
210 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics,
211 SkReduceOrder::kStroke_Style);
212 if (order == 2 && reducer.fLine[0] == reducer.fLine[1]) {
213 SkDebugf("[%d] line computed ends match order=%d\n", static_cast<int>(index), order);
214 REPORTER_ASSERT(reporter, 0);
215 }
216 if (controlsInside) {
217 if ( (reducer.fLine[0].fX != cubic[0].fX && reducer.fLine[0].fX != cubic[3].fX)
218 || (reducer.fLine[0].fY != cubic[0].fY && reducer.fLine[0].fY != cubic[3].fY)
219 || (reducer.fLine[1].fX != cubic[0].fX && reducer.fLine[1].fX != cubic[3].fX)
220 || (reducer.fLine[1].fY != cubic[0].fY && reducer.fLine[1].fY != cubic[3].fY)) {
221 SkDebugf("[%d] line computed ends order=%d\n", static_cast<int>(index), order);
222 REPORTER_ASSERT(reporter, 0);
223 }
224 } else {
225 // binary search for extrema, compare against actual results
226 // while a control point is outside of bounding box formed by end points, split
227 SkDRect bounds = {DBL_MAX, DBL_MAX, -DBL_MAX, -DBL_MAX};
228 find_tight_bounds(cubic, bounds);
229 if ( (!AlmostEqualUlps(reducer.fLine[0].fX, bounds.fLeft)
230 && !AlmostEqualUlps(reducer.fLine[0].fX, bounds.fRight))
231 || (!AlmostEqualUlps(reducer.fLine[0].fY, bounds.fTop)
232 && !AlmostEqualUlps(reducer.fLine[0].fY, bounds.fBottom))
233 || (!AlmostEqualUlps(reducer.fLine[1].fX, bounds.fLeft)
234 && !AlmostEqualUlps(reducer.fLine[1].fX, bounds.fRight))
235 || (!AlmostEqualUlps(reducer.fLine[1].fY, bounds.fTop)
236 && !AlmostEqualUlps(reducer.fLine[1].fY, bounds.fBottom))) {
237 SkDebugf("[%d] line computed tight bounds order=%d\n", static_cast<int>(index), order);
238 REPORTER_ASSERT(reporter, 0);
239 }
240 }
241 }
caryclark@google.com927b7022013-11-25 14:18:21 +0000242#endif
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000243}