1 /*
2 * Copyright 2015 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
8 // Unit tests for src/core/SkPoint3.cpp and its header
9
10 #include "include/core/SkPoint3.h"
11 #include "include/utils/SkRandom.h"
12 #include "tests/Test.h"
13
test_eq_ops(skiatest::Reporter * reporter)14 static void test_eq_ops(skiatest::Reporter* reporter) {
15 const SkPoint3 p0 = SkPoint3::Make(0, 0, 0);
16 const SkPoint3 p1 = SkPoint3::Make(1, 1, 1);
17 const SkPoint3 p2 = SkPoint3::Make(1, 1, 1);
18
19 REPORTER_ASSERT(reporter, p0 != p1);
20 REPORTER_ASSERT(reporter, p1 == p2);
21 }
22
test_ops(skiatest::Reporter * reporter)23 static void test_ops(skiatest::Reporter* reporter) {
24 SkPoint3 v = SkPoint3::Make(1, 1, 1);
25 v.normalize();
26 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(v.length(), SK_Scalar1));
27
28 // scale
29 SkPoint3 p = v.makeScale(3.0f);
30 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.length(), 3.0f));
31
32 p.scale(1.0f/3.0f);
33 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.length(), SK_Scalar1));
34
35 SkPoint3 p1 = SkPoint3::Make(20.0f, 2.0f, 10.0f);
36 SkPoint3 p2 = -p1;
37
38 // -
39 p = p1 - p1;
40 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.x(), 0.0f));
41 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.y(), 0.0f));
42 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.z(), 0.0f));
43
44 // +
45 p = p1 + p2;
46 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.x(), 0.0f));
47 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.y(), 0.0f));
48 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(p.z(), 0.0f));
49 }
50
test_dot(skiatest::Reporter * reporter)51 static void test_dot(skiatest::Reporter* reporter) {
52 const SkPoint3 xAxis = SkPoint3::Make(1.0f, 0.0f, 0.0f);
53 const SkPoint3 yAxis = SkPoint3::Make(0.0f, 1.0f, 0.0f);
54 const SkPoint3 zAxis = SkPoint3::Make(0.0f, 0.0f, 1.0f);
55
56 SkScalar dot = xAxis.dot(yAxis);
57 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, 0.0f));
58
59 dot = yAxis.dot(zAxis);
60 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, 0.0f));
61
62 dot = zAxis.dot(xAxis);
63 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, 0.0f));
64
65 SkPoint3 v = SkPoint3::Make(13.0f, 2.0f, 7.0f);
66 v.normalize();
67
68 dot = v.dot(v);
69 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, 1.0f));
70
71 v = SkPoint3::Make(SK_ScalarRoot2Over2, SK_ScalarRoot2Over2, 0.0f);
72
73 dot = xAxis.dot(v);
74 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, SK_ScalarRoot2Over2));
75
76 dot = yAxis.dot(v);
77 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dot, SK_ScalarRoot2Over2));
78 }
79
test_length(skiatest::Reporter * reporter,SkScalar x,SkScalar y,SkScalar z,SkScalar expectedLen)80 static void test_length(skiatest::Reporter* reporter,
81 SkScalar x, SkScalar y, SkScalar z, SkScalar expectedLen) {
82 SkPoint3 point = SkPoint3::Make(x, y, z);
83
84 SkScalar s1 = point.length();
85 SkScalar s2 = SkPoint3::Length(x, y, z);
86
87 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2));
88 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLen));
89 }
90
test_normalize(skiatest::Reporter * reporter,SkScalar x,SkScalar y,SkScalar z,SkScalar expectedLen)91 static void test_normalize(skiatest::Reporter* reporter,
92 SkScalar x, SkScalar y, SkScalar z, SkScalar expectedLen) {
93 SkPoint3 point = SkPoint3::Make(x, y, z);
94
95 bool result = point.normalize();
96 SkScalar newLength = point.length();
97
98 if (0 == expectedLen) {
99 const SkPoint3 empty = SkPoint3::Make(0.0f, 0.0f, 0.0f);
100
101 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, 0));
102 REPORTER_ASSERT(reporter, !result);
103 REPORTER_ASSERT(reporter, point == empty);
104 } else {
105 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1));
106 REPORTER_ASSERT(reporter, result);
107 }
108 SkRandom random;
109 random.setSeed(1234);
110 SkPoint3 pt3;
111 int testCount = 100000;
112 for (int index = 0; index < testCount; ++index) {
113 SkScalar testVal;
114 do {
115 testVal = random.nextRangeF(0, 2);
116 } while (!testVal);
117 pt3.set(testVal, 0, 0);
118 REPORTER_ASSERT(reporter, !pt3.normalize() || 1 == pt3.fX);
119 }
120 }
121
DEF_TEST(Point3,reporter)122 DEF_TEST(Point3, reporter) {
123 test_eq_ops(reporter);
124 test_ops(reporter);
125 test_dot(reporter);
126
127 static const struct {
128 SkScalar fX;
129 SkScalar fY;
130 SkScalar fZ;
131 SkScalar fLength;
132 } gRec[] = {
133 { 0.0f, 0.0f, 0.0f, 0.0f },
134 { 0.3f, 0.4f, 0.5f, SK_ScalarRoot2Over2 },
135 { 1.0e-37f, 1.0e-37f, 1.0e-37f, 0.0f }, // underflows
136 { 3.4e38f, 0.0f, 0.0f, 3.4e38f } // overflows
137 };
138
139 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
140 test_length(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fZ, gRec[i].fLength);
141 test_normalize(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fZ, gRec[i].fLength);
142 }
143 }
144