blob: 2d7b64ec3605273ee18cdcfa4c3a3e73d4fe783e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
reed@android.com5e5adfd2009-03-07 03:39:23 +00008#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000010#include "SkPathMeasure.h"
11
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000012static void test_small_segment3() {
reed@google.comded44142012-04-27 20:22:07 +000013 SkPath path;
14 const SkPoint pts[] = {
15 { 0, 0 },
16 { 100000000000.0f, 100000000000.0f }, { 0, 0 }, { 10, 10 },
17 { 10, 10 }, { 0, 0 }, { 10, 10 }
18 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000019
reed@google.comded44142012-04-27 20:22:07 +000020 path.moveTo(pts[0]);
bsalomon@google.com2fc23592012-10-03 19:10:31 +000021 for (size_t i = 1; i < SK_ARRAY_COUNT(pts); i += 3) {
reed@google.comded44142012-04-27 20:22:07 +000022 path.cubicTo(pts[i], pts[i + 1], pts[i + 2]);
23 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000024
reed@google.comded44142012-04-27 20:22:07 +000025 SkPathMeasure meas(path, false);
26 meas.getLength();
reed@google.comded44142012-04-27 20:22:07 +000027}
28
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000029static void test_small_segment2() {
reed@google.comded44142012-04-27 20:22:07 +000030 SkPath path;
31 const SkPoint pts[] = {
32 { 0, 0 },
rmistry@google.comd6176b02012-08-23 18:14:13 +000033 { 100000000000.0f, 100000000000.0f }, { 0, 0 },
34 { 10, 10 }, { 0, 0 },
reed@google.comded44142012-04-27 20:22:07 +000035 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000036
reed@google.comded44142012-04-27 20:22:07 +000037 path.moveTo(pts[0]);
38 for (size_t i = 1; i < SK_ARRAY_COUNT(pts); i += 2) {
39 path.quadTo(pts[i], pts[i + 1]);
40 }
41 SkPathMeasure meas(path, false);
42 meas.getLength();
reed@google.comded44142012-04-27 20:22:07 +000043}
44
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000045static void test_small_segment() {
reed@google.comfab1ddd2012-04-20 15:10:32 +000046 SkPath path;
47 const SkPoint pts[] = {
48 { 100000, 100000},
49 // big jump between these points, makes a big segment
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000050 { 1.0005f, 0.9999f },
reed@google.comfab1ddd2012-04-20 15:10:32 +000051 // tiny (non-zero) jump between these points
robertphillips@google.comc6ce7502012-05-08 13:15:37 +000052 { SK_Scalar1, SK_Scalar1 },
reed@google.comfab1ddd2012-04-20 15:10:32 +000053 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
reed@google.comfab1ddd2012-04-20 15:10:32 +000055 path.moveTo(pts[0]);
56 for (size_t i = 1; i < SK_ARRAY_COUNT(pts); ++i) {
57 path.lineTo(pts[i]);
58 }
59 SkPathMeasure meas(path, false);
60
61 /* this would assert (before a fix) because we added a segment with
62 the same length as the prev segment, due to the follow (bad) pattern
63
64 d = distance(pts[0], pts[1]);
65 distance += d;
66 seg->fDistance = distance;
reed@google.comded44142012-04-27 20:22:07 +000067
reed@google.comfab1ddd2012-04-20 15:10:32 +000068 SkASSERT(d > 0); // TRUE
69 SkASSERT(seg->fDistance > prevSeg->fDistance); // FALSE
70
71 This 2nd assert failes because (distance += d) didn't affect distance
72 because distance >>> d.
73 */
74 meas.getLength();
reed@google.comfab1ddd2012-04-20 15:10:32 +000075}
76
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000077DEF_TEST(PathMeasure, reporter) {
reed@android.com5e5adfd2009-03-07 03:39:23 +000078 SkPath path;
79
80 path.moveTo(0, 0);
81 path.lineTo(SK_Scalar1, 0);
82 path.lineTo(SK_Scalar1, SK_Scalar1);
83 path.lineTo(0, SK_Scalar1);
84
85 SkPathMeasure meas(path, true);
86 SkScalar length = meas.getLength();
87 SkASSERT(length == SK_Scalar1*4);
88
89 path.reset();
90 path.moveTo(0, 0);
91 path.lineTo(SK_Scalar1*3, SK_Scalar1*4);
92 meas.setPath(&path, false);
93 length = meas.getLength();
94 REPORTER_ASSERT(reporter, length == SK_Scalar1*5);
95
96 path.reset();
97 path.addCircle(0, 0, SK_Scalar1);
98 meas.setPath(&path, true);
99 length = meas.getLength();
100// SkDebugf("circle arc-length = %g\n", length);
101
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000102 // Test the behavior following a close not followed by a move.
103 path.reset();
104 path.lineTo(SK_Scalar1, 0);
105 path.lineTo(SK_Scalar1, SK_Scalar1);
106 path.lineTo(0, SK_Scalar1);
107 path.close();
108 path.lineTo(-SK_Scalar1, 0);
109 meas.setPath(&path, false);
110 length = meas.getLength();
111 REPORTER_ASSERT(reporter, length == SK_Scalar1 * 4);
112 meas.nextContour();
113 length = meas.getLength();
114 REPORTER_ASSERT(reporter, length == SK_Scalar1);
115 SkPoint position;
116 SkVector tangent;
117 REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent));
118 REPORTER_ASSERT(reporter,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000119 SkScalarNearlyEqual(position.fX,
120 -SK_ScalarHalf,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000121 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000122 REPORTER_ASSERT(reporter, position.fY == 0);
123 REPORTER_ASSERT(reporter, tangent.fX == -SK_Scalar1);
124 REPORTER_ASSERT(reporter, tangent.fY == 0);
125
126 // Test degenerate paths
127 path.reset();
128 path.moveTo(0, 0);
129 path.lineTo(0, 0);
130 path.lineTo(SK_Scalar1, 0);
131 path.quadTo(SK_Scalar1, 0, SK_Scalar1, 0);
132 path.quadTo(SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1 * 2);
133 path.cubicTo(SK_Scalar1, SK_Scalar1 * 2,
134 SK_Scalar1, SK_Scalar1 * 2,
135 SK_Scalar1, SK_Scalar1 * 2);
136 path.cubicTo(SK_Scalar1*2, SK_Scalar1 * 2,
137 SK_Scalar1*3, SK_Scalar1 * 2,
138 SK_Scalar1*4, SK_Scalar1 * 2);
139 meas.setPath(&path, false);
140 length = meas.getLength();
141 REPORTER_ASSERT(reporter, length == SK_Scalar1 * 6);
142 REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent));
143 REPORTER_ASSERT(reporter,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000144 SkScalarNearlyEqual(position.fX,
145 SK_ScalarHalf,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000146 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000147 REPORTER_ASSERT(reporter, position.fY == 0);
148 REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1);
149 REPORTER_ASSERT(reporter, tangent.fY == 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000150 REPORTER_ASSERT(reporter, meas.getPosTan(2.5f, &position, &tangent));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000151 REPORTER_ASSERT(reporter,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000152 SkScalarNearlyEqual(position.fX, SK_Scalar1, 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000153 REPORTER_ASSERT(reporter,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000154 SkScalarNearlyEqual(position.fY, 1.5f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000155 REPORTER_ASSERT(reporter, tangent.fX == 0);
156 REPORTER_ASSERT(reporter, tangent.fY == SK_Scalar1);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000157 REPORTER_ASSERT(reporter, meas.getPosTan(4.5f, &position, &tangent));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000158 REPORTER_ASSERT(reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000159 SkScalarNearlyEqual(position.fX,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000160 2.5f,
161 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000162 REPORTER_ASSERT(reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000163 SkScalarNearlyEqual(position.fY,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000164 2.0f,
165 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000166 REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1);
167 REPORTER_ASSERT(reporter, tangent.fY == 0);
168
169 path.reset();
170 path.moveTo(0, 0);
171 path.lineTo(SK_Scalar1, 0);
172 path.moveTo(SK_Scalar1, SK_Scalar1);
173 path.moveTo(SK_Scalar1 * 2, SK_Scalar1 * 2);
174 path.lineTo(SK_Scalar1, SK_Scalar1 * 2);
175 meas.setPath(&path, false);
176 length = meas.getLength();
177 REPORTER_ASSERT(reporter, length == SK_Scalar1);
178 REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent));
179 REPORTER_ASSERT(reporter,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000180 SkScalarNearlyEqual(position.fX,
181 SK_ScalarHalf,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000182 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000183 REPORTER_ASSERT(reporter, position.fY == 0);
184 REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1);
185 REPORTER_ASSERT(reporter, tangent.fY == 0);
186 meas.nextContour();
187 length = meas.getLength();
188 REPORTER_ASSERT(reporter, length == SK_Scalar1);
189 REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent));
190 REPORTER_ASSERT(reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000191 SkScalarNearlyEqual(position.fX,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000192 1.5f,
193 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000194 REPORTER_ASSERT(reporter,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000195 SkScalarNearlyEqual(position.fY,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000196 2.0f,
197 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000198 REPORTER_ASSERT(reporter, tangent.fX == -SK_Scalar1);
199 REPORTER_ASSERT(reporter, tangent.fY == 0);
reed@google.comfab1ddd2012-04-20 15:10:32 +0000200
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000201 test_small_segment();
202 test_small_segment2();
203 test_small_segment3();
reed@android.com5e5adfd2009-03-07 03:39:23 +0000204}