blob: 9a0f78f4a721ee908c97c5efd8f29cf3d92702be [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comd8730ea2009-02-27 22:06:06 +00008#include "Test.h"
9#include "SkGeometry.h"
10
reed@google.com6fc321a2011-07-27 13:54:36 +000011static bool nearly_equal(const SkPoint& a, const SkPoint& b) {
12 return SkScalarNearlyEqual(a.fX, b.fX) && SkScalarNearlyEqual(a.fY, b.fY);
13}
14
reed@google.com087d5aa2012-02-29 20:59:24 +000015static void testChopCubic(skiatest::Reporter* reporter) {
16 /*
17 Inspired by this test, which used to assert that the tValues had dups
18
19 <path stroke="#202020" d="M0,0 C0,0 1,1 2190,5130 C2190,5070 2220,5010 2205,4980" />
20 */
21 const SkPoint src[] = {
22 { SkIntToScalar(2190), SkIntToScalar(5130) },
23 { SkIntToScalar(2190), SkIntToScalar(5070) },
24 { SkIntToScalar(2220), SkIntToScalar(5010) },
25 { SkIntToScalar(2205), SkIntToScalar(4980) },
26 };
27 SkPoint dst[13];
28 SkScalar tValues[3];
reed@google.comc256cd12012-02-29 21:57:36 +000029 // make sure we don't assert internally
reed@google.com087d5aa2012-02-29 20:59:24 +000030 int count = SkChopCubicAtMaxCurvature(src, dst, tValues);
reed@google.com087d5aa2012-02-29 20:59:24 +000031}
32
33
reed@android.comd8730ea2009-02-27 22:06:06 +000034static void TestGeometry(skiatest::Reporter* reporter) {
35 SkPoint pts[3], dst[5];
36
37 pts[0].set(0, 0);
38 pts[1].set(100, 50);
39 pts[2].set(0, 100);
40
41 int count = SkChopQuadAtMaxCurvature(pts, dst);
42 REPORTER_ASSERT(reporter, count == 1 || count == 2);
reed@google.com6fc321a2011-07-27 13:54:36 +000043
44 pts[0].set(0, 0);
45 pts[1].set(SkIntToScalar(3), 0);
46 pts[2].set(SkIntToScalar(3), SkIntToScalar(3));
47 SkConvertQuadToCubic(pts, dst);
48 const SkPoint cubic[] = {
tomhudson@google.com221db3c2011-07-28 21:10:29 +000049 { 0, 0, },
50 { SkIntToScalar(2), 0, },
51 { SkIntToScalar(3), SkIntToScalar(1), },
52 { SkIntToScalar(3), SkIntToScalar(3) },
reed@google.com6fc321a2011-07-27 13:54:36 +000053 };
54 for (int i = 0; i < 4; ++i) {
55 REPORTER_ASSERT(reporter, nearly_equal(cubic[i], dst[i]));
56 }
reed@google.com087d5aa2012-02-29 20:59:24 +000057
58 testChopCubic(reporter);
reed@android.comd8730ea2009-02-27 22:06:06 +000059}
60
61#include "TestClassDef.h"
62DEFINE_TESTCLASS("Geometry", GeometryTestClass, TestGeometry)