| [764] | 1 | #include "machdefs.h" | 
|---|
|  | 2 | #include <math.h> | 
|---|
|  | 3 | #include "pexceptions.h" | 
|---|
|  | 4 | #include "vector3d.h" | 
|---|
|  | 5 | #include "utilgeom.h" | 
|---|
|  | 6 | //++ | 
|---|
|  | 7 | // Class        Vector3d | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // include      vector3d.h utilgeom.h  longlat.h math.h | 
|---|
|  | 10 | // | 
|---|
|  | 11 | // | 
|---|
|  | 12 | //    3-D geometry. | 
|---|
|  | 13 | //    All computations are made with angles in radians and with spherical | 
|---|
|  | 14 | //    coordinates theta, phi. | 
|---|
|  | 15 | // | 
|---|
|  | 16 | //    Concerning Euler's angles, the reference is : | 
|---|
|  | 17 | // | 
|---|
|  | 18 | //    "Classical Mechanics" 2nd edition, H. Goldstein, Addison Wesley | 
|---|
|  | 19 | //-- | 
|---|
|  | 20 | //++ | 
|---|
|  | 21 | // Titre        Constructors | 
|---|
|  | 22 | //-- | 
|---|
|  | 23 | //++ | 
|---|
|  | 24 | Vector3d::Vector3d() | 
|---|
|  | 25 | // | 
|---|
|  | 26 | //-- | 
|---|
|  | 27 | { | 
|---|
|  | 28 | Setxyz(1.,0.,0.); | 
|---|
|  | 29 | } | 
|---|
|  | 30 | //++ | 
|---|
|  | 31 | Vector3d::Vector3d(double x, double y, double z) | 
|---|
|  | 32 | // | 
|---|
|  | 33 | //-- | 
|---|
|  | 34 | { | 
|---|
|  | 35 | _x=x; | 
|---|
|  | 36 | _y=y; | 
|---|
|  | 37 | _z=z; | 
|---|
|  | 38 | xyz2ThetaPhi(); | 
|---|
|  | 39 | } | 
|---|
|  | 40 | //++ | 
|---|
|  | 41 | Vector3d::Vector3d(double theta, double phi) | 
|---|
|  | 42 | // | 
|---|
|  | 43 | //-- | 
|---|
|  | 44 | { | 
|---|
|  | 45 | _theta=mod(theta,M_PI); // dans [0;pi] | 
|---|
|  | 46 | _phi=mod(phi,pi2); // dans [0;2pi] | 
|---|
|  | 47 | ThetaPhi2xyz(); | 
|---|
|  | 48 | } | 
|---|
|  | 49 | //++ | 
|---|
|  | 50 | Vector3d::Vector3d(const LongLat& ll) | 
|---|
|  | 51 | // | 
|---|
|  | 52 | //-- | 
|---|
|  | 53 | { | 
|---|
|  | 54 | _theta=ll.Theta(); // dans [0;pi] | 
|---|
|  | 55 | _phi=ll.Phi(); // dans [0;2pi] | 
|---|
|  | 56 | ThetaPhi2xyz(); | 
|---|
|  | 57 | } | 
|---|
|  | 58 | //++ | 
|---|
|  | 59 | Vector3d::Vector3d(const Vector3d& v) | 
|---|
|  | 60 | // | 
|---|
|  | 61 | //-- | 
|---|
|  | 62 | { | 
|---|
|  | 63 | _x=v._x; | 
|---|
|  | 64 | _y=v._y; | 
|---|
|  | 65 | _z=v._z; | 
|---|
|  | 66 | _theta=v._theta; | 
|---|
|  | 67 | _phi=v._phi; | 
|---|
|  | 68 | } | 
|---|
|  | 69 | //++ | 
|---|
|  | 70 | // Titre        Public methods | 
|---|
|  | 71 | //-- | 
|---|
|  | 72 | //++ | 
|---|
|  | 73 | void Vector3d::SetThetaPhi(double theta, double phi) | 
|---|
|  | 74 | // | 
|---|
|  | 75 | //-- | 
|---|
|  | 76 | { | 
|---|
|  | 77 | _theta=mod(theta,M_PI); | 
|---|
|  | 78 | _phi=mod(phi,pi2); | 
|---|
|  | 79 | ThetaPhi2xyz(); | 
|---|
|  | 80 | } | 
|---|
|  | 81 | //++ | 
|---|
|  | 82 | void Vector3d::Setxyz(double x, double y, double z) | 
|---|
|  | 83 | // | 
|---|
|  | 84 | //-- | 
|---|
|  | 85 | { | 
|---|
|  | 86 | _x=x; | 
|---|
|  | 87 | _y=y; | 
|---|
|  | 88 | _z=z; | 
|---|
|  | 89 | xyz2ThetaPhi(); | 
|---|
|  | 90 | } | 
|---|
|  | 91 | //++ | 
|---|
|  | 92 | void Vector3d::ThetaPhi2xyz() | 
|---|
|  | 93 | // | 
|---|
|  | 94 | //-- | 
|---|
|  | 95 | { | 
|---|
|  | 96 | _x=sin(_theta)*cos(_phi); | 
|---|
|  | 97 | _y=sin(_theta)*sin(_phi); | 
|---|
|  | 98 | _z=cos(_theta); | 
|---|
|  | 99 | } | 
|---|
|  | 100 | //++ | 
|---|
|  | 101 | void Vector3d::xyz2ThetaPhi() | 
|---|
|  | 102 | // | 
|---|
|  | 103 | //-- | 
|---|
|  | 104 | { | 
|---|
|  | 105 | double norm=this->Norm(); | 
|---|
|  | 106 | if( norm != 0. ) | 
|---|
|  | 107 | { | 
|---|
|  | 108 | _theta=acos(_z/norm); // dans [0,Pi] | 
|---|
|  | 109 | if( mod(_theta,M_PI) == 0. ) _phi=0.; // on est sur +-Oz, le vecteur z est en phi=0 | 
|---|
|  | 110 | //      else _phi=acos(_x/sin(_theta)/norm)+M_PI*(_y<0); | 
|---|
|  | 111 | else _phi=scangle(_y/sin(_theta)/norm,_x/sin(_theta)/norm); | 
|---|
|  | 112 | } | 
|---|
|  | 113 | else // vecteur nul | 
|---|
|  | 114 | { | 
|---|
|  | 115 | _theta=0.; | 
|---|
|  | 116 | _phi=0.; | 
|---|
|  | 117 | } | 
|---|
|  | 118 | } | 
|---|
|  | 119 | //++ | 
|---|
|  | 120 | Vector3d& Vector3d::Normalize() | 
|---|
|  | 121 | // | 
|---|
|  | 122 | //-- | 
|---|
|  | 123 | { | 
|---|
|  | 124 | double norm=this->Norm(); | 
|---|
|  | 125 | if( norm != 0. )  (*this)/=norm; | 
|---|
|  | 126 | else cerr << "Division par zero" << endl; | 
|---|
|  | 127 | return *this; | 
|---|
|  | 128 | } | 
|---|
|  | 129 | //++ | 
|---|
|  | 130 | double Vector3d::Norm() const | 
|---|
|  | 131 | // | 
|---|
|  | 132 | //-- | 
|---|
|  | 133 | { | 
|---|
|  | 134 | return sqrt(_x*_x+_y*_y+_z*_z); | 
|---|
|  | 135 | } | 
|---|
|  | 136 | //++ | 
|---|
|  | 137 | double Vector3d::Psc(const Vector3d& v) const | 
|---|
|  | 138 | // | 
|---|
|  | 139 | //    dot product | 
|---|
|  | 140 | //-- | 
|---|
|  | 141 | { | 
|---|
|  | 142 | return _x*v._x+_y*v._y+_z*v._z; | 
|---|
|  | 143 | } | 
|---|
|  | 144 | //++ | 
|---|
|  | 145 | double Vector3d::SepAngle(const Vector3d& v) const | 
|---|
|  | 146 | // | 
|---|
|  | 147 | //    angular gap between 2 vectors in [0,Pi] | 
|---|
|  | 148 | //-- | 
|---|
|  | 149 | { | 
|---|
|  | 150 | double n1=this->Norm(); | 
|---|
|  | 151 | double n2=v.Norm(); | 
|---|
|  | 152 | double ret; | 
|---|
|  | 153 | if( n1!=0. && n2!=0. ) ret=acos((this->Psc(v))/n1/n2); | 
|---|
|  | 154 | else | 
|---|
|  | 155 | { | 
|---|
|  | 156 | cerr << "Division par zero" << endl; | 
|---|
|  | 157 | ret=0.; | 
|---|
|  | 158 | } | 
|---|
|  | 159 | return ret; | 
|---|
|  | 160 | } | 
|---|
|  | 161 | //++ | 
|---|
|  | 162 | Vector3d Vector3d::Vect(const Vector3d& v) const | 
|---|
|  | 163 | // | 
|---|
|  | 164 | //    vector product | 
|---|
|  | 165 | //-- | 
|---|
|  | 166 | { | 
|---|
|  | 167 | double xo=_y*v._z-_z*v._y; | 
|---|
|  | 168 | double yo=_z*v._x-_x*v._z; | 
|---|
|  | 169 | double zo=_x*v._y-_y*v._x; | 
|---|
|  | 170 | return Vector3d(xo,yo,zo); | 
|---|
|  | 171 | } | 
|---|
|  | 172 | //++ | 
|---|
|  | 173 | Vector3d Vector3d::VperpPhi() const | 
|---|
|  | 174 | // | 
|---|
|  | 175 | //    perpendicular vector, with equal phi | 
|---|
|  | 176 | //-- | 
|---|
|  | 177 | { | 
|---|
|  | 178 | double theta; | 
|---|
|  | 179 | if( _theta != pi_over_2 ) theta=_theta+(0.5-(_theta>pi_over_2))*M_PI; // on tourne theta de +-pi/2 | 
|---|
|  | 180 | else theta=0.; | 
|---|
|  | 181 | return Vector3d(theta,_phi); | 
|---|
|  | 182 | } | 
|---|
|  | 183 | //++ | 
|---|
|  | 184 | Vector3d Vector3d::VperpTheta() const | 
|---|
|  | 185 | // | 
|---|
|  | 186 | //    perpendicular vector with equal theta | 
|---|
|  | 187 | //-- | 
|---|
| [784] | 188 | { cerr<< " Erreur in Vector3d::VperpTheta()"<<endl; | 
|---|
| [764] | 189 | throw PError("Vector3d::VperpTheta() - Logic Error DY/Reza 20/02/2000"); | 
|---|
|  | 190 | // Bug ??? (D. Yvon, Fevrier 2000) | 
|---|
|  | 191 | //  double phi=mod(_phi+pi_over_2,pi2); // on tourne phi de pi/2 | 
|---|
|  | 192 | //  return Vector3d(_theta,phi); | 
|---|
|  | 193 | } | 
|---|
|  | 194 |  | 
|---|
|  | 195 | Vector3d Vector3d::EPhi() const | 
|---|
|  | 196 | { | 
|---|
|  | 197 | Vector3d temp; | 
|---|
|  | 198 | if ( fabs(_z) == 1. ) // si on est en +- Oz | 
|---|
|  | 199 | { | 
|---|
|  | 200 | temp=Vector3d(1.,0.,0.); | 
|---|
|  | 201 | } | 
|---|
|  | 202 | else | 
|---|
|  | 203 | { | 
|---|
|  | 204 | Vector3d k(0,0,-1); | 
|---|
|  | 205 | temp=this->Vect(k); | 
|---|
|  | 206 | temp.Normalize(); | 
|---|
|  | 207 | } | 
|---|
|  | 208 | return temp; | 
|---|
|  | 209 | } | 
|---|
|  | 210 | //++ | 
|---|
|  | 211 | Vector3d Vector3d::ETheta() const | 
|---|
|  | 212 | // | 
|---|
|  | 213 | //-- | 
|---|
|  | 214 | { | 
|---|
|  | 215 | Vector3d temp=this->Vect(EPhi()); | 
|---|
|  | 216 | temp.Normalize(); | 
|---|
|  | 217 | return temp; | 
|---|
|  | 218 | } | 
|---|
|  | 219 |  | 
|---|
|  | 220 | //++ | 
|---|
|  | 221 | Vector3d Vector3d::Euler(double phi, double theta, double psi) const | 
|---|
|  | 222 | // | 
|---|
|  | 223 | //    Euler's rotations | 
|---|
|  | 224 | //-- | 
|---|
|  | 225 | { | 
|---|
|  | 226 | double cpsi=cos(psi); | 
|---|
|  | 227 | double ctheta=cos(theta); | 
|---|
|  | 228 | double cphi=cos(phi); | 
|---|
|  | 229 | double spsi=sin(psi); | 
|---|
|  | 230 | double stheta=sin(theta); | 
|---|
|  | 231 | double sphi=sin(phi); | 
|---|
|  | 232 | double xnew=(cpsi*cphi-ctheta*sphi*spsi)*_x | 
|---|
|  | 233 | +(cpsi*sphi+ctheta*cphi*spsi)*_y | 
|---|
|  | 234 | +spsi*stheta*_z; | 
|---|
|  | 235 | double ynew=(-spsi*cphi-ctheta*sphi*cpsi)*_x | 
|---|
|  | 236 | +(-spsi*sphi+ctheta*cphi*cpsi)*_y | 
|---|
|  | 237 | +cpsi*stheta*_z; | 
|---|
|  | 238 | double znew=stheta*sphi*_x-stheta*cphi*_y+ctheta*_z; | 
|---|
|  | 239 | return Vector3d(xnew,ynew,znew); | 
|---|
|  | 240 | } | 
|---|
|  | 241 | //++ | 
|---|
|  | 242 | Vector3d Vector3d::InvEuler(double phi, double theta, double psi) const | 
|---|
|  | 243 | // | 
|---|
|  | 244 | //    inverse rotation | 
|---|
|  | 245 | //-- | 
|---|
|  | 246 | { | 
|---|
|  | 247 | double cpsi=cos(psi); | 
|---|
|  | 248 | double ctheta=cos(theta); | 
|---|
|  | 249 | double cphi=cos(phi); | 
|---|
|  | 250 | double spsi=sin(psi); | 
|---|
|  | 251 | double stheta=sin(theta); | 
|---|
|  | 252 | double sphi=sin(phi); | 
|---|
|  | 253 | double xnew=(cpsi*cphi-ctheta*sphi*spsi)*_x | 
|---|
|  | 254 | -(spsi*cphi+ctheta*sphi*cpsi)*_y | 
|---|
|  | 255 | +sphi*stheta*_z; | 
|---|
|  | 256 | double ynew=(cpsi*sphi+ctheta*cphi*spsi)*_x | 
|---|
|  | 257 | +(-spsi*sphi+ctheta*cphi*cpsi)*_y | 
|---|
|  | 258 | -cphi*stheta*_z; | 
|---|
|  | 259 | double znew=stheta*spsi*_x+stheta*cpsi*_y+ctheta*_z; | 
|---|
|  | 260 | return Vector3d(xnew,ynew,znew); | 
|---|
|  | 261 | } | 
|---|
|  | 262 | //++ | 
|---|
| [792] | 263 | Vector3d Vector3d::Rotate(const Vector3d& omega, double phi) const | 
|---|
| [764] | 264 | // | 
|---|
|  | 265 | //    rotation of angle phi around an axis omega (Maxwell's rule) | 
|---|
|  | 266 | //-- | 
|---|
|  | 267 | { | 
|---|
|  | 268 | Vector3d rotationaxis=omega; | 
|---|
|  | 269 | rotationaxis.Normalize(); | 
|---|
|  | 270 | double n=this->Psc(rotationaxis); | 
|---|
|  | 271 | Vector3d myself=*this; | 
|---|
|  | 272 | Vector3d rotate=n*rotationaxis+(myself-n*rotationaxis)*cos(phi)-(myself^rotationaxis)*sin(phi); | 
|---|
|  | 273 | return rotate; | 
|---|
|  | 274 | } | 
|---|
|  | 275 | //++ | 
|---|
|  | 276 | void Vector3d::Print(ostream& os) const | 
|---|
|  | 277 | // | 
|---|
|  | 278 | //-- | 
|---|
|  | 279 | { | 
|---|
|  | 280 | os << "Vector3: (X,Y,Z)= (" << _x << ", " << _y << ", " << _z | 
|---|
|  | 281 | << ") --- Theta,Phi= " << _theta << ", " << _phi << "\n" | 
|---|
|  | 282 | << "Norme = " << this->Norm() << endl; | 
|---|
|  | 283 | } | 
|---|
|  | 284 | //++ | 
|---|
|  | 285 | // Titre        Operators | 
|---|
|  | 286 | //-- | 
|---|
|  | 287 | //++ | 
|---|
|  | 288 | Vector3d& Vector3d::operator += (const Vector3d& v) | 
|---|
|  | 289 | // | 
|---|
|  | 290 | //-- | 
|---|
|  | 291 | { | 
|---|
|  | 292 | *this=*this+v; | 
|---|
|  | 293 | return *this; | 
|---|
|  | 294 | } | 
|---|
|  | 295 | //++ | 
|---|
|  | 296 | Vector3d& Vector3d::operator -= (const Vector3d& v) | 
|---|
|  | 297 | // | 
|---|
|  | 298 | //-- | 
|---|
|  | 299 | { | 
|---|
|  | 300 | *this=*this-v; | 
|---|
|  | 301 | return *this; | 
|---|
|  | 302 | } | 
|---|
|  | 303 | //++ | 
|---|
|  | 304 | Vector3d& Vector3d::operator += (double d) | 
|---|
|  | 305 | // | 
|---|
|  | 306 | //-- | 
|---|
|  | 307 | { | 
|---|
|  | 308 | Setxyz(_x+d,_y+d,_z+d); | 
|---|
|  | 309 | return *this; | 
|---|
|  | 310 | } | 
|---|
|  | 311 | //++ | 
|---|
|  | 312 | Vector3d& Vector3d::operator /= (double d) | 
|---|
|  | 313 | // | 
|---|
|  | 314 | //-- | 
|---|
|  | 315 | { | 
|---|
|  | 316 | if( d != 0. ) Setxyz(_x/d,_y/d,_z/d); | 
|---|
|  | 317 | else cerr << "Division par zero." << endl; | 
|---|
|  | 318 | return *this; | 
|---|
|  | 319 | } | 
|---|
|  | 320 | //++ | 
|---|
|  | 321 | Vector3d& Vector3d::operator *= (double d) | 
|---|
|  | 322 | // | 
|---|
|  | 323 | //-- | 
|---|
|  | 324 | { | 
|---|
|  | 325 | Setxyz(_x*d,_y*d,_z*d); | 
|---|
|  | 326 | return *this; | 
|---|
|  | 327 | } | 
|---|
|  | 328 | //++ | 
|---|
|  | 329 | Vector3d Vector3d::operator ^ (const Vector3d& v) const | 
|---|
|  | 330 | // | 
|---|
|  | 331 | //    vector product | 
|---|
|  | 332 | //-- | 
|---|
|  | 333 | { | 
|---|
|  | 334 | return this->Vect(v); | 
|---|
|  | 335 | } | 
|---|
|  | 336 | //++ | 
|---|
|  | 337 | Vector3d Vector3d::operator + (double d) const | 
|---|
|  | 338 | // | 
|---|
|  | 339 | //-- | 
|---|
|  | 340 | { | 
|---|
|  | 341 | return Vector3d(_x+d,_y+d,_z+d); | 
|---|
|  | 342 | } | 
|---|
|  | 343 | //++ | 
|---|
|  | 344 | Vector3d Vector3d::operator + (const Vector3d& v) const | 
|---|
|  | 345 | // | 
|---|
|  | 346 | //-- | 
|---|
|  | 347 | { | 
|---|
|  | 348 | return Vector3d(_x+v._x,_y+v._y,_z+v._z); | 
|---|
|  | 349 | } | 
|---|
|  | 350 | //++ | 
|---|
|  | 351 | Vector3d Vector3d::operator - (double d) const | 
|---|
|  | 352 | // | 
|---|
|  | 353 | //-- | 
|---|
|  | 354 | { | 
|---|
|  | 355 | return *this+(-d); | 
|---|
|  | 356 | } | 
|---|
|  | 357 | //++ | 
|---|
|  | 358 | Vector3d Vector3d::operator - (const Vector3d& v) const | 
|---|
|  | 359 | // | 
|---|
|  | 360 | //-- | 
|---|
|  | 361 | { | 
|---|
|  | 362 | return *this+(v*(-1.)); | 
|---|
|  | 363 | } | 
|---|
|  | 364 | //++ | 
|---|
|  | 365 | Vector3d Vector3d::operator * (double d) const | 
|---|
|  | 366 | // | 
|---|
|  | 367 | //-- | 
|---|
|  | 368 | { | 
|---|
|  | 369 | return Vector3d(d*_x,d*_y,d*_z); | 
|---|
|  | 370 | } | 
|---|
|  | 371 | //++ | 
|---|
|  | 372 | double Vector3d::operator * (const Vector3d& v) const | 
|---|
|  | 373 | // | 
|---|
|  | 374 | //    dot product | 
|---|
|  | 375 | //-- | 
|---|
|  | 376 | { | 
|---|
|  | 377 | return this->Psc(v); | 
|---|
|  | 378 | } | 
|---|
|  | 379 | //++ | 
|---|
|  | 380 | Vector3d Vector3d::operator / (double d) const | 
|---|
|  | 381 | // | 
|---|
|  | 382 | //-- | 
|---|
|  | 383 | { | 
|---|
|  | 384 | Vector3d ret=*this; | 
|---|
|  | 385 | if( d != 0. ) ret/=d; | 
|---|
|  | 386 | else  cerr << "Division par zero." << endl; | 
|---|
|  | 387 | return ret; | 
|---|
|  | 388 | } | 
|---|
|  | 389 | //++ | 
|---|
|  | 390 | Vector3d& Vector3d::operator = (const Vector3d& v) | 
|---|
|  | 391 | // | 
|---|
|  | 392 | //-- | 
|---|
|  | 393 | { | 
|---|
|  | 394 | if( this != &v ) | 
|---|
|  | 395 | { | 
|---|
|  | 396 | _x=v._x; | 
|---|
|  | 397 | _y=v._y; | 
|---|
|  | 398 | _z=v._z; | 
|---|
|  | 399 | _theta=v._theta; | 
|---|
|  | 400 | _phi=v._phi; | 
|---|
|  | 401 | } | 
|---|
|  | 402 | return *this; | 
|---|
|  | 403 | } | 
|---|
|  | 404 | //++ | 
|---|
|  | 405 | bool Vector3d::operator == (const Vector3d& v) | 
|---|
|  | 406 | // | 
|---|
|  | 407 | //-- | 
|---|
|  | 408 | { | 
|---|
|  | 409 | return (this==&v); | 
|---|
|  | 410 | } | 
|---|
|  | 411 |  | 
|---|