Geant4 Cross Reference |
1 // -*- C++ -*- 1 // -*- C++ -*- >> 2 // $Id:$ 2 // ------------------------------------------- 3 // --------------------------------------------------------------------------- 3 // 4 // 4 // This file is a part of the CLHEP - a Class 5 // This file is a part of the CLHEP - a Class Library for High Energy Physics. 5 // 6 // 6 // History: 7 // History: 7 // 12.06.01 E.Chernyaev - CLHEP-1.7: initial 8 // 12.06.01 E.Chernyaev - CLHEP-1.7: initial version 8 // 14.03.03 E.Chernyaev - CLHEP-1.9: template 9 // 14.03.03 E.Chernyaev - CLHEP-1.9: template version 9 // 10 // 10 11 11 #ifndef BASIC_VECTOR3D_H 12 #ifndef BASIC_VECTOR3D_H 12 #define BASIC_VECTOR3D_H 13 #define BASIC_VECTOR3D_H 13 14 14 #include <iosfwd> 15 #include <iosfwd> 15 #include <type_traits> << 16 #include "CLHEP/Vector/ThreeVector.h" 16 #include "CLHEP/Vector/ThreeVector.h" 17 17 18 namespace HepGeom { 18 namespace HepGeom { 19 /** 19 /** 20 * Base class for Point3D<T>, Vector3D<T> an 20 * Base class for Point3D<T>, Vector3D<T> and Normal3D<T>. 21 * It defines only common functionality for 21 * It defines only common functionality for those classes and 22 * should not be used as separate class. 22 * should not be used as separate class. 23 * 23 * 24 * @author Evgeni Chernyaev <Evgueni.Tcherni 24 * @author Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch> 25 * @ingroup geometry 25 * @ingroup geometry 26 */ 26 */ 27 template<class T> class BasicVector3D { 27 template<class T> class BasicVector3D { 28 protected: 28 protected: 29 T v_[3]; 29 T v_[3]; 30 30 31 /** 31 /** 32 * Default constructor. 32 * Default constructor. 33 * It is protected - this class should not 33 * It is protected - this class should not be instantiated directly. 34 */ 34 */ 35 BasicVector3D() { v_[0] = 0; v_[1] = 0; v_ 35 BasicVector3D() { v_[0] = 0; v_[1] = 0; v_[2] = 0; } 36 36 37 public: 37 public: 38 /** 38 /** 39 * Safe indexing of the coordinates when u 39 * Safe indexing of the coordinates when using with matrices, arrays, etc. 40 */ 40 */ 41 enum { 41 enum { 42 X = 0, /**< index for x- 42 X = 0, /**< index for x-component */ 43 Y = 1, /**< index for y- 43 Y = 1, /**< index for y-component */ 44 Z = 2, /**< index for z- 44 Z = 2, /**< index for z-component */ 45 NUM_COORDINATES = 3, /**< number of co 45 NUM_COORDINATES = 3, /**< number of components */ 46 SIZE = NUM_COORDINATES /**< number of co 46 SIZE = NUM_COORDINATES /**< number of components */ 47 }; 47 }; 48 48 49 /** 49 /** 50 * Constructor from three numbers. */ 50 * Constructor from three numbers. */ 51 BasicVector3D(T x1, T y1, T z1) { v_[0] = 51 BasicVector3D(T x1, T y1, T z1) { v_[0] = x1; v_[1] = y1; v_[2] = z1; } 52 52 53 /** 53 /** 54 * Copy constructor. */ << 54 * Copy constructor. 55 BasicVector3D(const BasicVector3D<T> &) = << 55 * Note: BasicVector3D<double> has constructors 56 << 56 * from BasicVector3D<double> (provided by compiler) and 57 /** << 57 * from BasicVector3D<float> (defined in this file); 58 * Constructor for BasicVector3D<double> f << 58 * BasicVector3D<float> has only the last one. 59 template<typename U = T, << 59 */ 60 typename = typename std::enable_i << 61 BasicVector3D(const BasicVector3D<float> & 60 BasicVector3D(const BasicVector3D<float> & v) { 62 v_[0] = v.x(); v_[1] = v.y(); v_[2] = v. 61 v_[0] = v.x(); v_[1] = v.y(); v_[2] = v.z(); 63 } 62 } 64 63 65 /** 64 /** 66 * Move constructor. */ << 67 BasicVector3D(BasicVector3D<T> &&) = defau << 68 << 69 /** << 70 * Destructor. */ 65 * Destructor. */ 71 virtual ~BasicVector3D() = default; << 66 virtual ~BasicVector3D() {} 72 67 73 // ------------------------- 68 // ------------------------- 74 // Interface to "good old C" 69 // Interface to "good old C" 75 // ------------------------- 70 // ------------------------- 76 71 77 /** 72 /** 78 * Conversion (cast) to ordinary array. */ 73 * Conversion (cast) to ordinary array. */ 79 operator T * () { return v_; } 74 operator T * () { return v_; } 80 75 81 /** 76 /** 82 * Conversion (cast) to ordinary const arr 77 * Conversion (cast) to ordinary const array. */ 83 operator const T * () const { return v_; } 78 operator const T * () const { return v_; } 84 79 85 /** 80 /** 86 * Conversion (cast) to CLHEP::Hep3Vector. 81 * Conversion (cast) to CLHEP::Hep3Vector. 87 * This operator is needed only for backwa 82 * This operator is needed only for backward compatibility and 88 * in principle should not exit. 83 * in principle should not exit. 89 */ << 84 */ 90 operator CLHEP::Hep3Vector () const { retu 85 operator CLHEP::Hep3Vector () const { return CLHEP::Hep3Vector(x(),y(),z()); } 91 86 92 // ----------------------------- 87 // ----------------------------- 93 // General arithmetic operations 88 // General arithmetic operations 94 // ----------------------------- 89 // ----------------------------- 95 90 96 /** 91 /** 97 * Assignment. */ 92 * Assignment. */ 98 BasicVector3D<T> & operator= (const BasicV << 93 BasicVector3D<T> & operator= (const BasicVector3D<T> & v) { 99 /** << 94 v_[0] = v.v_[0]; v_[1] = v.v_[1]; v_[2] = v.v_[2]; return *this; 100 * Move assignment. */ << 95 } 101 BasicVector3D<T> & operator= (BasicVector3 << 102 /** 96 /** 103 * Addition. */ 97 * Addition. */ 104 BasicVector3D<T> & operator+=(const BasicV 98 BasicVector3D<T> & operator+=(const BasicVector3D<T> & v) { 105 v_[0] += v.v_[0]; v_[1] += v.v_[1]; v_[2 99 v_[0] += v.v_[0]; v_[1] += v.v_[1]; v_[2] += v.v_[2]; return *this; 106 } 100 } 107 /** 101 /** 108 * Subtraction. */ 102 * Subtraction. */ 109 BasicVector3D<T> & operator-=(const BasicV 103 BasicVector3D<T> & operator-=(const BasicVector3D<T> & v) { 110 v_[0] -= v.v_[0]; v_[1] -= v.v_[1]; v_[2 104 v_[0] -= v.v_[0]; v_[1] -= v.v_[1]; v_[2] -= v.v_[2]; return *this; 111 } 105 } 112 /** 106 /** 113 * Multiplication by scalar. */ 107 * Multiplication by scalar. */ 114 BasicVector3D<T> & operator*=(double a) { 108 BasicVector3D<T> & operator*=(double a) { 115 v_[0] *= a; v_[1] *= a; v_[2] *= a; retu 109 v_[0] *= a; v_[1] *= a; v_[2] *= a; return *this; 116 } 110 } 117 /** 111 /** 118 * Division by scalar. */ 112 * Division by scalar. */ 119 BasicVector3D<T> & operator/=(double a) { 113 BasicVector3D<T> & operator/=(double a) { 120 v_[0] /= a; v_[1] /= a; v_[2] /= a; retu 114 v_[0] /= a; v_[1] /= a; v_[2] /= a; return *this; 121 } 115 } 122 116 123 // ------------ 117 // ------------ 124 // Subscripting 118 // Subscripting 125 // ------------ 119 // ------------ 126 120 127 /** 121 /** 128 * Gets components by index. */ 122 * Gets components by index. */ 129 T operator()(int i) const { return v_[i]; 123 T operator()(int i) const { return v_[i]; } 130 /** 124 /** 131 * Gets components by index. */ 125 * Gets components by index. */ 132 T operator[](int i) const { return v_[i]; 126 T operator[](int i) const { return v_[i]; } 133 << 127 134 /** 128 /** 135 * Sets components by index. */ 129 * Sets components by index. */ 136 T & operator()(int i) { return v_[i]; } 130 T & operator()(int i) { return v_[i]; } 137 /** 131 /** 138 * Sets components by index. */ 132 * Sets components by index. */ 139 T & operator[](int i) { return v_[i]; } 133 T & operator[](int i) { return v_[i]; } 140 << 134 141 // ------------------------------------ 135 // ------------------------------------ 142 // Cartesian coordinate system: x, y, z 136 // Cartesian coordinate system: x, y, z 143 // ------------------------------------ 137 // ------------------------------------ 144 138 145 /** 139 /** 146 * Gets x-component in cartesian coordinat << 140 * Gets x-component in cartesian coordinate system. */ 147 T x() const { return v_[0]; } 141 T x() const { return v_[0]; } 148 /** 142 /** 149 * Gets y-component in cartesian coordinat << 143 * Gets y-component in cartesian coordinate system. */ 150 T y() const { return v_[1]; } 144 T y() const { return v_[1]; } 151 /** 145 /** 152 * Gets z-component in cartesian coordinat << 146 * Gets z-component in cartesian coordinate system. */ 153 T z() const { return v_[2]; } 147 T z() const { return v_[2]; } 154 148 155 /** 149 /** 156 * Sets x-component in cartesian coordinat << 150 * Sets x-component in cartesian coordinate system. */ 157 void setX(T a) { v_[0] = a; } 151 void setX(T a) { v_[0] = a; } 158 /** 152 /** 159 * Sets y-component in cartesian coordinat << 153 * Sets y-component in cartesian coordinate system. */ 160 void setY(T a) { v_[1] = a; } 154 void setY(T a) { v_[1] = a; } 161 /** 155 /** 162 * Sets z-component in cartesian coordinat << 156 * Sets z-component in cartesian coordinate system. */ 163 void setZ(T a) { v_[2] = a; } 157 void setZ(T a) { v_[2] = a; } 164 158 165 /** 159 /** 166 * Sets components in cartesian coordinate 160 * Sets components in cartesian coordinate system. */ 167 void set(T x1, T y1, T z1) { v_[0] = x1; v 161 void set(T x1, T y1, T z1) { v_[0] = x1; v_[1] = y1; v_[2] = z1; } 168 162 169 // --------------------------------------- 163 // ------------------------------------------ 170 // Cylindrical coordinate system: rho, phi 164 // Cylindrical coordinate system: rho, phi, z 171 // --------------------------------------- 165 // ------------------------------------------ 172 166 173 /** 167 /** 174 * Gets transverse component squared. */ 168 * Gets transverse component squared. */ 175 T perp2() const { return x()*x()+y()*y(); 169 T perp2() const { return x()*x()+y()*y(); } 176 /** 170 /** 177 * Gets transverse component. */ 171 * Gets transverse component. */ 178 T perp() const { return std::sqrt(perp2()) 172 T perp() const { return std::sqrt(perp2()); } 179 /** 173 /** 180 * Gets rho-component in cylindrical coord 174 * Gets rho-component in cylindrical coordinate system */ 181 T rho() const { return perp(); } 175 T rho() const { return perp(); } 182 176 183 /** 177 /** 184 * Sets transverse component keeping phi a 178 * Sets transverse component keeping phi and z constant. */ 185 void setPerp(T rh) { 179 void setPerp(T rh) { 186 T factor = perp(); 180 T factor = perp(); 187 if (factor > 0) { 181 if (factor > 0) { 188 factor = rh/factor; v_[0] *= factor; v_[1] * 182 factor = rh/factor; v_[0] *= factor; v_[1] *= factor; 189 } << 183 } 190 } 184 } 191 185 192 // --------------------------------------- 186 // ------------------------------------------ 193 // Spherical coordinate system: r, phi, th 187 // Spherical coordinate system: r, phi, theta 194 // --------------------------------------- 188 // ------------------------------------------ 195 189 196 /** 190 /** 197 * Gets magnitude squared of the vector. * 191 * Gets magnitude squared of the vector. */ 198 T mag2() const { return x()*x()+y()*y()+z( 192 T mag2() const { return x()*x()+y()*y()+z()*z(); } 199 /** 193 /** 200 * Gets magnitude of the vector. */ 194 * Gets magnitude of the vector. */ 201 T mag() const { return std::sqrt(mag2()); 195 T mag() const { return std::sqrt(mag2()); } 202 /** 196 /** 203 * Gets r-component in spherical coordinat 197 * Gets r-component in spherical coordinate system */ 204 T r() const { return mag(); } 198 T r() const { return mag(); } 205 /** 199 /** 206 * Gets azimuth angle. */ 200 * Gets azimuth angle. */ 207 T phi() const { 201 T phi() const { 208 return x() == 0 && y() == 0 ? 0 : std::a 202 return x() == 0 && y() == 0 ? 0 : std::atan2(y(),x()); 209 } 203 } 210 /** 204 /** 211 * Gets polar angle. */ 205 * Gets polar angle. */ 212 T theta() const { 206 T theta() const { 213 return x() == 0 && y() == 0 && z() == 0 207 return x() == 0 && y() == 0 && z() == 0 ? 0 : std::atan2(perp(),z()); 214 } 208 } 215 /** 209 /** 216 * Gets cosine of polar angle. */ 210 * Gets cosine of polar angle. */ 217 T cosTheta() const { T ma = mag(); return 211 T cosTheta() const { T ma = mag(); return ma == 0 ? 1 : z()/ma; } 218 212 219 /** 213 /** 220 * Gets r-component in spherical coordinat 214 * Gets r-component in spherical coordinate system */ 221 T getR() const { return r(); } 215 T getR() const { return r(); } 222 /** << 216 /** 223 * Gets phi-component in spherical coordin 217 * Gets phi-component in spherical coordinate system */ 224 T getPhi() const { return phi(); } 218 T getPhi() const { return phi(); } 225 /** << 219 /** 226 * Gets theta-component in spherical coord 220 * Gets theta-component in spherical coordinate system */ 227 T getTheta() const { return theta(); } 221 T getTheta() const { return theta(); } 228 222 229 /** 223 /** 230 * Sets magnitude. */ 224 * Sets magnitude. */ 231 void setMag(T ma) { 225 void setMag(T ma) { 232 T factor = mag(); 226 T factor = mag(); 233 if (factor > 0) { 227 if (factor > 0) { 234 factor = ma/factor; v_[0] *= factor; v_[1] * 228 factor = ma/factor; v_[0] *= factor; v_[1] *= factor; v_[2] *= factor; 235 } << 229 } 236 } 230 } 237 /** 231 /** 238 * Sets r-component in spherical coordinat 232 * Sets r-component in spherical coordinate system. */ 239 void setR(T ma) { setMag(ma); } 233 void setR(T ma) { setMag(ma); } 240 /** 234 /** 241 * Sets phi-component in spherical coordin 235 * Sets phi-component in spherical coordinate system. */ 242 void setPhi(T ph) { T xy = perp(); setX(xy 236 void setPhi(T ph) { T xy = perp(); setX(xy*std::cos(ph)); setY(xy*std::sin(ph)); } 243 /** 237 /** 244 * Sets theta-component in spherical coord 238 * Sets theta-component in spherical coordinate system. */ 245 void setTheta(T th) { 239 void setTheta(T th) { 246 T ma = mag(); 240 T ma = mag(); 247 T ph = phi(); 241 T ph = phi(); 248 set(ma*std::sin(th)*std::cos(ph), ma*std 242 set(ma*std::sin(th)*std::cos(ph), ma*std::sin(th)*std::sin(ph), ma*std::cos(th)); 249 } 243 } 250 244 251 // --------------- 245 // --------------- 252 // Pseudo rapidity 246 // Pseudo rapidity 253 // --------------- 247 // --------------- 254 248 255 /** 249 /** 256 * Gets pseudo-rapidity: -ln(tan(theta/2)) 250 * Gets pseudo-rapidity: -ln(tan(theta/2)) */ 257 T pseudoRapidity() const; 251 T pseudoRapidity() const; 258 /** 252 /** 259 * Gets pseudo-rapidity. */ 253 * Gets pseudo-rapidity. */ 260 T eta() const { return pseudoRapidity(); } 254 T eta() const { return pseudoRapidity(); } 261 /** 255 /** 262 * Gets pseudo-rapidity. */ 256 * Gets pseudo-rapidity. */ 263 T getEta() const { return pseudoRapidity() 257 T getEta() const { return pseudoRapidity(); } 264 258 265 /** 259 /** 266 * Sets pseudo-rapidity, keeping magnitude 260 * Sets pseudo-rapidity, keeping magnitude and phi fixed. */ 267 void setEta(T a); 261 void setEta(T a); 268 262 269 // ------------------- 263 // ------------------- 270 // Combine two vectors 264 // Combine two vectors 271 // ------------------- 265 // ------------------- 272 266 273 /** 267 /** 274 * Scalar product. */ 268 * Scalar product. */ 275 T dot(const BasicVector3D<T> & v) const { 269 T dot(const BasicVector3D<T> & v) const { 276 return x()*v.x()+y()*v.y()+z()*v.z(); 270 return x()*v.x()+y()*v.y()+z()*v.z(); 277 } 271 } 278 272 279 /** 273 /** 280 * Vector product. */ 274 * Vector product. */ 281 BasicVector3D<T> cross(const BasicVector3D 275 BasicVector3D<T> cross(const BasicVector3D<T> & v) const { 282 return BasicVector3D<T>(y()*v.z()-v.y()* 276 return BasicVector3D<T>(y()*v.z()-v.y()*z(), 283 z()*v.x()-v.z()*x(), 277 z()*v.x()-v.z()*x(), 284 x()*v.y()-v.x()*y()); 278 x()*v.y()-v.x()*y()); 285 } 279 } 286 280 287 /** 281 /** 288 * Returns transverse component w.r.t. giv 282 * Returns transverse component w.r.t. given axis squared. */ 289 T perp2(const BasicVector3D<T> & v) const 283 T perp2(const BasicVector3D<T> & v) const { 290 T tot = v.mag2(), s = dot(v); 284 T tot = v.mag2(), s = dot(v); 291 return tot > 0 ? mag2()-s*s/tot : mag2() 285 return tot > 0 ? mag2()-s*s/tot : mag2(); 292 } 286 } 293 287 294 /** 288 /** 295 * Returns transverse component w.r.t. giv 289 * Returns transverse component w.r.t. given axis. */ 296 T perp(const BasicVector3D<T> & v) const { 290 T perp(const BasicVector3D<T> & v) const { 297 return std::sqrt(perp2(v)); 291 return std::sqrt(perp2(v)); 298 } 292 } 299 293 300 /** 294 /** 301 * Returns angle w.r.t. another vector. */ 295 * Returns angle w.r.t. another vector. */ 302 T angle(const BasicVector3D<T> & v) const; 296 T angle(const BasicVector3D<T> & v) const; 303 297 304 // --------------- 298 // --------------- 305 // Related vectors 299 // Related vectors 306 // --------------- 300 // --------------- 307 301 308 /** 302 /** 309 * Returns unit vector parallel to this. * << 303 * Returns unit vector parallel to this. */ 310 BasicVector3D<T> unit() const { 304 BasicVector3D<T> unit() const { 311 T len = mag(); 305 T len = mag(); 312 return (len > 0) ? 306 return (len > 0) ? 313 BasicVector3D<T>(x()/len, y()/len, z()/len) 307 BasicVector3D<T>(x()/len, y()/len, z()/len) : BasicVector3D<T>(); 314 } 308 } 315 309 316 /** 310 /** 317 * Returns orthogonal vector. */ << 311 * Returns orthogonal vector. */ 318 BasicVector3D<T> orthogonal() const { 312 BasicVector3D<T> orthogonal() const { 319 T dx = x() < 0 ? -x() : x(); 313 T dx = x() < 0 ? -x() : x(); 320 T dy = y() < 0 ? -y() : y(); 314 T dy = y() < 0 ? -y() : y(); 321 T dz = z() < 0 ? -z() : z(); 315 T dz = z() < 0 ? -z() : z(); 322 if (dx < dy) { 316 if (dx < dy) { 323 return dx < dz ? 317 return dx < dz ? 324 BasicVector3D<T>(0,z(),-y()) : BasicVector 318 BasicVector3D<T>(0,z(),-y()) : BasicVector3D<T>(y(),-x(),0); 325 }else{ 319 }else{ 326 return dy < dz ? 320 return dy < dz ? 327 BasicVector3D<T>(-z(),0,x()) : BasicVector 321 BasicVector3D<T>(-z(),0,x()) : BasicVector3D<T>(y(),-x(),0); 328 } 322 } 329 } 323 } 330 324 331 // --------- 325 // --------- 332 // Rotations 326 // Rotations 333 // --------- 327 // --------- 334 328 335 /** 329 /** 336 * Rotates around x-axis. */ 330 * Rotates around x-axis. */ 337 BasicVector3D<T> & rotateX(T a); 331 BasicVector3D<T> & rotateX(T a); 338 /** 332 /** 339 * Rotates around y-axis. */ 333 * Rotates around y-axis. */ 340 BasicVector3D<T> & rotateY(T a); 334 BasicVector3D<T> & rotateY(T a); 341 /** 335 /** 342 * Rotates around z-axis. */ 336 * Rotates around z-axis. */ 343 BasicVector3D<T> & rotateZ(T a); 337 BasicVector3D<T> & rotateZ(T a); 344 /** 338 /** 345 * Rotates around the axis specified by an 339 * Rotates around the axis specified by another vector. */ 346 BasicVector3D<T> & rotate(T a, const Basic 340 BasicVector3D<T> & rotate(T a, const BasicVector3D<T> & v); 347 }; 341 }; 348 342 349 /******************************************* 343 /************************************************************************* 350 * << 344 * * 351 * Non-member functions for BasicVector3D<fl 345 * Non-member functions for BasicVector3D<float> * 352 * << 346 * * 353 ******************************************* 347 *************************************************************************/ 354 348 355 /** 349 /** 356 * Output to stream. 350 * Output to stream. 357 * @relates BasicVector3D 351 * @relates BasicVector3D 358 */ 352 */ 359 std::ostream & 353 std::ostream & 360 operator<<(std::ostream &, const BasicVector 354 operator<<(std::ostream &, const BasicVector3D<float> &); 361 355 362 /** 356 /** 363 * Input from stream. 357 * Input from stream. 364 * @relates BasicVector3D 358 * @relates BasicVector3D 365 */ 359 */ 366 std::istream & 360 std::istream & 367 operator>>(std::istream &, BasicVector3D<flo 361 operator>>(std::istream &, BasicVector3D<float> &); 368 362 369 /** 363 /** 370 * Unary plus. 364 * Unary plus. 371 * @relates BasicVector3D 365 * @relates BasicVector3D 372 */ 366 */ 373 inline BasicVector3D<float> 367 inline BasicVector3D<float> 374 operator+(const BasicVector3D<float> & v) { 368 operator+(const BasicVector3D<float> & v) { return v; } 375 369 376 /** 370 /** 377 * Addition of two vectors. 371 * Addition of two vectors. 378 * @relates BasicVector3D 372 * @relates BasicVector3D 379 */ 373 */ 380 inline BasicVector3D<float> 374 inline BasicVector3D<float> 381 operator+(const BasicVector3D<float> & a, co 375 operator+(const BasicVector3D<float> & a, const BasicVector3D<float> & b) { 382 return BasicVector3D<float>(a.x()+b.x(), a 376 return BasicVector3D<float>(a.x()+b.x(), a.y()+b.y(), a.z()+b.z()); 383 } 377 } 384 378 385 /** 379 /** 386 * Unary minus. 380 * Unary minus. 387 * @relates BasicVector3D 381 * @relates BasicVector3D 388 */ 382 */ 389 inline BasicVector3D<float> 383 inline BasicVector3D<float> 390 operator-(const BasicVector3D<float> & v) { 384 operator-(const BasicVector3D<float> & v) { 391 return BasicVector3D<float>(-v.x(), -v.y() 385 return BasicVector3D<float>(-v.x(), -v.y(), -v.z()); 392 } 386 } 393 387 394 /** 388 /** 395 * Subtraction of two vectors. 389 * Subtraction of two vectors. 396 * @relates BasicVector3D 390 * @relates BasicVector3D 397 */ 391 */ 398 inline BasicVector3D<float> 392 inline BasicVector3D<float> 399 operator-(const BasicVector3D<float> & a, co 393 operator-(const BasicVector3D<float> & a, const BasicVector3D<float> & b) { 400 return BasicVector3D<float>(a.x()-b.x(), a 394 return BasicVector3D<float>(a.x()-b.x(), a.y()-b.y(), a.z()-b.z()); 401 } 395 } 402 396 403 /** 397 /** 404 * Multiplication vector by scalar. 398 * Multiplication vector by scalar. 405 * @relates BasicVector3D 399 * @relates BasicVector3D 406 */ 400 */ 407 inline BasicVector3D<float> 401 inline BasicVector3D<float> 408 operator*(const BasicVector3D<float> & v, do 402 operator*(const BasicVector3D<float> & v, double a) { 409 return BasicVector3D<float>(v.x()*static_c 403 return BasicVector3D<float>(v.x()*static_cast<float>(a), v.y()*static_cast<float>(a), v.z()*static_cast<float>(a)); 410 } 404 } 411 405 412 /** 406 /** 413 * Scalar product of two vectors. 407 * Scalar product of two vectors. 414 * @relates BasicVector3D 408 * @relates BasicVector3D 415 */ 409 */ 416 inline float 410 inline float 417 operator*(const BasicVector3D<float> & a, co 411 operator*(const BasicVector3D<float> & a, const BasicVector3D<float> & b) { 418 return a.dot(b); 412 return a.dot(b); 419 } 413 } 420 414 421 /** 415 /** 422 * Multiplication scalar by vector. 416 * Multiplication scalar by vector. 423 * @relates BasicVector3D 417 * @relates BasicVector3D 424 */ 418 */ 425 inline BasicVector3D<float> 419 inline BasicVector3D<float> 426 operator*(double a, const BasicVector3D<floa 420 operator*(double a, const BasicVector3D<float> & v) { 427 return BasicVector3D<float>(static_cast<fl 421 return BasicVector3D<float>(static_cast<float>(a)*v.x(), static_cast<float>(a)*v.y(), static_cast<float>(a)*v.z()); 428 } 422 } 429 423 430 /** 424 /** 431 * Division vector by scalar. 425 * Division vector by scalar. 432 * @relates BasicVector3D 426 * @relates BasicVector3D 433 */ 427 */ 434 inline BasicVector3D<float> 428 inline BasicVector3D<float> 435 operator/(const BasicVector3D<float> & v, do 429 operator/(const BasicVector3D<float> & v, double a) { 436 return BasicVector3D<float>(v.x()/static_c 430 return BasicVector3D<float>(v.x()/static_cast<float>(a), v.y()/static_cast<float>(a), v.z()/static_cast<float>(a)); 437 } 431 } 438 << 432 439 /** 433 /** 440 * Comparison of two vectors for equality. << 434 * Comparison of two vectors for equality. 441 * @relates BasicVector3D 435 * @relates BasicVector3D 442 */ 436 */ 443 inline bool 437 inline bool 444 operator==(const BasicVector3D<float> & a, c 438 operator==(const BasicVector3D<float> & a, const BasicVector3D<float> & b) { 445 return (a.x()==b.x() && a.y()==b.y() && a. 439 return (a.x()==b.x() && a.y()==b.y() && a.z()==b.z()); 446 } 440 } 447 441 448 /** 442 /** 449 * Comparison of two vectors for inequality. << 443 * Comparison of two vectors for inequality. 450 * @relates BasicVector3D 444 * @relates BasicVector3D 451 */ 445 */ 452 inline bool 446 inline bool 453 operator!=(const BasicVector3D<float> & a, c 447 operator!=(const BasicVector3D<float> & a, const BasicVector3D<float> & b) { 454 return (a.x()!=b.x() || a.y()!=b.y() || a. 448 return (a.x()!=b.x() || a.y()!=b.y() || a.z()!=b.z()); 455 } 449 } 456 450 457 /******************************************* 451 /************************************************************************* 458 * << 452 * * 459 * Non-member functions for BasicVector3D<do 453 * Non-member functions for BasicVector3D<double> * 460 * << 454 * * 461 ******************************************* 455 *************************************************************************/ 462 456 463 /** 457 /** 464 * Output to stream. 458 * Output to stream. 465 * @relates BasicVector3D 459 * @relates BasicVector3D 466 */ 460 */ 467 std::ostream & 461 std::ostream & 468 operator<<(std::ostream &, const BasicVector 462 operator<<(std::ostream &, const BasicVector3D<double> &); 469 463 470 /** 464 /** 471 * Input from stream. 465 * Input from stream. 472 * @relates BasicVector3D 466 * @relates BasicVector3D 473 */ 467 */ 474 std::istream & 468 std::istream & 475 operator>>(std::istream &, BasicVector3D<dou 469 operator>>(std::istream &, BasicVector3D<double> &); 476 470 477 /** 471 /** 478 * Unary plus. 472 * Unary plus. 479 * @relates BasicVector3D 473 * @relates BasicVector3D 480 */ 474 */ 481 inline BasicVector3D<double> 475 inline BasicVector3D<double> 482 operator+(const BasicVector3D<double> & v) { 476 operator+(const BasicVector3D<double> & v) { return v; } 483 477 484 /** 478 /** 485 * Addition of two vectors. 479 * Addition of two vectors. 486 * @relates BasicVector3D 480 * @relates BasicVector3D 487 */ 481 */ 488 inline BasicVector3D<double> 482 inline BasicVector3D<double> 489 operator+(const BasicVector3D<double> & a,co 483 operator+(const BasicVector3D<double> & a,const BasicVector3D<double> & b) { 490 return BasicVector3D<double>(a.x()+b.x(), 484 return BasicVector3D<double>(a.x()+b.x(), a.y()+b.y(), a.z()+b.z()); 491 } 485 } 492 486 493 /** 487 /** 494 * Unary minus. 488 * Unary minus. 495 * @relates BasicVector3D 489 * @relates BasicVector3D 496 */ 490 */ 497 inline BasicVector3D<double> 491 inline BasicVector3D<double> 498 operator-(const BasicVector3D<double> & v) { 492 operator-(const BasicVector3D<double> & v) { 499 return BasicVector3D<double>(-v.x(), -v.y( 493 return BasicVector3D<double>(-v.x(), -v.y(), -v.z()); 500 } 494 } 501 495 502 /** 496 /** 503 * Subtraction of two vectors. 497 * Subtraction of two vectors. 504 * @relates BasicVector3D 498 * @relates BasicVector3D 505 */ 499 */ 506 inline BasicVector3D<double> 500 inline BasicVector3D<double> 507 operator-(const BasicVector3D<double> & a,co 501 operator-(const BasicVector3D<double> & a,const BasicVector3D<double> & b) { 508 return BasicVector3D<double>(a.x()-b.x(), 502 return BasicVector3D<double>(a.x()-b.x(), a.y()-b.y(), a.z()-b.z()); 509 } 503 } 510 504 511 /** 505 /** 512 * Multiplication vector by scalar. 506 * Multiplication vector by scalar. 513 * @relates BasicVector3D 507 * @relates BasicVector3D 514 */ 508 */ 515 inline BasicVector3D<double> 509 inline BasicVector3D<double> 516 operator*(const BasicVector3D<double> & v, d 510 operator*(const BasicVector3D<double> & v, double a) { 517 return BasicVector3D<double>(v.x()*a, v.y( 511 return BasicVector3D<double>(v.x()*a, v.y()*a, v.z()*a); 518 } 512 } 519 513 520 /** 514 /** 521 * Scalar product of two vectors. 515 * Scalar product of two vectors. 522 * @relates BasicVector3D 516 * @relates BasicVector3D 523 */ 517 */ 524 inline double 518 inline double 525 operator*(const BasicVector3D<double> & a,co 519 operator*(const BasicVector3D<double> & a,const BasicVector3D<double> & b) { 526 return a.dot(b); 520 return a.dot(b); 527 } 521 } 528 522 529 /** 523 /** 530 * Multiplication scalar by vector. 524 * Multiplication scalar by vector. 531 * @relates BasicVector3D 525 * @relates BasicVector3D 532 */ 526 */ 533 inline BasicVector3D<double> 527 inline BasicVector3D<double> 534 operator*(double a, const BasicVector3D<doub 528 operator*(double a, const BasicVector3D<double> & v) { 535 return BasicVector3D<double>(a*v.x(), a*v. 529 return BasicVector3D<double>(a*v.x(), a*v.y(), a*v.z()); 536 } 530 } 537 531 538 /** 532 /** 539 * Division vector by scalar. 533 * Division vector by scalar. 540 * @relates BasicVector3D 534 * @relates BasicVector3D 541 */ 535 */ 542 inline BasicVector3D<double> 536 inline BasicVector3D<double> 543 operator/(const BasicVector3D<double> & v, d 537 operator/(const BasicVector3D<double> & v, double a) { 544 return BasicVector3D<double>(v.x()/a, v.y( 538 return BasicVector3D<double>(v.x()/a, v.y()/a, v.z()/a); 545 } 539 } 546 << 540 547 /** 541 /** 548 * Comparison of two vectors for equality. << 542 * Comparison of two vectors for equality. 549 * @relates BasicVector3D 543 * @relates BasicVector3D 550 */ 544 */ 551 inline bool 545 inline bool 552 operator==(const BasicVector3D<double> & a, 546 operator==(const BasicVector3D<double> & a, const BasicVector3D<double> & b) 553 { 547 { 554 return (a.x()==b.x() && a.y()==b.y() && a. 548 return (a.x()==b.x() && a.y()==b.y() && a.z()==b.z()); 555 } 549 } 556 550 557 /** 551 /** 558 * Comparison of two vectors for inequality. << 552 * Comparison of two vectors for inequality. 559 * @relates BasicVector3D 553 * @relates BasicVector3D 560 */ 554 */ 561 inline bool 555 inline bool 562 operator!=(const BasicVector3D<double> & a, 556 operator!=(const BasicVector3D<double> & a, const BasicVector3D<double> & b) 563 { 557 { 564 return (a.x()!=b.x() || a.y()!=b.y() || a. 558 return (a.x()!=b.x() || a.y()!=b.y() || a.z()!=b.z()); 565 } 559 } 566 } /* namespace HepGeom */ 560 } /* namespace HepGeom */ 567 561 568 #endif /* BASIC_VECTOR3D_H */ 562 #endif /* BASIC_VECTOR3D_H */ 569 563