testBasicVector.cc
Go to the documentation of this file.
1 
9 #include <utl/Point.h>
10 #include <utl/Vector.h>
11 #include <utl/AxialVector.h>
12 #include <utl/TransformationMatrix.h>
13 #include <utl/GeometryException.h>
14 #include <tst/Verify.h>
15 #include <utl/Triple.h>
16 #include "testBasicVectorHelper.h"
17 
18 #include <cppunit/extensions/HelperMacros.h>
19 
20 #include <cmath>
21 
22 using namespace utl;
23 using namespace tst;
24 using std::cos;
25 using std::sin;
26 
27 #define TEST_COLLECTION(Type_) \
28  CPPUNIT_TEST_EXCEPTION(testConstructorThrow<Type_>, CoordinateSystemException); \
29  CPPUNIT_TEST(testConstructor<Type_>); \
30  CPPUNIT_TEST(testConstructorCylindrical<Type_>); \
31  CPPUNIT_TEST(testConstructorSpherical<Type_>); \
32  CPPUNIT_TEST_EXCEPTION(testTransformation<Type_>, CoordinateSystemException); \
33  CPPUNIT_TEST(testOperations<Type_>)
34 
35 
39 class testBasicVector : public CppUnit::TestFixture {
40 
41  CPPUNIT_TEST_SUITE(testBasicVector);
45  CPPUNIT_TEST(testNormalize);
46  CPPUNIT_TEST_SUITE_END();
47 
48 private:
49  template<class VectorType>
50  void
52  {
53  const CoordinateSystemPtr CsNull;
54  const VectorType v(0,0,0, CsNull);
55  }
56 
57  template<class VectorType>
58  void
60  {
61  const CoordinateSystemPtr cs = CoordinateSystem::GetRootCoordinateSystem();
62  const VectorType p(0,0,0, cs);
63 
64  CPPUNIT_ASSERT(Verify<CloseTo>(p.GetX(cs), 0.));
65  CPPUNIT_ASSERT(Verify<CloseTo>(p.GetY(cs), 0.));
66  CPPUNIT_ASSERT(Verify<CloseTo>(p.GetZ(cs), 0.));
67  CPPUNIT_ASSERT(Verify<CloseTo>(
68  p.GetCoordinates(cs), Triple(0,0,0)
69  ));
70 
71  const VectorType q(1,2,3, cs);
72  CPPUNIT_ASSERT(Verify<CloseTo>(q.GetX(cs), 1.));
73  CPPUNIT_ASSERT(Verify<CloseTo>(q.GetY(cs), 2.));
74  CPPUNIT_ASSERT(Verify<CloseTo>(q.GetZ(cs), 3.));
75  CPPUNIT_ASSERT(Verify<CloseTo>(
76  q.GetCoordinates(cs), Triple(1,2,3)
77  ));
78 
79  const VectorType x(1,0,0, cs);
80  CPPUNIT_ASSERT(Verify<CloseTo>(
81  x.GetSphericalCoordinates(cs), Triple(1,M_PI/2,0)
82  ));
83 
84  const VectorType x2(1,0,0, cs, VectorType::kCartesian);
85  CPPUNIT_ASSERT(Verify<CloseTo>((x-x2).GetR(cs), 0.));
86 
87  const VectorType x3(1,0,0, cs, tst::cartesian<VectorType>());
88  CPPUNIT_ASSERT(Verify<CloseTo>((x-x3).GetR(cs), 0.));
89  }
90 
91  template<class VectorType>
92  void
94  {
95  const double root2 = std::sqrt(2.);
96 
97  const CoordinateSystemPtr cs = CoordinateSystem::GetRootCoordinateSystem();
98 
99  const VectorType p(1,M_PI/4,2, cs, VectorType::kCylindrical);
100  CPPUNIT_ASSERT(Verify<CloseTo>(
101  p.GetCoordinates(cs), Triple(root2/2,root2/2,2)
102  ));
103 
104  const VectorType q(1,M_PI/4,2, cs, tst::cylindrical<VectorType>());
105  CPPUNIT_ASSERT(Verify<CloseTo>(
106  q.GetCoordinates(cs), Triple(root2/2,root2/2,2)
107  ));
108  }
109 
110  template<class VectorType>
111  void
113  {
114  const double root2 = std::sqrt(2.);
115 
116  const CoordinateSystemPtr cs = CoordinateSystem::GetRootCoordinateSystem();
117 
118  const VectorType p1(1,M_PI/2,M_PI/4, cs, VectorType::kSpherical);
119  CPPUNIT_ASSERT(Verify<CloseTo>(
120  p1.GetCoordinates(cs), Triple(root2/2,root2/2,0)
121  ));
122 
123  const VectorType p2(1,M_PI/4,M_PI/3, cs, VectorType::kSpherical);
124  CPPUNIT_ASSERT(Verify<CloseTo>(
125  p2.GetCoordinates(cs),
126  Triple(root2/2*cos(M_PI/3), root2/2*sin(M_PI/3), root2/2)
127  ));
128 
129  const VectorType p3(1,M_PI/4,M_PI/3, cs, tst::spherical<VectorType>());
130  CPPUNIT_ASSERT(Verify<CloseTo>(
131  p3.GetCoordinates(cs),
132  Triple(root2/2*cos(M_PI/3), root2/2*sin(M_PI/3), root2/2)
133  ));
134  }
135 
136  template<class VectorType>
137  void
139  {
140  const CoordinateSystemPtr csNull;
141  const CoordinateSystemPtr cs = CoordinateSystem::GetRootCoordinateSystem();
142  const VectorType v(0, 0, 0, cs);
143  v.TransformTo(csNull);
144  }
145 
146  template<class VectorType>
147  void
149  {
150  const CoordinateSystemPtr cs = CoordinateSystem::GetRootCoordinateSystem();
151 
152  const VectorType v1(0,0,0, cs);
153  CPPUNIT_ASSERT(Verify<CloseTo>(v1.GetR(cs), 0.));
154 
155  VectorType v2(1,0,0, cs);
156  v2 *= 2;
157  CPPUNIT_ASSERT(Verify<CloseTo>(
158  v2.GetCoordinates(cs), Triple(2,0,0)
159  ));
160  v2 /= 4;
161  CPPUNIT_ASSERT(Verify<CloseTo>(
162  v2.GetCoordinates(cs), Triple(0.5,0,0)
163  ));
164 
165  CPPUNIT_ASSERT(v1 != v2);
166  const VectorType v3(0.5, 0, 0, cs);
167  CPPUNIT_ASSERT(v2 == v3);
168 
169  CPPUNIT_ASSERT(v1 == v1);
170  CPPUNIT_ASSERT(Verify<Not<CloseTo>>(
171  v1.GetCoordinates(cs), v2.GetCoordinates(cs)
172  ));
173  }
174 
175  void
177  {
178  const CoordinateSystemPtr cs = CoordinateSystem::GetRootCoordinateSystem();
179  Vector v1(2,3,4, cs);
180  v1.Normalize();
181  CPPUNIT_ASSERT(Verify<CloseTo>(v1.GetR2(cs), 1.));
182  CPPUNIT_ASSERT(Verify<CloseTo>(v1.GetR(cs), 1.));
183 
184  AxialVector v2(2,3,4, cs);
185  v2.Normalize();
186  CPPUNIT_ASSERT(Verify<CloseTo>(v2.GetR2(cs), 1.));
187  CPPUNIT_ASSERT(Verify<CloseTo>(v2.GetR(cs), 1.));
188  }
189 
190 };
191 
192 
void Normalize()
Definition: Vector.h:64
Point object.
Definition: Point.h:32
void testConstructorCylindrical()
#define TEST_COLLECTION(Type_)
double GetR(const CoordinateSystemPtr &coordinateSystem) const
radius r in spherical coordinates coordinates (distance to origin)
Definition: BasicVector.h:257
void testConstructorThrow()
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
Definition: Test.h:180
boost::shared_ptr< const CoordinateTransformer > CoordinateSystemPtr
Shared pointer for coordinate systems.
boost::tuple< double, double, double > Triple
Coordinate triple for easy getting or setting of coordinates.
Definition: Triple.h:15
bool Verify(const Predicate &pred, const T &lhs, const T &rhs)
Test condition by evaluating a predicate and print on failure.
Definition: Verify.h:38
void testConstructorSpherical()
double GetR2(const CoordinateSystemPtr &coordinateSystem) const
radius r^2 in spherical coordinates coordinates (distance to origin)^2
Definition: BasicVector.h:260
Vector object.
Definition: Vector.h:30
AxialVector object.
Definition: AxialVector.h:30

, generated on Tue Sep 26 2023.