source: Sophya/trunk/SophyaLib/Samba/vector3d.cc@ 413

Last change on this file since 413 was 264, checked in by ansari, 26 years ago

petite erreur (abs()-fabs()) Reza 23/04/99

File size: 5.9 KB
Line 
1#include <math.h>
2#include "vector3d.h"
3#include "utilgeom.h"
4
5Vector3d::Vector3d()
6{
7 Setxyz(1.,0.,0.);
8}
9
10Vector3d::Vector3d(double x, double y, double z)
11{
12 _x=x;
13 _y=y;
14 _z=z;
15 xyz2ThetaPhi();
16}
17
18Vector3d::Vector3d(double theta, double phi)
19{
20 _theta=mod(theta,M_PI); // dans [0;pi]
21 _phi=mod(phi,pi2); // dans [0;2pi]
22 ThetaPhi2xyz();
23}
24
25Vector3d::Vector3d(const LongLat& ll)
26{
27 _theta=ll.Theta(); // dans [0;pi]
28 _phi=ll.Phi(); // dans [0;2pi]
29 ThetaPhi2xyz();
30}
31
32Vector3d::Vector3d(const Vector3d& v)
33{
34 _x=v._x;
35 _y=v._y;
36 _z=v._z;
37 _theta=v._theta;
38 _phi=v._phi;
39}
40
41void Vector3d::SetThetaPhi(double theta, double phi)
42{
43 _theta=mod(theta,M_PI);
44 _phi=mod(phi,pi2);
45 ThetaPhi2xyz();
46}
47
48void Vector3d::Setxyz(double x, double y, double z)
49{
50 _x=x;
51 _y=y;
52 _z=z;
53 xyz2ThetaPhi();
54}
55
56void Vector3d::ThetaPhi2xyz()
57{
58 _x=sin(_theta)*cos(_phi);
59 _y=sin(_theta)*sin(_phi);
60 _z=cos(_theta);
61}
62
63void Vector3d::xyz2ThetaPhi()
64{
65 double norm=this->Norm();
66 if( norm != 0. )
67 {
68 _theta=acos(_z/norm); // dans [0,Pi]
69 if( mod(_theta,M_PI) == 0. ) _phi=0.; // on est sur +-Oz, le vecteur z est en phi=0
70 // else _phi=acos(_x/sin(_theta)/norm)+M_PI*(_y<0);
71 else _phi=scangle(_y/sin(_theta)/norm,_x/sin(_theta)/norm);
72 }
73 else // vecteur nul
74 {
75 _theta=0.;
76 _phi=0.;
77 }
78}
79
80Vector3d& Vector3d::Normalize()
81{
82 double norm=this->Norm();
83 if( norm != 0. ) (*this)/=norm;
84 else cerr << "Division par zero" << endl;
85 return *this;
86}
87
88double Vector3d::Norm() const
89{
90 return sqrt(_x*_x+_y*_y+_z*_z);
91}
92
93double Vector3d::Psc(const Vector3d& v) const
94{
95 return _x*v._x+_y*v._y+_z*v._z;
96}
97
98double Vector3d::SepAngle(const Vector3d& v) const
99{
100 double n1=this->Norm();
101 double n2=v.Norm();
102 double ret;
103 if( n1!=0. && n2!=0. ) ret=acos((this->Psc(v))/n1/n2);
104 else
105 {
106 cerr << "Division par zero" << endl;
107 ret=0.;
108 }
109 return ret;
110}
111
112Vector3d Vector3d::Vect(const Vector3d& v) const
113{
114 double xo=_y*v._z-_z*v._y;
115 double yo=_z*v._x-_x*v._z;
116 double zo=_x*v._y-_y*v._x;
117 return Vector3d(xo,yo,zo);
118}
119
120Vector3d Vector3d::VperpPhi() const // vecteur perpendiculaire de meme phi
121{
122 double theta;
123 if( _theta != pi_over_2 ) theta=_theta+(0.5-(_theta>pi_over_2))*M_PI; // on tourne theta de +-pi/2
124 else theta=0.;
125 return Vector3d(theta,_phi);
126}
127
128Vector3d Vector3d::VperpTheta() const // vecteur perpendiculaire de meme theta
129{
130 double phi=mod(_phi+pi_over_2,pi2); // on tourne phi de pi/2
131 return Vector3d(_theta,phi);
132}
133
134Vector3d Vector3d::EPhi() const
135{
136 Vector3d temp;
137 if ( fabs(_z) == 1. ) // si on est en +- Oz
138 {
139 temp=Vector3d(1.,0.,0.);
140 }
141 else
142 {
143 Vector3d k(0,0,-1);
144 temp=this->Vect(k);
145 temp.Normalize();
146 }
147 return temp;
148}
149
150Vector3d Vector3d::ETheta() const
151{
152 Vector3d temp=this->Vect(EPhi());
153 temp.Normalize();
154 return temp;
155}
156
157
158Vector3d Vector3d::Euler(double phi, double theta, double psi) const
159{
160 double cpsi=cos(psi);
161 double ctheta=cos(theta);
162 double cphi=cos(phi);
163 double spsi=sin(psi);
164 double stheta=sin(theta);
165 double sphi=sin(phi);
166 double xnew=(cpsi*cphi-ctheta*sphi*spsi)*_x
167 +(cpsi*sphi+ctheta*cphi*spsi)*_y
168 +spsi*stheta*_z;
169 double ynew=(-spsi*cphi-ctheta*sphi*cpsi)*_x
170 +(-spsi*sphi+ctheta*cphi*cpsi)*_y
171 +cpsi*stheta*_z;
172 double znew=stheta*sphi*_x-stheta*cphi*_y+ctheta*_z;
173 return Vector3d(xnew,ynew,znew);
174}
175
176Vector3d Vector3d::InvEuler(double phi, double theta, double psi) const
177{
178 double cpsi=cos(psi);
179 double ctheta=cos(theta);
180 double cphi=cos(phi);
181 double spsi=sin(psi);
182 double stheta=sin(theta);
183 double sphi=sin(phi);
184 double xnew=(cpsi*cphi-ctheta*sphi*spsi)*_x
185 -(spsi*cphi+ctheta*sphi*cpsi)*_y
186 +sphi*stheta*_z;
187 double ynew=(cpsi*sphi+ctheta*cphi*spsi)*_x
188 +(-spsi*sphi+ctheta*cphi*cpsi)*_y
189 -cphi*stheta*_z;
190 double znew=stheta*spsi*_x+stheta*cpsi*_y+ctheta*_z;
191 return Vector3d(xnew,ynew,znew);
192}
193
194Vector3d Vector3d::Rotate(const Vector3d& omega, double phi)
195{
196 Vector3d rotationaxis=omega;
197 rotationaxis.Normalize();
198 double n=this->Psc(rotationaxis);
199 Vector3d myself=*this;
200 Vector3d rotate=n*rotationaxis+(myself-n*rotationaxis)*cos(phi)-(myself^rotationaxis)*sin(phi);
201 return rotate;
202}
203
204Vector3d& Vector3d::operator+=(const Vector3d& v)
205{
206 *this=*this+v;
207 return *this;
208}
209
210Vector3d& Vector3d::operator-=(const Vector3d& v)
211{
212 *this=*this-v;
213 return *this;
214}
215
216Vector3d& Vector3d::operator+=(double d)
217{
218 Setxyz(_x+d,_y+d,_z+d);
219 return *this;
220}
221
222Vector3d& Vector3d::operator/=(double d)
223{
224 if( d != 0. ) Setxyz(_x/d,_y/d,_z/d);
225 else cerr << "Division par zero." << endl;
226 return *this;
227}
228
229Vector3d& Vector3d::operator*=(double d)
230{
231 Setxyz(_x*d,_y*d,_z*d);
232 return *this;
233}
234
235Vector3d Vector3d::operator^(const Vector3d& v) const
236{
237 return this->Vect(v);
238}
239
240Vector3d Vector3d::operator+(double d) const
241{
242 return Vector3d(_x+d,_y+d,_z+d);
243}
244
245Vector3d Vector3d::operator+(const Vector3d& v) const
246{
247 return Vector3d(_x+v._x,_y+v._y,_z+v._z);
248}
249
250Vector3d Vector3d::operator-(double d) const
251{
252 return *this+(-d);
253}
254
255Vector3d Vector3d::operator-(const Vector3d& v) const
256{
257 return *this+(v*(-1.));
258}
259
260Vector3d Vector3d::operator*(double d) const
261{
262 return Vector3d(d*_x,d*_y,d*_z);
263}
264
265double Vector3d::operator*(const Vector3d& v) const
266{
267 return this->Psc(v);
268}
269
270Vector3d Vector3d::operator/(double d) const
271{
272 Vector3d ret=*this;
273 if( d != 0. ) ret/=d;
274 else cerr << "Division par zero." << endl;
275 return ret;
276}
277
278Vector3d& Vector3d::operator=(const Vector3d& v)
279{
280 if( this != &v )
281 {
282 _x=v._x;
283 _y=v._y;
284 _z=v._z;
285 _theta=v._theta;
286 _phi=v._phi;
287 }
288 return *this;
289}
290
291bool Vector3d::operator==(const Vector3d& v)
292{
293 return (this==&v);
294}
295
296void Vector3d::Print(ostream& os) const
297{
298 os << "Vector3: (X,Y,Z)= (" << _x << ", " << _y << ", " << _z
299 << ") --- Theta,Phi= " << _theta << ", " << _phi << "\n"
300 << "Norme = " << this->Norm() << endl;
301}
Note: See TracBrowser for help on using the repository browser.