test_vec3.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2016-2017, the mcl_3dl authors
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the copyright holder nor the names of its 
00014  *       contributors may be used to endorse or promote products derived from 
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
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   // Test vector elements constructor and copy constructor
00040   const mcl_3dl::Vec3 a(1.0, 2.0, 3.0);
00041   const mcl_3dl::Vec3 b(a);
00042 
00043   // Test elements
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   // Test ==,!= operators
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   // Test +, -, +=, -= operators
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   // Test -() operator
00081   ASSERT_TRUE(-a == mcl_3dl::Vec3(-1.0, -2.0, -3.0));
00082 
00083   // Test scalar *, / operators
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   // Check times operation (element-by-element multiplication)
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   // Check norm operations
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   // Check cross and dot products
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       // Check dot products based on the distributive property
00137       ASSERT_LT((a - b).dot(a - b) - a.dot(a) - b.dot(b) + 2.0 * a.dot(b), 1e-6);
00138 
00139       // Check cross products based on the geometric meaning
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 }


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 20 2019 20:04:43