00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <cstddef>
00031 #include <cmath>
00032
00033 #include <gtest/gtest.h>
00034
00035 #include <mcl_3dl/vec3.h>
00036
00037 TEST(Vec3, Constructors)
00038 {
00039
00040 const mcl_3dl::Vec3 a(1.0, 2.0, 3.0);
00041 const mcl_3dl::Vec3 b(a);
00042
00043
00044 ASSERT_TRUE(a.x_ == 1.0);
00045 ASSERT_TRUE(a.y_ == 2.0);
00046 ASSERT_TRUE(a.z_ == 3.0);
00047 ASSERT_TRUE(b.x_ == 1.0);
00048 ASSERT_TRUE(b.y_ == 2.0);
00049 ASSERT_TRUE(b.z_ == 3.0);
00050 }
00051
00052 TEST(Vec3, Operators)
00053 {
00054 const mcl_3dl::Vec3 a(1.0, 2.0, 3.0);
00055
00056
00057 ASSERT_TRUE(mcl_3dl::Vec3(1.0, 2.0, 3.0) == a);
00058 ASSERT_FALSE(mcl_3dl::Vec3(1.0, 2.0, 3.0) != a);
00059
00060 for (uint32_t i = 1; i < (1 << 3); i++)
00061 {
00062 const float xp = (i & (1 << 0)) ? 0.1 : 0.0;
00063 const float yp = (i & (1 << 1)) ? 0.1 : 0.0;
00064 const float zp = (i & (1 << 2)) ? 0.1 : 0.0;
00065 ASSERT_TRUE(mcl_3dl::Vec3(1.0 + xp, 2.0 + yp, 3.0 + zp) != a);
00066 ASSERT_FALSE(mcl_3dl::Vec3(1.0 + xp, 2.0 + yp, 3.0 + zp) == a);
00067 }
00068
00069
00070 const mcl_3dl::Vec3 adding(0.5, -0.5, 1.0);
00071 mcl_3dl::Vec3 a_plus = a;
00072 mcl_3dl::Vec3 a_minus = a;
00073 a_plus += adding;
00074 a_minus -= adding;
00075 ASSERT_TRUE(a + adding == mcl_3dl::Vec3(1.5, 1.5, 4.0));
00076 ASSERT_TRUE(a + adding == a_plus);
00077 ASSERT_TRUE(a - adding == mcl_3dl::Vec3(0.5, 2.5, 2.0));
00078 ASSERT_TRUE(a - adding == a_minus);
00079
00080
00081 ASSERT_TRUE(-a == mcl_3dl::Vec3(-1.0, -2.0, -3.0));
00082
00083
00084 mcl_3dl::Vec3 a_mul = a;
00085 mcl_3dl::Vec3 a_div = a;
00086 a_mul *= 0.5;
00087 a_div /= 2.0;
00088 ASSERT_TRUE(a * 0.5 == mcl_3dl::Vec3(0.5, 1.0, 1.5));
00089 ASSERT_TRUE(a / 2.0 == a * 0.5);
00090 ASSERT_TRUE(a * 0.5 == a_mul);
00091 ASSERT_TRUE(a / 2.0 == a_div);
00092 }
00093
00094 TEST(Vec3, Times)
00095 {
00096
00097 const mcl_3dl::Vec3 a(1.0, 2.0, 3.0);
00098 const mcl_3dl::Vec3 b(-4.0, 5.0, 6.0);
00099 ASSERT_TRUE(a.times(b) == mcl_3dl::Vec3(-4.0, 10.0, 18.0));
00100 }
00101
00102 TEST(Vec3, Norm)
00103 {
00104
00105 const mcl_3dl::Vec3 a(1.0, 2.0, 3.0);
00106 const mcl_3dl::Vec3 b(-4.0, 5.0, 6.0);
00107 ASSERT_LT(fabs(a.norm() - 3.741657), 1e-6);
00108 ASSERT_LT(fabs(b.norm() - 8.774964), 1e-6);
00109 ASSERT_LT(fabs(a.normalized().norm() - 1.0), 1e-6);
00110 ASSERT_LT(fabs(b.normalized().norm() - 1.0), 1e-6);
00111 }
00112
00113 TEST(Vec3, Products)
00114 {
00115
00116 const int num_samples = 8;
00117 const mcl_3dl::Vec3 samples[num_samples] =
00118 {
00119 mcl_3dl::Vec3(1.5, 2.5, 3.5),
00120 mcl_3dl::Vec3(-0.5, 1.0, 1.0),
00121 mcl_3dl::Vec3(0.5, -1.0, 2.0),
00122 mcl_3dl::Vec3(0.5, 1.0, -2.0),
00123 mcl_3dl::Vec3(-2.0, -5.0, 4.0),
00124 mcl_3dl::Vec3(2.0, -5.0, -4.0),
00125 mcl_3dl::Vec3(-2.0, 5.0, -4.0),
00126 mcl_3dl::Vec3(-3.0, -1.0, -2.0)
00127 };
00128
00129 for (int i = 0; i < num_samples; ++i)
00130 {
00131 for (int j = 0; j < num_samples; ++j)
00132 {
00133 const mcl_3dl::Vec3& a = samples[i];
00134 const mcl_3dl::Vec3& b = samples[j];
00135
00136
00137 ASSERT_LT((a - b).dot(a - b) - a.dot(a) - b.dot(b) + 2.0 * a.dot(b), 1e-6);
00138
00139
00140 ASSERT_LT(a.dot(a.cross(b)), 1e-6);
00141 ASSERT_LT(a.dot(a.cross(b)), 1e-6);
00142 }
00143 }
00144 }
00145
00146 int main(int argc, char** argv)
00147 {
00148 testing::InitGoogleTest(&argc, argv);
00149
00150 return RUN_ALL_TESTS();
00151 }