Navigation

Main Page
Download
Support
Installation
Tutorial
Examples
Reference Manual
   Version 1.0.4
   Version 1.0.4dev
      Class Index
      File List
Publications


Hosted by Get Ion Beam Simulator at SourceForge.net. Fast, secure and Free Open Source software downloads

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 
00149     bool operator!=( const Vec3D &x ) const;
00150 
00156     bool operator==( const Vec3D &x ) const;    
00157 
00160     Vec3D &operator=( const Vec3D &x ) { 
00161         p[0] = x[0];
00162         p[1] = x[1];
00163         p[2] = x[2];
00164         return( *this );
00165     }
00166 
00169     Vec3D &operator=( const double &x ) { 
00170         p[0] = x;
00171         p[1] = x;
00172         p[2] = x;
00173         return( *this );
00174     }
00175 
00178     void abs( void ) {
00179         p[0] = fabs( p[0] );
00180         p[1] = fabs( p[1] );
00181         p[2] = fabs( p[2] );
00182     }
00183 
00186     void normalize( void ) {
00187         double inv_norm = 1.0/sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] );
00188         p[0] *= inv_norm;
00189         p[1] *= inv_norm;
00190         p[2] *= inv_norm;
00191     }
00192 
00197     double norm2( void ) const {
00198         return( sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ) );
00199     }
00200 
00205     double ssqr( void ) const {
00206         return( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] );
00207     }
00208 
00211     int min_element( void ) const;
00212 
00215     Vec3D arb_perpendicular( void ) const;
00216 
00219     void save( std::ostream &os ) const { 
00220         write_double( os, p[0] );
00221         write_double( os, p[1] );
00222         write_double( os, p[2] ); 
00223     }
00224 
00227     static Vec3D standard_basis( int i );
00228 
00231     friend Vec3D cross( const Vec3D &vec1, const Vec3D &vec2 );
00232 
00235     friend double norm2( const Vec3D &vec );
00236 
00239     friend Vec3D operator*( double x, const Vec3D &vec );
00240 
00243     friend Vec3D operator*( double x, const class Int3D &i );
00244 
00247     friend std::ostream &operator<<( std::ostream &os, const Vec3D &vec );
00248 };
00249 
00250 
00251 inline double norm2( const Vec3D &vec ) {
00252     return( vec.norm2() );
00253 }
00254 
00255 
00256 inline Vec3D cross( const Vec3D &vec1, const Vec3D &vec2 ) { 
00257     return( Vec3D( vec1[1] * vec2[2] - vec1[2] * vec2[1], 
00258                    vec1[2] * vec2[0] - vec1[0] * vec2[2],
00259                    vec1[0] * vec2[1] - vec1[1] * vec2[0] ) );
00260 }
00261 
00262 
00263 inline Vec3D operator*( double x, const Vec3D &vec )
00264 {
00265     return( Vec3D( x*vec.p[0], x*vec.p[1], x*vec.p[2] ) );
00266 }
00267 
00268 
00269 inline std::ostream &operator<<( std::ostream &os, const Vec3D &vec ) 
00270 {
00271     os << std::setw(12) << to_string(vec[0]).substr(0,12) << " ";
00272     os << std::setw(12) << to_string(vec[1]).substr(0,12) << " ";
00273     os << std::setw(12) << to_string(vec[2]).substr(0,12);
00274     return( os );
00275 }
00276 
00277 
00280 class Int3D {
00281     int32_t l[3];
00282 
00283 public:
00284 
00285     Int3D() { l[0] = 0; l[1] = 0; l[2] = 0; }
00286     Int3D( int32_t i ) { l[0] = i; l[1] = 0; l[2] = 0; }
00287     Int3D( int32_t i, int32_t j ) { l[0] = i; l[1] = j; l[2] = 0; }
00288     Int3D( int32_t i, int32_t j, int32_t k ) { l[0] = i; l[1] = j; l[2] = k; }
00289     Int3D( std::istream &s ) {
00290         l[0] = read_int32( s );
00291         l[1] = read_int32( s );
00292         l[2] = read_int32( s );
00293     }
00294     ~Int3D() {}
00295 
00296     int32_t &operator[]( int i ) { return( l[i] ); }
00297     const int32_t &operator[]( int i ) const { return( l[i] ); }
00298     int32_t &operator()( int i ) { return( l[i] ); }
00299     const int32_t &operator()( int i ) const { return( l[i] ); }
00300 
00301     Int3D operator-( const Int3D &i ) {
00302         return( Int3D( l[0] - i[0],
00303                        l[1] - i[1],
00304                        l[2] - i[2] ) );
00305     } 
00306 
00307     Vec3D operator*( double x ) { 
00308         return( Vec3D( x*l[0], x*l[1], x*l[2] ) );
00309     }
00310 
00313     bool operator!=( const Int3D &i ) const { 
00314         if( l[0] != i.l[0] || l[1] != i.l[1] || l[2] != i.l[2] )
00315             return( true );
00316         return( false ); 
00317     }
00318 
00321     bool operator==( const Int3D &i ) const { 
00322         if( l[0] == i.l[0] && l[1] == i.l[1] && l[2] == i.l[2] )
00323             return( true );
00324         return( false ); 
00325     }
00326 
00327     void save( std::ostream &s ) const { 
00328         write_int32( s, l[0] );
00329         write_int32( s, l[1] );
00330         write_int32( s, l[2] ); 
00331     }
00332 
00333     friend Vec3D operator*( double x, const Int3D &i );
00334     friend std::ostream &operator<<( std::ostream &os, const Vec3D &vec );
00335 };
00336 
00337 
00338 inline Vec3D operator*( double x, const Int3D &i )
00339 {
00340     Vec3D res;
00341     res[0] = x*i.l[0];
00342     res[1] = x*i.l[1];
00343     res[2] = x*i.l[2];
00344     return( res );
00345 }
00346 
00347 
00348 inline std::ostream &operator<<( std::ostream &os, const Int3D &vec ) 
00349 {
00350     os << std::setw(12) << to_string(vec[0]).substr(0,12) << " ";
00351     os << std::setw(12) << to_string(vec[1]).substr(0,12) << " ";
00352     os << std::setw(12) << to_string(vec[2]).substr(0,12);
00353     return( os );
00354 }
00355 
00356 
00357 #endif


Reference manual for Ion Beam Simulator 1.0.4dev
Generated by Doxygen 1.7.1 on Wed May 18 2011 23:03:48.