blob: 6714ca460954e63df15c0638bc364d8262115b69 [file] [log] [blame]
caryclark@google.comad65a3e2013-04-15 19:13:59 +00001/*
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 "SkOpSegment.h"
8#include "Test.h"
9
10static const SkPoint cubics[][4] = {
11 {{0, 1}, {2, 6}, {4, 2}, {5, 3}}
12};
13
14static const SkPoint lines[][2] = {
15 {{6, 2}, {2, 4}}
16};
17
18struct SortSet {
19 const SkPoint* ptData;
20 int ptCount;
21 double tStart;
22 double tEnd;
23};
24
25static const SortSet set1[] = {
caryclark@google.comb3f09212013-04-17 15:49:16 +000026 {cubics[0], 4, 0.66666987081928919, 0.875},
27 {lines[0], 2, 0.574070336, 0.388888889},
28 {cubics[0], 4, 0.66666987081928919, 0.4050371120499307 },
29 {lines[0], 2, 0.574070336, 0.9140625},
caryclark@google.comad65a3e2013-04-15 19:13:59 +000030};
31
32static const SortSet set2[] = {
caryclark@google.comb3f09212013-04-17 15:49:16 +000033 {cubics[0], 4, 0.666666667, 0.875},
caryclark@google.comad65a3e2013-04-15 19:13:59 +000034 {lines[0], 2, 0.574074074, 0.388888889},
35 {cubics[0], 4, 0.666666667, 0.405037112},
36 {lines[0], 2, 0.574074074, 0.9140625},
caryclark@google.comad65a3e2013-04-15 19:13:59 +000037};
38
39struct SortSetTests {
40 const SortSet* set;
41 size_t count;
42};
43
44static const SortSetTests tests[] = {
caryclark@google.comb3f09212013-04-17 15:49:16 +000045 { set2, SK_ARRAY_COUNT(set2) },
46 { set1, SK_ARRAY_COUNT(set1) }
caryclark@google.comad65a3e2013-04-15 19:13:59 +000047};
48
skia.committer@gmail.com7841c632013-04-16 07:01:17 +000049static void setup(const SortSet* set, const size_t idx, SkPoint const ** data,
caryclark@google.comb3f09212013-04-17 15:49:16 +000050 SkOpSegment* seg, int* ts) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +000051 SkPoint start, end;
52 if (set[idx].ptCount == 2) {
caryclark@google.comb3f09212013-04-17 15:49:16 +000053 *data = set[idx].ptData;
caryclark@google.comad65a3e2013-04-15 19:13:59 +000054 seg->addLine(*data, false, false);
55 SkDLine dLine;
56 dLine.set(set[idx].ptData);
57 start = dLine.xyAtT(set[idx].tStart).asSkPoint();
58 end = dLine.xyAtT(set[idx].tEnd).asSkPoint();
59 } else if (set[idx].ptCount == 4) {
caryclark@google.comb3f09212013-04-17 15:49:16 +000060 *data = set[idx].ptData;
caryclark@google.comad65a3e2013-04-15 19:13:59 +000061 seg->addCubic(*data, false, false);
62 SkDCubic dCubic;
63 dCubic.set(set[idx].ptData);
64 start = dCubic.xyAtT(set[idx].tStart).asSkPoint();
65 end = dCubic.xyAtT(set[idx].tEnd).asSkPoint();
66 }
67 seg->addT(NULL, start, set[idx].tStart);
68 seg->addT(NULL, end, set[idx].tEnd);
69 seg->addT(NULL, set[idx].ptData[0], 0);
70 seg->addT(NULL, set[idx].ptData[set[idx].ptCount - 1], 1);
caryclark@google.comb3f09212013-04-17 15:49:16 +000071 int tIndex = 0;
72 do {
73 if (seg->t(tIndex) == set[idx].tStart) {
74 ts[0] = tIndex;
75 }
76 if (seg->t(tIndex) == set[idx].tEnd) {
77 ts[1] = tIndex;
78 }
79 if (seg->t(tIndex) >= 1) {
80 break;
81 }
82 } while (++tIndex);
caryclark@google.comad65a3e2013-04-15 19:13:59 +000083}
84
85static void PathOpsAngleTest(skiatest::Reporter* reporter) {
86 for (size_t index = 0; index < SK_ARRAY_COUNT(tests); ++index) {
87 const SortSetTests& test = tests[index];
88 for (size_t idxL = 0; idxL < test.count - 1; ++idxL) {
89 SkOpSegment lesser, greater;
caryclark@google.comb3f09212013-04-17 15:49:16 +000090 int lesserTs[2], greaterTs[2];
caryclark@google.comad65a3e2013-04-15 19:13:59 +000091 const SkPoint* lesserData, * greaterData;
92 const SortSet* set = test.set;
caryclark@google.comb3f09212013-04-17 15:49:16 +000093 setup(set, idxL, &lesserData, &lesser, lesserTs);
caryclark@google.comad65a3e2013-04-15 19:13:59 +000094 size_t idxG = idxL + 1;
caryclark@google.comb3f09212013-04-17 15:49:16 +000095 setup(set, idxG, &greaterData, &greater, greaterTs);
caryclark@google.comad65a3e2013-04-15 19:13:59 +000096 SkOpAngle first, second;
97 first.set(lesserData, (SkPath::Verb) (set[idxL].ptCount - 1), &lesser,
caryclark@google.comb3f09212013-04-17 15:49:16 +000098 lesserTs[0], lesserTs[1], lesser.spans());
caryclark@google.comad65a3e2013-04-15 19:13:59 +000099 second.set(greaterData, (SkPath::Verb) (set[idxG].ptCount - 1), &greater,
caryclark@google.comb3f09212013-04-17 15:49:16 +0000100 greaterTs[0], greaterTs[1], greater.spans());
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000101 bool compare = first < second;
102 if (!compare) {
103 SkDebugf("%s test[%d]: lesser[%d] > greater[%d]\n", __FUNCTION__,
104 index, idxL, idxG);
105 }
106 REPORTER_ASSERT(reporter, compare);
107 }
108 }
109}
110
111#include "TestClassDef.h"
112DEFINE_TESTCLASS_SHORT(PathOpsAngleTest)