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" |
| 8 | #include "Test.h" |
| 9 | |
| 10 | static const SkPoint cubics[][4] = { |
| 11 | {{0, 1}, {2, 6}, {4, 2}, {5, 3}} |
| 12 | }; |
| 13 | |
| 14 | static const SkPoint lines[][2] = { |
| 15 | {{6, 2}, {2, 4}} |
| 16 | }; |
| 17 | |
| 18 | struct SortSet { |
| 19 | const SkPoint* ptData; |
| 20 | int ptCount; |
| 21 | double tStart; |
| 22 | double tEnd; |
| 23 | }; |
| 24 | |
| 25 | static const SortSet set1[] = { |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 26 | {cubics[0], 4, 0.66666987081928919, 0.875}, |
| 27 | {lines[0], 2, 0.574070336, 0.388888889}, |
| 28 | {cubics[0], 4, 0.66666987081928919, 0.4050371120499307 }, |
| 29 | {lines[0], 2, 0.574070336, 0.9140625}, |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static const SortSet set2[] = { |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 33 | {cubics[0], 4, 0.666666667, 0.875}, |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 34 | {lines[0], 2, 0.574074074, 0.388888889}, |
| 35 | {cubics[0], 4, 0.666666667, 0.405037112}, |
| 36 | {lines[0], 2, 0.574074074, 0.9140625}, |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct SortSetTests { |
| 40 | const SortSet* set; |
| 41 | size_t count; |
| 42 | }; |
| 43 | |
| 44 | static const SortSetTests tests[] = { |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 45 | { set2, SK_ARRAY_COUNT(set2) }, |
| 46 | { set1, SK_ARRAY_COUNT(set1) } |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
skia.committer@gmail.com | 7841c63 | 2013-04-16 07:01:17 +0000 | [diff] [blame] | 49 | static void setup(const SortSet* set, const size_t idx, SkPoint const ** data, |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 50 | SkOpSegment* seg, int* ts) { |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 51 | SkPoint start, end; |
| 52 | if (set[idx].ptCount == 2) { |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 53 | *data = set[idx].ptData; |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 54 | seg->addLine(*data, false, false); |
| 55 | SkDLine dLine; |
| 56 | dLine.set(set[idx].ptData); |
| 57 | start = dLine.xyAtT(set[idx].tStart).asSkPoint(); |
| 58 | end = dLine.xyAtT(set[idx].tEnd).asSkPoint(); |
| 59 | } else if (set[idx].ptCount == 4) { |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 60 | *data = set[idx].ptData; |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 61 | seg->addCubic(*data, false, false); |
| 62 | SkDCubic dCubic; |
| 63 | dCubic.set(set[idx].ptData); |
| 64 | start = dCubic.xyAtT(set[idx].tStart).asSkPoint(); |
| 65 | end = dCubic.xyAtT(set[idx].tEnd).asSkPoint(); |
| 66 | } |
| 67 | seg->addT(NULL, start, set[idx].tStart); |
| 68 | seg->addT(NULL, end, set[idx].tEnd); |
| 69 | seg->addT(NULL, set[idx].ptData[0], 0); |
| 70 | seg->addT(NULL, set[idx].ptData[set[idx].ptCount - 1], 1); |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 71 | int tIndex = 0; |
| 72 | do { |
| 73 | if (seg->t(tIndex) == set[idx].tStart) { |
| 74 | ts[0] = tIndex; |
| 75 | } |
| 76 | if (seg->t(tIndex) == set[idx].tEnd) { |
| 77 | ts[1] = tIndex; |
| 78 | } |
| 79 | if (seg->t(tIndex) >= 1) { |
| 80 | break; |
| 81 | } |
| 82 | } while (++tIndex); |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static void PathOpsAngleTest(skiatest::Reporter* reporter) { |
| 86 | for (size_t index = 0; index < SK_ARRAY_COUNT(tests); ++index) { |
| 87 | const SortSetTests& test = tests[index]; |
| 88 | for (size_t idxL = 0; idxL < test.count - 1; ++idxL) { |
| 89 | SkOpSegment lesser, greater; |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 90 | int lesserTs[2], greaterTs[2]; |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 91 | const SkPoint* lesserData, * greaterData; |
| 92 | const SortSet* set = test.set; |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 93 | setup(set, idxL, &lesserData, &lesser, lesserTs); |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 94 | size_t idxG = idxL + 1; |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 95 | setup(set, idxG, &greaterData, &greater, greaterTs); |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 96 | SkOpAngle first, second; |
| 97 | first.set(lesserData, (SkPath::Verb) (set[idxL].ptCount - 1), &lesser, |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 98 | lesserTs[0], lesserTs[1], lesser.spans()); |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 99 | second.set(greaterData, (SkPath::Verb) (set[idxG].ptCount - 1), &greater, |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 100 | greaterTs[0], greaterTs[1], greater.spans()); |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 101 | bool compare = first < second; |
| 102 | if (!compare) { |
| 103 | SkDebugf("%s test[%d]: lesser[%d] > greater[%d]\n", __FUNCTION__, |
| 104 | index, idxL, idxG); |
| 105 | } |
| 106 | REPORTER_ASSERT(reporter, compare); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | #include "TestClassDef.h" |
| 112 | DEFINE_TESTCLASS_SHORT(PathOpsAngleTest) |