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