aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/Intersection/LineQuadraticIntersection_Test.cpp
blob: 3e7e230ac9daad3a9ba19d2da9e245b23cb2450e (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
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * 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 "CurveIntersection.h"
#include "CurveUtilities.h"
#include "EdgeWalker_Test.h"
#include "Intersection_Tests.h"
#include "Intersections.h"
#include "TestUtilities.h"

struct lineQuad {
    Quadratic quad;
    _Line line;
    int result;
    _Point expected[2];
} lineQuadTests[] = {
    //        quad                    line                  results
    {{{1, 1}, {2, 1}, {0, 2}},  {{0, 0}, {1, 1}},  1,  {{1, 1}        }},
    {{{0, 0}, {1, 1}, {3, 1}},  {{0, 0}, {3, 1}},  2,  {{0, 0}, {3, 1}}},
    {{{2, 0}, {1, 1}, {2, 2}},  {{0, 0}, {0, 2}},  0                   },
    {{{4, 0}, {0, 1}, {4, 2}},  {{3, 1}, {4, 1}},  0,                  },
    {{{0, 0}, {0, 1}, {1, 1}},  {{0, 1}, {1, 0}},  1,  {{.25, .75}    }},
};

size_t lineQuadTests_count = sizeof(lineQuadTests) / sizeof(lineQuadTests[0]);

const int firstLineQuadIntersectionTest = 0;

static int doIntersect(Intersections& intersections, const Quadratic& quad, const _Line& line, bool& flipped) {
    int result;
    flipped = false;
    if (line[0].x == line[1].x) {
        double top = line[0].y;
        double bottom = line[1].y;
        flipped = top > bottom;
        if (flipped) {
            SkTSwap<double>(top, bottom);
        }
        result = verticalIntersect(quad, top, bottom, line[0].x, flipped, intersections);
    } else if (line[0].y == line[1].y) {
        double left = line[0].x;
        double right = line[1].x;
        flipped = left > right;
        if (flipped) {
            SkTSwap<double>(left, right);
        }
        result = horizontalIntersect(quad, left, right, line[0].y, flipped, intersections);
    } else {
        intersect(quad, line, intersections);
        result = intersections.fUsed;
    }
    return result;
}

static struct oneLineQuad {
    Quadratic quad;
    _Line line;
} oneOffs[] = {
    {{{369.848602,145.680267}, {382.360413,121.298294}, {406.207703,121.298294}},
        {{406.207703,121.298294}, {348.781738,123.864815}}}
    };

static size_t oneOffs_count = sizeof(oneOffs) / sizeof(oneOffs[0]);


static void testOneOffs() {
    Intersections intersections;
    bool flipped = false;
    for (size_t index = 0; index < oneOffs_count; ++index) {
        const Quadratic& quad = oneOffs[index].quad;
        const _Line& line = oneOffs[index].line;
        int result = doIntersect(intersections, quad, line, flipped);
        for (int inner = 0; inner < result; ++inner) {
            double quadT = intersections.fT[0][inner];
            double quadX, quadY;
            xy_at_t(quad, quadT, quadX, quadY);
            double lineT = intersections.fT[1][inner];
            double lineX, lineY;
            xy_at_t(line, lineT, lineX, lineY);
            SkASSERT(AlmostEqualUlps(quadX, lineX)
                    && AlmostEqualUlps(quadY, lineY));
        }
    }
}

void LineQuadraticIntersection_Test() {
    if (1) {
        testOneOffs();
    }
    for (size_t index = firstLineQuadIntersectionTest; index < lineQuadTests_count; ++index) {
        const Quadratic& quad = lineQuadTests[index].quad;
        const _Line& line = lineQuadTests[index].line;
        Quadratic reduce1;
        _Line reduce2;
        int order1 = reduceOrder(quad, reduce1, kReduceOrder_TreatAsFill);
        int order2 = reduceOrder(line, reduce2);
        if (order1 < 3) {
            SkDebugf("%s [%d] quad order=%d\n", __FUNCTION__, (int) index, order1);
            SkASSERT(0);
        }
        if (order2 < 2) {
            SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, (int) index, order2);
            SkASSERT(0);
        }
        Intersections intersections;
        bool flipped = false;
        int result = doIntersect(intersections, quad, line, flipped);
        SkASSERT(result == lineQuadTests[index].result);
        if (!intersections.intersected()) {
            continue;
        }
        for (int pt = 0; pt < result; ++pt) {
            double tt1 = intersections.fT[0][pt];
            SkASSERT(tt1 >= 0 && tt1 <= 1);
            _Point t1, t2;
            xy_at_t(quad, tt1, t1.x, t1.y);
            double tt2 = intersections.fT[1][pt];
            SkASSERT(tt2 >= 0 && tt2 <= 1);
            xy_at_t(line, tt2, t2.x, t2.y);
            if (!AlmostEqualUlps(t1.x, t2.x)) {
                SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
                    __FUNCTION__, (int)index, pt, tt1, t1.x, t1.y, tt2, t2.x, t2.y);
                SkASSERT(0);
            }
            if (!AlmostEqualUlps(t1.y, t2.y)) {
                SkDebugf("%s [%d,%d] y!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
                    __FUNCTION__, (int)index, pt, tt1, t1.x, t1.y, tt2, t2.x, t2.y);
                SkASSERT(0);
            }
            if (!t1.approximatelyEqual(lineQuadTests[index].expected[0])
                    && (lineQuadTests[index].result == 1
                    || !t1.approximatelyEqual(lineQuadTests[index].expected[1]))) {
                SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.x, t1.y);
                SkASSERT(0);
            }
        }
    }
}

