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 
94 TEST(Vec3, Times)
95 {
96  // Check times operation (element-by-element multiplication)
97  const mcl_3dl::Vec3 a(1.0, 2.0, 3.0);
98  const mcl_3dl::Vec3 b(-4.0, 5.0, 6.0);
99  ASSERT_TRUE(a.times(b) == mcl_3dl::Vec3(-4.0, 10.0, 18.0));
100 }
101 
102 TEST(Vec3, Norm)
103 {
104  // Check norm operations
105  const mcl_3dl::Vec3 a(1.0, 2.0, 3.0);
106  const mcl_3dl::Vec3 b(-4.0, 5.0, 6.0);
107  ASSERT_LT(fabs(a.norm() - 3.741657), 1e-6);
108  ASSERT_LT(fabs(b.norm() - 8.774964), 1e-6);
109  ASSERT_LT(fabs(a.normalized().norm() - 1.0), 1e-6);
110  ASSERT_LT(fabs(b.normalized().norm() - 1.0), 1e-6);
111 }
112 
113 TEST(Vec3, Products)
114 {
115  // Check cross and dot products
116  const int num_samples = 8;
117  const mcl_3dl::Vec3 samples[num_samples] =
118  {
119  mcl_3dl::Vec3(1.5, 2.5, 3.5),
120  mcl_3dl::Vec3(-0.5, 1.0, 1.0),
121  mcl_3dl::Vec3(0.5, -1.0, 2.0),
122  mcl_3dl::Vec3(0.5, 1.0, -2.0),
123  mcl_3dl::Vec3(-2.0, -5.0, 4.0),
124  mcl_3dl::Vec3(2.0, -5.0, -4.0),
125  mcl_3dl::Vec3(-2.0, 5.0, -4.0),
126  mcl_3dl::Vec3(-3.0, -1.0, -2.0)
127  };
128 
129  for (int i = 0; i < num_samples; ++i)
130  {
131  for (int j = 0; j < num_samples; ++j)
132  {
133  const mcl_3dl::Vec3& a = samples[i];
134  const mcl_3dl::Vec3& b = samples[j];
135 
136  // Check dot products based on the distributive property
137  ASSERT_LT((a - b).dot(a - b) - a.dot(a) - b.dot(b) + 2.0 * a.dot(b), 1e-6);
138 
139  // Check cross products based on the geometric meaning
140  ASSERT_LT(a.dot(a.cross(b)), 1e-6);
141  ASSERT_LT(a.dot(a.cross(b)), 1e-6);
142  }
143  }
144 }
145 
146 int main(int argc, char** argv)
147 {
148  testing::InitGoogleTest(&argc, argv);
149 
150  return RUN_ALL_TESTS();
151 }
float z_
Definition: vec3.h:40
float dot(const Vec3 &q) const
Definition: vec3.h:135
float norm() const
Definition: vec3.h:149
Vec3 times(const Vec3 &q) const
Definition: vec3.h:145
doubleAcc dot(const VectorAcc &lhs, const VectorAcc &rhs)
Vec3 cross(const Vec3 &q) const
Definition: vec3.h:139
float y_
Definition: vec3.h:39
int main(int argc, char **argv)
Definition: test_vec3.cpp:146
float x_
Definition: vec3.h:38
Vec3 normalized() const
Definition: vec3.h:153
TEST(Vec3, Constructors)
Definition: test_vec3.cpp:37


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Mon Jul 8 2019 03:32:36