aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/Intersection/SimplifyFindTop_Test.cpp
blob: 5f267503a9c03bf03d89b50ff2dd5b0fa4905fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "Simplify.h"

namespace SimplifyFindTopTest {

#include "Simplify.cpp"

} // end of SimplifyFindTopTest namespace

#include "Intersection_Tests.h"

static const SimplifyFindTopTest::Segment* testCommon(
        SkTArray<SimplifyFindTopTest::Contour>& contours,
        int& index, int& end) {
    SkTDArray<SimplifyFindTopTest::Contour*> contourList;
    makeContourList(contours, contourList, false, false);
    addIntersectTs(contourList[0], contourList[0]);
    if (contours.count() > 1) {
        SkASSERT(contours.count() == 2);
        addIntersectTs(contourList[0], contourList[1]);
        addIntersectTs(contourList[1], contourList[1]);
    }
    fixOtherTIndex(contourList);
#if SORTABLE_CONTOURS // old way
    SimplifyFindTopTest::Segment* topStart = findTopContour(contourList);
    const SimplifyFindTopTest::Segment* topSegment = topStart->findTop(index,
            end);
#else
    SkPoint bestXY = {SK_ScalarMin, SK_ScalarMin};
    bool done, unsortable = false;
    const SimplifyFindTopTest::Segment* topSegment =
            findSortableTop(contourList, index, end, bestXY, unsortable, done, true);
#endif
    return topSegment;
}

static void test(const SkPath& path) {
    SkTArray<SimplifyFindTopTest::Contour> contours;
    SimplifyFindTopTest::EdgeBuilder builder(path, contours);
    int index, end;
    testCommon(contours, index, end);
    SkASSERT(index + 1 == end);
}

static void test(const SkPath& path, SkScalar x1, SkScalar y1,
        SkScalar x2, SkScalar y2) {
    SkTArray<SimplifyFindTopTest::Contour> contours;
    SimplifyFindTopTest::EdgeBuilder builder(path, contours);
    int index, end;
    const SimplifyFindTopTest::Segment* topSegment =
            testCommon(contours, index, end);
    SkPoint pts[2];
    double firstT = topSegment->t(index);
    pts[0] = topSegment->xyAtT(&topSegment->span(index));
    int direction = index < end ? 1 : -1;
    do {
        index += direction;
        double nextT = topSegment->t(index);
        if (nextT == firstT) {
            continue;
        }
        pts[1] = topSegment->xyAtT(&topSegment->span(index));
        if (pts[0] != pts[1]) {
            break;
        }
    } while (true);
    SkASSERT(pts[0].fX == x1);
    SkASSERT(pts[0].fY == y1);
    SkASSERT(pts[1].fX == x2);
    SkASSERT(pts[1].fY == y2);
}

static void testLine1() {
    SkPath path;
    path.moveTo(2,0);
    path.lineTo(1,1);
    path.lineTo(0,0);
    path.close();
    test(path);
}

static void addInnerCWTriangle(SkPath& path) {
    path.moveTo(3,0);
    path.lineTo(4,1);
    path.lineTo(2,1);
    path.close();
}

static void addInnerCCWTriangle(SkPath& path) {
    path.moveTo(3,0);
    path.lineTo(2,1);
    path.lineTo(4,1);
    path.close();
}

static void addOuterCWTriangle(SkPath& path) {
    path.moveTo(3,0);
    path.lineTo(6,2);
    path.lineTo(0,2);
    path.close();
}

static void addOuterCCWTriangle(SkPath& path) {
    path.moveTo(3,0);
    path.lineTo(0,2);
    path.lineTo(6,2);
    path.close();
}

static void testLine2() {
    SkPath path;
    addInnerCWTriangle(path);
    addOuterCWTriangle(path);
    test(path, 0, 2, 3, 0);
}

static void testLine3() {
    SkPath path;
    addOuterCWTriangle(path);
    addInnerCWTriangle(path);
    test(path, 0, 2, 3, 0);
}

static void testLine4() {
    SkPath path;
    addInnerCCWTriangle(path);
    addOuterCWTriangle(path);
    test(path, 0, 2, 3, 0);
}

static void testLine5() {
    SkPath path;
    addOuterCWTriangle(path);
    addInnerCCWTriangle(path);
    test(path, 0, 2, 3, 0);
}

static void testLine6() {
    SkPath path;
    addInnerCWTriangle(path);
    addOuterCCWTriangle(path);
    test(path, 0, 2, 3, 0);
}

static void testLine7() {
    SkPath path;
    addOuterCCWTriangle(path);
    addInnerCWTriangle(path);
    test(path, 0, 2, 3, 0);
}

static void testLine8() {
    SkPath path;
    addInnerCCWTriangle(path);
    addOuterCCWTriangle(path);
    test(path, 0, 2, 3, 0);
}

static void testLine9() {
    SkPath path;
    addOuterCCWTriangle(path);
    addInnerCCWTriangle(path);
    test(path, 0, 2, 3, 0);
}

static void testQuads() {
    SkPath path;
    path.moveTo(2,0);
    path.quadTo(1,1, 0,0);
    path.close();
    test(path);
}

static void testCubics() {
    SkPath path;
    path.moveTo(2,0);
    path.cubicTo(2,3, 1,1, 0,0);
    path.close();
    test(path);
}

static void (*tests[])() = {
    testLine1,
    testLine2,
    testLine3,
    testLine4,
    testLine5,
    testLine6,
    testLine7,
    testLine8,
    testLine9,
    testQuads,
    testCubics
};

static const size_t testCount = sizeof(tests) / sizeof(tests[0]);

static void (*firstTest)() = 0;
static bool skipAll = false;

void SimplifyFindTop_Test() {
    if (skipAll) {
        return;
    }
    size_t index = 0;
    if (firstTest) {
        while (index < testCount && tests[index] != firstTest) {
            ++index;
        }
    }
    bool firstTestComplete = false;
    for ( ; index < testCount; ++index) {
        (*tests[index])();
        firstTestComplete = true;
    }
}