xref: /aosp_15_r20/external/skia/tests/PathOpsQuadLineIntersectionTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkTypes.h"
8 #include "include/private/base/SkDebug.h"
9 #include "src/pathops/SkIntersections.h"
10 #include "src/pathops/SkPathOpsLine.h"
11 #include "src/pathops/SkPathOpsPoint.h"
12 #include "src/pathops/SkPathOpsQuad.h"
13 #include "src/pathops/SkReduceOrder.h"
14 #include "tests/PathOpsTestCommon.h"
15 #include "tests/Test.h"
16 
17 #include <array>
18 #include <cstddef>
19 #include <utility>
20 
21 static struct lineQuad {
22     QuadPts quad;
23     SkDLine line;
24     int result;
25     SkDPoint expected[2];
26 } lineQuadTests[] = {
27     //        quad                    line                  results
28     {{{{1, 1}, {2, 1}, {0, 2}}}, {{{0, 0}, {1, 1}}},  1,  {{1, 1}, {0, 0}} },
29     {{{{0, 0}, {1, 1}, {3, 1}}}, {{{0, 0}, {3, 1}}},  2,  {{0, 0}, {3, 1}} },
30     {{{{2, 0}, {1, 1}, {2, 2}}}, {{{0, 0}, {0, 2}}},  0,  {{0, 0}, {0, 0}} },
31     {{{{4, 0}, {0, 1}, {4, 2}}}, {{{3, 1}, {4, 1}}},  0,  {{0, 0}, {0, 0}} },
32     {{{{0, 0}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}},  1,  {{.25, .75}, {0, 0}} },
33 };
34 
35 static size_t lineQuadTests_count = std::size(lineQuadTests);
36 
doIntersect(SkIntersections & intersections,const SkDQuad & quad,const SkDLine & line,bool & flipped)37 static int doIntersect(SkIntersections& intersections, const SkDQuad& quad, const SkDLine& line,
38                        bool& flipped) {
39     int result;
40     flipped = false;
41     if (line[0].fX == line[1].fX) {
42         double top = line[0].fY;
43         double bottom = line[1].fY;
44         flipped = top > bottom;
45         if (flipped) {
46             using std::swap;
47             swap(top, bottom);
48         }
49         result = intersections.vertical(quad, top, bottom, line[0].fX, flipped);
50     } else if (line[0].fY == line[1].fY) {
51         double left = line[0].fX;
52         double right = line[1].fX;
53         flipped = left > right;
54         if (flipped) {
55             using std::swap;
56             swap(left, right);
57         }
58         result = intersections.horizontal(quad, left, right, line[0].fY, flipped);
59     } else {
60         intersections.intersect(quad, line);
61         result = intersections.used();
62     }
63     return result;
64 }
65 
66 static struct oneLineQuad {
67     QuadPts quad;
68     SkDLine line;
69 } oneOffs[] = {
70     {{{{97.9337616,100}, {88,112.94265}, {88,130}}},
71      {{{88.919838,120}, {107.058823,120}}}},
72     {{{{447.96701049804687, 894.4381103515625}, {448.007080078125, 894.4239501953125},
73        {448.0140380859375, 894.4215087890625}}},
74      {{{490.43548583984375, 879.40740966796875}, {405.59262084960937, 909.435546875}}}},
75     {{{{142.589081, 102.283646}, {149.821579, 100}, {158, 100}}},
76         {{{90, 230}, {160, 60}}}},
77     {{{{1101, 10}, {1101, 8.3431453704833984}, {1099.828857421875, 7.1711997985839844}}},
78         {{{1099.828857421875,7.1711711883544922}, {1099.121337890625,7.8786783218383789}}}},
79     {{{{973, 507}, {973, 508.24264526367187}, {972.12158203125, 509.12161254882812}}},
80         {{{930, 467}, {973, 510}}}},
81     {{{{369.848602, 145.680267}, {382.360413, 121.298294}, {406.207703, 121.298294}}},
82         {{{406.207703, 121.298294}, {348.781738, 123.864815}}}},
83 };
84 
85 static size_t oneOffs_count = std::size(oneOffs);
86 
testOneOffs(skiatest::Reporter * reporter)87 static void testOneOffs(skiatest::Reporter* reporter) {
88     bool flipped = false;
89     for (size_t index = 0; index < oneOffs_count; ++index) {
90         const QuadPts& q = oneOffs[index].quad;
91         SkDQuad quad;
92         quad.debugSet(q.fPts);
93         SkASSERT(ValidQuad(quad));
94         const SkDLine& line = oneOffs[index].line;
95         SkASSERT(ValidLine(line));
96         SkIntersections intersections;
97         int result = doIntersect(intersections, quad, line, flipped);
98         for (int inner = 0; inner < result; ++inner) {
99             double quadT = intersections[0][inner];
100             SkDPoint quadXY = quad.ptAtT(quadT);
101             double lineT = intersections[1][inner];
102             SkDPoint lineXY = line.ptAtT(lineT);
103             if (!quadXY.approximatelyEqual(lineXY)) {
104                 quadXY.approximatelyEqual(lineXY);
105             }
106             REPORTER_ASSERT(reporter, quadXY.approximatelyEqual(lineXY));
107         }
108     }
109 }
110 
DEF_TEST(PathOpsQuadLineIntersectionOneOff,reporter)111 DEF_TEST(PathOpsQuadLineIntersectionOneOff, reporter) {
112     testOneOffs(reporter);
113 }
114 
DEF_TEST(PathOpsQuadLineIntersection,reporter)115 DEF_TEST(PathOpsQuadLineIntersection, reporter) {
116     for (size_t index = 0; index < lineQuadTests_count; ++index) {
117         int iIndex = static_cast<int>(index);
118         const QuadPts& q = lineQuadTests[index].quad;
119         SkDQuad quad;
120         quad.debugSet(q.fPts);
121         SkASSERT(ValidQuad(quad));
122         const SkDLine& line = lineQuadTests[index].line;
123         SkASSERT(ValidLine(line));
124         SkReduceOrder reducer1, reducer2;
125         int order1 = reducer1.reduce(quad);
126         int order2 = reducer2.reduce(line);
127         if (order1 < 3) {
128             SkDebugf("%s [%d] quad order=%d\n", __FUNCTION__, iIndex, order1);
129             REPORTER_ASSERT(reporter, 0);
130         }
131         if (order2 < 2) {
132             SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2);
133             REPORTER_ASSERT(reporter, 0);
134         }
135         SkIntersections intersections;
136         bool flipped = false;
137         int result = doIntersect(intersections, quad, line, flipped);
138         REPORTER_ASSERT(reporter, result == lineQuadTests[index].result);
139         if (intersections.used() <= 0) {
140             continue;
141         }
142         for (int pt = 0; pt < result; ++pt) {
143             double tt1 = intersections[0][pt];
144             REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1);
145             SkDPoint t1 = quad.ptAtT(tt1);
146             double tt2 = intersections[1][pt];
147             REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1);
148             SkDPoint t2 = line.ptAtT(tt2);
149             if (!t1.approximatelyEqual(t2)) {
150                 SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
151                     __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY);
152                 REPORTER_ASSERT(reporter, 0);
153             }
154             if (!t1.approximatelyEqual(lineQuadTests[index].expected[0])
155                     && (lineQuadTests[index].result == 1
156                     || !t1.approximatelyEqual(lineQuadTests[index].expected[1]))) {
157                 SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY);
158                 REPORTER_ASSERT(reporter, 0);
159             }
160         }
161     }
162 }
163