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
vec4d.hpp
Go to the documentation of this file.
1 
5 /* Copyright (c) 2005-2010 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 VEC4D_HPP
44 #define VEC4D_HPP 1
45 
46 
47 #include <math.h>
48 #include <stdint.h>
49 #include <iostream>
50 #include <iostream>
51 #include <iomanip>
52 #include "vec3d.hpp"
53 #include "file.hpp"
54 #include "error.hpp"
55 
56 
67 class Vec4D {
68 
69  double p[4];
70 
71 public:
72 
73  Vec4D() { p[0] = 0.0; p[1] = 0.0; p[2] = 0.0; p[3] = 0.0; }
74  Vec4D( double x ) { p[0] = x; p[1] = 0.0; p[2] = 0.0; p[3] = 0.0; }
75  Vec4D( double x, double y ) { p[0] = x; p[1] = y; p[2] = 0.0; p[3] = 0.0; }
76  Vec4D( double x, double y, double z ) { p[0] = x; p[1] = y; p[2] = z; p[3] = 0.0; }
77  Vec4D( double x, double y, double z, double w ) { p[0] = x; p[1] = y; p[2] = z; p[3] = w; }
78 
83  Vec4D( const class Vec3D &vec );
84 
85  Vec4D( std::istream &s ) {
86  p[0] = read_double( s );
87  p[1] = read_double( s );
88  p[2] = read_double( s );
89  p[3] = read_double( s );
90  }
91  ~Vec4D() {}
92 
93  double &operator[]( int i ) { return( p[i] ); }
94  const double &operator[]( int i ) const { return( p[i] ); }
95  double &operator()( int i ) { return( p[i] ); }
96  const double &operator()( int i ) const { return( p[i] ); }
97 
103  Vec4D operator+( const Vec4D &vec ) const {
104  return( Vec4D( p[0] + vec[0],
105  p[1] + vec[1],
106  p[2] + vec[2],
107  (p[2] == vec[2] ? 0.0 : 1.0) ) );
108  }
109 
115  Vec4D operator-( const Vec4D &vec ) const {
116  return( Vec4D( p[0] - vec[0],
117  p[1] - vec[1],
118  p[2] - vec[2],
119  (p[2] == vec[2] ? 0.0 : 1.0) ) );
120  }
121 
127  Vec4D &operator+=( const Vec4D &vec ) {
128  p[0] += vec[0];
129  p[1] += vec[1];
130  p[2] += vec[2];
131  return( *this );
132  }
133 
138  double operator*( const Vec4D &vec ) const {
139  return( p[0] * vec[0] +
140  p[1] * vec[1] +
141  p[2] * vec[2] );
142  }
143 
148  Vec4D operator*( double x ) const {
149  return( Vec4D( x*p[0], x*p[1], x*p[2], p[3] ) );
150  }
151 
156  Vec4D &operator*=( double x ) {
157  p[0] *= x;
158  p[1] *= x;
159  p[2] *= x;
160  return( *this );
161  }
162 
167  Vec4D &operator/=( double x ) {
168  double div = 1.0/x;
169  p[0] *= div;
170  p[1] *= div;
171  p[2] *= div;
172  return( *this );
173  }
174 
179  bool operator!=( const Vec4D &x ) {
180  if( p[0] != x.p[0] || p[1] != x.p[1] || p[2] != x.p[2] || p[3] != x.p[3] )
181  return( true );
182  return( false );
183  }
184 
189  bool operator==( const Vec4D &x ) {
190  if( p[0] == x.p[0] && p[1] == x.p[1] && p[2] == x.p[2] && p[3] == x.p[3] )
191  return( true );
192  return( false );
193  }
194 
197  Vec4D &operator=( const Vec4D &x ) {
198  p[0] = x[0];
199  p[1] = x[1];
200  p[2] = x[2];
201  p[3] = x[3];
202  return( *this );
203  }
204 
210  void homogenize() {
211  double inv_w = 1.0/p[3];
212  p[0] *= inv_w;
213  p[1] *= inv_w;
214  p[2] *= inv_w;
215  p[3] = 1.0;
216  }
217 
222  void normalize() {
223  double inv_norm = 1.0/sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] );
224  p[0] *= inv_norm;
225  p[1] *= inv_norm;
226  p[2] *= inv_norm;
227  p[3] = 0.0;
228  }
229 
234  double norm2() const {
235  return( sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ) );
236  }
237 
242  double ssqr() const {
243  return( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] );
244  }
245 
246  void save( std::ostream &s ) const {
247  write_double( s, p[0] );
248  write_double( s, p[1] );
249  write_double( s, p[2] );
250  write_double( s, p[3] );
251  }
252 
257  friend Vec4D cross( const Vec4D &vec1, const Vec4D &vec2 );
258 
261  friend double norm2( const Vec4D &vec );
262 
267  friend Vec4D operator*( double x, const Vec4D &vec );
268 
271  friend std::ostream &operator<<( std::ostream &os, const Vec4D &vec );
272 };
273 
274 
275 inline double norm2( const Vec4D &vec ) {
276  return( vec.norm2() );
277 }
278 
279 inline Vec4D cross( const Vec4D &vec1, const Vec4D &vec2 ) {
280  return( Vec4D( vec1[1] * vec2[2] - vec1[2] * vec2[1],
281  vec1[2] * vec2[0] - vec1[0] * vec2[2],
282  vec1[0] * vec2[1] - vec1[1] * vec2[0],
283  0.0 ) );
284 }
285 
286 
287 inline Vec4D operator*( double x, const Vec4D &vec )
288 {
289  return( Vec4D( x*vec[0], x*vec[1], x*vec[2], vec[3] ) );
290 }
291 
292 
293 inline std::ostream &operator<<( std::ostream &os, const Vec4D &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  os << std::setw(12) << to_string(vec[3]).substr(0,12);
299  return( os );
300 }
301 
302 
303 #endif
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
316 
317 
318 
319 
320 
321 
322 
Three dimensional vector.
Definition: vec3d.hpp:58
Homogenous vector for three dimensional space.
Definition: vec4d.hpp:67
bool operator==(const Vec4D &x)
Equality test.
Definition: vec4d.hpp:189
double operator*(const Vec4D &vec) const
Dot product.
Definition: vec4d.hpp:138
Vec4D & operator/=(double x)
Vector scaling with divisor.
Definition: vec4d.hpp:167
Vec4D & operator*=(double x)
Vector scaling.
Definition: vec4d.hpp:156
Vec4D & operator+=(const Vec4D &vec)
Accumulation.
Definition: vec4d.hpp:127
friend Vec4D cross(const Vec4D &vec1, const Vec4D &vec2)
Cross product.
Definition: vec4d.hpp:279
void normalize()
Normalize vector.
Definition: vec4d.hpp:222
bool operator!=(const Vec4D &x)
Inequality test.
Definition: vec4d.hpp:179
double ssqr() const
Returns square of 2-norm of vector.
Definition: vec4d.hpp:242
Vec4D & operator=(const Vec4D &x)
Assignment.
Definition: vec4d.hpp:197
Vec4D operator*(double x) const
Vector scaling.
Definition: vec4d.hpp:148
double norm2() const
Returns 2-norm of vector.
Definition: vec4d.hpp:234
friend std::ostream & operator<<(std::ostream &os, const Vec4D &vec)
Outputting to stream.
Definition: vec4d.hpp:293
Vec4D operator-(const Vec4D &vec) const
Difference.
Definition: vec4d.hpp:115
Vec4D operator+(const Vec4D &vec) const
Addition.
Definition: vec4d.hpp:103
void homogenize()
Homogenize vector.
Definition: vec4d.hpp:210
Error classes and handling
std::string to_string(const T &t)
Function for converting a type to string.
Definition: error.hpp:62
void write_double(std::ostream &s, double value)
Write double value into stream os.
Definition: file.cpp:88
double read_double(std::istream &s)
Readd double from stream is.
Definition: file.cpp:157
Bindary file writing and reading tools.
double norm2(const Vector &vec)
Definition: mvector.cpp:474
Three dimensional vectors.
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.