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 #include "PathOpsTestCommon.h" 8 #include "SkIntersections.h" 9 #include "SkPathOpsCubic.h" 10 #include "SkPathOpsLine.h" 11 #include "SkReduceOrder.h" 12 #include "Test.h" 13 14 struct lineCubic { 15 SkDCubic cubic; 16 SkDLine line; 17 }; 18 19 static lineCubic failLineCubicTests[] = { 20 {{{{37.5273438,-1.44140625}, {37.8736992,-1.69921875}, {38.1640625,-2.140625}, 21 {38.3984375,-2.765625}}}, 22 {{{40.625,-5.7890625}, {37.7109375,1.3515625}}}}, 23 }; 24 25 static const size_t failLineCubicTests_count = SK_ARRAY_COUNT(failLineCubicTests); 26 27 static void testFail(skiatest::Reporter* reporter, int iIndex) { 28 const SkDCubic& cubic = failLineCubicTests[iIndex].cubic; 29 SkASSERT(ValidCubic(cubic)); 30 const SkDLine& line = failLineCubicTests[iIndex].line; 31 SkASSERT(ValidLine(line)); 32 SkReduceOrder reduce1; 33 SkReduceOrder reduce2; 34 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics); 35 int order2 = reduce2.reduce(line); 36 if (order1 < 4) { 37 SkDebugf("[%d] cubic order=%d\n", iIndex, order1); 38 REPORTER_ASSERT(reporter, 0); 39 } 40 if (order2 < 2) { 41 SkDebugf("[%d] line order=%d\n", iIndex, order2); 42 REPORTER_ASSERT(reporter, 0); 43 } 44 if (order1 == 4 && order2 == 2) { 45 SkIntersections i; 46 int roots = i.intersect(cubic, line); 47 REPORTER_ASSERT(reporter, roots == 0); 48 } 49 } 50 51 static lineCubic lineCubicTests[] = { 52 {{{{0, 6}, {1.0851458311080933, 4.3722810745239258}, {1.5815209150314331, 3.038947582244873}, {1.9683018922805786, 1.9999997615814209}}}, 53 {{{3,2}, {1,2}}}}, 54 55 {{{{0.468027353,4}, {1.06734705,1.33333337}, {1.36700678,0}, {3,0}}}, 56 {{{2,1}, {0,1}}}}, 57 58 {{{{-634.60540771484375, -481.262939453125}, {266.2696533203125, -752.70867919921875}, 59 {-751.8370361328125, -317.37921142578125}, {-969.7427978515625, 824.7255859375}}}, 60 {{{-287.9506133720805678, -557.1376476615772617}, 61 {-285.9506133720805678, -557.1376476615772617}}}}, 62 63 {{{{36.7184372,0.888650894}, {36.7184372,0.888650894}, {35.1233864,0.554015458}, 64 {34.5114098,-0.115255356}}}, {{{35.4531212,0}, {31.9375,0}}}}, 65 66 {{{{421, 378}, {421, 380.209137f}, {418.761414f, 382}, {416, 382}}}, 67 {{{320, 378}, {421, 378.000031f}}}}, 68 69 {{{{416, 383}, {418.761414f, 383}, {421, 380.761414f}, {421, 378}}}, 70 {{{320, 378}, {421, 378.000031f}}}}, 71 72 {{{{154,715}, {151.238571,715}, {149,712.761414}, {149,710}}}, 73 {{{149,675}, {149,710.001465}}}}, 74 75 {{{{0,1}, {1,6}, {4,1}, {4,3}}}, 76 {{{6,1}, {1,4}}}}, 77 78 {{{{0,1}, {2,6}, {4,1}, {5,4}}}, 79 {{{6,2}, {1,4}}}}, 80 81 {{{{0,4}, {3,4}, {6,2}, {5,2}}}, 82 {{{4,3}, {2,6}}}}, 83 #if 0 84 {{{{258, 122}, {260.761414, 122}, { 263, 124.238579}, {263, 127}}}, 85 {{{259.82843, 125.17157}, {261.535522, 123.46447}}}}, 86 #endif 87 {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125}, 88 {1030.318359375,321}}}, 89 {{{979.30487060546875,561}, {1036.695068359375,291}}}}, 90 {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875}, 91 {235.68154907226562,531}}}, 92 {{{286.69512939453125,291}, {229.30485534667969,561}}}}, 93 {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}}, 94 {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}}, 95 }; 96 97 static const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests); 98 99 static int doIntersect(SkIntersections& intersections, const SkDCubic& cubic, const SkDLine& line) { 100 int result; 101 bool flipped = false; 102 if (line[0].fX == line[1].fX) { 103 double top = line[0].fY; 104 double bottom = line[1].fY; 105 flipped = top > bottom; 106 if (flipped) { 107 SkTSwap<double>(top, bottom); 108 } 109 result = intersections.vertical(cubic, top, bottom, line[0].fX, flipped); 110 } else if (line[0].fY == line[1].fY) { 111 double left = line[0].fX; 112 double right = line[1].fX; 113 flipped = left > right; 114 if (flipped) { 115 SkTSwap<double>(left, right); 116 } 117 result = intersections.horizontal(cubic, left, right, line[0].fY, flipped); 118 } else { 119 intersections.intersect(cubic, line); 120 result = intersections.used(); 121 } 122 return result; 123 } 124 125 static void testOne(skiatest::Reporter* reporter, int iIndex) { 126 const SkDCubic& cubic = lineCubicTests[iIndex].cubic; 127 SkASSERT(ValidCubic(cubic)); 128 const SkDLine& line = lineCubicTests[iIndex].line; 129 SkASSERT(ValidLine(line)); 130 SkReduceOrder reduce1; 131 SkReduceOrder reduce2; 132 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics); 133 int order2 = reduce2.reduce(line); 134 if (order1 < 4) { 135 SkDebugf("[%d] cubic order=%d\n", iIndex, order1); 136 REPORTER_ASSERT(reporter, 0); 137 } 138 if (order2 < 2) { 139 SkDebugf("[%d] line order=%d\n", iIndex, order2); 140 REPORTER_ASSERT(reporter, 0); 141 } 142 if (order1 == 4 && order2 == 2) { 143 SkIntersections i; 144 int roots = doIntersect(i, cubic, line); 145 for (int pt = 0; pt < roots; ++pt) { 146 double tt1 = i[0][pt]; 147 SkDPoint xy1 = cubic.ptAtT(tt1); 148 double tt2 = i[1][pt]; 149 SkDPoint xy2 = line.ptAtT(tt2); 150 if (!xy1.approximatelyEqual(xy2)) { 151 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n", 152 __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY); 153 } 154 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2)); 155 } 156 #if ONE_OFF_DEBUG 157 double cubicT = i[0][0]; 158 SkDPoint prev = cubic.ptAtT(cubicT * 2 - 1); 159 SkDPoint sect = cubic.ptAtT(cubicT); 160 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY); 161 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY); 162 SkDPoint prevL = line.ptAtT(i[1][0] - 0.0000007); 163 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY); 164 SkDPoint nextL = line.ptAtT(i[1][0] + 0.0000007); 165 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", i.pt(0).fX, i.pt(0).fY, nextL.fX, nextL.fY); 166 SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL), 167 sect.distance(i.pt(0)), cubic[3].distance(prevL)); 168 #endif 169 } 170 } 171 172 DEF_TEST(PathOpsFailCubicLineIntersection, reporter) { 173 for (size_t index = 0; index < failLineCubicTests_count; ++index) { 174 int iIndex = static_cast<int>(index); 175 testFail(reporter, iIndex); 176 reporter->bumpTestCount(); 177 } 178 } 179 180 DEF_TEST(PathOpsCubicLineIntersection, reporter) { 181 for (size_t index = 0; index < lineCubicTests_count; ++index) { 182 int iIndex = static_cast<int>(index); 183 testOne(reporter, iIndex); 184 reporter->bumpTestCount(); 185 } 186 } 187 188 DEF_TEST(PathOpsCubicLineIntersectionOneOff, reporter) { 189 int iIndex = 0; 190 testOne(reporter, iIndex); 191 const SkDCubic& cubic = lineCubicTests[iIndex].cubic; 192 const SkDLine& line = lineCubicTests[iIndex].line; 193 SkIntersections i; 194 i.intersect(cubic, line); 195 SkASSERT(i.used() == 1); 196 } 197