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 "PathOpsExtendedTest.h" 8 #include "PathOpsTestCommon.h" 9 #include "SkGeometry.h" 10 #include "SkIntersections.h" 11 #include "SkPathOpsConic.h" 12 #include "SkPathOpsLine.h" 13 #include "SkReduceOrder.h" 14 #include "Test.h" 15 16 static struct lineConic { 17 ConicPts conic; 18 SkDLine line; 19 int result; 20 SkDPoint expected[2]; 21 } lineConicTests[] = { 22 { 23 {{{{30.6499996,25.6499996}, {30.6499996,20.6499996}, {25.6499996,20.6499996}}}, 0.707107008f}, 24 {{{25.6499996,20.6499996}, {45.6500015,20.6499996}}}, 25 1, 26 {{25.6499996,20.6499996}, {0,0}} 27 }, 28 }; 29 30 static size_t lineConicTests_count = SK_ARRAY_COUNT(lineConicTests); 31 32 static int doIntersect(SkIntersections& intersections, const SkDConic& conic, const SkDLine& line, 33 bool& flipped) { 34 int result; 35 flipped = false; 36 if (line[0].fX == line[1].fX) { 37 double top = line[0].fY; 38 double bottom = line[1].fY; 39 flipped = top > bottom; 40 if (flipped) { 41 SkTSwap<double>(top, bottom); 42 } 43 result = intersections.vertical(conic, top, bottom, line[0].fX, flipped); 44 } else if (line[0].fY == line[1].fY) { 45 double left = line[0].fX; 46 double right = line[1].fX; 47 flipped = left > right; 48 if (flipped) { 49 SkTSwap<double>(left, right); 50 } 51 result = intersections.horizontal(conic, left, right, line[0].fY, flipped); 52 } else { 53 intersections.intersect(conic, line); 54 result = intersections.used(); 55 } 56 return result; 57 } 58 59 static struct oneLineConic { 60 ConicPts conic; 61 SkDLine line; 62 } oneOffs[] = { 63 {{{{{30.6499996,25.6499996}, {30.6499996,20.6499996}, {25.6499996,20.6499996}}}, 0.707107008f}, 64 {{{25.6499996,20.6499996}, {45.6500015,20.6499996}}}} 65 }; 66 67 static size_t oneOffs_count = SK_ARRAY_COUNT(oneOffs); 68 69 static void testOneOffs(skiatest::Reporter* reporter) { 70 bool flipped = false; 71 for (size_t index = 0; index < oneOffs_count; ++index) { 72 const ConicPts& c = oneOffs[index].conic; 73 SkDConic conic; 74 conic.debugSet(c.fPts.fPts, c.fWeight); 75 SkASSERT(ValidConic(conic)); 76 const SkDLine& line = oneOffs[index].line; 77 SkASSERT(ValidLine(line)); 78 SkIntersections intersections; 79 int result = doIntersect(intersections, conic, line, flipped); 80 for (int inner = 0; inner < result; ++inner) { 81 double conicT = intersections[0][inner]; 82 SkDPoint conicXY = conic.ptAtT(conicT); 83 double lineT = intersections[1][inner]; 84 SkDPoint lineXY = line.ptAtT(lineT); 85 if (!conicXY.approximatelyEqual(lineXY)) { 86 conicXY.approximatelyEqual(lineXY); 87 SkDebugf(""); 88 } 89 REPORTER_ASSERT(reporter, conicXY.approximatelyEqual(lineXY)); 90 } 91 } 92 } 93 94 DEF_TEST(PathOpsConicLineIntersectionOneOff, reporter) { 95 testOneOffs(reporter); 96 } 97 98 DEF_TEST(PathOpsConicLineIntersection, reporter) { 99 for (size_t index = 0; index < lineConicTests_count; ++index) { 100 int iIndex = static_cast<int>(index); 101 const ConicPts& c = lineConicTests[index].conic; 102 SkDConic conic; 103 conic.debugSet(c.fPts.fPts, c.fWeight); 104 SkASSERT(ValidConic(conic)); 105 const SkDLine& line = lineConicTests[index].line; 106 SkASSERT(ValidLine(line)); 107 SkReduceOrder reducer; 108 SkPoint pts[3] = { conic.fPts.fPts[0].asSkPoint(), conic.fPts.fPts[1].asSkPoint(), 109 conic.fPts.fPts[2].asSkPoint() }; 110 SkPoint reduced[3]; 111 SkConic floatConic; 112 floatConic.set(pts, conic.fWeight); 113 SkPath::Verb order1 = SkReduceOrder::Conic(floatConic, reduced); 114 if (order1 != SkPath::kConic_Verb) { 115 SkDebugf("%s [%d] conic verb=%d\n", __FUNCTION__, iIndex, order1); 116 REPORTER_ASSERT(reporter, 0); 117 } 118 int order2 = reducer.reduce(line); 119 if (order2 < 2) { 120 SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2); 121 REPORTER_ASSERT(reporter, 0); 122 } 123 SkIntersections intersections; 124 bool flipped = false; 125 int result = doIntersect(intersections, conic, line, flipped); 126 REPORTER_ASSERT(reporter, result == lineConicTests[index].result); 127 if (intersections.used() <= 0) { 128 continue; 129 } 130 for (int pt = 0; pt < result; ++pt) { 131 double tt1 = intersections[0][pt]; 132 REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1); 133 SkDPoint t1 = conic.ptAtT(tt1); 134 double tt2 = intersections[1][pt]; 135 REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1); 136 SkDPoint t2 = line.ptAtT(tt2); 137 if (!t1.approximatelyEqual(t2)) { 138 SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n", 139 __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY); 140 REPORTER_ASSERT(reporter, 0); 141 } 142 if (!t1.approximatelyEqual(lineConicTests[index].expected[0]) 143 && (lineConicTests[index].result == 1 144 || !t1.approximatelyEqual(lineConicTests[index].expected[1]))) { 145 SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY); 146 REPORTER_ASSERT(reporter, 0); 147 } 148 } 149 } 150 } 151