CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
testBasicVector3D.cc
Go to the documentation of this file.
1// -*- C++ -*-
2// $Id: testBasicVector3D.cc,v 1.5 2010/06/16 16:21:27 garren Exp $
3// ---------------------------------------------------------------------------
4
5#include "CLHEP/Geometry/Point3D.h"
6#include "CLHEP/Geometry/Vector3D.h"
7#include "CLHEP/Geometry/Normal3D.h"
8#include "CLHEP/Geometry/Transform3D.h"
9#include "CLHEP/Units/PhysicalConstants.h"
10
11#include <assert.h>
12#include <cmath>
13#include <iostream>
14
15bool EQUAL(double a, double b) {
16 double del = a - b;
17 if (del < 0) del = -del;
18 return del < 0.000001;
19}
20
21using namespace HepGeom;
22
23
24#define CHECK(point,type) \
25 /* Check default constructor */ \
26 point p00; \
27 assert(p00.x() == 0 && p00.y() == 0 && p00.z() == 0); \
28 \
29 /* Check constructor from three numbers */ \
30 point p01(1,2,3); \
31 point p02(4.,5.,6.); \
32 assert(p01.x() == 1 && p01.y() == 2 && p01.z() == 3); \
33 assert(p02.x() == 4 && p02.y() == 5 && p02.z() == 6); \
34 \
35 /* Check constructor from array */ \
36 float farray[] = {1,2,3}; \
37 type darray[] = {4,5,6}; \
38 point p03(farray); assert(p03 == point(1,2,3)); \
39 point p04(darray); assert(p04 == point(4,5,6)); \
40 \
41 /* Check conversion to array */ \
42 const point p05(1,2,3); \
43 const type * a = p05; \
44 assert(a[0] == 1 && a[1] == 2 && a[2] == 3); \
45 point p06(4,5,6); \
46 a = p06; \
47 assert(a[0] == 4 && a[1] == 5 && a[2] == 6); \
48 type * b = p06; \
49 b[0] = 7; \
50 b[1] = 8; \
51 b[2] = 9; assert(p06 == point(7,8,9)); \
52 \
53 /* Check copy constructor */ \
54 point p10(p01); assert(p10 == point(1,2,3)); \
55 point p11(Point3D <float>(1,2,3)); assert(p11 == point(1,2,3)); \
56 point p12(Vector3D<float>(4,5,6)); assert(p12 == point(4,5,6)); \
57 point p13(Normal3D<float>(7,8,9)); assert(p13 == point(7,8,9)); \
58 point p14(Point3D <type> (1,2,3)); assert(p14 == point(1,2,3)); \
59 point p15(Vector3D<type> (4,5,6)); assert(p15 == point(4,5,6)); \
60 point p16(Normal3D<type> (7,8,9)); assert(p16 == point(7,8,9)); \
61 \
62 /* Check assignment */ \
63 point p20; \
64 p20 = Point3D <float>(1,2,3); assert(p20 == point(1,2,3)); \
65 p20 = Vector3D<float>(4,5,6); assert(p20 == point(4,5,6)); \
66 p20 = Normal3D<float>(7,8,9); assert(p20 == point(7,8,9)); \
67 p20 = Point3D <type> (1,2,3); assert(p20 == point(1,2,3)); \
68 p20 = Vector3D<type> (4,5,6); assert(p20 == point(4,5,6)); \
69 p20 = Normal3D<type> (7,8,9); assert(p20 == point(7,8,9)); \
70 \
71 /* Check arithmetic operations */ \
72 point p21(1,2,3); \
73 p21 += point(1,2,3); assert(p21 == point(2,4,6)); \
74 p21 += Point3D <float>(1,1,1); assert(p21 == point(3,5,7)); \
75 p21 += Vector3D<float>(1,1,1); assert(p21 == point(4,6,8)); \
76 p21 += Normal3D<float>(1,1,1); assert(p21 == point(5,7,9)); \
77 p21 -= point(1,2,3); assert(p21 == point(4,5,6)); \
78 p21 -= Point3D <type>(1,1,1); assert(p21 == point(3,4,5)); \
79 p21 -= Vector3D<type>(1,1,1); assert(p21 == point(2,3,4)); \
80 p21 -= Normal3D<type>(1,1,1); assert(p21 == point(1,2,3)); \
81 p21 *= 2; assert(p21 == point(2,4,6)); \
82 p21 /= 2; assert(p21 == point(1,2,3)); \
83 p21 *= 2.0f; assert(p21 == point(2,4,6)); \
84 p21 /= 2.0f; assert(p21 == point(1,2,3)); \
85 p21 *= 2.0; assert(p21 == point(2,4,6)); \
86 p21 /= 2.0; assert(p21 == point(1,2,3)); \
87 \
88 /* Check subscripting */ \
89 point p22(1,2,3); \
90 assert(p22(0) == 1 && p22(1) == 2 && p22(2) == 3); \
91 assert(p22[0] == 1 && p22[1] == 2 && p22[2] == 3); \
92 p22(0) = 4; \
93 p22(1) = 5; \
94 p22(2) = 6; assert(p22 == point(4,5,6)); \
95 p22[0] = 7; \
96 p22[1] = 8; \
97 p22[2] = 9; assert(p22 == point(7,8,9)); \
98 \
99 /* Check carthesian coordinate system */ \
100 point p30; \
101 p30.setX(1); \
102 p30.setY(2); \
103 p30.setZ(3); assert(p30 == point(1,2,3)); \
104 p30.set(4,5,6); \
105 assert(p30.x() == 4 && p30.y() == 5 && p30.z() == 6); \
106 \
107 /* Check cylindrical coordinate system */ \
108 point p40(12,16,1); assert(p40.perp2() == 400); \
109 assert(p40.perp() == 20); \
110 assert(p40.rho() == 20); \
111 p40.setPerp(5); assert(p40 == point(3,4,1)); \
112 p40.set(0,0,1); \
113 p40.setPerp(5); assert(p40 == point(0,0,1)); \
114 \
115 /* Check spherical coordinate system */ \
116 point p50(2,3,6); assert(p50.mag2() == 49); \
117 assert(p50.mag() == 7); \
118 assert(p50.r() == 7); \
119 assert(p50.getR() == 7); \
120 point p51(0,0,1); assert(p51.phi() == 0); \
121 point p52(2,4,5); assert(EQUAL(p52.phi() ,std::atan2(2.,1.))); \
122 assert(EQUAL(p52.getPhi() ,std::atan2(2.,1.))); \
123 point p53(0,0,0); assert(p53.theta() == 0); \
124 assert(p53.cosTheta() == 1); \
125 point p54(3,4,10); assert(EQUAL(p54.theta() ,std::atan2(1.,2.))); \
126 assert(EQUAL(p54.getTheta(),std::atan2(1.,2.))); \
127 assert(EQUAL(p54.cosTheta(),std::sqrt(0.8))); \
128 point p55(2,3,6); \
129 p55.setMag(14); assert(p55 == point(4,6,12)); \
130 p55.setR(7); assert(p55 == point(2,3,6)); \
131 point p56 = p55; \
132 p56.setPhi(CLHEP::pi/6); assert(EQUAL(p56.getPhi(),CLHEP::pi/6)); \
133 assert(EQUAL(p56.mag() ,p55.mag())); \
134 assert(EQUAL(p56.theta() ,p55.theta())); \
135 point p57 = p55; \
136 p57.setTheta(CLHEP::pi/3); assert(EQUAL(p57.cosTheta(),0.5)); \
137 assert(EQUAL(p57.mag() ,p55.mag())); \
138 assert(EQUAL(p57.phi() ,p55.phi())); \
139 \
140 /* Check pseudo-rapidity */ \
141 point p60(2,3,6); \
142 point p61 = p60; \
143 p61.setEta(2); assert(EQUAL(p61.pseudoRapidity(),2)); \
144 assert(EQUAL(p61.getEta() ,2)); \
145 assert(EQUAL(p61.eta() ,2)); \
146 assert(EQUAL(p61.mag() ,7)); \
147 assert(EQUAL(p61.phi(),p60.phi())); \
148 \
149 /* Check combination of two vectors */ \
150 point p70(1,2,3); assert(p70.dot(p70) == p70.mag2()); \
151 point p71( 1,2, 3); \
152 point p72(-3,2,-1); assert(p71.cross(p72) == point(-8,-8,8)); \
153 point p73(3,4,0); assert(p73.perp2(point(0,1,0)) == 9); \
154 assert(p73.perp (point(1,0,0)) == 4); \
155 point p74(1,0,0); \
156 point p75(1,1,0); assert(EQUAL(p74.angle(p75),CLHEP::pi/4)); \
157 assert(EQUAL(p75.angle(p00),CLHEP::pi/2)); \
158 \
159 /* Check related vectors */ \
160 point p80(1,2,3); \
161 point p81 = p80.unit(); assert(EQUAL(p81.mag() ,1)); \
162 point p82 = p80.orthogonal(); assert(EQUAL(p82.dot(p81),0)); \
163 \
164 /* Check rotations */ \
165 point p90(2,0.5,std::sqrt(3.)/2); \
166 p90.rotateX(CLHEP::pi/6); assert(p90.x() == 2); \
167 assert(EQUAL(p90.y(),0)); \
168 assert(EQUAL(p90.z(),1)); \
169 point p91(std::sqrt(3.)/2,2,0.5); \
170 p91.rotateY(CLHEP::pi/6); assert(EQUAL(p91.x(),1)); \
171 assert(p91.y() == 2); \
172 assert(EQUAL(p91.z(),0)); \
173 point p92(0.5,std::sqrt(3.)/2,2); \
174 p92.rotateZ(CLHEP::pi/6); assert(EQUAL(p92.x(),0)); \
175 assert(EQUAL(p92.y(),1)); \
176 assert(p92.z() == 2); \
177 point p93(1,1,std::sqrt(2.)); \
178 p93.rotate(CLHEP::pi,Vector3D<float>(-1,-1,std::sqrt(2.))); \
179 assert(EQUAL(p93.x(),-1)); \
180 assert(EQUAL(p93.y(),-1)); \
181 assert(EQUAL(p93.z(),-std::sqrt(2.))); \
182 \
183 /* Check transformations */ \
184 point p100(1,1,std::sqrt(2.)); \
185 Transform3D m; \
186 m = Rotate3D(CLHEP::pi,Vector3D<float>(-1,-1,std::sqrt(2.))); \
187 p100.transform(m); assert(EQUAL(p100.x(),-1)); \
188 assert(EQUAL(p100.y(),-1)); \
189 assert(EQUAL(p100.z(),-std::sqrt(2.))); \
190 \
191 /* Check input/output */ \
192 point p110; \
193 std::cin >> p110; \
194 std::cout << p110 << std::endl; \
195 \
196 /* Check non-member arithmetics */ \
197 point p120(-1,-2,-3); \
198 p120 = +p120; assert(p120 == point(-1,-2,-3)); \
199 p120 = -p120; assert(p120 == point(1,2,3)); \
200 point p121(1,2,3); \
201 p121 = p121 + Point3D <float>(1,1,1); assert(p121 == point(2,3,4));\
202 p121 = p121 + Vector3D<float>(1,1,1); assert(p121 == point(3,4,5));\
203 p121 = p121 + Normal3D<float>(1,1,1); assert(p121 == point(4,5,6));\
204 p121 = p121 - Point3D <type> (1,1,1); assert(p121 == point(3,4,5));\
205 p121 = p121 - Vector3D<type> (1,1,1); assert(p121 == point(2,3,4));\
206 p121 = p121 - Normal3D<type> (1,1,1); assert(p121 == point(1,2,3));\
207 p121 = p121 * 2; assert(p121 == point(2,4,6)); \
208 p121 = p121 / 2; assert(p121 == point(1,2,3)); \
209 p121 = p121 * 2.0f; assert(p121 == point(2,4,6)); \
210 p121 = p121 / 2.0f; assert(p121 == point(1,2,3)); \
211 p121 = p121 * 2.0; assert(p121 == point(2,4,6)); \
212 p121 = p121 / 2.0; assert(p121 == point(1,2,3)); \
213 p121 = 2 * p121; assert(p121 == point(2,4,6)); \
214 p121 = 0.5f * p121; assert(p121 == point(1,2,3)); \
215 p121 = 2.0 * p121; assert(p121 == point(2,4,6)); \
216 assert(p121 * p121 == p121.mag2()); \
217 assert(p121 * Point3D <float>(1,1,1) == 12); \
218 assert(p121 * Vector3D<float>(1,1,1) == 12); \
219 assert(p121 * Normal3D<float>(1,1,1) == 12); \
220 assert(p121 == Point3D <float>(2,4,6)); \
221 assert(p121 == Vector3D<float>(2,4,6)); \
222 assert(p121 == Normal3D<float>(2,4,6)); \
223 assert(p121 != Point3D <type> (3,4,6)); \
224 assert(p121 != Vector3D<type> (2,5,6)); \
225 assert(p121 != Normal3D<type> (2,4,7)); \
226
227// don't generate warnings about unused variables inside assert
234
235int main()
236{
243 return 0;
244}
void CheckVectorDouble()
void CheckPointDouble()
void CheckNormalDouble()
void CheckVectorFloat()
#define CHECK(point, type)
bool EQUAL(double a, double b)
void CheckPointFloat()
int main()
void CheckNormalFloat()