caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +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 | */ |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 7 | #include "PathOpsTestCommon.h" |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 8 | #include "SkIntersections.h" |
| 9 | #include "SkPathOpsCubic.h" |
| 10 | #include "SkPathOpsLine.h" |
| 11 | #include "SkReduceOrder.h" |
| 12 | #include "Test.h" |
| 13 | |
| 14 | static struct lineCubic { |
| 15 | SkDCubic cubic; |
| 16 | SkDLine line; |
| 17 | } lineCubicTests[] = { |
skia.committer@gmail.com | b0a0589 | 2013-10-03 07:01:37 +0000 | [diff] [blame] | 18 | {{{{0,1}, {1,6}, {4,1}, {4,3}}}, |
| 19 | {{{6,1}, {1,4}}}}, |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 20 | |
| 21 | {{{{0,1}, {2,6}, {4,1}, {5,4}}}, |
| 22 | {{{6,2}, {1,4}}}}, |
| 23 | |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 24 | {{{{0,4}, {3,4}, {6,2}, {5,2}}}, |
| 25 | {{{4,3}, {2,6}}}}, |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 26 | #if 0 |
| 27 | {{{{258, 122}, {260.761414, 122}, { 263, 124.238579}, {263, 127}}}, |
| 28 | {{{259.82843, 125.17157}, {261.535522, 123.46447}}}}, |
| 29 | #endif |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 30 | {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125}, |
| 31 | {1030.318359375,321}}}, |
| 32 | {{{979.30487060546875,561}, {1036.695068359375,291}}}}, |
| 33 | {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875}, |
| 34 | {235.68154907226562,531}}}, |
| 35 | {{{286.69512939453125,291}, {229.30485534667969,561}}}}, |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 36 | {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}}, |
| 37 | {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}}, |
| 38 | }; |
| 39 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 40 | static const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 41 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 42 | static void testOne(skiatest::Reporter* reporter, int iIndex) { |
| 43 | const SkDCubic& cubic = lineCubicTests[iIndex].cubic; |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 44 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 45 | const SkDLine& line = lineCubicTests[iIndex].line; |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 46 | SkASSERT(ValidLine(line)); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 47 | SkReduceOrder reduce1; |
| 48 | SkReduceOrder reduce2; |
| 49 | int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics, |
| 50 | SkReduceOrder::kFill_Style); |
| 51 | int order2 = reduce2.reduce(line); |
| 52 | if (order1 < 4) { |
| 53 | SkDebugf("[%d] cubic order=%d\n", iIndex, order1); |
| 54 | REPORTER_ASSERT(reporter, 0); |
| 55 | } |
| 56 | if (order2 < 2) { |
| 57 | SkDebugf("[%d] line order=%d\n", iIndex, order2); |
| 58 | REPORTER_ASSERT(reporter, 0); |
| 59 | } |
| 60 | if (order1 == 4 && order2 == 2) { |
| 61 | SkIntersections i; |
| 62 | int roots = i.intersect(cubic, line); |
| 63 | for (int pt = 0; pt < roots; ++pt) { |
| 64 | double tt1 = i[0][pt]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 65 | SkDPoint xy1 = cubic.ptAtT(tt1); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 66 | double tt2 = i[1][pt]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 67 | SkDPoint xy2 = line.ptAtT(tt2); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 68 | if (!xy1.approximatelyEqual(xy2)) { |
| 69 | SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n", |
| 70 | __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 71 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 72 | REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2)); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 77 | static void PathOpsCubicLineIntersectionTest(skiatest::Reporter* reporter) { |
| 78 | for (size_t index = 0; index < lineCubicTests_count; ++index) { |
| 79 | int iIndex = static_cast<int>(index); |
| 80 | testOne(reporter, iIndex); |
| 81 | reporter->bumpTestCount(); |
| 82 | } |
| 83 | } |
| 84 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 85 | static void PathOpsCubicLineIntersectionOneOffTest(skiatest::Reporter* reporter) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 86 | int iIndex = 0; |
| 87 | testOne(reporter, iIndex); |
| 88 | const SkDCubic& cubic = lineCubicTests[iIndex].cubic; |
| 89 | const SkDLine& line = lineCubicTests[iIndex].line; |
| 90 | SkIntersections i; |
| 91 | i.intersect(cubic, line); |
| 92 | SkASSERT(i.used() == 1); |
| 93 | #if ONE_OFF_DEBUG |
| 94 | double cubicT = i[0][0]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 95 | SkDPoint prev = cubic.ptAtT(cubicT * 2 - 1); |
| 96 | SkDPoint sect = cubic.ptAtT(cubicT); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 97 | double left[3] = { line.isLeft(prev), line.isLeft(sect), line.isLeft(cubic[3]) }; |
| 98 | SkDebugf("cubic=(%1.9g, %1.9g, %1.9g)\n", left[0], left[1], left[2]); |
| 99 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY); |
| 100 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 101 | SkDPoint prevL = line.ptAtT(i[1][0] - 0.0000007); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 102 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 103 | SkDPoint nextL = line.ptAtT(i[1][0] + 0.0000007); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 104 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", i.pt(0).fX, i.pt(0).fY, nextL.fX, nextL.fY); |
skia.committer@gmail.com | a4aced4 | 2013-07-09 07:00:56 +0000 | [diff] [blame] | 105 | SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL), |
| 106 | sect.distance(i.pt(0)), cubic[3].distance(prevL)); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 107 | #endif |
| 108 | } |
| 109 | |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 110 | #include "TestClassDef.h" |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 111 | DEFINE_TESTCLASS_SHORT(PathOpsCubicLineIntersectionTest) |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 112 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 113 | DEFINE_TESTCLASS_SHORT(PathOpsCubicLineIntersectionOneOffTest) |