PathOpsCubicQuadIntersectionTest.cpp revision 8d0a524a4847bc7e1cc63a93b78922739466c201
1/*
2 * Copyright 2013 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 "SkIntersections.h"
9#include "SkPathOpsCubic.h"
10#include "SkPathOpsQuad.h"
11#include "SkReduceOrder.h"
12#include "Test.h"
13
14static struct lineCubic {
15    SkDCubic cubic;
16    SkDQuad quad;
17    int answerCount;
18    SkDPoint answers[2];
19} quadCubicTests[] = {
20    {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}},
21        {{{18,226}, {14.686291694641113,226}, {12.342399597167969,228.3424072265625}}}, 1,
22        {{18,226}, {0,0}}},
23    {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}},
24        {{{12.342399597167969,228.3424072265625}, {10,230.68629455566406}, {10,234}}}, 1,
25        {{10,234}, {0,0}}},
26};
27
28static const size_t quadCubicTests_count = SK_ARRAY_COUNT(quadCubicTests);
29
30static void PathOpsCubicQuadIntersectionTest(skiatest::Reporter* reporter) {
31    for (size_t index = 0; index < quadCubicTests_count; ++index) {
32        int iIndex = static_cast<int>(index);
33        const SkDCubic& cubic = quadCubicTests[index].cubic;
34        SkASSERT(ValidCubic(cubic));
35        const SkDQuad& quad = quadCubicTests[index].quad;
36        SkASSERT(ValidQuad(quad));
37        SkReduceOrder reduce1;
38        SkReduceOrder reduce2;
39        int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics,
40                SkReduceOrder::kFill_Style);
41        int order2 = reduce2.reduce(quad, SkReduceOrder::kFill_Style);
42        if (order1 != 4) {
43            SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
44            REPORTER_ASSERT(reporter, 0);
45        }
46        if (order2 != 3) {
47            SkDebugf("[%d] quad order=%d\n", iIndex, order2);
48            REPORTER_ASSERT(reporter, 0);
49        }
50        SkIntersections i;
51        int roots = i.intersect(cubic, quad);
52        SkASSERT(roots == quadCubicTests[index].answerCount);
53        for (int pt = 0; pt < roots; ++pt) {
54            double tt1 = i[0][pt];
55            SkDPoint xy1 = cubic.xyAtT(tt1);
56            double tt2 = i[1][pt];
57            SkDPoint xy2 = quad.xyAtT(tt2);
58            if (!xy1.approximatelyEqual(xy2)) {
59                SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
60                    __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
61            }
62            REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
63            bool found = false;
64            for (int idx2 = 0; idx2 < quadCubicTests[index].answerCount; ++idx2) {
65                found |= quadCubicTests[index].answers[idx2].approximatelyEqual(xy1);
66            }
67            REPORTER_ASSERT(reporter, found);
68        }
69        reporter->bumpTestCount();
70    }
71}
72
73#include "TestClassDef.h"
74DEFINE_TESTCLASS_SHORT(PathOpsCubicQuadIntersectionTest)
75