caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "Simplify.h" |
| 9 | |
| 10 | namespace SimplifyAngleTest { |
| 11 | |
| 12 | #include "Simplify.cpp" |
| 13 | |
| 14 | } // end of SimplifyAngleTest namespace |
| 15 | |
| 16 | #include "Intersection_Tests.h" |
| 17 | |
| 18 | static const SkPoint lines[][2] = { |
| 19 | { { 10, 10}, { 10, 20} }, |
| 20 | { { 10, 10}, { 20, 10} }, |
| 21 | { { 10, 10}, {-20, 10} }, |
| 22 | { { 10, 10}, { 10, -20} }, |
| 23 | { { 10, 10}, { 20, 20} }, |
| 24 | { { 10, 10}, {-20, -20} }, |
| 25 | { { 10, 10}, {-20, 40} }, |
| 26 | { { 10, 10}, { 40, -20} } |
| 27 | }; |
| 28 | |
| 29 | static const size_t lineCount = sizeof(lines) / sizeof(lines[0]); |
| 30 | |
| 31 | static const SkPoint quads[][3] = { |
| 32 | {{ 1, 1}, { 2, 2}, { 1, 3}}, // 0 |
| 33 | {{ 1, 1}, { 3, 3}, { 1, 5}}, // 1 |
| 34 | {{ 1, 1}, { 4, 4}, { 1, 7}}, // 2 |
| 35 | {{ 1, 1}, { 5, 5}, { 9, 9}}, // 3 |
| 36 | {{ 1, 1}, { 4, 4}, { 7, 1}}, // 4 |
| 37 | {{ 1, 1}, { 3, 3}, { 5, 1}}, // 5 |
| 38 | {{ 1, 1}, { 2, 2}, { 3, 1}}, // 6 |
| 39 | }; |
| 40 | |
| 41 | static const size_t quadCount = sizeof(quads) / sizeof(quads[0]); |
| 42 | |
| 43 | static const SkPoint cubics[][4] = { |
| 44 | {{ 1, 1}, { 2, 2}, { 2, 3}, { 1, 4}}, |
| 45 | {{ 1, 1}, { 3, 3}, { 3, 5}, { 1, 7}}, |
| 46 | {{ 1, 1}, { 4, 4}, { 4, 7}, { 1, 10}}, |
| 47 | {{ 1, 1}, { 5, 5}, { 8, 8}, { 9, 9}}, |
| 48 | {{ 1, 1}, { 4, 4}, { 7, 4}, { 10, 1}}, |
| 49 | {{ 1, 1}, { 3, 3}, { 5, 3}, { 7, 1}}, |
| 50 | {{ 1, 1}, { 2, 2}, { 3, 2}, { 4, 1}}, |
| 51 | }; |
| 52 | |
| 53 | static const size_t cubicCount = sizeof(cubics) / sizeof(cubics[0]); |
| 54 | |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame^] | 55 | struct segment { |
| 56 | SkPath::Verb verb; |
| 57 | SkPoint pts[4]; |
| 58 | }; |
| 59 | |
| 60 | static const segment segmentTest1[] = { |
| 61 | {SkPath::kLine_Verb, {{2, 1}, {1, 0} }}, |
| 62 | {SkPath::kQuad_Verb, {{2, 1}, {1, 0}, {0, 0}}}, |
| 63 | {SkPath::kQuad_Verb, {{2, 1}, {3, 2}, {2, 3}}}, |
| 64 | {SkPath::kLine_Verb, {{2, 1}, {3, 2} }}, |
| 65 | {SkPath::kMove_Verb } |
| 66 | }; |
| 67 | |
| 68 | static const segment* segmentTests[] = { |
| 69 | segmentTest1 |
| 70 | }; |
| 71 | |
| 72 | static const size_t segmentTestCount = sizeof(segmentTests) / sizeof(segmentTests[0]); |
| 73 | |
| 74 | static void testSegments(bool testFlat) { |
| 75 | for (size_t testIndex = 0; testIndex < segmentTestCount; ++testIndex) { |
| 76 | const segment* segPtr = segmentTests[testIndex]; |
| 77 | SimplifyAngleTest::Angle lesser, greater; |
| 78 | int index = 0; |
| 79 | do { |
| 80 | int next = index + 1; |
| 81 | if (testFlat) { |
| 82 | lesser.setFlat(segPtr[index].pts, segPtr[index].verb, 0, index, next); |
| 83 | } else { |
| 84 | lesser.set(segPtr[index].pts, segPtr[index].verb, 0, index, next); |
| 85 | } |
| 86 | if (segPtr[next].verb == SkPath::kMove_Verb) { |
| 87 | break; |
| 88 | } |
| 89 | if (testFlat) { |
| 90 | greater.setFlat(segPtr[next].pts, segPtr[next].verb, 0, index, next); |
| 91 | } else { |
| 92 | greater.set(segPtr[next].pts, segPtr[next].verb, 0, index, next); |
| 93 | } |
| 94 | bool result = lesser < greater; |
| 95 | SkASSERT(result); |
| 96 | index = next; |
| 97 | } while (true); |
| 98 | } |
| 99 | } |
| 100 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 101 | static void testLines(bool testFlat) { |
| 102 | // create angles in a circle |
| 103 | SkTDArray<SimplifyAngleTest::Angle> angles; |
| 104 | SkTDArray<SimplifyAngleTest::Angle* > angleList; |
| 105 | SkTDArray<double> arcTans; |
| 106 | size_t x; |
| 107 | for (x = 0; x < lineCount; ++x) { |
| 108 | SimplifyAngleTest::Angle* angle = angles.append(); |
| 109 | if (testFlat) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 110 | angle->setFlat(lines[x], SkPath::kLine_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 111 | } else { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 112 | angle->set(lines[x], SkPath::kLine_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 113 | } |
| 114 | double arcTan = atan2(lines[x][0].fX - lines[x][1].fX, |
| 115 | lines[x][0].fY - lines[x][1].fY); |
| 116 | arcTans.push(arcTan); |
| 117 | } |
| 118 | for (x = 0; x < lineCount; ++x) { |
| 119 | angleList.push(&angles[x]); |
| 120 | } |
| 121 | QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1); |
| 122 | bool first = true; |
| 123 | bool wrap = false; |
| 124 | double base, last; |
| 125 | for (size_t x = 0; x < lineCount; ++x) { |
| 126 | const SimplifyAngleTest::Angle* angle = angleList[x]; |
| 127 | int span = angle->start(); |
| 128 | // SkDebugf("%s [%d] %1.9g (%1.9g,%1.9g %1.9g,%1.9g)\n", __FUNCTION__, |
| 129 | // span, arcTans[span], lines[span][0].fX, lines[span][0].fY, |
| 130 | // lines[span][1].fX, lines[span][1].fY); |
| 131 | if (first) { |
| 132 | base = last = arcTans[span]; |
| 133 | first = false; |
| 134 | continue; |
| 135 | } |
| 136 | if (last < arcTans[span]) { |
| 137 | last = arcTans[span]; |
| 138 | continue; |
| 139 | } |
| 140 | if (!wrap) { |
| 141 | if (base < arcTans[span]) { |
| 142 | SkDebugf("%s !wrap [%d] %g\n", __FUNCTION__, span, arcTans[span]); |
| 143 | SkASSERT(0); |
| 144 | } |
| 145 | last = arcTans[span]; |
| 146 | wrap = true; |
| 147 | continue; |
| 148 | } |
| 149 | SkDebugf("%s wrap [%d] %g\n", __FUNCTION__, span, arcTans[span]); |
| 150 | SkASSERT(0); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static void testQuads(bool testFlat) { |
| 155 | SkTDArray<SimplifyAngleTest::Angle> angles; |
| 156 | SkTDArray<SimplifyAngleTest::Angle* > angleList; |
| 157 | size_t x; |
| 158 | for (x = 0; x < quadCount; ++x) { |
| 159 | SimplifyAngleTest::Angle* angle = angles.append(); |
| 160 | if (testFlat) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 161 | angle->setFlat(quads[x], SkPath::kQuad_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 162 | } else { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 163 | angle->set(quads[x], SkPath::kQuad_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | for (x = 0; x < quadCount; ++x) { |
| 167 | angleList.push(&angles[x]); |
| 168 | } |
| 169 | QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1); |
| 170 | for (size_t x = 0; x < quadCount; ++x) { |
| 171 | *angleList[x] < *angleList[x + 1]; |
| 172 | SkASSERT(x == quadCount - 1 || *angleList[x] < *angleList[x + 1]); |
| 173 | const SimplifyAngleTest::Angle* angle = angleList[x]; |
caryclark@google.com | f25edfe | 2012-06-01 18:20:10 +0000 | [diff] [blame] | 174 | if ((int) x != angle->start()) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 175 | SkDebugf("%s [%d] [%d]\n", __FUNCTION__, x, angle->start()); |
| 176 | SkASSERT(0); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static void testCubics(bool testFlat) { |
| 182 | SkTDArray<SimplifyAngleTest::Angle> angles; |
| 183 | SkTDArray<SimplifyAngleTest::Angle* > angleList; |
| 184 | for (size_t x = 0; x < cubicCount; ++x) { |
| 185 | SimplifyAngleTest::Angle* angle = angles.append(); |
| 186 | if (testFlat) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 187 | angle->setFlat(cubics[x], SkPath::kCubic_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 188 | } else { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 189 | angle->set(cubics[x], SkPath::kCubic_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 190 | } |
| 191 | angleList.push(angle); |
| 192 | } |
| 193 | QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1); |
| 194 | for (size_t x = 0; x < cubicCount; ++x) { |
| 195 | const SimplifyAngleTest::Angle* angle = angleList[x]; |
caryclark@google.com | f25edfe | 2012-06-01 18:20:10 +0000 | [diff] [blame] | 196 | if ((int) x != angle->start()) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 197 | SkDebugf("%s [%d] [%d]\n", __FUNCTION__, x, angle->start()); |
| 198 | SkASSERT(0); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static void (*tests[])(bool) = { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame^] | 204 | testSegments, |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 205 | testLines, |
| 206 | testQuads, |
| 207 | testCubics |
| 208 | }; |
| 209 | |
| 210 | static const size_t testCount = sizeof(tests) / sizeof(tests[0]); |
| 211 | |
| 212 | static void (*firstTest)(bool) = 0; |
| 213 | static bool skipAll = false; |
| 214 | |
| 215 | void SimplifyAngle_Test() { |
| 216 | if (skipAll) { |
| 217 | return; |
| 218 | } |
| 219 | size_t index = 0; |
| 220 | if (firstTest) { |
| 221 | while (index < testCount && tests[index] != firstTest) { |
| 222 | ++index; |
| 223 | } |
| 224 | } |
| 225 | bool firstTestComplete = false; |
| 226 | for ( ; index < testCount; ++index) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame^] | 227 | (*tests[index])(false); // set to true to exercise setFlat |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 228 | firstTestComplete = true; |
| 229 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 230 | } |