Navigation

Main Page
Download
Support
Installation
Tutorial
Examples
Reference Manual
   Version 1.0.6
      Class Index
      File List
   Version 1.0.5new_solver
   Version 1.0.5dev
   Version 1.0.5b
   Version 1.0.4dev
   Version 1.0.4
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.
1 
5 /* Copyright (c) 2005-2012 Taneli Kalvas. All rights reserved.
6  *
7  * You can redistribute this software and/or modify it under the terms
8  * of the GNU General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this library (file "COPYING" included in the package);
19  * if not, write to the Free Software Foundation, Inc., 51 Franklin
20  * Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * If you have questions about your rights to use or distribute this
23  * software, please contact Berkeley Lab's Technology Transfer
24  * Department at TTD@lbl.gov. Other questions, comments and bug
25  * reports should be sent directly to the author via email at
26  * taneli.kalvas@jyu.fi.
27  *
28  * NOTICE. This software was developed under partial funding from the
29  * U.S. Department of Energy. As such, the U.S. Government has been
30  * granted for itself and others acting on its behalf a paid-up,
31  * nonexclusive, irrevocable, worldwide license in the Software to
32  * reproduce, prepare derivative works, and perform publicly and
33  * display publicly. Beginning five (5) years after the date
34  * permission to assert copyright is obtained from the U.S. Department
35  * of Energy, and subject to any subsequent five (5) year renewals,
36  * the U.S. Government is granted for itself and others acting on its
37  * behalf a paid-up, nonexclusive, irrevocable, worldwide license in
38  * the Software to reproduce, prepare derivative works, distribute
39  * copies to the public, perform publicly and display publicly, and to
40  * permit others to do so.
41  */
42 
43 #ifndef VEC3D_HPP
44 #define VEC3D_HPP 1
45 
46 
47 #include <math.h>
48 #include <stdint.h>
49 #include <iostream>
50 #include <iostream>
51 #include <iomanip>
52 #include "vec4d.hpp"
53 #include "file.hpp"
54 
55 
58 class Vec3D {
59 
60  double p[3];
61 
62 public:
63 
64  Vec3D() { p[0] = 0.0; p[1] = 0.0; p[2] = 0.0; }
65  Vec3D( double x ) { p[0] = x; p[1] = 0.0; p[2] = 0.0; }
66  Vec3D( double x, double y ) { p[0] = x; p[1] = y; p[2] = 0.0; }
67  Vec3D( double x, double y, double z ) { p[0] = x; p[1] = y; p[2] = z; }
68 
69  Vec3D( const class Vec4D &vec );
70 
71  Vec3D( std::istream &s ) {
72  p[0] = read_double( s );
73  p[1] = read_double( s );
74  p[2] = read_double( s );
75  }
76  ~Vec3D() {}
77 
78  double &operator[]( int i ) { return( p[i] ); }
79  const double &operator[]( int i ) const { return( p[i] ); }
80  double &operator()( int i ) { return( p[i] ); }
81  const double &operator()( int i ) const { return( p[i] ); }
82 
85  Vec3D operator+( const Vec3D &vec ) const {
86  return( Vec3D( p[0] + vec[0],
87  p[1] + vec[1],
88  p[2] + vec[2] ) );
89  }
90 
93  Vec3D operator-( const Vec3D &vec ) const {
94  return( Vec3D( p[0] - vec[0],
95  p[1] - vec[1],
96  p[2] - vec[2] ) );
97  }
98 
101  Vec3D &operator+=( const Vec3D &vec ) {
102  p[0] += vec[0];
103  p[1] += vec[1];
104  p[2] += vec[2];
105  return( *this );
106  }
107 
110  double operator*( const Vec3D &vec ) const {
111  return( p[0] * vec[0] +
112  p[1] * vec[1] +
113  p[2] * vec[2] );
114  }
115 
118  Vec3D operator*( double x ) const {
119  return( Vec3D( x*p[0], x*p[1], x*p[2] ) );
120  }
121 
124  Vec3D operator-( void ) const {
125  return( Vec3D( -p[0], -p[1], -p[2] ) );
126  }
127 
130  Vec3D &operator*=( double x ) {
131  p[0] *= x;
132  p[1] *= x;
133  p[2] *= x;
134  return( *this );
135  }
136 
139  Vec3D &operator/=( double x ) {
140  double div = 1.0/x;
141  p[0] *= div;
142  p[1] *= div;
143  p[2] *= div;
144  return( *this );
145  }
146 
151  bool operator!=( const Vec3D &x ) const;
152 
157  bool operator==( const Vec3D &x ) const;
158 
165  bool approx( const Vec3D &x, double eps = 1.0e-6 ) const;
166 
169  Vec3D &operator=( const Vec3D &x ) {
170  p[0] = x[0];
171  p[1] = x[1];
172  p[2] = x[2];
173  return( *this );
174  }
175 
178  Vec3D &operator=( const double &x ) {
179  p[0] = x;
180  p[1] = x;
181  p[2] = x;
182  return( *this );
183  }
184 
187  void abs( void ) {
188  p[0] = fabs( p[0] );
189  p[1] = fabs( p[1] );
190  p[2] = fabs( p[2] );
191  }
192 
195  void normalize( void ) {
196  double inv_norm = 1.0/sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] );
197  p[0] *= inv_norm;
198  p[1] *= inv_norm;
199  p[2] *= inv_norm;
200  }
201 
206  double norm2( void ) const {
207  return( sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ) );
208  }
209 
214  double max( void ) const;
215 
220  double ssqr( void ) const {
221  return( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] );
222  }
223 
226  int min_element( void ) const;
227 
230  Vec3D arb_perpendicular( void ) const;
231 
234  void save( std::ostream &os ) const {
235  write_double( os, p[0] );
236  write_double( os, p[1] );
237  write_double( os, p[2] );
238  }
239 
242  static Vec3D standard_basis( int i );
243 
246  friend Vec3D cross( const Vec3D &vec1, const Vec3D &vec2 );
247 
250  friend double norm2( const Vec3D &vec );
251 
254  friend double ssqr( const Vec3D &vec );
255 
258  friend Vec3D operator*( double x, const Vec3D &vec );
259 
262  friend Vec3D operator*( double x, const class Int3D &i );
263 
266  friend std::ostream &operator<<( std::ostream &os, const Vec3D &vec );
267 };
268 
269 
270 inline double norm2( const Vec3D &vec ) {
271  return( vec.norm2() );
272 }
273 
274 
275 inline double ssqr( const Vec3D &vec ) {
276  return( vec.ssqr() );
277 }
278 
279 
280 inline Vec3D cross( const Vec3D &vec1, const Vec3D &vec2 ) {
281  return( Vec3D( vec1[1] * vec2[2] - vec1[2] * vec2[1],
282  vec1[2] * vec2[0] - vec1[0] * vec2[2],
283  vec1[0] * vec2[1] - vec1[1] * vec2[0] ) );
284 }
285 
286 
287 inline Vec3D operator*( double x, const Vec3D &vec )
288 {
289  return( Vec3D( x*vec.p[0], x*vec.p[1], x*vec.p[2] ) );
290 }
291 
292 
293 inline std::ostream &operator<<( std::ostream &os, const Vec3D &vec )
294 {
295  os << std::setw(12) << to_string(vec[0]).substr(0,12) << " ";
296  os << std::setw(12) << to_string(vec[1]).substr(0,12) << " ";
297  os << std::setw(12) << to_string(vec[2]).substr(0,12);
298  return( os );
299 }
300 
301 
304 class Int3D {
305  int32_t l[3];
306 
307 public:
308 
309  Int3D() { l[0] = 0; l[1] = 0; l[2] = 0; }
310  Int3D( int32_t i ) { l[0] = i; l[1] = 0; l[2] = 0; }
311  Int3D( int32_t i, int32_t j ) { l[0] = i; l[1] = j; l[2] = 0; }
312  Int3D( int32_t i, int32_t j, int32_t k ) { l[0] = i; l[1] = j; l[2] = k; }
313  Int3D( std::istream &s ) {
314  l[0] = read_int32( s );
315  l[1] = read_int32( s );
316  l[2] = read_int32( s );
317  }
318  ~Int3D() {}
319 
320  int32_t &operator[]( int i ) { return( l[i] ); }
321  const int32_t &operator[]( int i ) const { return( l[i] ); }
322  int32_t &operator()( int i ) { return( l[i] ); }
323  const int32_t &operator()( int i ) const { return( l[i] ); }
324 
327  Int3D operator+( const Int3D &i ) const {
328  return( Int3D( l[0] + i[0],
329  l[1] + i[1],
330  l[2] + i[2] ) );
331  }
332 
335  Int3D operator-( const Int3D &i ) {
336  return( Int3D( l[0] - i[0],
337  l[1] - i[1],
338  l[2] - i[2] ) );
339  }
340 
341  Int3D operator*( int i ) {
342  return( Int3D( i*l[0], i*l[1], i*l[2] ) );
343  }
344 
345  Vec3D operator*( double x ) {
346  return( Vec3D( x*l[0], x*l[1], x*l[2] ) );
347  }
348 
351  bool operator!=( const Int3D &i ) const {
352  if( l[0] != i.l[0] || l[1] != i.l[1] || l[2] != i.l[2] )
353  return( true );
354  return( false );
355  }
356 
359  bool operator==( const Int3D &i ) const {
360  if( l[0] == i.l[0] && l[1] == i.l[1] && l[2] == i.l[2] )
361  return( true );
362  return( false );
363  }
364 
365 
368  int32_t max( void ) const;
369 
370  void save( std::ostream &s ) const {
371  write_int32( s, l[0] );
372  write_int32( s, l[1] );
373  write_int32( s, l[2] );
374  }
375 
376  friend Vec3D operator*( double x, const Int3D &i );
377  friend Int3D operator*( int x, const Int3D &i );
378  friend std::ostream &operator<<( std::ostream &os, const Vec3D &vec );
379 };
380 
381 
382 inline Vec3D operator*( double x, const Int3D &i )
383 {
384  Vec3D res;
385  res[0] = x*i.l[0];
386  res[1] = x*i.l[1];
387  res[2] = x*i.l[2];
388  return( res );
389 }
390 
391 
392 inline Int3D operator*( int x, const Int3D &i )
393 {
394  Int3D res;
395  res[0] = x*i.l[0];
396  res[1] = x*i.l[1];
397  res[2] = x*i.l[2];
398  return( res );
399 }
400 
401 
402 inline std::ostream &operator<<( std::ostream &os, const Int3D &vec )
403 {
404  os << std::setw(12) << to_string(vec[0]).substr(0,12) << " ";
405  os << std::setw(12) << to_string(vec[1]).substr(0,12) << " ";
406  os << std::setw(12) << to_string(vec[2]).substr(0,12);
407  return( os );
408 }
409 
410 
411 #endif
412 
bool operator!=(const Vec3D &x) const
Inequality test.
Definition: vec3d.cpp:55
Vec3D operator+(const Vec3D &vec) const
Vector addition
Definition: vec3d.hpp:85
int min_element(void) const
Returns the index of element with minimum magnitude (abs).
Definition: vec3d.cpp:120
Vec3D & operator=(const Vec3D &x)
Assignment.
Definition: vec3d.hpp:169
void write_double(std::ostream &s, double value)
Write double value into stream os.
Definition: file.cpp:88
Vec3D arb_perpendicular(void) const
Returns arbitrary vector perpendicular to input vector.
Definition: vec3d.cpp:142
Vec4D cross(const Vec4D &vec1, const Vec4D &vec2)
Definition: vec4d.hpp:279
bool operator!=(const Int3D &i) const
Inequality test.
Definition: vec3d.hpp:351
Vec4D operator*(double x, const Vec4D &vec)
Definition: vec4d.hpp:287
friend Vec3D cross(const Vec3D &vec1, const Vec3D &vec2)
Cross product.
Definition: vec3d.hpp:280
double read_double(std::istream &s)
Readd double from stream is.
Definition: file.cpp:157
Bindary file writing and reading tools.
std::string to_string(const T &t)
Function for converting a type to string.
Definition: error.hpp:62
Vec3D & operator+=(const Vec3D &vec)
Vector accumulation
Definition: vec3d.hpp:101
double norm2(void) const
Returns 2-norm of vector.
Definition: vec3d.hpp:206
friend std::ostream & operator<<(std::ostream &os, const Vec3D &vec)
Outputting to stream.
Definition: vec3d.hpp:293
bool approx(const Vec3D &x, double eps=1.0e-6) const
Approximate equality test.
Definition: vec3d.cpp:108
Vec3D operator-(const Vec3D &vec) const
Vector difference
Definition: vec3d.hpp:93
Vec3D & operator/=(double x)
Vector scaling with divisor.
Definition: vec3d.hpp:139
Int3D operator-(const Int3D &i)
Integer vector difference.
Definition: vec3d.hpp:335
void save(std::ostream &os) const
Saves data to stream os.
Definition: vec3d.hpp:234
static Vec3D standard_basis(int i)
Returns standard basis vector i.
Definition: vec3d.cpp:134
void write_int32(std::ostream &s, int32_t value)
Write int32_t value into stream os.
Definition: file.cpp:70
Vec3D operator-(void) const
Unary minus.
Definition: vec3d.hpp:124
double operator*(const Vec3D &vec) const
Dot product.
Definition: vec3d.hpp:110
Vec3D & operator*=(double x)
Vector scaling.
Definition: vec3d.hpp:130
Homogenous vector for three dimensional space.
Definition: vec4d.hpp:67
bool operator==(const Int3D &i) const
Equality test.
Definition: vec3d.hpp:359
3D Integer vector class.
Definition: vec3d.hpp:304
Homogenous vectors for three dimensional space.
double max(void) const
Returns inf-norm of vector.
Definition: vec3d.cpp:76
int32_t max(void) const
Returns maximum component of vector.
Definition: vec3d.cpp:92
Vec3D operator*(double x) const
Vector scaling.
Definition: vec3d.hpp:118
Int3D operator+(const Int3D &i) const
Integer vector addition.
Definition: vec3d.hpp:327
Vec3D & operator=(const double &x)
Assignment of every coordinate.
Definition: vec3d.hpp:178
Three dimensional vector.
Definition: vec3d.hpp:58
int32_t read_int32(std::istream &s)
Read int32_t from stream is.
Definition: file.cpp:135
double ssqr(void) const
Returns square of 2-norm of vector.
Definition: vec3d.hpp:220
bool operator==(const Vec3D &x) const
Equality test.
Definition: vec3d.cpp:65
void abs(void)
Calculate absolute value of each component.
Definition: vec3d.hpp:187
double norm2(const Vector &vec)
Definition: mvector.cpp:474
double ssqr(const Vector &vec)
Definition: mvector.cpp:487
void normalize(void)
Normalize vector.
Definition: vec3d.hpp:195


Reference manual for Ion Beam Simulator 1.0.6
Generated by Doxygen 1.8.5 on Mon Jun 15 2015 09:59:32.