caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "SkOpSegment.h" |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 8 | #include "SkTArray.h" |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 9 | #include "Test.h" |
| 10 | |
| 11 | static const SkPoint cubics[][4] = { |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 12 | /* 0 */ {{0, 1}, {2, 6}, {4, 2}, {5, 3}}, |
| 13 | /* 1 */ {{10, 234}, {10, 229.581726f}, {13.5817204f, 226}, {18, 226}}, |
| 14 | /* 2 */ {{132, 11419}, {130.89543151855469f, 11419}, {130, 11418.1044921875f}, {130, 11417}}, |
| 15 | /* 3 */ {{130.04275512695312f, 11417.4130859375f}, {130.23307800292969f, 11418.3193359375f}, |
| 16 | {131.03709411621094f, 11419}, {132, 11419}}, |
| 17 | /* 4 */ {{0,1}, {0,5}, {4,1}, {6,4}}, |
| 18 | /* 5 */ {{1,5}, {4,6}, {1,0}, {4,0}}, |
| 19 | /* 6 */ {{0,1}, {0,4}, {5,1}, {6,4}}, |
| 20 | /* 7 */ {{0,1}, {1,2}, {1,0}, {6,1}}, |
| 21 | /* 8 */ {{0,3}, {0,1}, {2,0}, {1,0}}, |
| 22 | /* 9 */ {{189,7}, {189,5.3431458473205566f}, {190.3431396484375f,4}, {192,4}}, |
| 23 | /* 10 */ {{0,1}, {1,3}, {1,0}, {6,4}}, |
| 24 | /* 11 */ {{0,1}, {2,3}, {2,1}, {4,3}}, |
| 25 | /* 12 */ {{1,2}, {3,4}, {1,0}, {3,2}}, |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 26 | /* 13 */ {{0,1}, {4,6}, {4,3}, {5,4}}, |
caryclark@google.com | 4b66f36 | 2013-06-04 18:14:11 +0000 | [diff] [blame] | 27 | /* 14 */ {{806,11419}, {806.962890625f,11419}, {807.76690673828125f,11418.3193359375f}, {807.957275390625f,11417.4130859375f}}, |
| 28 | /* 15 */ {{808,11417}, {808,11418.1044921875f}, {807.10455322265625f,11419}, {806,11419}}, |
| 29 | /* 16 */ {{132,11419}, {130.89543151855469f,11419}, {130,11418.1044921875f}, {130,11417}}, |
| 30 | /* 17 */ {{130.04275512695312f,11417.4130859375f}, {130.23312377929687f,11418.3193359375f}, {131.03707885742187f,11419}, {132,11419}}, |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 31 | /* 18 */ {{1006.6951293945312f,291}, {1023.263671875f,291}, {1033.8402099609375f,304.43145751953125f}, {1030.318359375f,321}}, |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static const SkPoint quads[][3] = { |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 35 | /* 0 */ {{12.3423996f, 228.342407f}, {10, 230.686295f}, {10, 234}}, |
| 36 | /* 1 */ {{304.24319458007812f,591.75677490234375f}, {306,593.51470947265625f}, {306,596}}, |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 37 | /* 2 */ {{0,0}, {3,1}, {0,3}}, |
| 38 | /* 3 */ {{0,1}, {3,1}, {0,2}}, |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | static const SkPoint lines[][2] = { |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 42 | /* 0 */ {{6, 2}, {2, 4}}, |
| 43 | /* 1 */ {{306,617}, {306,590}}, |
| 44 | /* 2 */ {{306,596}, {306,617}}, |
| 45 | /* 3 */ {{6,4}, {0,1}}, |
| 46 | /* 4 */ {{6,1}, {0,1}}, |
| 47 | /* 5 */ {{1,0}, {0,3}}, |
| 48 | /* 6 */ {{246,4}, {189,4}}, |
| 49 | /* 7 */ {{192,4}, {243,4}}, |
| 50 | /* 8 */ {{4,3}, {0,1}}, |
| 51 | /* 9 */ {{3,2}, {1,2}}, |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 52 | /* 10 */ {{6,4}, {3,4}}, |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 53 | /* 11 */ {{979.30487060546875f,561}, {1036.695068359375f,291}}, |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct SortSet { |
| 57 | const SkPoint* ptData; |
| 58 | int ptCount; |
| 59 | double tStart; |
| 60 | double tEnd; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 61 | SkPoint endPt; |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static const SortSet set1[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 65 | {cubics[0], 4, 0.66666987081928919, 0.875, {0, 0}}, |
| 66 | {lines[0], 2, 0.574070336, 0.388888889, {0, 0}}, |
| 67 | {cubics[0], 4, 0.66666987081928919, 0.4050371120499307, {0, 0}}, |
| 68 | {lines[0], 2, 0.574070336, 0.9140625, {0, 0}}, |
| 69 | }; |
| 70 | |
| 71 | static const SortSet set1a[] = { |
| 72 | {cubics[0], 4, 0.666666667, 0.405037112, {4.58007812f,2.83203125f}}, |
| 73 | {lines[0], 2, 0.574074074, 0.9140625, {4.44444466f,2.77777767f}}, |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | static const SortSet set2[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 77 | {cubics[0], 4, 0.666666667, 0.875, {0, 0}}, |
| 78 | {lines[0], 2, 0.574074074, 0.388888889, {0, 0}}, |
| 79 | {cubics[0], 4, 0.666666667, 0.405037112, {0, 0}}, |
| 80 | {lines[0], 2, 0.574074074, 0.9140625, {0, 0}}, |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 83 | static const SortSet set3[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 84 | {cubics[1], 4, 0, 1, {0, 0}}, |
| 85 | {quads[0], 3, 1, 0, {0, 0}}, |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 88 | static const SortSet set4[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 89 | {cubics[2], 4, 0.812114222, 1, {0, 0}}, |
| 90 | {cubics[3], 4, 0.0684734759, 0, {0, 0}}, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static const SortSet set5[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 94 | {lines[1], 2, 0.777777778, 1, {0, 0}}, |
| 95 | {quads[1], 3, 1, 4.34137342e-06, {0, 0}}, |
| 96 | {lines[2], 2, 0, 1, {0, 0}}, |
| 97 | }; |
| 98 | |
| 99 | static const SortSet set5a[] = { |
| 100 | {lines[1], 2, 0.777777778, 1, {306,590}}, |
| 101 | {quads[1], 3, 1, 4.34137342e-06, {304.243195f,591.756775f}}, |
| 102 | {lines[2], 2, 0, 1, {306,617}}, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | static const SortSet set6[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 106 | {lines[3], 2, 0.407407407, 0.554627832, {0, 0}}, |
| 107 | {cubics[4], 4, 0.666666667, 0.548022446, {0, 0}}, |
| 108 | {lines[3], 2, 0.407407407, 0, {0, 0}}, |
| 109 | {cubics[4], 4, 0.666666667, 1, {0, 0}}, |
| 110 | }; |
| 111 | |
| 112 | static const SortSet set6a[] = { |
| 113 | {lines[3], 2, 0.407407407, 0.554627832, {2.6722331f,2.33611655f}}, |
| 114 | {cubics[4], 4, 0.666666667, 0.548022446, {2.61642241f,2.83718514f}}, |
| 115 | {lines[3], 2, 0.407407407, 0, {6,4}}, |
| 116 | {cubics[4], 4, 0.666666667, 1, {6,4}}, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static const SortSet set7[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 120 | {cubics[5], 4, 0.545233342, 0.545454545, {0, 0}}, |
| 121 | {cubics[6], 4, 0.484938134, 0.484805744, {0, 0}}, |
| 122 | {cubics[5], 4, 0.545233342, 0, {0, 0}}, |
| 123 | {cubics[6], 4, 0.484938134, 0.545454545, {0, 0}}, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | static const SortSet set8[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 127 | {cubics[7], 4, 0.5, 0.522986744, {0, 0}}, |
| 128 | {lines[4], 2, 0.75, 1, {0, 0}}, |
| 129 | {cubics[7], 4, 0.5, 0, {0, 0}}, |
| 130 | {lines[4], 2, 0.75, 0.737654321, {0, 0}}, |
| 131 | }; |
| 132 | |
| 133 | static const SortSet set8a[] = { |
| 134 | {cubics[7], 4, 0.5, 0.522986744, {1.60668361f,0.965592742f}}, |
| 135 | {lines[4], 2, 0.75, 1, {0,1}}, |
| 136 | {cubics[7], 4, 0.5, 0, {0,1}}, |
| 137 | {lines[4], 2, 0.75, 0.737654321, {1.57407403f,1}}, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static const SortSet set9[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 141 | {cubics[8], 4, 0.4, 1, {0, 0}}, |
| 142 | {lines[5], 2, 0.36, 0, {0, 0}}, |
| 143 | {cubics[8], 4, 0.4, 0.394675838, {0, 0}}, |
| 144 | {lines[5], 2, 0.36, 0.363999782, {0, 0}}, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | static const SortSet set10[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 148 | {lines[6], 2, 0.947368421, 1, {0, 0}}, |
| 149 | {cubics[9], 4, 1, 0.500000357, {0, 0}}, |
| 150 | {lines[7], 2, 0, 1, {0, 0}}, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | static const SortSet set11[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 154 | {lines[3], 2, 0.75, 1, {0, 0}}, |
| 155 | {cubics[10], 4, 0.5, 0.228744269, {0, 0}}, |
| 156 | {lines[3], 2, 0.75, 0.627112191, {0, 0}}, |
| 157 | {cubics[10], 4, 0.5, 0.6339746, {0, 0}}, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | static const SortSet set12[] = { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 161 | {cubics[12], 4, 0.5, 1, {0, 0}}, |
| 162 | {lines[8], 2, 0.5, 1, {0, 0}}, |
| 163 | {cubics[11], 4, 0.5, 0, {0, 0}}, |
| 164 | {lines[9], 2, 0.5, 1, {0, 0}}, |
| 165 | {cubics[12], 4, 0.5, 0, {0, 0}}, |
| 166 | {lines[8], 2, 0.5, 0, {0, 0}}, |
| 167 | {cubics[11], 4, 0.5, 1, {0, 0}}, |
| 168 | {lines[9], 2, 0.5, 0, {0, 0}}, |
| 169 | }; |
| 170 | |
| 171 | static const SortSet set13[] = { |
| 172 | {cubics[13], 4, 0.5, 0.400631046, {0, 0}}, |
| 173 | {lines[10], 2, 0.791666667, 0.928, {0, 0}}, |
| 174 | {lines[10], 2, 0.791666667, 0.333333333, {0, 0}}, |
| 175 | {cubics[13], 4, 0.5, 0.866666667, {0, 0}}, |
| 176 | }; |
| 177 | |
| 178 | static const SortSet set14[] = { |
| 179 | {quads[2], 3, 0.5, 0.310102051, {0, 0}}, |
| 180 | {quads[3], 3, 0.5, 0.2, {0, 0}}, |
| 181 | {quads[3], 3, 0.5, 0.770156212, {0, 0}}, |
| 182 | {quads[2], 3, 0.5, 0.7, {0, 0}}, |
| 183 | }; |
| 184 | |
| 185 | static const SortSet set15[] = { |
| 186 | {cubics[14], 4, 0.93081374, 1, {0, 0}}, |
| 187 | {cubics[15], 4, 0.188518131, 0, {0, 0}}, |
| 188 | {cubics[14], 4, 0.93081374, 0, {0, 0}}, |
| 189 | }; |
| 190 | |
| 191 | static const SortSet set16[] = { |
| 192 | {cubics[17], 4, 0.0682619216, 0, {130.042755f,11417.4131f}}, |
| 193 | {cubics[16], 4, 0.812302088, 1, {130,11417}}, |
| 194 | {cubics[17], 4, 0.0682619216, 1, {132,11419}}, |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 197 | static const SortSet set17[] = { |
| 198 | {lines[11], 2, 0.888889581, 1, {0, 0}}, |
| 199 | {cubics[18], 4, 0.999996241, 0, {0, 0}}, |
| 200 | {lines[11], 2, 0.888889581, 0, {0, 0}}, |
| 201 | {cubics[18], 4, 0.999996241, 1, {0, 0}}, |
| 202 | }; |
| 203 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 204 | struct SortSetTests { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 205 | const char* name; |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 206 | const SortSet* set; |
| 207 | size_t count; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 208 | SkPoint startPt; |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 211 | #define TEST_ENTRY(name) #name, name, SK_ARRAY_COUNT(name) |
| 212 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 213 | static const SortSetTests tests[] = { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 214 | { TEST_ENTRY(set17), {0, 0}}, |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 215 | { TEST_ENTRY(set16), {130.090179f,11417.5957f} }, |
| 216 | // { TEST_ENTRY(set15), {0, 0}}, |
| 217 | { TEST_ENTRY(set14), {0, 0}}, |
| 218 | { TEST_ENTRY(set13), {0, 0}}, |
| 219 | { TEST_ENTRY(set12), {0, 0}}, |
| 220 | { TEST_ENTRY(set11), {0, 0}}, |
| 221 | { TEST_ENTRY(set10), {0, 0}}, |
| 222 | { TEST_ENTRY(set9), {0, 0}}, |
| 223 | { TEST_ENTRY(set6a), {3.55555558f,2.77777767f} }, |
| 224 | { TEST_ENTRY(set8a), {1.5f,1} }, |
| 225 | { TEST_ENTRY(set8), {0, 0}}, |
| 226 | { TEST_ENTRY(set7), {0, 0}}, |
| 227 | { TEST_ENTRY(set6a), {3.55555558f,2.77777767f} }, |
| 228 | { TEST_ENTRY(set6), {0, 0}}, |
| 229 | { TEST_ENTRY(set5a), {306,596} }, |
| 230 | { TEST_ENTRY(set5), {0, 0}}, |
| 231 | // { TEST_ENTRY(set4), {0, 0}}, |
| 232 | { TEST_ENTRY(set3), {0, 0}}, |
| 233 | { TEST_ENTRY(set2), {0, 0}}, |
| 234 | // { TEST_ENTRY(set1a), {3.70370364f,3.14814806f} }, |
| 235 | { TEST_ENTRY(set1), {0, 0}}, |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 238 | #undef TEST_ENTRY |
| 239 | |
| 240 | static void setup(const SortSet* set, const size_t idx, |
| 241 | SkOpSegment* seg, int* ts, const SkPoint& startPt) { |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 242 | SkPoint start, end; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 243 | const SkPoint* data = set[idx].ptData; |
| 244 | bool useIntersectPt = startPt.fX != 0 || startPt.fY != 0; |
| 245 | if (useIntersectPt) { |
| 246 | start = startPt; |
| 247 | end = set[idx].endPt; |
| 248 | } |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 249 | switch(set[idx].ptCount) { |
| 250 | case 2: { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 251 | seg->addLine(data, false, false); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 252 | SkDLine dLine; |
| 253 | dLine.set(set[idx].ptData); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 254 | if (useIntersectPt) { |
| 255 | break; |
| 256 | } |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 257 | start = dLine.xyAtT(set[idx].tStart).asSkPoint(); |
| 258 | end = dLine.xyAtT(set[idx].tEnd).asSkPoint(); |
| 259 | } break; |
| 260 | case 3: { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 261 | seg->addQuad(data, false, false); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 262 | SkDQuad dQuad; |
| 263 | dQuad.set(set[idx].ptData); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 264 | if (useIntersectPt) { |
| 265 | break; |
| 266 | } |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 267 | start = dQuad.xyAtT(set[idx].tStart).asSkPoint(); |
| 268 | end = dQuad.xyAtT(set[idx].tEnd).asSkPoint(); |
| 269 | } break; |
| 270 | case 4: { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 271 | seg->addCubic(data, false, false); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 272 | SkDCubic dCubic; |
| 273 | dCubic.set(set[idx].ptData); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 274 | if (useIntersectPt) { |
| 275 | break; |
| 276 | } |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 277 | start = dCubic.xyAtT(set[idx].tStart).asSkPoint(); |
| 278 | end = dCubic.xyAtT(set[idx].tEnd).asSkPoint(); |
| 279 | } break; |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 280 | } |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 281 | double tStart = set[idx].tStart; |
| 282 | double tEnd = set[idx].tEnd; |
| 283 | seg->addT(NULL, start, tStart); |
| 284 | seg->addT(NULL, end, tEnd); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 285 | if (tStart != 0 && tEnd != 0) { |
| 286 | seg->addT(NULL, set[idx].ptData[0], 0); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 287 | } |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 288 | if (tStart != 1 && tEnd != 1) { |
| 289 | seg->addT(NULL, set[idx].ptData[set[idx].ptCount - 1], 1); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 290 | } |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 291 | int tIndex = 0; |
caryclark@google.com | 20d5461 | 2013-06-04 18:30:02 +0000 | [diff] [blame] | 292 | ts[0] = 0; |
| 293 | ts[1] = 1; |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 294 | do { |
| 295 | if (seg->t(tIndex) == set[idx].tStart) { |
| 296 | ts[0] = tIndex; |
| 297 | } |
| 298 | if (seg->t(tIndex) == set[idx].tEnd) { |
| 299 | ts[1] = tIndex; |
| 300 | } |
| 301 | if (seg->t(tIndex) >= 1) { |
| 302 | break; |
| 303 | } |
| 304 | } while (++tIndex); |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 305 | } |
| 306 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 307 | static void testOne(skiatest::Reporter* reporter, const SortSetTests& test) { |
| 308 | SkTDArray<SkOpAngle> angles; |
| 309 | bool unsortable = false; |
| 310 | bool unorderable = false; |
| 311 | SkTArray<SkOpSegment> segs; |
| 312 | for (size_t idx = 0; idx < test.count; ++idx) { |
| 313 | int ts[2]; |
| 314 | const SortSet* set = test.set; |
| 315 | SkOpSegment& seg = segs.push_back(); |
| 316 | setup(set, idx, &seg, ts, test.startPt); |
| 317 | SkOpAngle* angle = angles.append(); |
| 318 | angle->set(&seg, ts[0], ts[1]); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 319 | #if DEBUG_ANGLE |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 320 | angle->setID(idx); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 321 | #endif |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 322 | if (angle->unsortable()) { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 323 | #if DEBUG_ANGLE |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 324 | SkDebugf("%s test[%s]: angle[%d] unsortable\n", __FUNCTION__, test.name, idx); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 325 | #endif |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 326 | unsortable = true; |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 327 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 328 | if (angle->unorderable()) { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 329 | #if DEBUG_ANGLE |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 330 | SkDebugf("%s test[%s]: angle[%d] unorderable\n", __FUNCTION__, test.name, idx); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 331 | #endif |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 332 | unorderable = true; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 333 | } |
| 334 | reporter->bumpTestCount(); |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 335 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 336 | if (unsortable || unorderable) { |
| 337 | return; |
| 338 | } |
| 339 | #if DEBUG_ANGLE |
| 340 | SkDebugf("%s test[%s]\n", __FUNCTION__, test.name); |
| 341 | #endif |
| 342 | for (size_t idxL = 0; idxL < test.count; ++idxL) { |
| 343 | const SkOpAngle& first = angles[idxL]; |
| 344 | for (size_t idxG = 0; idxG < test.count; ++idxG) { |
| 345 | if (idxL == idxG) { |
| 346 | continue; |
| 347 | } |
| 348 | const SkOpAngle& second = angles[idxG]; |
| 349 | bool compare = first < second; |
| 350 | if (idxL < idxG) { |
| 351 | if (!compare) { |
| 352 | SkDebugf("%s test[%s]: first[%d] > second[%d]\n", __FUNCTION__, |
| 353 | test.name, idxL, idxG); |
| 354 | compare = first < second; |
| 355 | } |
| 356 | REPORTER_ASSERT(reporter, compare); |
| 357 | } else { |
| 358 | SkASSERT(idxL > idxG); |
| 359 | if (compare) { |
| 360 | SkDebugf("%s test[%s]: first[%d] < second[%d]\n", __FUNCTION__, |
| 361 | test.name, idxL, idxG); |
| 362 | compare = first < second; |
| 363 | } |
| 364 | REPORTER_ASSERT(reporter, !compare); |
| 365 | } |
| 366 | compare = second < first; |
| 367 | if (idxL < idxG) { |
| 368 | if (compare) { |
| 369 | SkDebugf("%s test[%s]: second[%d] < first[%d]\n", __FUNCTION__, |
| 370 | test.name, idxL, idxG); |
| 371 | compare = second < first; |
| 372 | } |
| 373 | REPORTER_ASSERT(reporter, !compare); |
| 374 | } else { |
| 375 | SkASSERT(idxL > idxG); |
| 376 | if (!compare) { |
| 377 | SkDebugf("%s test[%s]: second[%d] > first[%d]\n", __FUNCTION__, |
| 378 | test.name, idxL, idxG); |
| 379 | compare = second < first; |
| 380 | } |
| 381 | REPORTER_ASSERT(reporter, compare); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | static void PathOpsAngleTest(skiatest::Reporter* reporter) { |
| 388 | for (size_t index = 0; index < SK_ARRAY_COUNT(tests); ++index) { |
| 389 | const SortSetTests& test = tests[index]; |
| 390 | testOne(reporter, test); |
| 391 | reporter->bumpTestCount(); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | static void PathOpsAngleTestOne(skiatest::Reporter* reporter) { |
| 396 | size_t index = 0; |
| 397 | const SortSetTests& test = tests[index]; |
| 398 | testOne(reporter, test); |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 399 | } |
| 400 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 401 | #if 0 |
| 402 | static int find_slop(double x, double y, double rx, double ry) { |
| 403 | int slopBits = 0; |
| 404 | bool less1, less2; |
| 405 | double absX = fabs(x); |
| 406 | double absY = fabs(y); |
| 407 | double length = absX < absY ? absX / 2 + absY : absX + absY / 2; |
| 408 | int exponent; |
| 409 | (void) frexp(length, &exponent); |
| 410 | double epsilon = ldexp(FLT_EPSILON, exponent); |
| 411 | do { |
| 412 | // get the length as the larger plus half the smaller (both same signs) |
| 413 | // find the ulps of the length |
| 414 | // compute the offsets from there |
| 415 | double xSlop = epsilon * slopBits; |
| 416 | double ySlop = x * y < 0 ? -xSlop : xSlop; // OPTIMIZATION: use copysign / _copysign ? |
| 417 | double x1 = x - xSlop; |
| 418 | double y1 = y + ySlop; |
| 419 | double x_ry1 = x1 * ry; |
| 420 | double rx_y1 = rx * y1; |
| 421 | less1 = x_ry1 < rx_y1; |
| 422 | double x2 = x + xSlop; |
| 423 | double y2 = y - ySlop; |
| 424 | double x_ry2 = x2 * ry; |
| 425 | double rx_y2 = rx * y2; |
| 426 | less2 = x_ry2 < rx_y2; |
| 427 | } while (less1 == less2 && ++slopBits); |
| 428 | return slopBits; |
| 429 | } |
| 430 | |
| 431 | // from http://stackoverflow.com/questions/1427422/cheap-algorithm-to-find-measure-of-angle-between-vectors |
| 432 | static double diamond_angle(double y, double x) |
| 433 | { |
| 434 | if (y >= 0) |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 435 | return (x >= 0 ? y/(x+y) : 1-x/(-x+y)); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 436 | else |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 437 | return (x < 0 ? 2-y/(-x-y) : 3+x/(x-y)); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static const double slopTests[][4] = { |
| 441 | // x y rx ry |
| 442 | {-0.058554756452593892, -0.18804585843827226, -0.018568569646021160, -0.059615294434479438}, |
| 443 | {-0.0013717412948608398, 0.0041152238845825195, -0.00045837944195925573, 0.0013753175735478074}, |
| 444 | {-2.1033774145221198, -1.4046019261273715e-008, -0.70062688352066704, -1.2706324683777995e-008}, |
| 445 | }; |
| 446 | |
| 447 | static void PathOpsAngleFindSlop(skiatest::Reporter* reporter) { |
| 448 | for (size_t index = 0; index < SK_ARRAY_COUNT(slopTests); ++index) { |
| 449 | const double* slopTest = slopTests[index]; |
| 450 | double x = slopTest[0]; |
| 451 | double y = slopTest[1]; |
| 452 | double rx = slopTest[2]; |
| 453 | double ry = slopTest[3]; |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 454 | SkDebugf("%s xy %d=%d\n", __FUNCTION__, (int) index, find_slop(x, y, rx, ry)); |
| 455 | SkDebugf("%s rxy %d=%d\n", __FUNCTION__, (int) index, find_slop(rx, ry, x, y)); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 456 | double angle = diamond_angle(y, x); |
| 457 | double rAngle = diamond_angle(ry, rx); |
| 458 | double diff = fabs(angle - rAngle); |
| 459 | SkDebugf("%s diamond xy=%1.9g rxy=%1.9g diff=%1.9g factor=%d\n", __FUNCTION__, |
| 460 | angle, rAngle, diff, (int) (diff / FLT_EPSILON)); |
| 461 | |
| 462 | } |
| 463 | } |
| 464 | #endif |
| 465 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 466 | #include "TestClassDef.h" |
| 467 | DEFINE_TESTCLASS_SHORT(PathOpsAngleTest) |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 468 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 469 | DEFINE_TESTCLASS_SHORT(PathOpsAngleTestOne) |
| 470 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 471 | // DEFINE_TESTCLASS_SHORT(PathOpsAngleFindSlop) |