blob: 40bb5f6993d3c93a6368df3a6c9f6c00a5453c16 [file] [log] [blame]
caryclark1049f122015-04-20 08:31:59 -07001/*
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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#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"
caryclark1049f122015-04-20 08:31:59 -070015
Ben Wagnerf08d1d02018-06-18 15:11:00 -040016#include <utility>
17
caryclark1049f122015-04-20 08:31:59 -070018static struct lineConic {
caryclarka35ab3e2016-10-20 08:32:18 -070019 ConicPts conic;
caryclark1049f122015-04-20 08:31:59 -070020 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}}},
halcanary9d524f22016-03-29 09:03:52 -070027 1,
caryclark1049f122015-04-20 08:31:59 -070028 {{25.6499996,20.6499996}, {0,0}}
29 },
30};
31
32static size_t lineConicTests_count = SK_ARRAY_COUNT(lineConicTests);
33
34static 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) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -040043 using std::swap;
44 swap(top, bottom);
caryclark1049f122015-04-20 08:31:59 -070045 }
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) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -040052 using std::swap;
53 swap(left, right);
caryclark1049f122015-04-20 08:31:59 -070054 }
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
63static struct oneLineConic {
caryclarka35ab3e2016-10-20 08:32:18 -070064 ConicPts conic;
caryclark1049f122015-04-20 08:31:59 -070065 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
71static size_t oneOffs_count = SK_ARRAY_COUNT(oneOffs);
72
73static void testOneOffs(skiatest::Reporter* reporter) {
74 bool flipped = false;
75 for (size_t index = 0; index < oneOffs_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -070076 const ConicPts& c = oneOffs[index].conic;
77 SkDConic conic;
78 conic.debugSet(c.fPts.fPts, c.fWeight);
caryclark1049f122015-04-20 08:31:59 -070079 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
98DEF_TEST(PathOpsConicLineIntersectionOneOff, reporter) {
99 testOneOffs(reporter);
100}
101
102DEF_TEST(PathOpsConicLineIntersection, reporter) {
103 for (size_t index = 0; index < lineConicTests_count; ++index) {
104 int iIndex = static_cast<int>(index);
caryclarka35ab3e2016-10-20 08:32:18 -0700105 const ConicPts& c = lineConicTests[index].conic;
106 SkDConic conic;
107 conic.debugSet(c.fPts.fPts, c.fWeight);
caryclark1049f122015-04-20 08:31:59 -0700108 SkASSERT(ValidConic(conic));
109 const SkDLine& line = lineConicTests[index].line;
110 SkASSERT(ValidLine(line));
111 SkReduceOrder reducer;
halcanary9d524f22016-03-29 09:03:52 -0700112 SkPoint pts[3] = { conic.fPts.fPts[0].asSkPoint(), conic.fPts.fPts[1].asSkPoint(),
caryclark1049f122015-04-20 08:31:59 -0700113 conic.fPts.fPts[2].asSkPoint() };
114 SkPoint reduced[3];
caryclark27c015d2016-09-23 05:47:20 -0700115 SkConic floatConic;
116 floatConic.set(pts, conic.fWeight);
117 SkPath::Verb order1 = SkReduceOrder::Conic(floatConic, reduced);
caryclark1049f122015-04-20 08:31:59 -0700118 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}