1 /*! 2 * \file src/NUMODIS/Vect3.cxx 3 * \brief 4 * \author Laurent Dupuy 5 * \date 9/06/2017 6 * \copyright Copyright (C) 2006-2018 CEA/DEN, EDF R&D. All rights 7 * reserved. 8 * This project is publicly released under either the GNU GPL Licence 9 * or the CECILL-A licence. A copy of thoses licences are delivered 10 * with the sources of TFEL. CEA or EDF may also distribute this 11 * project under specific licensing conditions. 12 */ 13 14 #include <cmath> 15 #include <limits> 16 #include <ostream> 17 #include <stdexcept> 18 19 #include "TFEL/Raise.hxx" 20 #include "NUMODIS/Vect3.hxx" 21 22 namespace numodis 23 { 24 25 //=============================================================== 26 // Vect3::Vect3 27 //--------------------------------------------------------------- 28 //! Constructor 29 //--------------------------------------------------------------- 30 /*! Note: the vector is set to 0 */ 31 //=============================================================== Vect3()32 Vect3::Vect3() 33 { 34 this->_vector[0]=0.0; 35 this->_vector[1]=0.0; 36 this->_vector[2]=0.0; 37 } 38 39 //=============================================================== 40 // Vect3::Vect3 41 //--------------------------------------------------------------- 42 //! Constructor 43 //--------------------------------------------------------------- 44 /*! 45 \param x x 46 \param y y 47 \param z z 48 */ 49 //=============================================================== Vect3(double x,double y,double z)50 Vect3::Vect3(double x, 51 double y, 52 double z) 53 { 54 this->_vector[0]=x; 55 this->_vector[1]=y; 56 this->_vector[2]=z; 57 } 58 59 //=============================================================== 60 // Vect3::Vect3 61 //--------------------------------------------------------------- 62 //! Copy constructor 63 //--------------------------------------------------------------- 64 /*! \param vec vector to be copied */ 65 //=============================================================== Vect3(const Vect3 & vec)66 Vect3::Vect3(const Vect3& vec) 67 { 68 this->_vector[0]=vec._vector[0]; 69 this->_vector[1]=vec._vector[1]; 70 this->_vector[2]=vec._vector[2]; 71 } 72 73 //=============================================================== 74 // Vect3::Vect3 75 //--------------------------------------------------------------- 76 //! Copy operator 77 //--------------------------------------------------------------- 78 /*! 79 \param vec vector to be copied 80 \return reference to the current vector 81 */ 82 //=============================================================== operator =(const Vect3 & vec)83 Vect3& Vect3::operator= (const Vect3& vec) 84 { 85 this->_vector[0]=vec._vector[0]; 86 this->_vector[1]=vec._vector[1]; 87 this->_vector[2]=vec._vector[2]; 88 return *this; 89 } 90 91 //=============================================================== 92 // Vect3::Vect3 93 //--------------------------------------------------------------- 94 //! Comparison operator 95 //--------------------------------------------------------------- 96 /*! 97 \param vec vector to be compared to 98 \return true if equal, false otherwise 99 */ 100 //=============================================================== operator ==(const Vect3 & vec)101 bool Vect3::operator== (const Vect3& vec) 102 { 103 return(this->_vector[0]==vec._vector[0] && 104 this->_vector[1]==vec._vector[1] && 105 this->_vector[2]==vec._vector[2]); 106 } 107 108 //=============================================================== 109 // Vect3::Vect3 110 //--------------------------------------------------------------- 111 //! Assignment operator 112 //--------------------------------------------------------------- 113 /*! 114 \param scalar initial value for all the terms of the vector 115 \return reference to the current vector 116 */ 117 //=============================================================== operator =(const double scalar)118 Vect3& Vect3::operator= (const double scalar) 119 { 120 this->_vector[0]=scalar; 121 this->_vector[1]=scalar; 122 this->_vector[2]=scalar; 123 return *this; 124 } 125 126 //=============================================================== 127 // Vect3::Cross 128 //--------------------------------------------------------------- 129 //! Return the cross product of two vectors W = U x V 130 //--------------------------------------------------------------- 131 /*! 132 Note : This object is the first vector U 133 \param V second vector 134 \return Cross-product W = U x V 135 */ 136 //=============================================================== Cross(const Vect3 & vec) const137 Vect3 Vect3::Cross(const Vect3& vec) const 138 { 139 return Vect3(this->_vector[1]*vec._vector[2]-this->_vector[2]*vec._vector[1], 140 this->_vector[2]*vec._vector[0]-this->_vector[0]*vec._vector[2], 141 this->_vector[0]*vec._vector[1]-this->_vector[1]*vec._vector[0]); 142 } 143 144 //=============================================================== 145 // Vect3::UnitCross 146 //--------------------------------------------------------------- 147 //! Cross product of two vectors W = U x V and normalization 148 //--------------------------------------------------------------- 149 /*! 150 Note : This object is the first vector U 151 \param V second vector 152 \return Cross-product W = U x V as a unit vector 153 */ 154 //=============================================================== UnitCross(const Vect3 & vec) const155 Vect3 Vect3::UnitCross(const Vect3& vec) const 156 { 157 Vect3 cross=this->Cross(vec); 158 cross.Normalize(); 159 return cross; 160 } 161 162 //=============================================================== 163 // Vect3::operator* 164 //--------------------------------------------------------------- 165 //! Multiplies a vector by a scalar 166 //--------------------------------------------------------------- 167 /*! 168 \param scalar 169 \return new output vector 170 */ 171 //=============================================================== operator *(const double scalar) const172 Vect3 Vect3::operator* (const double scalar) const 173 { 174 return Vect3(this->_vector[0]*scalar,this->_vector[1]*scalar,this->_vector[2]*scalar); 175 } 176 177 //=============================================================== 178 // Vect3::operator -- operator -(const double scalar) const179 Vect3 Vect3::operator- (const double scalar) const 180 { 181 return Vect3(this->_vector[0]-scalar,this->_vector[1]-scalar,this->_vector[2]-scalar); 182 } 183 operator *(const double scalar,const Vect3 & vec)184 Vect3 operator* (const double scalar, 185 const Vect3& vec) 186 { 187 return Vect3(vec[0]*scalar,vec[1]*scalar,vec[2]*scalar); 188 } 189 operator -(const double scalar,const Vect3 & vec)190 Vect3 operator- (const double scalar, 191 const Vect3& vec) 192 { 193 return Vect3(vec[0]-scalar,vec[1]-scalar,vec[2]-scalar); 194 } 195 operator +(const Vect3 & vec) const196 Vect3 Vect3::operator+ (const Vect3& vec)const 197 { 198 return Vect3(this->_vector[0]+vec[0],this->_vector[1]+vec[1],this->_vector[2]+vec[2]); 199 } 200 operator -(const Vect3 & vec) const201 Vect3 Vect3::operator- (const Vect3& vec)const 202 { 203 return Vect3(this->_vector[0]-vec[0],this->_vector[1]-vec[1],this->_vector[2]-vec[2]); 204 } 205 operator /(double scalar) const206 Vect3 Vect3::operator/ (double scalar) const 207 { 208 return Vect3(this->_vector[0]/scalar,this->_vector[1]/scalar,this->_vector[2]/scalar); 209 } 210 operator *=(const double scalar)211 Vect3& Vect3::operator*= (const double scalar) 212 { 213 this->_vector[0]*=scalar; 214 this->_vector[1]*=scalar; 215 this->_vector[2]*=scalar; 216 return *this; 217 } 218 operator /=(const double scalar)219 Vect3& Vect3::operator/= (const double scalar) 220 { 221 this->_vector[0]/=scalar; 222 this->_vector[1]/=scalar; 223 this->_vector[2]/=scalar; 224 return *this; 225 } 226 operator +=(const Vect3 & vec)227 Vect3& Vect3::operator+= (const Vect3& vec) 228 { 229 this->_vector[0]+=vec._vector[0]; 230 this->_vector[1]+=vec._vector[1]; 231 this->_vector[2]+=vec._vector[2]; 232 return *this; 233 } 234 operator +=(const double scalar)235 Vect3& Vect3::operator+= (const double scalar) 236 { 237 this->_vector[0]+=scalar; 238 this->_vector[1]+=scalar; 239 this->_vector[2]+=scalar; 240 return *this; 241 } 242 operator -=(const Vect3 & vec)243 Vect3& Vect3::operator-= (const Vect3& vec) 244 { 245 this->_vector[0]-=vec._vector[0]; 246 this->_vector[1]-=vec._vector[1]; 247 this->_vector[2]-=vec._vector[2]; 248 return *this; 249 } 250 operator -() const251 Vect3 Vect3::operator- () const 252 { 253 return Vect3(-this->_vector[0],-this->_vector[1],-this->_vector[2]); 254 } 255 operator [](int i) const256 double Vect3::operator[](int i) const 257 { 258 return this->_vector[i]; 259 } 260 operator [](int i)261 double& Vect3::operator[](int i) 262 { 263 return this->_vector[i]; 264 } 265 Dot(const Vect3 & vec) const266 double Vect3::Dot(const Vect3& vec) const 267 { 268 return (this->_vector[0]*vec._vector[0]+this->_vector[1]*vec._vector[1]+this->_vector[2]*vec._vector[2]); 269 } 270 Swap(Vect3 & vec)271 void Vect3::Swap(Vect3& vec) 272 { 273 std::swap(this->_vector[0],vec._vector[0]); 274 std::swap(this->_vector[1],vec._vector[1]); 275 std::swap(this->_vector[2],vec._vector[2]); 276 } 277 Length() const278 double Vect3::Length() const 279 { 280 return sqrt(this->_vector[0]*this->_vector[0]+this->_vector[1]*this->_vector[1]+this->_vector[2]*this->_vector[2]); 281 } 282 SquareLength() const283 double Vect3::SquareLength() const 284 { 285 return (this->_vector[0]*this->_vector[0]+this->_vector[1]*this->_vector[1]+this->_vector[2]*this->_vector[2]); 286 } 287 Normalize()288 bool Vect3::Normalize() 289 { 290 const auto length = Length(); 291 tfel::raise_if(length<=std::numeric_limits<double>::min(), 292 "Vect3::Normalize: " 293 "Trying to normalize null vector"); 294 *this/=length; 295 return true; 296 } 297 UnitVector() const298 Vect3 Vect3::UnitVector() const 299 { 300 double length = Length(); 301 302 return Vect3(this->_vector[0]/length,this->_vector[1]/length,this->_vector[2]/length); 303 304 } 305 306 //=============================================================== 307 // Vect3 : Operator << 308 //--------------------------------------------------------------- 309 //! Print the data of the vector in a stream 310 //--------------------------------------------------------------- 311 /*! 312 \param os a stream 313 \param vect3 vector to be displayed 314 */ 315 //=============================================================== operator <<(std::ostream & stream,const Vect3 & vect3)316 std::ostream& operator << (std::ostream& stream, 317 const Vect3& vect3) 318 { 319 stream << "[" << vect3._vector[0] << ", " << vect3._vector[1] << ", " << vect3._vector[2] << "]"; 320 return stream; 321 } 322 323 } // end of namespace numodis 324