Navigation

Main Page
Download
Support
Installation
Tutorial
Examples
Reference Manual
   Version 1.0.6dev
      Class Index
      File List
   Version 1.0.6
   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,2016 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  friend std::basic_ostream<wchar_t> &operator<<( std::basic_ostream<wchar_t> &os, const Vec3D &vec );
268 };
269 
270 
271 inline double norm2( const Vec3D &vec ) {
272  return( vec.norm2() );
273 }
274 
275 
276 inline double ssqr( const Vec3D &vec ) {
277  return( vec.ssqr() );
278 }
279 
280 
281 inline Vec3D cross( const Vec3D &vec1, const Vec3D &vec2 ) {
282  return( Vec3D( vec1[1] * vec2[2] - vec1[2] * vec2[1],
283  vec1[2] * vec2[0] - vec1[0] * vec2[2],
284  vec1[0] * vec2[1] - vec1[1] * vec2[0] ) );
285 }
286 
287 
288 inline Vec3D operator*( double x, const Vec3D &vec )
289 {
290  return( Vec3D( x*vec.p[0], x*vec.p[1], x*vec.p[2] ) );
291 }
292 
293 
294 inline std::ostream &operator<<( std::ostream &os, const Vec3D &vec )
295 {
296  os << std::setw(12) << vec[0] << " "
297  << std::setw(12) << vec[1] << " "
298  << std::setw(12) << vec[2];
299  //os << std::setw(12) << to_string(vec[0]).substr(0,12) << " ";
300  //os << std::setw(12) << to_string(vec[1]).substr(0,12) << " ";
301  //os << std::setw(12) << to_string(vec[2]).substr(0,12);
302  return( os );
303 }
304 
305 
306 inline std::basic_ostream<wchar_t> &operator<<( std::basic_ostream<wchar_t> &os, const Vec3D &vec )
307 {
308  os << std::setw(12) << vec[0] << " "
309  << std::setw(12) << vec[1] << " "
310  << std::setw(12) << vec[2];
311  //os << std::setw(12) << to_wstring(vec[0]).substr(0,12) << " ";
312  //os << std::setw(12) << to_wstring(vec[1]).substr(0,12) << " ";
313  //os << std::setw(12) << to_wstring(vec[2]).substr(0,12);
314  return( os );
315 }
316 
317 
320 class Int3D {
321  int32_t l[3];
322 
323 public:
324 
325  Int3D() { l[0] = 0; l[1] = 0; l[2] = 0; }
326  Int3D( int32_t i ) { l[0] = i; l[1] = 0; l[2] = 0; }
327  Int3D( int32_t i, int32_t j ) { l[0] = i; l[1] = j; l[2] = 0; }
328  Int3D( int32_t i, int32_t j, int32_t k ) { l[0] = i; l[1] = j; l[2] = k; }
329  Int3D( std::istream &s ) {
330  l[0] = read_int32( s );
331  l[1] = read_int32( s );
332  l[2] = read_int32( s );
333  }
334  ~Int3D() {}
335 
336  int32_t &operator[]( int i ) { return( l[i] ); }
337  const int32_t &operator[]( int i ) const { return( l[i] ); }
338  int32_t &operator()( int i ) { return( l[i] ); }
339  const int32_t &operator()( int i ) const { return( l[i] ); }
340 
343  Int3D operator+( const Int3D &i ) const {
344  return( Int3D( l[0] + i[0],
345  l[1] + i[1],
346  l[2] + i[2] ) );
347  }
348 
351  Int3D operator-( const Int3D &i ) {
352  return( Int3D( l[0] - i[0],
353  l[1] - i[1],
354  l[2] - i[2] ) );
355  }
356 
357  Int3D operator*( int i ) {
358  return( Int3D( i*l[0], i*l[1], i*l[2] ) );
359  }
360 
361  Vec3D operator*( double x ) {
362  return( Vec3D( x*l[0], x*l[1], x*l[2] ) );
363  }
364 
367  bool operator!=( const Int3D &i ) const {
368  if( l[0] != i.l[0] || l[1] != i.l[1] || l[2] != i.l[2] )
369  return( true );
370  return( false );
371  }
372 
375  bool operator==( const Int3D &i ) const {
376  if( l[0] == i.l[0] && l[1] == i.l[1] && l[2] == i.l[2] )
377  return( true );
378  return( false );
379  }
380 
381 
384  int32_t max( void ) const;
385 
386  void save( std::ostream &s ) const {
387  write_int32( s, l[0] );
388  write_int32( s, l[1] );
389  write_int32( s, l[2] );
390  }
391 
392  friend Vec3D operator*( double x, const Int3D &i );
393  friend Int3D operator*( int x, const Int3D &i );
394  friend std::ostream &operator<<( std::ostream &os, const Int3D &vec );
395  friend std::basic_ostream<wchar_t> &operator<<( std::basic_ostream<wchar_t> &os, const Int3D &vec );
396 };
397 
398 
399 inline Vec3D operator*( double x, const Int3D &i )
400 {
401  Vec3D res;
402  res[0] = x*i.l[0];
403  res[1] = x*i.l[1];
404  res[2] = x*i.l[2];
405  return( res );
406 }
407 
408 
409 inline Int3D operator*( int x, const Int3D &i )
410 {
411  Int3D res;
412  res[0] = x*i.l[0];
413  res[1] = x*i.l[1];
414  res[2] = x*i.l[2];
415  return( res );
416 }
417 
418 
419 inline std::ostream &operator<<( std::ostream &os, const Int3D &vec )
420 {
421  os << std::setw(12) << vec[0] << " "
422  << std::setw(12) << vec[1] << " "
423  << std::setw(12) << vec[2];
424  //os << std::setw(12) << to_string(vec[0]).substr(0,12) << " ";
425  //os << std::setw(12) << to_string(vec[1]).substr(0,12) << " ";
426  //os << std::setw(12) << to_string(vec[2]).substr(0,12);
427  return( os );
428 }
429 
430 
431 inline std::basic_ostream<wchar_t> &operator<<( std::basic_ostream<wchar_t> &os, const Int3D &vec )
432 {
433  os << std::setw(12) << vec[0] << " "
434  << std::setw(12) << vec[1] << " "
435  << std::setw(12) << vec[2];
436  //os << std::setw(12) << to_wstring(vec[0]).substr(0,12) << " ";
437  //os << std::setw(12) << to_wstring(vec[1]).substr(0,12) << " ";
438  //os << std::setw(12) << to_wstring(vec[2]).substr(0,12);
439  return( os );
440 }
441 
442 
443 #endif
444 
3D Integer vector class.
Definition: vec3d.hpp:320
bool operator!=(const Int3D &i) const
Inequality test.
Definition: vec3d.hpp:367
Int3D operator+(const Int3D &i) const
Integer vector addition.
Definition: vec3d.hpp:343
Int3D operator-(const Int3D &i)
Integer vector difference.
Definition: vec3d.hpp:351
bool operator==(const Int3D &i) const
Equality test.
Definition: vec3d.hpp:375
int32_t max(void) const
Returns maximum component of vector.
Definition: vec3d.cpp:92
Three dimensional vector.
Definition: vec3d.hpp:58
void normalize(void)
Normalize vector.
Definition: vec3d.hpp:195
Vec3D operator-(const Vec3D &vec) const
Vector difference
Definition: vec3d.hpp:93
Vec3D operator*(double x) const
Vector scaling.
Definition: vec3d.hpp:118
friend Vec3D cross(const Vec3D &vec1, const Vec3D &vec2)
Cross product.
Definition: vec3d.hpp:281
double operator*(const Vec3D &vec) const
Dot product.
Definition: vec3d.hpp:110
Vec3D & operator/=(double x)
Vector scaling with divisor.
Definition: vec3d.hpp:139
Vec3D & operator*=(double x)
Vector scaling.
Definition: vec3d.hpp:130
bool approx(const Vec3D &x, double eps=1.0e-6) const
Approximate equality test.
Definition: vec3d.cpp:108
Vec3D & operator+=(const Vec3D &vec)
Vector accumulation
Definition: vec3d.hpp:101
static Vec3D standard_basis(int i)
Returns standard basis vector i.
Definition: vec3d.cpp:134
Vec3D operator-(void) const
Unary minus.
Definition: vec3d.hpp:124
double norm2(void) const
Returns 2-norm of vector.
Definition: vec3d.hpp:206
int min_element(void) const
Returns the index of element with minimum magnitude (abs).
Definition: vec3d.cpp:120
void abs(void)
Calculate absolute value of each component.
Definition: vec3d.hpp:187
bool operator!=(const Vec3D &x) const
Inequality test.
Definition: vec3d.cpp:55
bool operator==(const Vec3D &x) const
Equality test.
Definition: vec3d.cpp:65
double max(void) const
Returns inf-norm of vector.
Definition: vec3d.cpp:76
double ssqr(void) const
Returns square of 2-norm of vector.
Definition: vec3d.hpp:220
Vec3D & operator=(const double &x)
Assignment of every coordinate.
Definition: vec3d.hpp:178
Vec3D operator+(const Vec3D &vec) const
Vector addition
Definition: vec3d.hpp:85
friend Vec3D operator*(double x, const class Int3D &i)
Vector scaling for integer vector.
Vec3D & operator=(const Vec3D &x)
Assignment.
Definition: vec3d.hpp:169
Vec3D arb_perpendicular(void) const
Returns arbitrary vector perpendicular to input vector.
Definition: vec3d.cpp:142
friend std::ostream & operator<<(std::ostream &os, const Vec3D &vec)
Outputting to stream.
Definition: vec3d.hpp:294
void save(std::ostream &os) const
Saves data to stream os.
Definition: vec3d.hpp:234
Homogenous vector for three dimensional space.
Definition: vec4d.hpp:67
void write_double(std::ostream &s, double value)
Write double value into stream os.
Definition: file.cpp:88
void write_int32(std::ostream &s, int32_t value)
Write int32_t value into stream os.
Definition: file.cpp:70
double read_double(std::istream &s)
Readd double from stream is.
Definition: file.cpp:157
int32_t read_int32(std::istream &s)
Read int32_t from stream is.
Definition: file.cpp:135
Bindary file writing and reading tools.
double norm2(const Vector &vec)
Definition: mvector.cpp:474
double ssqr(const Vector &vec)
Definition: mvector.cpp:487
Homogenous vectors for three dimensional space.
Vec4D operator*(double x, const Vec4D &vec)
Definition: vec4d.hpp:287
Vec4D cross(const Vec4D &vec1, const Vec4D &vec2)
Definition: vec4d.hpp:279


Reference manual for Ion Beam Simulator 1.0.6dev
Generated by Doxygen 1.9.1 on Thu Sep 11 2025 09:37:24.