static void testLineIntersect(State4& state, const Quadratic& quad, const _Line& line,
        const double x, const double y) {
    char pathStr[1024];
    bzero(pathStr, sizeof(pathStr));
    char* str = pathStr;
    str += sprintf(str, "    path.moveTo(%1.9g, %1.9g);\n", quad[0].x, quad[0].y);
    str += sprintf(str, "    path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n", quad[1].x, quad[1].y, quad[2].x, quad[2].y);
    str += sprintf(str, "    path.moveTo(%1.9g, %1.9g);\n", line[0].x, line[0].y);
    str += sprintf(str, "    path.lineTo(%1.9g, %1.9g);\n", line[1].x, line[1].y);

    Intersections intersections;
    bool flipped = false;
    int result = doIntersect(intersections, quad, line, flipped);
    bool found = false;
    for (int index = 0; index < result; ++index) {
        double quadT = intersections.fT[0][index];
        double quadX, quadY;
        xy_at_t(quad, quadT, quadX, quadY);
        double lineT = intersections.fT[1][index];
        double lineX, lineY;
        xy_at_t(line, lineT, lineX, lineY);
        if (fabs(quadX - lineX) < FLT_EPSILON && fabs(quadY - lineY) < FLT_EPSILON
                && fabs(x - lineX) < FLT_EPSILON && fabs(y - lineY) < FLT_EPSILON) {
            found = true;
        }
    }
    SkASSERT(found);
    state.testsRun++;
}


// find a point on a quad by choosing a t from 0 to 1
// create a vertical span above and below the point
// verify that intersecting the vertical span and the quad returns t
// verify that a vertical span starting at quad[0] intersects at t=0
// verify that a vertical span starting at quad[2] intersects at t=1
static void* testQuadLineIntersectMain(void* data)
{
    SkASSERT(data);
    State4& state = *(State4*) data;
    do {
        int ax = state.a & 0x03;
        int ay = state.a >> 2;
        int bx = state.b & 0x03;
        int by = state.b >> 2;
        int cx = state.c & 0x03;
        int cy = state.c >> 2;
        Quadratic quad = {{ax, ay}, {bx, by}, {cx, cy}};
        Quadratic reduced;
        int order = reduceOrder(quad, reduced, kReduceOrder_TreatAsFill);
        if (order < 3) {
            continue; // skip degenerates
        }
        for (int tIndex = 0; tIndex <= 4; ++tIndex) {
            double x, y;
            xy_at_t(quad, tIndex / 4.0, x, y);
            for (int h = -2; h <= 2; ++h) {
                for (int v = -2; v <= 2; ++v) {
                    if (h == v && abs(h) != 1) {
                        continue;
                    }
                    _Line line = {{x - h, y - v}, {x, y}};
                    testLineIntersect(state, quad, line, x, y);
                    _Line line2 = {{x, y}, {x + h, y + v}};
                    testLineIntersect(state, quad, line2, x, y);
                    _Line line3 = {{x - h, y - v}, {x + h, y + v}};
                    testLineIntersect(state, quad, line3, x, y);
                }
            }
        }
    } while (runNextTestSet(state));
    return NULL;
}

void QuadLineIntersectThreaded_Test(int& testsRun)
{
    SkDebugf("%s\n", __FUNCTION__);
    const char testStr[] = "testQuadLineIntersect";
    initializeTests(testStr, sizeof(testStr));
    int testsStart = testsRun;
    for (int a = 0; a < 16; ++a) {
        for (int b = 0 ; b < 16; ++b) {
            for (int c = 0 ; c < 16; ++c) {
                testsRun += dispatchTest4(testQuadLineIntersectMain,
                        a, b, c, 0);
            }
            if (!gRunTestsInOneThread) SkDebugf(".");
        }
        if (!gRunTestsInOneThread) SkDebugf("%d", a);
    }
    testsRun += waitForCompletion();
    SkDebugf("\n%s tests=%d total=%d\n", __FUNCTION__, testsRun - testsStart, testsRun);
}