PathOpsConicIntersectionTest.cpp revision 1049f1246e7be4ccb68001361efceb8933e6f81c
1/*
2 * Copyright 2015 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 "PathOpsTestCommon.h"
8#include "SkGeometry.h"
9#include "SkIntersections.h"
10#include "Test.h"
11
12/*
13manually compute the intersection of a pair of circles and see if the conic intersection matches
14  given two circles
15    construct a line connecting their centers
16
17 */
18
19static const SkDConic testSet[] = {
20    {{{{-4,1}, {-4,5}, {0,5}}}, 0.707106769f},
21    {{{{-3,4}, {-3,1}, {0,1}}}, 0.707106769f},
22
23    {{{{0, 0}, {0, 1}, {1, 1}}}, 0.5f},
24    {{{{1, 0}, {0, 0}, {0, 1}}}, 0.5f},
25};
26
27const int testSetCount = (int) SK_ARRAY_COUNT(testSet);
28
29static void oneOff(skiatest::Reporter* reporter, const SkDConic& c1, const SkDConic& c2,
30        bool coin) {
31    SkASSERT(ValidConic(c1));
32    SkASSERT(ValidConic(c2));
33    SkIntersections intersections;
34    intersections.intersect(c1, c2);
35    if (coin && intersections.used() != 2) {
36        SkDebugf("");
37    }
38    REPORTER_ASSERT(reporter, !coin || intersections.used() == 2);
39    double tt1, tt2;
40    SkDPoint xy1, xy2;
41    for (int pt3 = 0; pt3 < intersections.used(); ++pt3) {
42        tt1 = intersections[0][pt3];
43        xy1 = c1.ptAtT(tt1);
44        tt2 = intersections[1][pt3];
45        xy2 = c2.ptAtT(tt2);
46        const SkDPoint& iPt = intersections.pt(pt3);
47        REPORTER_ASSERT(reporter, xy1.approximatelyEqual(iPt));
48        REPORTER_ASSERT(reporter, xy2.approximatelyEqual(iPt));
49        REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
50    }
51    reporter->bumpTestCount();
52}
53
54static void oneOff(skiatest::Reporter* reporter, int outer, int inner) {
55    const SkDConic& c1 = testSet[outer];
56    const SkDConic& c2 = testSet[inner];
57    oneOff(reporter, c1, c2, false);
58}
59
60static void oneOffTests(skiatest::Reporter* reporter) {
61    for (int outer = 0; outer < testSetCount - 1; ++outer) {
62        for (int inner = outer + 1; inner < testSetCount; ++inner) {
63            oneOff(reporter, outer, inner);
64        }
65    }
66}
67
68DEF_TEST(PathOpsConicIntersectionOneOff, reporter) {
69    oneOff(reporter, 0, 1);
70}
71
72DEF_TEST(PathOpsConicIntersection, reporter) {
73    oneOffTests(reporter);
74}
75