blob: c3d4a2afe3a4c2d2a14ff2f40e3b5cfb328f82af [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 */
7#include "PathOpsExtendedTest.h"
8#include "PathOpsTestCommon.h"
caryclark27c015d2016-09-23 05:47:20 -07009#include "SkGeometry.h"
caryclark1049f122015-04-20 08:31:59 -070010#include "SkIntersections.h"
11#include "SkPathOpsConic.h"
12#include "SkPathOpsLine.h"
13#include "SkReduceOrder.h"
14#include "Test.h"
15
16static struct lineConic {
caryclarka35ab3e2016-10-20 08:32:18 -070017 ConicPts conic;
caryclark1049f122015-04-20 08:31:59 -070018 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}}},
halcanary9d524f22016-03-29 09:03:52 -070025 1,
caryclark1049f122015-04-20 08:31:59 -070026 {{25.6499996,20.6499996}, {0,0}}
27 },
28};
29
30static size_t lineConicTests_count = SK_ARRAY_COUNT(lineConicTests);
31
32static 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
59static struct oneLineConic {
caryclarka35ab3e2016-10-20 08:32:18 -070060 ConicPts conic;
caryclark1049f122015-04-20 08:31:59 -070061 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
67static size_t oneOffs_count = SK_ARRAY_COUNT(oneOffs);
68
69static void testOneOffs(skiatest::Reporter* reporter) {
70 bool flipped = false;
71 for (size_t index = 0; index < oneOffs_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -070072 const ConicPts& c = oneOffs[index].conic;
73 SkDConic conic;
74 conic.debugSet(c.fPts.fPts, c.fWeight);
caryclark1049f122015-04-20 08:31:59 -070075 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
94DEF_TEST(PathOpsConicLineIntersectionOneOff, reporter) {
95 testOneOffs(reporter);
96}
97
98DEF_TEST(PathOpsConicLineIntersection, reporter) {
99 for (size_t index = 0; index < lineConicTests_count; ++index) {
100 int iIndex = static_cast<int>(index);
caryclarka35ab3e2016-10-20 08:32:18 -0700101 const ConicPts& c = lineConicTests[index].conic;
102 SkDConic conic;
103 conic.debugSet(c.fPts.fPts, c.fWeight);
caryclark1049f122015-04-20 08:31:59 -0700104 SkASSERT(ValidConic(conic));
105 const SkDLine& line = lineConicTests[index].line;
106 SkASSERT(ValidLine(line));
107 SkReduceOrder reducer;
halcanary9d524f22016-03-29 09:03:52 -0700108 SkPoint pts[3] = { conic.fPts.fPts[0].asSkPoint(), conic.fPts.fPts[1].asSkPoint(),
caryclark1049f122015-04-20 08:31:59 -0700109 conic.fPts.fPts[2].asSkPoint() };
110 SkPoint reduced[3];
caryclark27c015d2016-09-23 05:47:20 -0700111 SkConic floatConic;
112 floatConic.set(pts, conic.fWeight);
113 SkPath::Verb order1 = SkReduceOrder::Conic(floatConic, reduced);
caryclark1049f122015-04-20 08:31:59 -0700114 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}