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 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 68 | static const segment segmentTest2[] = { |
| 69 | {SkPath::kLine_Verb, {{1, 0}, {0, 0} }}, |
| 70 | {SkPath::kQuad_Verb, {{1, 0}, {1.89897954f, 0.898979485f}, {2.39387703f, 1.59591794f}}}, |
| 71 | {SkPath::kLine_Verb, {{1, 0}, {3, 2} }}, |
| 72 | {SkPath::kMove_Verb } |
| 73 | }; |
| 74 | |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame^] | 75 | static const segment segmentTest3[] = { |
| 76 | {SkPath::kQuad_Verb, {{0, 0}, {2, 0}, {0, 1}}}, |
| 77 | {SkPath::kQuad_Verb, {{0, 0}, {1, 0}, {0, 1}}}, |
| 78 | {SkPath::kMove_Verb } |
| 79 | }; |
| 80 | |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 81 | static const segment* segmentTests[] = { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame^] | 82 | segmentTest3, |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 83 | segmentTest2, |
| 84 | segmentTest1, |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static const size_t segmentTestCount = sizeof(segmentTests) / sizeof(segmentTests[0]); |
| 88 | |
| 89 | static void testSegments(bool testFlat) { |
| 90 | for (size_t testIndex = 0; testIndex < segmentTestCount; ++testIndex) { |
| 91 | const segment* segPtr = segmentTests[testIndex]; |
| 92 | SimplifyAngleTest::Angle lesser, greater; |
| 93 | int index = 0; |
| 94 | do { |
| 95 | int next = index + 1; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 96 | #if HIGH_DEF_ANGLES==0 |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 97 | if (testFlat) { |
| 98 | lesser.setFlat(segPtr[index].pts, segPtr[index].verb, 0, index, next); |
| 99 | } else { |
| 100 | lesser.set(segPtr[index].pts, segPtr[index].verb, 0, index, next); |
| 101 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 102 | #else |
| 103 | lesser.set(segPtr[index].pts, segPtr[index].verb, 0, index, next, 0, 1); |
| 104 | #endif |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 105 | if (segPtr[next].verb == SkPath::kMove_Verb) { |
| 106 | break; |
| 107 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 108 | #if HIGH_DEF_ANGLES==0 |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 109 | if (testFlat) { |
| 110 | greater.setFlat(segPtr[next].pts, segPtr[next].verb, 0, index, next); |
| 111 | } else { |
| 112 | greater.set(segPtr[next].pts, segPtr[next].verb, 0, index, next); |
| 113 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 114 | #else |
| 115 | greater.set(segPtr[next].pts, segPtr[next].verb, 0, index, next, 0, 1); |
| 116 | #endif |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 117 | bool result = lesser < greater; |
| 118 | SkASSERT(result); |
| 119 | index = next; |
| 120 | } while (true); |
| 121 | } |
| 122 | } |
| 123 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 124 | static void testLines(bool testFlat) { |
| 125 | // create angles in a circle |
| 126 | SkTDArray<SimplifyAngleTest::Angle> angles; |
| 127 | SkTDArray<SimplifyAngleTest::Angle* > angleList; |
| 128 | SkTDArray<double> arcTans; |
| 129 | size_t x; |
| 130 | for (x = 0; x < lineCount; ++x) { |
| 131 | SimplifyAngleTest::Angle* angle = angles.append(); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 132 | #if HIGH_DEF_ANGLES==0 |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 133 | if (testFlat) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 134 | angle->setFlat(lines[x], SkPath::kLine_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 135 | } else { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 136 | angle->set(lines[x], SkPath::kLine_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 137 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 138 | #else |
| 139 | angle->set(lines[x], SkPath::kLine_Verb, 0, x, x + 1, 0, 1); |
| 140 | #endif |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 141 | double arcTan = atan2(lines[x][0].fX - lines[x][1].fX, |
| 142 | lines[x][0].fY - lines[x][1].fY); |
| 143 | arcTans.push(arcTan); |
| 144 | } |
| 145 | for (x = 0; x < lineCount; ++x) { |
| 146 | angleList.push(&angles[x]); |
| 147 | } |
| 148 | QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1); |
| 149 | bool first = true; |
| 150 | bool wrap = false; |
| 151 | double base, last; |
| 152 | for (size_t x = 0; x < lineCount; ++x) { |
| 153 | const SimplifyAngleTest::Angle* angle = angleList[x]; |
| 154 | int span = angle->start(); |
| 155 | // SkDebugf("%s [%d] %1.9g (%1.9g,%1.9g %1.9g,%1.9g)\n", __FUNCTION__, |
| 156 | // span, arcTans[span], lines[span][0].fX, lines[span][0].fY, |
| 157 | // lines[span][1].fX, lines[span][1].fY); |
| 158 | if (first) { |
| 159 | base = last = arcTans[span]; |
| 160 | first = false; |
| 161 | continue; |
| 162 | } |
| 163 | if (last < arcTans[span]) { |
| 164 | last = arcTans[span]; |
| 165 | continue; |
| 166 | } |
| 167 | if (!wrap) { |
| 168 | if (base < arcTans[span]) { |
| 169 | SkDebugf("%s !wrap [%d] %g\n", __FUNCTION__, span, arcTans[span]); |
| 170 | SkASSERT(0); |
| 171 | } |
| 172 | last = arcTans[span]; |
| 173 | wrap = true; |
| 174 | continue; |
| 175 | } |
| 176 | SkDebugf("%s wrap [%d] %g\n", __FUNCTION__, span, arcTans[span]); |
| 177 | SkASSERT(0); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static void testQuads(bool testFlat) { |
| 182 | SkTDArray<SimplifyAngleTest::Angle> angles; |
| 183 | SkTDArray<SimplifyAngleTest::Angle* > angleList; |
| 184 | size_t x; |
| 185 | for (x = 0; x < quadCount; ++x) { |
| 186 | SimplifyAngleTest::Angle* angle = angles.append(); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 187 | #if HIGH_DEF_ANGLES==0 |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 188 | if (testFlat) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 189 | angle->setFlat(quads[x], SkPath::kQuad_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 190 | } else { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 191 | angle->set(quads[x], SkPath::kQuad_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 192 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 193 | #else |
| 194 | angle->set(quads[x], SkPath::kQuad_Verb, 0, x, x + 1, 0, 1); |
| 195 | #endif |
| 196 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 197 | for (x = 0; x < quadCount; ++x) { |
| 198 | angleList.push(&angles[x]); |
| 199 | } |
| 200 | QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1); |
| 201 | for (size_t x = 0; x < quadCount; ++x) { |
| 202 | *angleList[x] < *angleList[x + 1]; |
| 203 | SkASSERT(x == quadCount - 1 || *angleList[x] < *angleList[x + 1]); |
| 204 | const SimplifyAngleTest::Angle* angle = angleList[x]; |
caryclark@google.com | f25edfe | 2012-06-01 18:20:10 +0000 | [diff] [blame] | 205 | if ((int) x != angle->start()) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 206 | SkDebugf("%s [%d] [%d]\n", __FUNCTION__, x, angle->start()); |
| 207 | SkASSERT(0); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | static void testCubics(bool testFlat) { |
| 213 | SkTDArray<SimplifyAngleTest::Angle> angles; |
| 214 | SkTDArray<SimplifyAngleTest::Angle* > angleList; |
| 215 | for (size_t x = 0; x < cubicCount; ++x) { |
| 216 | SimplifyAngleTest::Angle* angle = angles.append(); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 217 | #if HIGH_DEF_ANGLES==0 |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 218 | if (testFlat) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 219 | angle->setFlat(cubics[x], SkPath::kCubic_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 220 | } else { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 221 | angle->set(cubics[x], SkPath::kCubic_Verb, 0, x, x + 1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 222 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 223 | #else |
| 224 | angle->set(cubics[x], SkPath::kCubic_Verb, 0, x, x + 1, 0, 1); |
| 225 | #endif |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 226 | angleList.push(angle); |
| 227 | } |
| 228 | QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1); |
| 229 | for (size_t x = 0; x < cubicCount; ++x) { |
| 230 | const SimplifyAngleTest::Angle* angle = angleList[x]; |
caryclark@google.com | f25edfe | 2012-06-01 18:20:10 +0000 | [diff] [blame] | 231 | if ((int) x != angle->start()) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 232 | SkDebugf("%s [%d] [%d]\n", __FUNCTION__, x, angle->start()); |
| 233 | SkASSERT(0); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | static void (*tests[])(bool) = { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 239 | testSegments, |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 240 | testLines, |
| 241 | testQuads, |
| 242 | testCubics |
| 243 | }; |
| 244 | |
| 245 | static const size_t testCount = sizeof(tests) / sizeof(tests[0]); |
| 246 | |
| 247 | static void (*firstTest)(bool) = 0; |
| 248 | static bool skipAll = false; |
| 249 | |
| 250 | void SimplifyAngle_Test() { |
| 251 | if (skipAll) { |
| 252 | return; |
| 253 | } |
| 254 | size_t index = 0; |
| 255 | if (firstTest) { |
| 256 | while (index < testCount && tests[index] != firstTest) { |
| 257 | ++index; |
| 258 | } |
| 259 | } |
| 260 | bool firstTestComplete = false; |
| 261 | for ( ; index < testCount; ++index) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 262 | (*tests[index])(false); // set to true to exercise setFlat |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 263 | firstTestComplete = true; |
| 264 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 265 | } |