vec3d.hpp
Go to the documentation of this file.
00001 00005 /* Copyright (c) 2005-2011 Taneli Kalvas. All rights reserved. 00006 * 00007 * You can redistribute this software and/or modify it under the terms 00008 * of the GNU General Public License as published by the Free Software 00009 * Foundation; either version 2 of the License, or (at your option) 00010 * any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this library (file "COPYING" included in the package); 00019 * if not, write to the Free Software Foundation, Inc., 51 Franklin 00020 * Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 * 00022 * If you have questions about your rights to use or distribute this 00023 * software, please contact Berkeley Lab's Technology Transfer 00024 * Department at TTD@lbl.gov. Other questions, comments and bug 00025 * reports should be sent directly to the author via email at 00026 * taneli.kalvas@jyu.fi. 00027 * 00028 * NOTICE. This software was developed under partial funding from the 00029 * U.S. Department of Energy. As such, the U.S. Government has been 00030 * granted for itself and others acting on its behalf a paid-up, 00031 * nonexclusive, irrevocable, worldwide license in the Software to 00032 * reproduce, prepare derivative works, and perform publicly and 00033 * display publicly. Beginning five (5) years after the date 00034 * permission to assert copyright is obtained from the U.S. Department 00035 * of Energy, and subject to any subsequent five (5) year renewals, 00036 * the U.S. Government is granted for itself and others acting on its 00037 * behalf a paid-up, nonexclusive, irrevocable, worldwide license in 00038 * the Software to reproduce, prepare derivative works, distribute 00039 * copies to the public, perform publicly and display publicly, and to 00040 * permit others to do so. 00041 */ 00042 00043 #ifndef VEC3D_HPP 00044 #define VEC3D_HPP 1 00045 00046 00047 #include <math.h> 00048 #include <stdint.h> 00049 #include <iostream> 00050 #include <iostream> 00051 #include <iomanip> 00052 #include "vec4d.hpp" 00053 #include "file.hpp" 00054 00055 00058 class Vec3D { 00059 00060 double p[3]; 00061 00062 public: 00063 00064 Vec3D() { p[0] = 0.0; p[1] = 0.0; p[2] = 0.0; } 00065 Vec3D( double x ) { p[0] = x; p[1] = 0.0; p[2] = 0.0; } 00066 Vec3D( double x, double y ) { p[0] = x; p[1] = y; p[2] = 0.0; } 00067 Vec3D( double x, double y, double z ) { p[0] = x; p[1] = y; p[2] = z; } 00068 00069 Vec3D( const class Vec4D &vec ); 00070 00071 Vec3D( std::istream &s ) { 00072 p[0] = read_double( s ); 00073 p[1] = read_double( s ); 00074 p[2] = read_double( s ); 00075 } 00076 ~Vec3D() {} 00077 00078 double &operator[]( int i ) { return( p[i] ); } 00079 const double &operator[]( int i ) const { return( p[i] ); } 00080 double &operator()( int i ) { return( p[i] ); } 00081 const double &operator()( int i ) const { return( p[i] ); } 00082 00085 Vec3D operator+( const Vec3D &vec ) const { 00086 return( Vec3D( p[0] + vec[0], 00087 p[1] + vec[1], 00088 p[2] + vec[2] ) ); 00089 } 00090 00093 Vec3D operator-( const Vec3D &vec ) const { 00094 return( Vec3D( p[0] - vec[0], 00095 p[1] - vec[1], 00096 p[2] - vec[2] ) ); 00097 } 00098 00101 Vec3D &operator+=( const Vec3D &vec ) { 00102 p[0] += vec[0]; 00103 p[1] += vec[1]; 00104 p[2] += vec[2]; 00105 return( *this ); 00106 } 00107 00110 double operator*( const Vec3D &vec ) const { 00111 return( p[0] * vec[0] + 00112 p[1] * vec[1] + 00113 p[2] * vec[2] ); 00114 } 00115 00118 Vec3D operator*( double x ) const { 00119 return( Vec3D( x*p[0], x*p[1], x*p[2] ) ); 00120 } 00121 00124 Vec3D operator-( void ) const { 00125 return( Vec3D( -p[0], -p[1], -p[2] ) ); 00126 } 00127 00130 Vec3D &operator*=( double x ) { 00131 p[0] *= x; 00132 p[1] *= x; 00133 p[2] *= x; 00134 return( *this ); 00135 } 00136 00139 Vec3D &operator/=( double x ) { 00140 double div = 1.0/x; 00141 p[0] *= div; 00142 p[1] *= div; 00143 p[2] *= div; 00144 return( *this ); 00145 } 00146 00151 bool operator!=( const Vec3D &x ) const; 00152 00157 bool operator==( const Vec3D &x ) const; 00158 00165 bool approx( const Vec3D &x, double eps = 1.0e-6 ) const; 00166 00169 Vec3D &operator=( const Vec3D &x ) { 00170 p[0] = x[0]; 00171 p[1] = x[1]; 00172 p[2] = x[2]; 00173 return( *this ); 00174 } 00175 00178 Vec3D &operator=( const double &x ) { 00179 p[0] = x; 00180 p[1] = x; 00181 p[2] = x; 00182 return( *this ); 00183 } 00184 00187 void abs( void ) { 00188 p[0] = fabs( p[0] ); 00189 p[1] = fabs( p[1] ); 00190 p[2] = fabs( p[2] ); 00191 } 00192 00195 void normalize( void ) { 00196 double inv_norm = 1.0/sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ); 00197 p[0] *= inv_norm; 00198 p[1] *= inv_norm; 00199 p[2] *= inv_norm; 00200 } 00201 00206 double norm2( void ) const { 00207 return( sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ) ); 00208 } 00209 00214 double ssqr( void ) const { 00215 return( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ); 00216 } 00217 00220 int min_element( void ) const; 00221 00224 Vec3D arb_perpendicular( void ) const; 00225 00228 void save( std::ostream &os ) const { 00229 write_double( os, p[0] ); 00230 write_double( os, p[1] ); 00231 write_double( os, p[2] ); 00232 } 00233 00236 static Vec3D standard_basis( int i ); 00237 00240 friend Vec3D cross( const Vec3D &vec1, const Vec3D &vec2 ); 00241 00244 friend double norm2( const Vec3D &vec ); 00245 00248 friend Vec3D operator*( double x, const Vec3D &vec ); 00249 00252 friend Vec3D operator*( double x, const class Int3D &i ); 00253 00256 friend std::ostream &operator<<( std::ostream &os, const Vec3D &vec ); 00257 }; 00258 00259 00260 inline double norm2( const Vec3D &vec ) { 00261 return( vec.norm2() ); 00262 } 00263 00264 00265 inline Vec3D cross( const Vec3D &vec1, const Vec3D &vec2 ) { 00266 return( Vec3D( vec1[1] * vec2[2] - vec1[2] * vec2[1], 00267 vec1[2] * vec2[0] - vec1[0] * vec2[2], 00268 vec1[0] * vec2[1] - vec1[1] * vec2[0] ) ); 00269 } 00270 00271 00272 inline Vec3D operator*( double x, const Vec3D &vec ) 00273 { 00274 return( Vec3D( x*vec.p[0], x*vec.p[1], x*vec.p[2] ) ); 00275 } 00276 00277 00278 inline std::ostream &operator<<( std::ostream &os, const Vec3D &vec ) 00279 { 00280 os << std::setw(12) << to_string(vec[0]).substr(0,12) << " "; 00281 os << std::setw(12) << to_string(vec[1]).substr(0,12) << " "; 00282 os << std::setw(12) << to_string(vec[2]).substr(0,12); 00283 return( os ); 00284 } 00285 00286 00289 class Int3D { 00290 int32_t l[3]; 00291 00292 public: 00293 00294 Int3D() { l[0] = 0; l[1] = 0; l[2] = 0; } 00295 Int3D( int32_t i ) { l[0] = i; l[1] = 0; l[2] = 0; } 00296 Int3D( int32_t i, int32_t j ) { l[0] = i; l[1] = j; l[2] = 0; } 00297 Int3D( int32_t i, int32_t j, int32_t k ) { l[0] = i; l[1] = j; l[2] = k; } 00298 Int3D( std::istream &s ) { 00299 l[0] = read_int32( s ); 00300 l[1] = read_int32( s ); 00301 l[2] = read_int32( s ); 00302 } 00303 ~Int3D() {} 00304 00305 int32_t &operator[]( int i ) { return( l[i] ); } 00306 const int32_t &operator[]( int i ) const { return( l[i] ); } 00307 int32_t &operator()( int i ) { return( l[i] ); } 00308 const int32_t &operator()( int i ) const { return( l[i] ); } 00309 00310 Int3D operator-( const Int3D &i ) { 00311 return( Int3D( l[0] - i[0], 00312 l[1] - i[1], 00313 l[2] - i[2] ) ); 00314 } 00315 00316 Vec3D operator*( double x ) { 00317 return( Vec3D( x*l[0], x*l[1], x*l[2] ) ); 00318 } 00319 00322 bool operator!=( const Int3D &i ) const { 00323 if( l[0] != i.l[0] || l[1] != i.l[1] || l[2] != i.l[2] ) 00324 return( true ); 00325 return( false ); 00326 } 00327 00330 bool operator==( const Int3D &i ) const { 00331 if( l[0] == i.l[0] && l[1] == i.l[1] && l[2] == i.l[2] ) 00332 return( true ); 00333 return( false ); 00334 } 00335 00336 void save( std::ostream &s ) const { 00337 write_int32( s, l[0] ); 00338 write_int32( s, l[1] ); 00339 write_int32( s, l[2] ); 00340 } 00341 00342 friend Vec3D operator*( double x, const Int3D &i ); 00343 friend std::ostream &operator<<( std::ostream &os, const Vec3D &vec ); 00344 }; 00345 00346 00347 inline Vec3D operator*( double x, const Int3D &i ) 00348 { 00349 Vec3D res; 00350 res[0] = x*i.l[0]; 00351 res[1] = x*i.l[1]; 00352 res[2] = x*i.l[2]; 00353 return( res ); 00354 } 00355 00356 00357 inline std::ostream &operator<<( std::ostream &os, const Int3D &vec ) 00358 { 00359 os << std::setw(12) << to_string(vec[0]).substr(0,12) << " "; 00360 os << std::setw(12) << to_string(vec[1]).substr(0,12) << " "; 00361 os << std::setw(12) << to_string(vec[2]).substr(0,12); 00362 return( os ); 00363 } 00364 00365 00366 #endif 00367