test_vec3.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2017, the mcl_3dl authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <cstddef>
31 #include <cmath>
32 
33 #include <gtest/gtest.h>
34 
35 #include <mcl_3dl/vec3.h>
36 
37 TEST(Vec3, Constructors)
38 {
39  // Test vector elements constructor and copy constructor
40  const mcl_3dl::Vec3 a(1.0, 2.0, 3.0);
41  const mcl_3dl::Vec3 b(a);
42 
43  // Test elements
44  ASSERT_TRUE(a.x_ == 1.0);
45  ASSERT_TRUE(a.y_ == 2.0);
46  ASSERT_TRUE(a.z_ == 3.0);
47  ASSERT_TRUE(b.x_ == 1.0);
48  ASSERT_TRUE(b.y_ == 2.0);
49  ASSERT_TRUE(b.z_ == 3.0);
50 }
51 
52 TEST(Vec3, Operators)
53 {
54  const mcl_3dl::Vec3 a(1.0, 2.0, 3.0);
55 
56  // Test ==,!= operators
57  ASSERT_TRUE(mcl_3dl::Vec3(1.0, 2.0, 3.0) == a);
58  ASSERT_FALSE(mcl_3dl::Vec3(1.0, 2.0, 3.0) != a);
59 
60  for (uint32_t i = 1; i < (1 << 3); i++)
61  {
62  const float xp = (i & (1 << 0)) ? 0.1 : 0.0;
63  const float yp = (i & (1 << 1)) ? 0.1 : 0.0;
64  const float zp = (i & (1 << 2)) ? 0.1 : 0.0;
65  ASSERT_TRUE(mcl_3dl::Vec3(1.0 + xp, 2.0 + yp, 3.0 + zp) != a);
66  ASSERT_FALSE(mcl_3dl::Vec3(1.0 + xp, 2.0 + yp, 3.0 + zp) == a);
67  }
68 
69  // Test +, -, +=, -= operators
70  const mcl_3dl::Vec3 adding(0.5, -0.5, 1.0);
71  mcl_3dl::Vec3 a_plus = a;
72  mcl_3dl::Vec3 a_minus = a;
73  a_plus += adding;
74  a_minus -= adding;
75  ASSERT_TRUE(a + adding == mcl_3dl::Vec3(1.5, 1.5, 4.0));
76  ASSERT_TRUE(a + adding == a_plus);
77  ASSERT_TRUE(a - adding == mcl_3dl::Vec3(0.5, 2.5, 2.0));
78  ASSERT_TRUE(a - adding == a_minus);
79 
80  // Test -() operator
81  ASSERT_TRUE(-a == mcl_3dl::Vec3(-1.0, -2.0, -3.0));
82 
83  // Test scalar *, / operators
84  mcl_3dl::Vec3 a_mul = a;
85  mcl_3dl::Vec3 a_div = a;
86  a_mul *= 0.5;
87  a_div /= 2.0;
88  ASSERT_TRUE(a * 0.5 == mcl_3dl::Vec3(0.5, 1.0, 1.5));
89  ASSERT_TRUE(a / 2.0 == a * 0.5);
90  ASSERT_TRUE(a * 0.5 == a_mul);
91  ASSERT_TRUE(a / 2.0 == a_div);
92 
93  // Test [] operator
94  ASSERT_EQ(1.0, a[0]);
95  ASSERT_EQ(2.0, a[1]);
96  ASSERT_EQ(3.0, a[2]);
97  ASSERT_EQ(1.0, a[3]); // exceeded index points 0
98 }
99 
100 TEST(Vec3, Times)
101 {
102  // Check times operation (element-by-element multiplication)
103  const mcl_3dl::Vec3 a(1.0, 2.0, 3.0);
104  const mcl_3dl::Vec3 b(-4.0, 5.0, 6.0);
105  ASSERT_TRUE(a.times(b) == mcl_3dl::Vec3(-4.0, 10.0, 18.0));
106 }
107 
108 TEST(Vec3, Norm)
109 {
110  // Check norm operations
111  const mcl_3dl::Vec3 a(1.0, 2.0, 3.0);
112  const mcl_3dl::Vec3 b(-4.0, 5.0, 6.0);
113  ASSERT_LT(fabs(a.norm() - 3.741657), 1e-6);
114  ASSERT_LT(fabs(b.norm() - 8.774964), 1e-6);
115  ASSERT_LT(fabs(a.normalized().norm() - 1.0), 1e-6);
116  ASSERT_LT(fabs(b.normalized().norm() - 1.0), 1e-6);
117 }
118 
119 TEST(Vec3, Products)
120 {
121  // Check cross and dot products
122  const int num_samples = 8;
123  const mcl_3dl::Vec3 samples[num_samples] =
124  {
125  mcl_3dl::Vec3(1.5, 2.5, 3.5),
126  mcl_3dl::Vec3(-0.5, 1.0, 1.0),
127  mcl_3dl::Vec3(0.5, -1.0, 2.0),
128  mcl_3dl::Vec3(0.5, 1.0, -2.0),
129  mcl_3dl::Vec3(-2.0, -5.0, 4.0),
130  mcl_3dl::Vec3(2.0, -5.0, -4.0),
131  mcl_3dl::Vec3(-2.0, 5.0, -4.0),
132  mcl_3dl::Vec3(-3.0, -1.0, -2.0)
133  };
134 
135  for (int i = 0; i < num_samples; ++i)
136  {
137  for (int j = 0; j < num_samples; ++j)
138  {
139  const mcl_3dl::Vec3& a = samples[i];
140  const mcl_3dl::Vec3& b = samples[j];
141 
142  // Check dot products based on the distributive property
143  ASSERT_LT((a - b).dot(a - b) - a.dot(a) - b.dot(b) + 2.0 * a.dot(b), 1e-6);
144 
145  // Check cross products based on the geometric meaning
146  ASSERT_LT(a.dot(a.cross(b)), 1e-6);
147  ASSERT_LT(a.dot(a.cross(b)), 1e-6);
148  }
149  }
150 }
151 
152 int main(int argc, char** argv)
153 {
154  testing::InitGoogleTest(&argc, argv);
155 
156  return RUN_ALL_TESTS();
157 }
float z_
Definition: vec3.h:42
constexpr float dot(const Vec3 &q) const
Definition: vec3.h:139
float norm() const
Definition: vec3.h:153
doubleAcc dot(const VectorAcc &lhs, const VectorAcc &rhs)
float y_
Definition: vec3.h:41
int main(int argc, char **argv)
Definition: test_vec3.cpp:152
constexpr Vec3 times(const Vec3 &q) const
Definition: vec3.h:149
float x_
Definition: vec3.h:40
Vec3 normalized() const
Definition: vec3.h:157
constexpr Vec3 cross(const Vec3 &q) const
Definition: vec3.h:143
TEST(Vec3, Constructors)
Definition: test_vec3.cpp:37


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:16:29