QuadraticIntersection_Test.cpp revision a3f05facab01712a1b58e60a70b0dbdb90a39830
1#include "CurveIntersection.h"
2#include "Intersection_Tests.h"
3#include "Intersections.h"
4#include "QuadraticIntersection_TestData.h"
5#include "TestUtilities.h"
6#include "SkTypes.h"
7
8const int firstQuadIntersectionTest = 9;
9
10static void standardTestCases() {
11    for (size_t index = firstQuadIntersectionTest; index < quadraticTests_count; ++index) {
12        const Quadratic& quad1 = quadraticTests[index][0];
13        const Quadratic& quad2 = quadraticTests[index][1];
14        Quadratic reduce1, reduce2;
15        int order1 = reduceOrder(quad1, reduce1);
16        int order2 = reduceOrder(quad2, reduce2);
17        if (order1 < 3) {
18            printf("[%d] quad1 order=%d\n", (int) index, order1);
19        }
20        if (order2 < 3) {
21            printf("[%d] quad2 order=%d\n", (int) index, order2);
22        }
23        if (order1 == 3 && order2 == 3) {
24            Intersections intersections;
25            intersect(reduce1, reduce2, intersections);
26            if (intersections.intersected()) {
27                for (int pt = 0; pt < intersections.used(); ++pt) {
28                    double tt1 = intersections.fT[0][pt];
29                    double tx1, ty1;
30                    xy_at_t(quad1, tt1, tx1, ty1);
31                    double tt2 = intersections.fT[1][pt];
32                    double tx2, ty2;
33                    xy_at_t(quad2, tt2, tx2, ty2);
34                    if (!approximately_equal(tx1, tx2)) {
35                        printf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
36                            __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
37                    }
38                    if (!approximately_equal(ty1, ty2)) {
39                        printf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
40                            __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
41                    }
42                }
43            }
44        }
45    }
46}
47
48static const Quadratic testSet[] = {
49    {{8, 8}, {10, 10}, {8, -10}},
50    {{8, 8}, {12, 12}, {14, 4}},
51    {{8, 8}, {9, 9}, {10, 8}}
52};
53
54const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]);
55
56static void oneOffTest() {
57    for (int outer = 0; outer < testSetCount - 1; ++outer) {
58        for (int inner = outer + 1; inner < testSetCount; ++inner) {
59            const Quadratic& quad1 = testSet[outer];
60            const Quadratic& quad2 = testSet[inner];
61            Intersections intersections;
62            intersect(quad1, quad2, intersections);
63            if (!intersections.intersected()) {
64                SkDebugf("%s no intersection!\n", __FUNCTION__);
65            }
66            for (int pt = 0; pt < intersections.used(); ++pt) {
67                double tt1 = intersections.fT[0][pt];
68                double tx1, ty1;
69                xy_at_t(quad1, tt1, tx1, ty1);
70                double tt2 = intersections.fT[1][pt];
71                double tx2, ty2;
72                xy_at_t(quad2, tt2, tx2, ty2);
73                if (!approximately_equal(tx1, tx2)) {
74                    SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
75                        __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
76                    SkASSERT(0);
77                }
78                if (!approximately_equal(ty1, ty2)) {
79                    SkDebugf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
80                        __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
81                    SkASSERT(0);
82                }
83                SkDebugf("%s [%d][%d] t1=%1.9g (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
84                    outer, inner, tt1, tx1, tx2, tt2);
85            }
86        }
87    }
88}
89
90void QuadraticIntersection_Test() {
91    oneOffTest();
92    standardTestCases();
93}
94