IBSimu  1.0.4
vec3d.hpp
Go to the documentation of this file.
00001 
00005 /* Copyright (c) 2005-2010 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 
00149     bool operator!=( const Vec3D &x ) { 
00150         if( p[0] != x.p[0] || p[1] != x.p[1] || p[2] != x.p[2] )
00151             return( true );
00152         return( false ); 
00153     }
00154 
00157     bool operator==( const Vec3D &x ) { 
00158         if( p[0] == x.p[0] && p[1] == x.p[1] && p[2] == x.p[2] )
00159             return( true );
00160         return( false ); 
00161     }
00162 
00165     Vec3D &operator=( const Vec3D &x ) { 
00166         p[0] = x[0];
00167         p[1] = x[1];
00168         p[2] = x[2];
00169         return( *this );
00170     }
00171 
00174     Vec3D &operator=( const double &x ) { 
00175         p[0] = x;
00176         p[1] = x;
00177         p[2] = x;
00178         return( *this );
00179     }
00180 
00183     void normalize() {
00184         double inv_norm = 1.0/sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] );
00185         p[0] *= inv_norm;
00186         p[1] *= inv_norm;
00187         p[2] *= inv_norm;
00188     }
00189 
00194     double norm2() const {
00195         return( sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ) );
00196     }
00197 
00202     double ssqr() const {
00203         return( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] );
00204     }
00205 
00206     void save( std::ostream &s ) const { 
00207         write_double( s, p[0] );
00208         write_double( s, p[1] );
00209         write_double( s, p[2] ); 
00210     }
00211 
00214     friend Vec3D cross( const Vec3D &vec1, const Vec3D &vec2 );
00215 
00218     friend double norm2( const Vec3D &vec );
00219 
00222     friend Vec3D operator*( double x, const Vec3D &vec );
00223 
00226     friend Vec3D operator*( double x, const class Int3D &i );
00227 
00230     friend std::ostream &operator<<( std::ostream &os, const Vec3D &vec );
00231 };
00232 
00233 
00234 inline double norm2( const Vec3D &vec ) {
00235     return( vec.norm2() );
00236 }
00237 
00238 inline Vec3D cross( const Vec3D &vec1, const Vec3D &vec2 ) { 
00239     return( Vec3D( vec1[1] * vec2[2] - vec1[2] * vec2[1], 
00240                    vec1[2] * vec2[0] - vec1[0] * vec2[2],
00241                    vec1[0] * vec2[1] - vec1[1] * vec2[0] ) );
00242 }
00243 
00244 
00245 inline Vec3D operator*( double x, const Vec3D &vec )
00246 {
00247     return( Vec3D( x*vec.p[0], x*vec.p[1], x*vec.p[2] ) );
00248 }
00249 
00250 
00251 inline std::ostream &operator<<( std::ostream &os, const Vec3D &vec ) 
00252 {
00253     os << std::setw(12) << to_string(vec[0]).substr(0,12) << " ";
00254     os << std::setw(12) << to_string(vec[1]).substr(0,12) << " ";
00255     os << std::setw(12) << to_string(vec[2]).substr(0,12);
00256     return( os );
00257 }
00258 
00259 
00262 class Int3D {
00263     int32_t l[3];
00264 
00265 public:
00266 
00267     Int3D() { l[0] = 0; l[1] = 0; l[2] = 0; }
00268     Int3D( int32_t i ) { l[0] = i; l[1] = 0; l[2] = 0; }
00269     Int3D( int32_t i, int32_t j ) { l[0] = i; l[1] = j; l[2] = 0; }
00270     Int3D( int32_t i, int32_t j, int32_t k ) { l[0] = i; l[1] = j; l[2] = k; }
00271     Int3D( std::istream &s ) {
00272         l[0] = read_int32( s );
00273         l[1] = read_int32( s );
00274         l[2] = read_int32( s );
00275     }
00276     ~Int3D() {}
00277 
00278     int32_t &operator[]( int i ) { return( l[i] ); }
00279     const int32_t &operator[]( int i ) const { return( l[i] ); }
00280     int32_t &operator()( int i ) { return( l[i] ); }
00281     const int32_t &operator()( int i ) const { return( l[i] ); }
00282 
00283     Int3D operator-( const Int3D &i ) {
00284         return( Int3D( l[0] - i[0],
00285                        l[1] - i[1],
00286                        l[2] - i[2] ) );
00287     } 
00288 
00289     Vec3D operator*( double x ) { 
00290         return( Vec3D( x*l[0], x*l[1], x*l[2] ) );
00291     }
00292 
00293     bool operator!=( const Int3D &i ) { 
00294         if( l[0] != i.l[0] || l[1] != i.l[1] || l[2] != i.l[2] )
00295             return( true );
00296         return( false ); 
00297     }
00298 
00299     void save( std::ostream &s ) const { 
00300         write_int32( s, l[0] );
00301         write_int32( s, l[1] );
00302         write_int32( s, l[2] ); 
00303     }
00304 
00305     friend Vec3D operator*( double x, const Int3D &i );
00306     friend std::ostream &operator<<( std::ostream &os, const Vec3D &vec );
00307 };
00308 
00309 
00310 inline Vec3D operator*( double x, const Int3D &i )
00311 {
00312     Vec3D res;
00313     res[0] = x*i.l[0];
00314     res[1] = x*i.l[1];
00315     res[2] = x*i.l[2];
00316     return( res );
00317 }
00318 
00319 
00320 inline std::ostream &operator<<( std::ostream &os, const Int3D &vec ) 
00321 {
00322     os << std::setw(12) << to_string(vec[0]).substr(0,12) << " ";
00323     os << std::setw(12) << to_string(vec[1]).substr(0,12) << " ";
00324     os << std::setw(12) << to_string(vec[2]).substr(0,12);
00325     return( os );
00326 }
00327 
00328 
00329 #endif
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00338 
00339 
00340 
00341 
00342 
00343 
00344 
00345 
00346 
00347