blob: 2a958edf678930550223beb091d4ac79835a9630 [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"
9#include "SkIntersections.h"
10#include "SkPathOpsConic.h"
11#include "SkPathOpsLine.h"
12#include "SkReduceOrder.h"
13#include "Test.h"
14
15static struct lineConic {
16 SkDConic conic;
17 SkDLine line;
18 int result;
19 SkDPoint expected[2];
20} lineConicTests[] = {
21 {
22 {{{{30.6499996,25.6499996}, {30.6499996,20.6499996}, {25.6499996,20.6499996}}}, 0.707107008f},
23 {{{25.6499996,20.6499996}, {45.6500015,20.6499996}}},
24 1,
25 {{25.6499996,20.6499996}, {0,0}}
26 },
27};
28
29static size_t lineConicTests_count = SK_ARRAY_COUNT(lineConicTests);
30
31static int doIntersect(SkIntersections& intersections, const SkDConic& conic, const SkDLine& line,
32 bool& flipped) {
33 int result;
34 flipped = false;
35 if (line[0].fX == line[1].fX) {
36 double top = line[0].fY;
37 double bottom = line[1].fY;
38 flipped = top > bottom;
39 if (flipped) {
40 SkTSwap<double>(top, bottom);
41 }
42 result = intersections.vertical(conic, top, bottom, line[0].fX, flipped);
43 } else if (line[0].fY == line[1].fY) {
44 double left = line[0].fX;
45 double right = line[1].fX;
46 flipped = left > right;
47 if (flipped) {
48 SkTSwap<double>(left, right);
49 }
50 result = intersections.horizontal(conic, left, right, line[0].fY, flipped);
51 } else {
52 intersections.intersect(conic, line);
53 result = intersections.used();
54 }
55 return result;
56}
57
58static struct oneLineConic {
59 SkDConic conic;
60 SkDLine line;
61} oneOffs[] = {
62 {{{{{30.6499996,25.6499996}, {30.6499996,20.6499996}, {25.6499996,20.6499996}}}, 0.707107008f},
63 {{{25.6499996,20.6499996}, {45.6500015,20.6499996}}}}
64};
65
66static size_t oneOffs_count = SK_ARRAY_COUNT(oneOffs);
67
68static void testOneOffs(skiatest::Reporter* reporter) {
69 bool flipped = false;
70 for (size_t index = 0; index < oneOffs_count; ++index) {
71 const SkDConic& conic = oneOffs[index].conic;
72 SkASSERT(ValidConic(conic));
73 const SkDLine& line = oneOffs[index].line;
74 SkASSERT(ValidLine(line));
75 SkIntersections intersections;
76 int result = doIntersect(intersections, conic, line, flipped);
77 for (int inner = 0; inner < result; ++inner) {
78 double conicT = intersections[0][inner];
79 SkDPoint conicXY = conic.ptAtT(conicT);
80 double lineT = intersections[1][inner];
81 SkDPoint lineXY = line.ptAtT(lineT);
82 if (!conicXY.approximatelyEqual(lineXY)) {
83 conicXY.approximatelyEqual(lineXY);
84 SkDebugf("");
85 }
86 REPORTER_ASSERT(reporter, conicXY.approximatelyEqual(lineXY));
87 }
88 }
89}
90
91DEF_TEST(PathOpsConicLineIntersectionOneOff, reporter) {
92 testOneOffs(reporter);
93}
94
95DEF_TEST(PathOpsConicLineIntersection, reporter) {
96 for (size_t index = 0; index < lineConicTests_count; ++index) {
97 int iIndex = static_cast<int>(index);
98 const SkDConic& conic = lineConicTests[index].conic;
99 SkASSERT(ValidConic(conic));
100 const SkDLine& line = lineConicTests[index].line;
101 SkASSERT(ValidLine(line));
102 SkReduceOrder reducer;
103 SkPoint pts[3] = { conic.fPts.fPts[0].asSkPoint(), conic.fPts.fPts[1].asSkPoint(),
104 conic.fPts.fPts[2].asSkPoint() };
105 SkPoint reduced[3];
106 SkPath::Verb order1 = SkReduceOrder::Conic(pts, conic.fWeight, reduced);
107 if (order1 != SkPath::kConic_Verb) {
108 SkDebugf("%s [%d] conic verb=%d\n", __FUNCTION__, iIndex, order1);
109 REPORTER_ASSERT(reporter, 0);
110 }
111 int order2 = reducer.reduce(line);
112 if (order2 < 2) {
113 SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2);
114 REPORTER_ASSERT(reporter, 0);
115 }
116 SkIntersections intersections;
117 bool flipped = false;
118 int result = doIntersect(intersections, conic, line, flipped);
119 REPORTER_ASSERT(reporter, result == lineConicTests[index].result);
120 if (intersections.used() <= 0) {
121 continue;
122 }
123 for (int pt = 0; pt < result; ++pt) {
124 double tt1 = intersections[0][pt];
125 REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1);
126 SkDPoint t1 = conic.ptAtT(tt1);
127 double tt2 = intersections[1][pt];
128 REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1);
129 SkDPoint t2 = line.ptAtT(tt2);
130 if (!t1.approximatelyEqual(t2)) {
131 SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
132 __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY);
133 REPORTER_ASSERT(reporter, 0);
134 }
135 if (!t1.approximatelyEqual(lineConicTests[index].expected[0])
136 && (lineConicTests[index].result == 1
137 || !t1.approximatelyEqual(lineConicTests[index].expected[1]))) {
138 SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY);
139 REPORTER_ASSERT(reporter, 0);
140 }
141 }
142 }
143}