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