Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2012 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 "SkPathOpsLine.h"
     11 #include "SkReduceOrder.h"
     12 #include "Test.h"
     13 
     14 static struct lineCubic {
     15     SkDCubic cubic;
     16     SkDLine line;
     17 } lineCubicTests[] = {
     18 #if 0
     19     {{{{258, 122}, {260.761414, 122}, { 263, 124.238579}, {263, 127}}},
     20             {{{259.82843, 125.17157}, {261.535522, 123.46447}}}},
     21 #endif
     22     {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125},
     23             {1030.318359375,321}}},
     24             {{{979.30487060546875,561}, {1036.695068359375,291}}}},
     25     {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875},
     26             {235.68154907226562,531}}},
     27             {{{286.69512939453125,291}, {229.30485534667969,561}}}},
     28     {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}},
     29     {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}},
     30 };
     31 
     32 static const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests);
     33 
     34 static void testOne(skiatest::Reporter* reporter, int iIndex) {
     35     const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
     36     SkASSERT(ValidCubic(cubic));
     37     const SkDLine& line = lineCubicTests[iIndex].line;
     38     SkASSERT(ValidLine(line));
     39     SkReduceOrder reduce1;
     40     SkReduceOrder reduce2;
     41     int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics,
     42             SkReduceOrder::kFill_Style);
     43     int order2 = reduce2.reduce(line);
     44     if (order1 < 4) {
     45         SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
     46         REPORTER_ASSERT(reporter, 0);
     47     }
     48     if (order2 < 2) {
     49         SkDebugf("[%d] line order=%d\n", iIndex, order2);
     50         REPORTER_ASSERT(reporter, 0);
     51     }
     52     if (order1 == 4 && order2 == 2) {
     53         SkIntersections i;
     54         int roots = i.intersect(cubic, line);
     55         for (int pt = 0; pt < roots; ++pt) {
     56             double tt1 = i[0][pt];
     57             SkDPoint xy1 = cubic.ptAtT(tt1);
     58             double tt2 = i[1][pt];
     59             SkDPoint xy2 = line.ptAtT(tt2);
     60             if (!xy1.approximatelyEqual(xy2)) {
     61                 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
     62                     __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
     63             }
     64             REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
     65         }
     66     }
     67 }
     68 
     69 static void PathOpsCubicLineIntersectionTest(skiatest::Reporter* reporter) {
     70     for (size_t index = 0; index < lineCubicTests_count; ++index) {
     71         int iIndex = static_cast<int>(index);
     72         testOne(reporter, iIndex);
     73         reporter->bumpTestCount();
     74     }
     75 }
     76 
     77 static void PathOpsCubicLineIntersectionOneOffTest(skiatest::Reporter* reporter) {
     78     int iIndex = 0;
     79     testOne(reporter, iIndex);
     80     const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
     81     const SkDLine& line = lineCubicTests[iIndex].line;
     82     SkIntersections i;
     83     i.intersect(cubic, line);
     84     SkASSERT(i.used() == 1);
     85 #if ONE_OFF_DEBUG
     86     double cubicT = i[0][0];
     87     SkDPoint prev = cubic.ptAtT(cubicT * 2 - 1);
     88     SkDPoint sect = cubic.ptAtT(cubicT);
     89     double left[3] = { line.isLeft(prev), line.isLeft(sect), line.isLeft(cubic[3]) };
     90     SkDebugf("cubic=(%1.9g, %1.9g, %1.9g)\n", left[0], left[1], left[2]);
     91     SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY);
     92     SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY);
     93     SkDPoint prevL = line.ptAtT(i[1][0] - 0.0000007);
     94     SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY);
     95     SkDPoint nextL = line.ptAtT(i[1][0] + 0.0000007);
     96     SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", i.pt(0).fX, i.pt(0).fY, nextL.fX, nextL.fY);
     97     SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL),
     98             sect.distance(i.pt(0)), cubic[3].distance(prevL));
     99 #endif
    100 }
    101 
    102 #include "TestClassDef.h"
    103 DEFINE_TESTCLASS_SHORT(PathOpsCubicLineIntersectionTest)
    104 
    105 DEFINE_TESTCLASS_SHORT(PathOpsCubicLineIntersectionOneOffTest)
    106