blob: 91136e0240c5184eaa2d37e03eb9e1527b9a2dbc [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.comd8730ea2009-02-27 22:06:06 +00008#include "SkGeometry.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "Test.h"
reed65cb2cd2015-03-19 10:18:47 -070010#include "SkRandom.h"
reed@android.comd8730ea2009-02-27 22:06:06 +000011
reed@google.com6fc321a2011-07-27 13:54:36 +000012static bool nearly_equal(const SkPoint& a, const SkPoint& b) {
13 return SkScalarNearlyEqual(a.fX, b.fX) && SkScalarNearlyEqual(a.fY, b.fY);
14}
15
reed@google.com087d5aa2012-02-29 20:59:24 +000016static void testChopCubic(skiatest::Reporter* reporter) {
17 /*
18 Inspired by this test, which used to assert that the tValues had dups
rmistry@google.comd6176b02012-08-23 18:14:13 +000019
reed@google.com087d5aa2012-02-29 20:59:24 +000020 <path stroke="#202020" d="M0,0 C0,0 1,1 2190,5130 C2190,5070 2220,5010 2205,4980" />
21 */
22 const SkPoint src[] = {
23 { SkIntToScalar(2190), SkIntToScalar(5130) },
24 { SkIntToScalar(2190), SkIntToScalar(5070) },
25 { SkIntToScalar(2220), SkIntToScalar(5010) },
26 { SkIntToScalar(2205), SkIntToScalar(4980) },
27 };
28 SkPoint dst[13];
29 SkScalar tValues[3];
reed@google.comc256cd12012-02-29 21:57:36 +000030 // make sure we don't assert internally
reed@google.com087d5aa2012-02-29 20:59:24 +000031 int count = SkChopCubicAtMaxCurvature(src, dst, tValues);
caryclark@google.com42639cd2012-06-06 12:03:39 +000032 if (false) { // avoid bit rot, suppress warning
33 REPORTER_ASSERT(reporter, count);
34 }
reed@google.com087d5aa2012-02-29 20:59:24 +000035}
36
reed40b7dd52015-03-20 06:01:08 -070037static void check_pairs(skiatest::Reporter* reporter, int index, SkScalar t, const char name[],
38 SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1) {
39 bool eq = SkScalarNearlyEqual(x0, x1) && SkScalarNearlyEqual(y0, y1);
40 if (!eq) {
41 SkDebugf("%s [%d %g] p0 [%10.8f %10.8f] p1 [%10.8f %10.8f]\n",
42 name, index, t, x0, y0, x1, y1);
43 REPORTER_ASSERT(reporter, eq);
44 }
45}
46
reed65cb2cd2015-03-19 10:18:47 -070047static void test_evalquadat(skiatest::Reporter* reporter) {
48 SkRandom rand;
49 for (int i = 0; i < 1000; ++i) {
50 SkPoint pts[3];
51 for (int j = 0; j < 3; ++j) {
52 pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100);
53 }
reed65cb2cd2015-03-19 10:18:47 -070054 const SkScalar dt = SK_Scalar1 / 128;
reed40b7dd52015-03-20 06:01:08 -070055 SkScalar t = dt;
56 for (int j = 1; j < 128; ++j) {
reed65cb2cd2015-03-19 10:18:47 -070057 SkPoint r0;
58 SkEvalQuadAt(pts, t, &r0);
59 SkPoint r1 = SkEvalQuadAt(pts, t);
reed40b7dd52015-03-20 06:01:08 -070060 check_pairs(reporter, i, t, "quad-pos", r0.fX, r0.fY, r1.fX, r1.fY);
halcanary9d524f22016-03-29 09:03:52 -070061
reed40b7dd52015-03-20 06:01:08 -070062 SkVector v0;
halcanary96fcdcc2015-08-27 07:41:13 -070063 SkEvalQuadAt(pts, t, nullptr, &v0);
reed40b7dd52015-03-20 06:01:08 -070064 SkVector v1 = SkEvalQuadTangentAt(pts, t);
65 check_pairs(reporter, i, t, "quad-tan", v0.fX, v0.fY, v1.fX, v1.fY);
reed40b7dd52015-03-20 06:01:08 -070066
reed65cb2cd2015-03-19 10:18:47 -070067 t += dt;
68 }
69 }
70}
71
reedb6402032015-03-20 13:23:43 -070072static void test_conic_eval_pos(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) {
73 SkPoint p0, p1;
halcanary96fcdcc2015-08-27 07:41:13 -070074 conic.evalAt(t, &p0, nullptr);
reedb6402032015-03-20 13:23:43 -070075 p1 = conic.evalAt(t);
76 check_pairs(reporter, 0, t, "conic-pos", p0.fX, p0.fY, p1.fX, p1.fY);
77}
78
79static void test_conic_eval_tan(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) {
80 SkVector v0, v1;
halcanary96fcdcc2015-08-27 07:41:13 -070081 conic.evalAt(t, nullptr, &v0);
reedb6402032015-03-20 13:23:43 -070082 v1 = conic.evalTangentAt(t);
83 check_pairs(reporter, 0, t, "conic-tan", v0.fX, v0.fY, v1.fX, v1.fY);
84}
85
reedb6402032015-03-20 13:23:43 -070086static void test_conic(skiatest::Reporter* reporter) {
87 SkRandom rand;
88 for (int i = 0; i < 1000; ++i) {
89 SkPoint pts[3];
90 for (int j = 0; j < 3; ++j) {
91 pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100);
92 }
93 for (int k = 0; k < 10; ++k) {
94 SkScalar w = rand.nextUScalar1() * 2;
95 SkConic conic(pts, w);
reedb6402032015-03-20 13:23:43 -070096
97 const SkScalar dt = SK_Scalar1 / 128;
98 SkScalar t = dt;
99 for (int j = 1; j < 128; ++j) {
100 test_conic_eval_pos(reporter, conic, t);
101 test_conic_eval_tan(reporter, conic, t);
102 t += dt;
103 }
104 }
105 }
106}
107
caryclark45398df2015-08-25 13:19:06 -0700108static void test_quad_tangents(skiatest::Reporter* reporter) {
109 SkPoint pts[] = {
110 {10, 20}, {10, 20}, {20, 30},
111 {10, 20}, {15, 25}, {20, 30},
112 {10, 20}, {20, 30}, {20, 30},
113 };
114 int count = (int) SK_ARRAY_COUNT(pts) / 3;
115 for (int index = 0; index < count; ++index) {
116 SkConic conic(&pts[index * 3], 0.707f);
117 SkVector start = SkEvalQuadTangentAt(&pts[index * 3], 0);
118 SkVector mid = SkEvalQuadTangentAt(&pts[index * 3], .5f);
119 SkVector end = SkEvalQuadTangentAt(&pts[index * 3], 1);
120 REPORTER_ASSERT(reporter, start.fX && start.fY);
121 REPORTER_ASSERT(reporter, mid.fX && mid.fY);
122 REPORTER_ASSERT(reporter, end.fX && end.fY);
123 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
124 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
125 }
126}
127
128static void test_conic_tangents(skiatest::Reporter* reporter) {
129 SkPoint pts[] = {
130 { 10, 20}, {10, 20}, {20, 30},
131 { 10, 20}, {15, 25}, {20, 30},
132 { 10, 20}, {20, 30}, {20, 30}
133 };
134 int count = (int) SK_ARRAY_COUNT(pts) / 3;
135 for (int index = 0; index < count; ++index) {
136 SkConic conic(&pts[index * 3], 0.707f);
137 SkVector start = conic.evalTangentAt(0);
138 SkVector mid = conic.evalTangentAt(.5f);
139 SkVector end = conic.evalTangentAt(1);
140 REPORTER_ASSERT(reporter, start.fX && start.fY);
141 REPORTER_ASSERT(reporter, mid.fX && mid.fY);
142 REPORTER_ASSERT(reporter, end.fX && end.fY);
143 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
144 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
145 }
146}
147
reedb1b12f82016-07-13 10:56:53 -0700148static void test_this_conic_to_quad(skiatest::Reporter* r, const SkPoint pts[3], SkScalar w) {
149 SkAutoConicToQuads quadder;
150 const SkPoint* qpts = quadder.computeQuads(pts, w, 0.25);
151 const int qcount = quadder.countQuads();
152 const int pcount = qcount * 2 + 1;
153
154 REPORTER_ASSERT(r, SkPointsAreFinite(qpts, pcount));
155}
156
157/**
158 * We need to ensure that when a conic is approximated by quads, that we always return finite
159 * values in the quads.
160 *
161 * Inspired by crbug_627414
162 */
163static void test_conic_to_quads(skiatest::Reporter* reporter) {
164 const SkPoint triples[] = {
165 { 0, 0 }, { 1, 0 }, { 1, 1 },
msarett16ef4652016-07-13 13:08:44 -0700166 { 0, 0 }, { 3.58732e-43f, 2.72084f }, { 3.00392f, 3.00392f },
reedb1b12f82016-07-13 10:56:53 -0700167 { 0, 0 }, { 100000, 0 }, { 100000, 100000 },
168 { 0, 0 }, { 1e30f, 0 }, { 1e30f, 1e30f },
169 };
170 const int N = sizeof(triples) / sizeof(SkPoint);
171
172 for (int i = 0; i < N; i += 3) {
173 const SkPoint* pts = &triples[i];
174
175 SkRect bounds;
176 bounds.set(pts, 3);
177
178 SkScalar w = 1e30f;
179 do {
180 w *= 2;
181 test_this_conic_to_quad(reporter, pts, w);
182 } while (SkScalarIsFinite(w));
183 test_this_conic_to_quad(reporter, pts, SK_ScalarNaN);
184 }
185}
186
caryclark45398df2015-08-25 13:19:06 -0700187static void test_cubic_tangents(skiatest::Reporter* reporter) {
188 SkPoint pts[] = {
189 { 10, 20}, {10, 20}, {20, 30}, {30, 40},
190 { 10, 20}, {15, 25}, {20, 30}, {30, 40},
191 { 10, 20}, {20, 30}, {30, 40}, {30, 40},
192 };
193 int count = (int) SK_ARRAY_COUNT(pts) / 4;
194 for (int index = 0; index < count; ++index) {
195 SkConic conic(&pts[index * 3], 0.707f);
196 SkVector start, mid, end;
halcanary96fcdcc2015-08-27 07:41:13 -0700197 SkEvalCubicAt(&pts[index * 4], 0, nullptr, &start, nullptr);
198 SkEvalCubicAt(&pts[index * 4], .5f, nullptr, &mid, nullptr);
199 SkEvalCubicAt(&pts[index * 4], 1, nullptr, &end, nullptr);
caryclark45398df2015-08-25 13:19:06 -0700200 REPORTER_ASSERT(reporter, start.fX && start.fY);
201 REPORTER_ASSERT(reporter, mid.fX && mid.fY);
202 REPORTER_ASSERT(reporter, end.fX && end.fY);
203 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
204 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
205 }
206}
207
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000208DEF_TEST(Geometry, reporter) {
reed@android.comd8730ea2009-02-27 22:06:06 +0000209 SkPoint pts[3], dst[5];
210
211 pts[0].set(0, 0);
212 pts[1].set(100, 50);
213 pts[2].set(0, 100);
214
215 int count = SkChopQuadAtMaxCurvature(pts, dst);
216 REPORTER_ASSERT(reporter, count == 1 || count == 2);
reed@google.com6fc321a2011-07-27 13:54:36 +0000217
218 pts[0].set(0, 0);
reeddaee7ea2015-03-26 20:22:33 -0700219 pts[1].set(3, 0);
220 pts[2].set(3, 3);
reed@google.com6fc321a2011-07-27 13:54:36 +0000221 SkConvertQuadToCubic(pts, dst);
222 const SkPoint cubic[] = {
reeddaee7ea2015-03-26 20:22:33 -0700223 { 0, 0, }, { 2, 0, }, { 3, 1, }, { 3, 3 },
reed@google.com6fc321a2011-07-27 13:54:36 +0000224 };
225 for (int i = 0; i < 4; ++i) {
226 REPORTER_ASSERT(reporter, nearly_equal(cubic[i], dst[i]));
227 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000228
reed@google.com087d5aa2012-02-29 20:59:24 +0000229 testChopCubic(reporter);
reed65cb2cd2015-03-19 10:18:47 -0700230 test_evalquadat(reporter);
reedb6402032015-03-20 13:23:43 -0700231 test_conic(reporter);
caryclark45398df2015-08-25 13:19:06 -0700232 test_cubic_tangents(reporter);
233 test_quad_tangents(reporter);
234 test_conic_tangents(reporter);
reedb1b12f82016-07-13 10:56:53 -0700235 test_conic_to_quads(reporter);
reed@android.comd8730ea2009-02-27 22:06:06 +0000236}