blob: d131007a61d231bbba90c680037f9155de06b54e [file] [log] [blame]
robertphillips3d32d762015-07-13 13:16:44 -07001/*
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 "SkPoint3.h"
Cary Clark48cb2232018-08-29 15:18:39 -040011#include "SkRandom.h"
robertphillips3d32d762015-07-13 13:16:44 -070012#include "Test.h"
13
14static 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
23static 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
51static 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
halcanary9d524f22016-03-29 09:03:52 -070080static void test_length(skiatest::Reporter* reporter,
robertphillips3d32d762015-07-13 13:16:44 -070081 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
91static 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 }
Cary Clark48cb2232018-08-29 15:18:39 -0400108 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 }
robertphillips3d32d762015-07-13 13:16:44 -0700120}
121
122DEF_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;
halcanary9d524f22016-03-29 09:03:52 -0700131 SkScalar fLength;
robertphillips3d32d762015-07-13 13:16:44 -0700132 } 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}