blob: ecc4a47f179f044efd1fdc64fd03f4c89c6ae01e [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 {
17 SkDConic conic;
18 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 {
60 SkDConic conic;
61 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) {
72 const SkDConic& conic = oneOffs[index].conic;
73 SkASSERT(ValidConic(conic));
74 const SkDLine& line = oneOffs[index].line;
75 SkASSERT(ValidLine(line));
76 SkIntersections intersections;
77 int result = doIntersect(intersections, conic, line, flipped);
78 for (int inner = 0; inner < result; ++inner) {
79 double conicT = intersections[0][inner];
80 SkDPoint conicXY = conic.ptAtT(conicT);
81 double lineT = intersections[1][inner];
82 SkDPoint lineXY = line.ptAtT(lineT);
83 if (!conicXY.approximatelyEqual(lineXY)) {
84 conicXY.approximatelyEqual(lineXY);
85 SkDebugf("");
86 }
87 REPORTER_ASSERT(reporter, conicXY.approximatelyEqual(lineXY));
88 }
89 }
90}
91
92DEF_TEST(PathOpsConicLineIntersectionOneOff, reporter) {
93 testOneOffs(reporter);
94}
95
96DEF_TEST(PathOpsConicLineIntersection, reporter) {
97 for (size_t index = 0; index < lineConicTests_count; ++index) {
98 int iIndex = static_cast<int>(index);
99 const SkDConic& conic = lineConicTests[index].conic;
100 SkASSERT(ValidConic(conic));
101 const SkDLine& line = lineConicTests[index].line;
102 SkASSERT(ValidLine(line));
103 SkReduceOrder reducer;
halcanary9d524f22016-03-29 09:03:52 -0700104 SkPoint pts[3] = { conic.fPts.fPts[0].asSkPoint(), conic.fPts.fPts[1].asSkPoint(),
caryclark1049f122015-04-20 08:31:59 -0700105 conic.fPts.fPts[2].asSkPoint() };
106 SkPoint reduced[3];
caryclark27c015d2016-09-23 05:47:20 -0700107 SkConic floatConic;
108 floatConic.set(pts, conic.fWeight);
109 SkPath::Verb order1 = SkReduceOrder::Conic(floatConic, reduced);
caryclark1049f122015-04-20 08:31:59 -0700110 if (order1 != SkPath::kConic_Verb) {
111 SkDebugf("%s [%d] conic verb=%d\n", __FUNCTION__, iIndex, order1);
112 REPORTER_ASSERT(reporter, 0);
113 }
114 int order2 = reducer.reduce(line);
115 if (order2 < 2) {
116 SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2);
117 REPORTER_ASSERT(reporter, 0);
118 }
119 SkIntersections intersections;
120 bool flipped = false;
121 int result = doIntersect(intersections, conic, line, flipped);
122 REPORTER_ASSERT(reporter, result == lineConicTests[index].result);
123 if (intersections.used() <= 0) {
124 continue;
125 }
126 for (int pt = 0; pt < result; ++pt) {
127 double tt1 = intersections[0][pt];
128 REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1);
129 SkDPoint t1 = conic.ptAtT(tt1);
130 double tt2 = intersections[1][pt];
131 REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1);
132 SkDPoint t2 = line.ptAtT(tt2);
133 if (!t1.approximatelyEqual(t2)) {
134 SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
135 __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY);
136 REPORTER_ASSERT(reporter, 0);
137 }
138 if (!t1.approximatelyEqual(lineConicTests[index].expected[0])
139 && (lineConicTests[index].result == 1
140 || !t1.approximatelyEqual(lineConicTests[index].expected[1]))) {
141 SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY);
142 REPORTER_ASSERT(reporter, 0);
143 }
144 }
145 }
146}