• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/SkGeometry.h"
8 #include "src/pathops/SkIntersections.h"
9 #include "src/pathops/SkPathOpsConic.h"
10 #include "src/pathops/SkPathOpsLine.h"
11 #include "src/pathops/SkReduceOrder.h"
12 #include "tests/PathOpsExtendedTest.h"
13 #include "tests/PathOpsTestCommon.h"
14 #include "tests/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             }
92             REPORTER_ASSERT(reporter, conicXY.approximatelyEqual(lineXY));
93         }
94     }
95 }
96 
DEF_TEST(PathOpsConicLineIntersectionOneOff,reporter)97 DEF_TEST(PathOpsConicLineIntersectionOneOff, reporter) {
98     testOneOffs(reporter);
99 }
100 
DEF_TEST(PathOpsConicLineIntersection,reporter)101 DEF_TEST(PathOpsConicLineIntersection, reporter) {
102     for (size_t index = 0; index < lineConicTests_count; ++index) {
103         int iIndex = static_cast<int>(index);
104         const ConicPts& c = lineConicTests[index].conic;
105         SkDConic conic;
106         conic.debugSet(c.fPts.fPts, c.fWeight);
107         SkASSERT(ValidConic(conic));
108         const SkDLine& line = lineConicTests[index].line;
109         SkASSERT(ValidLine(line));
110         SkReduceOrder reducer;
111         SkPoint pts[3] = { conic.fPts.fPts[0].asSkPoint(), conic.fPts.fPts[1].asSkPoint(),
112             conic.fPts.fPts[2].asSkPoint() };
113         SkPoint reduced[3];
114         SkConic floatConic;
115         floatConic.set(pts, conic.fWeight);
116         SkPath::Verb order1 = SkReduceOrder::Conic(floatConic, reduced);
117         if (order1 != SkPath::kConic_Verb) {
118             SkDebugf("%s [%d] conic verb=%d\n", __FUNCTION__, iIndex, order1);
119             REPORTER_ASSERT(reporter, 0);
120         }
121         int order2 = reducer.reduce(line);
122         if (order2 < 2) {
123             SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2);
124             REPORTER_ASSERT(reporter, 0);
125         }
126         SkIntersections intersections;
127         bool flipped = false;
128         int result = doIntersect(intersections, conic, line, flipped);
129         REPORTER_ASSERT(reporter, result == lineConicTests[index].result);
130         if (intersections.used() <= 0) {
131             continue;
132         }
133         for (int pt = 0; pt < result; ++pt) {
134             double tt1 = intersections[0][pt];
135             REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1);
136             SkDPoint t1 = conic.ptAtT(tt1);
137             double tt2 = intersections[1][pt];
138             REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1);
139             SkDPoint t2 = line.ptAtT(tt2);
140             if (!t1.approximatelyEqual(t2)) {
141                 SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
142                     __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY);
143                 REPORTER_ASSERT(reporter, 0);
144             }
145             if (!t1.approximatelyEqual(lineConicTests[index].expected[0])
146                     && (lineConicTests[index].result == 1
147                     || !t1.approximatelyEqual(lineConicTests[index].expected[1]))) {
148                 SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY);
149                 REPORTER_ASSERT(reporter, 0);
150             }
151         }
152     }
153 }
154