blob: 89046a86b199f3f39b3a7dc6a7a1028ea071440a [file] [log] [blame]
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001/*
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 "Simplify.h"
caryclark@google.comfa0588f2012-04-26 21:01:06 +00008
9namespace SimplifyAddIntersectingTsTest {
10
11#include "Simplify.cpp"
12
13} // end of SimplifyAddIntersectingTsTest namespace
caryclark@google.com9e49fb62012-08-27 14:11:33 +000014/*
15 * Copyright 2012 Google Inc.
16 *
17 * Use of this source code is governed by a BSD-style license that can be
18 * found in the LICENSE file.
19 */
caryclark@google.comfa0588f2012-04-26 21:01:06 +000020
21#include "Intersection_Tests.h"
22
23static const SkPoint lines[][2] = {
24 {{ 1, 1}, { 1, 1}}, // degenerate
25 {{ 1, 1}, { 4, 1}}, // horizontal
26 {{ 4, 1}, { 9, 1}},
27 {{ 2, 1}, { 3, 1}},
28 {{ 2, 1}, { 6, 1}},
29 {{ 5, 1}, { 9, 1}},
30 {{ 1, 1}, { 1, 4}}, // vertical
31 {{ 1, 2}, { 1, 3}},
32 {{ 1, 2}, { 1, 6}},
33 {{ 1, 5}, { 1, 9}},
34 {{ 1, 1}, { 3, 3}}, // diagonal
35 {{ 2, 2}, { 4, 4}},
36 {{ 2, 4}, { 4, 2}},
37};
38
39static const size_t lineCount = sizeof(lines) / sizeof(lines[0]);
40
41static const SkPoint quads[][3] = {
42 {{ 1, 1}, { 1, 1}, { 1, 1}}, // degenerate
43 {{ 1, 1}, { 4, 1}, { 5, 1}}, // line
44 {{ 1, 1}, { 4, 1}, { 4, 4}}, // curve
45};
46
47static const size_t quadCount = sizeof(quads) / sizeof(quads[0]);
48
49static const SkPoint cubics[][4] = {
50 {{ 1, 1}, { 1, 1}, { 1, 1}, { 1, 1}}, // degenerate
51 {{ 1, 1}, { 4, 1}, { 5, 1}, { 6, 1}}, // line
52 {{ 1, 1}, { 3, 1}, { 4, 2}, { 4, 4}}, // curve
53};
54
55static const size_t cubicCount = sizeof(cubics) / sizeof(cubics[0]);
56static const size_t testCount = lineCount + quadCount + cubicCount;
57
caryclark@google.comf25edfe2012-06-01 18:20:10 +000058static SkPath::Verb setPath(size_t outer, SkPath& path, const SkPoint*& pts1) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +000059 SkPath::Verb c1Type;
60 if (outer < lineCount) {
61 path.moveTo(lines[outer][0].fX, lines[outer][0].fY);
62 path.lineTo(lines[outer][1].fX, lines[outer][1].fY);
63 c1Type = SkPath::kLine_Verb;
64 pts1 = lines[outer];
65 } else {
66 outer -= lineCount;
67 if (outer < quadCount) {
68 path.moveTo(quads[outer][0].fX, quads[outer][0].fY);
69 path.quadTo(quads[outer][1].fX, quads[outer][1].fY,
70 quads[outer][2].fX, quads[outer][2].fY);
71 c1Type = SkPath::kQuad_Verb;
72 pts1 = quads[outer];
73 } else {
74 outer -= quadCount;
75 path.moveTo(cubics[outer][0].fX, cubics[outer][0].fY);
76 path.cubicTo(cubics[outer][1].fX, cubics[outer][1].fY,
77 cubics[outer][2].fX, cubics[outer][2].fY,
78 cubics[outer][3].fX, cubics[outer][3].fY);
79 c1Type = SkPath::kCubic_Verb;
80 pts1 = cubics[outer];
81 }
82 }
83 return c1Type;
84}
85
86static void testPath(const SkPath& path, const SkPoint* pts1, SkPath::Verb c1Type,
87 const SkPoint* pts2, SkPath::Verb c2Type) {
88 SkTArray<SimplifyAddIntersectingTsTest::Contour> contour;
89 SimplifyAddIntersectingTsTest::EdgeBuilder builder(path, contour);
90 if (contour.count() < 2) {
91 return;
92 }
93 SimplifyAddIntersectingTsTest::Contour& c1 = contour[0];
94 SimplifyAddIntersectingTsTest::Contour& c2 = contour[1];
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000095 addIntersectTs(&c1, &c2);
caryclark@google.com47580692012-07-23 12:14:49 +000096#if DEBUG_DUMP
caryclark@google.com8dcf1142012-07-02 20:27:02 +000097 bool c1Intersected = c1.segments()[0].intersected();
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000098 // bool c2Intersected = c2.fSegments[0].intersected();
caryclark@google.comfa0588f2012-04-26 21:01:06 +000099 SkDebugf("%s %s (%1.9g,%1.9g %1.9g,%1.9g) %s %s (%1.9g,%1.9g %1.9g,%1.9g)\n",
100 __FUNCTION__, SimplifyAddIntersectingTsTest::kLVerbStr[c1Type],
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101 pts1[0].fX, pts1[0].fY,
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000102 pts1[c1Type].fX, pts1[c1Type].fY,
103 c1Intersected ? "intersects" : "does not intersect",
104 SimplifyAddIntersectingTsTest::kLVerbStr[c2Type],
rmistry@google.comd6176b02012-08-23 18:14:13 +0000105 pts2[0].fX, pts2[0].fY,
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000106 pts2[c2Type].fX, pts2[c2Type].fY);
107 if (c1Intersected) {
108 c1.dump();
109 c2.dump();
110 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111#endif
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000112}
113
caryclark@google.comf25edfe2012-06-01 18:20:10 +0000114static const size_t firstO = 6;
115static const size_t firstI = 1;
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000116
117void SimplifyAddIntersectingTs_Test() {
118 const SkPoint* pts1, * pts2;
119 if (firstO > 0 || firstI > 0) {
120 SkPath path;
121 SkPath::Verb c1Type = setPath(firstO, path, pts1);
122 SkPath path2(path);
123 SkPath::Verb c2Type = setPath(firstI, path2, pts2);
124 testPath(path2, pts1, c1Type, pts2, c2Type);
125 }
caryclark@google.comf25edfe2012-06-01 18:20:10 +0000126 for (size_t o = 0; o < testCount; ++o) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000127 SkPath path;
128 SkPath::Verb c1Type = setPath(o, path, pts1);
caryclark@google.comf25edfe2012-06-01 18:20:10 +0000129 for (size_t i = 0; i < testCount; ++i) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000130 SkPath path2(path);
131 SkPath::Verb c2Type = setPath(i, path2, pts2);
132 testPath(path2, pts1, c1Type, pts2, c2Type);
133 }
134 }
135}