blob: 98662e20494ff4bd4a43360a1c8cfdd4de993da9 [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "CurveIntersection.h"
caryclark@google.com27accef2012-01-25 18:57:23 +00002#include "CubicIntersection_TestData.h"
3#include "Intersection_Tests.h"
4#include "QuadraticIntersection_TestData.h"
5#include "TestUtilities.h"
6
7void CubicReduceOrder_Test() {
8 size_t index;
9 Cubic reduce;
10 int order;
11 enum {
12 RunAll,
13 RunPointDegenerates,
14 RunNotPointDegenerates,
15 RunLines,
16 RunNotLines,
17 RunModEpsilonLines,
18 RunLessEpsilonLines,
19 RunNegEpsilonLines,
20 RunQuadraticLines,
21 RunQuadraticModLines,
22 RunComputedLines,
23 RunNone
24 } run = RunAll;
25 int firstTestIndex = 0;
26#if 0
27 run = RunComputedLines;
28 firstTestIndex = 18;
29#endif
30 int firstPointDegeneratesTest = run == RunAll ? 0 : run == RunPointDegenerates ? firstTestIndex : INT_MAX;
31 int firstNotPointDegeneratesTest = run == RunAll ? 0 : run == RunNotPointDegenerates ? firstTestIndex : INT_MAX;
32 int firstLinesTest = run == RunAll ? 0 : run == RunLines ? firstTestIndex : INT_MAX;
33 int firstNotLinesTest = run == RunAll ? 0 : run == RunNotLines ? firstTestIndex : INT_MAX;
34 int firstModEpsilonTest = run == RunAll ? 0 : run == RunModEpsilonLines ? firstTestIndex : INT_MAX;
35 int firstLessEpsilonTest = run == RunAll ? 0 : run == RunLessEpsilonLines ? firstTestIndex : INT_MAX;
36 int firstNegEpsilonTest = run == RunAll ? 0 : run == RunNegEpsilonLines ? firstTestIndex : INT_MAX;
37 int firstQuadraticLineTest = run == RunAll ? 0 : run == RunQuadraticLines ? firstTestIndex : INT_MAX;
38 int firstQuadraticModLineTest = run == RunAll ? 0 : run == RunQuadraticModLines ? firstTestIndex : INT_MAX;
39 int firstComputedLinesTest = run == RunAll ? 0 : run == RunComputedLines ? firstTestIndex : INT_MAX;
40
41 for (index = firstPointDegeneratesTest; index < pointDegenerates_count; ++index) {
42 const Cubic& cubic = pointDegenerates[index];
43 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
44 if (order != 1) {
45 printf("[%d] pointDegenerates order=%d\n", (int) index, order);
46 }
47 }
48 for (index = firstNotPointDegeneratesTest; index < notPointDegenerates_count; ++index) {
49 const Cubic& cubic = notPointDegenerates[index];
50 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
51 if (order == 1) {
52 printf("[%d] notPointDegenerates order=%d\n", (int) index, order);
53 }
54 }
55 for (index = firstLinesTest; index < lines_count; ++index) {
56 const Cubic& cubic = lines[index];
57 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
58 if (order != 2) {
59 printf("[%d] lines order=%d\n", (int) index, order);
60 }
61 }
62 for (index = firstNotLinesTest; index < notLines_count; ++index) {
63 const Cubic& cubic = notLines[index];
64 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
65 if (order == 2) {
66 printf("[%d] notLines order=%d\n", (int) index, order);
67 }
68 }
69 for (index = firstModEpsilonTest; index < modEpsilonLines_count; ++index) {
70 const Cubic& cubic = modEpsilonLines[index];
71 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
72 if (order == 2) {
73 printf("[%d] line mod by epsilon order=%d\n", (int) index, order);
74 }
75 }
76 for (index = firstLessEpsilonTest; index < lessEpsilonLines_count; ++index) {
77 const Cubic& cubic = lessEpsilonLines[index];
78 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
79 if (order != 2) {
80 printf("[%d] line less by epsilon/2 order=%d\n", (int) index, order);
81 }
82 }
83 for (index = firstNegEpsilonTest; index < negEpsilonLines_count; ++index) {
84 const Cubic& cubic = negEpsilonLines[index];
85 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
86 if (order != 2) {
87 printf("[%d] line neg by epsilon/2 order=%d\n", (int) index, order);
88 }
89 }
90 for (index = firstQuadraticLineTest; index < quadraticLines_count; ++index) {
91 const Quadratic& quad = quadraticLines[index];
92 Cubic cubic;
93 quad_to_cubic(quad, cubic);
94 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
95 if (order != 2) {
96 printf("[%d] line quad order=%d\n", (int) index, order);
97 }
98 }
99 for (index = firstQuadraticModLineTest; index < quadraticModEpsilonLines_count; ++index) {
100 const Quadratic& quad = quadraticModEpsilonLines[index];
101 Cubic cubic;
102 quad_to_cubic(quad, cubic);
103 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
104 if (order != 3) {
105 printf("[%d] line mod quad order=%d\n", (int) index, order);
106 }
107 }
108
109 // test if computed line end points are valid
110 for (index = firstComputedLinesTest; index < lines_count; ++index) {
111 const Cubic& cubic = lines[index];
112 bool controlsInside = controls_inside(cubic);
113 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
114 if (reduce[0].x == reduce[1].x && reduce[0].y == reduce[1].y) {
115 printf("[%d] line computed ends match order=%d\n", (int) index, order);
116 }
117 if (controlsInside) {
118 if ( reduce[0].x != cubic[0].x && reduce[0].x != cubic[3].x
119 || reduce[0].y != cubic[0].y && reduce[0].y != cubic[3].y
120 || reduce[1].x != cubic[0].x && reduce[1].x != cubic[3].x
121 || reduce[1].y != cubic[0].y && reduce[1].y != cubic[3].y) {
122 printf("[%d] line computed ends order=%d\n", (int) index, order);
123 }
124 } else {
125 // binary search for extrema, compare against actual results
126 // while a control point is outside of bounding box formed by end points, split
127 _Rect bounds = {DBL_MAX, DBL_MAX, -DBL_MAX, -DBL_MAX};
128 find_tight_bounds(cubic, bounds);
129 if ( !approximately_equal(reduce[0].x, bounds.left) && !approximately_equal(reduce[0].x, bounds.right)
130 || !approximately_equal(reduce[0].y, bounds.top) && !approximately_equal(reduce[0].y, bounds.bottom)
131 || !approximately_equal(reduce[1].x, bounds.left) && !approximately_equal(reduce[1].x, bounds.right)
132 || !approximately_equal(reduce[1].y, bounds.top) && !approximately_equal(reduce[1].y, bounds.bottom)) {
133 printf("[%d] line computed tight bounds order=%d\n", (int) index, order);
134 }
135
136 }
137 }
138}