vec3d.hpp
Go to the documentation of this file.
1 
5 /* Copyright (c) 2005-2011 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 ssqr( void ) const {
215  return( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] );
216  }
217 
220  int min_element( void ) const;
221 
224  Vec3D arb_perpendicular( void ) const;
225 
228  void save( std::ostream &os ) const {
229  write_double( os, p[0] );
230  write_double( os, p[1] );
231  write_double( os, p[2] );
232  }
233 
236  static Vec3D standard_basis( int i );
237 
240  friend Vec3D cross( const Vec3D &vec1, const Vec3D &vec2 );
241 
244  friend double norm2( const Vec3D &vec );
245 
248  friend Vec3D operator*( double x, const Vec3D &vec );
249 
252  friend Vec3D operator*( double x, const class Int3D &i );
253 
256  friend std::ostream &operator<<( std::ostream &os, const Vec3D &vec );
257 };
258 
259 
260 inline double norm2( const Vec3D &vec ) {
261  return( vec.norm2() );
262 }
263 
264 
265 inline Vec3D cross( const Vec3D &vec1, const Vec3D &vec2 ) {
266  return( Vec3D( vec1[1] * vec2[2] - vec1[2] * vec2[1],
267  vec1[2] * vec2[0] - vec1[0] * vec2[2],
268  vec1[0] * vec2[1] - vec1[1] * vec2[0] ) );
269 }
270 
271 
272 inline Vec3D operator*( double x, const Vec3D &vec )
273 {
274  return( Vec3D( x*vec.p[0], x*vec.p[1], x*vec.p[2] ) );
275 }
276 
277 
278 inline std::ostream &operator<<( std::ostream &os, const Vec3D &vec )
279 {
280  os << std::setw(12) << to_string(vec[0]).substr(0,12) << " ";
281  os << std::setw(12) << to_string(vec[1]).substr(0,12) << " ";
282  os << std::setw(12) << to_string(vec[2]).substr(0,12);
283  return( os );
284 }
285 
286 
289 class Int3D {
290  int32_t l[3];
291 
292 public:
293 
294  Int3D() { l[0] = 0; l[1] = 0; l[2] = 0; }
295  Int3D( int32_t i ) { l[0] = i; l[1] = 0; l[2] = 0; }
296  Int3D( int32_t i, int32_t j ) { l[0] = i; l[1] = j; l[2] = 0; }
297  Int3D( int32_t i, int32_t j, int32_t k ) { l[0] = i; l[1] = j; l[2] = k; }
298  Int3D( std::istream &s ) {
299  l[0] = read_int32( s );
300  l[1] = read_int32( s );
301  l[2] = read_int32( s );
302  }
303  ~Int3D() {}
304 
305  int32_t &operator[]( int i ) { return( l[i] ); }
306  const int32_t &operator[]( int i ) const { return( l[i] ); }
307  int32_t &operator()( int i ) { return( l[i] ); }
308  const int32_t &operator()( int i ) const { return( l[i] ); }
309 
310  Int3D operator-( const Int3D &i ) {
311  return( Int3D( l[0] - i[0],
312  l[1] - i[1],
313  l[2] - i[2] ) );
314  }
315 
316  Vec3D operator*( double x ) {
317  return( Vec3D( x*l[0], x*l[1], x*l[2] ) );
318  }
319 
322  bool operator!=( const Int3D &i ) const {
323  if( l[0] != i.l[0] || l[1] != i.l[1] || l[2] != i.l[2] )
324  return( true );
325  return( false );
326  }
327 
330  bool operator==( const Int3D &i ) const {
331  if( l[0] == i.l[0] && l[1] == i.l[1] && l[2] == i.l[2] )
332  return( true );
333  return( false );
334  }
335 
336  void save( std::ostream &s ) const {
337  write_int32( s, l[0] );
338  write_int32( s, l[1] );
339  write_int32( s, l[2] );
340  }
341 
342  friend Vec3D operator*( double x, const Int3D &i );
343  friend std::ostream &operator<<( std::ostream &os, const Vec3D &vec );
344 };
345 
346 
347 inline Vec3D operator*( double x, const Int3D &i )
348 {
349  Vec3D res;
350  res[0] = x*i.l[0];
351  res[1] = x*i.l[1];
352  res[2] = x*i.l[2];
353  return( res );
354 }
355 
356 
357 inline std::ostream &operator<<( std::ostream &os, const Int3D &vec )
358 {
359  os << std::setw(12) << to_string(vec[0]).substr(0,12) << " ";
360  os << std::setw(12) << to_string(vec[1]).substr(0,12) << " ";
361  os << std::setw(12) << to_string(vec[2]).substr(0,12);
362  return( os );
363 }
364 
365 
366 #endif
367 
static Vec3D standard_basis(int i)
Returns standard basis vector i.
bool operator!=(const Vec3D &x) const
Inequality test.
Vec3D operator*(double x)
Definition: vec3d.hpp:316
Vec3D(double x, double y)
Definition: vec3d.hpp:66
Vec3D cross(const Vec3D &vec1, const Vec3D &vec2)
Definition: vec3d.hpp:265
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).
void write_int32(std::ostream &os, int32_t value)
Write int32_t value into stream os.
double & operator()(int i)
Definition: vec3d.hpp:80
std::ostream & operator<<(std::ostream &os, const Vec3D &vec)
Definition: vec3d.hpp:278
Vec3D & operator=(const Vec3D &x)
Assignment.
Definition: vec3d.hpp:169
const int32_t & operator[](int i) const
Definition: vec3d.hpp:306
Vec3D()
Definition: vec3d.hpp:64
Int3D(std::istream &s)
Definition: vec3d.hpp:298
double norm2(const Vec3D &vec)
Definition: vec3d.hpp:260
Vec3D arb_perpendicular(void) const
Returns arbitrary vector perpendicular to input vector.
bool operator!=(const Int3D &i) const
Inequality test.
Definition: vec3d.hpp:322
Vec3D(std::istream &s)
Definition: vec3d.hpp:71
friend Vec3D cross(const Vec3D &vec1, const Vec3D &vec2)
Cross product.
Definition: vec3d.hpp:265
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:278
double & operator[](int i)
Definition: vec3d.hpp:78
bool approx(const Vec3D &x, double eps=1.0e-6) const
Approximate equality test.
void write_double(std::ostream &os, double value)
Write double value into stream os.
void save(std::ostream &s) const
Definition: vec3d.hpp:336
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()
Definition: vec3d.hpp:303
int32_t & operator()(int i)
Definition: vec3d.hpp:307
Int3D operator-(const Int3D &i)
Definition: vec3d.hpp:310
void save(std::ostream &os) const
Saves data to stream os.
Definition: vec3d.hpp:228
const int32_t & operator()(int i) const
Definition: vec3d.hpp:308
Int3D(int32_t i)
Definition: vec3d.hpp:295
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:330
3D Integer vector class.
Definition: vec3d.hpp:289
Homogenous vectors for three dimensional space.
Vec3D(double x, double y, double z)
Definition: vec3d.hpp:67
Int3D(int32_t i, int32_t j, int32_t k)
Definition: vec3d.hpp:297
const double & operator()(int i) const
Definition: vec3d.hpp:81
int32_t read_int32(std::istream &is)
Read int32_t from stream is.
int32_t & operator[](int i)
Definition: vec3d.hpp:305
Vec3D(double x)
Definition: vec3d.hpp:65
friend std::ostream & operator<<(std::ostream &os, const Vec3D &vec)
Definition: vec3d.hpp:278
Vec3D operator*(double x) const
Vector scaling.
Definition: vec3d.hpp:118
Vec3D operator*(double x, const Vec3D &vec)
Definition: vec3d.hpp:272
~Vec3D()
Definition: vec3d.hpp:76
const double & operator[](int i) const
Definition: vec3d.hpp:79
Int3D(int32_t i, int32_t j)
Definition: vec3d.hpp:296
Int3D()
Definition: vec3d.hpp:294
Vec3D & operator=(const double &x)
Assignment of every coordinate.
Definition: vec3d.hpp:178
Three dimensional vector.
Definition: vec3d.hpp:58
double ssqr(void) const
Returns square of 2-norm of vector.
Definition: vec3d.hpp:214
bool operator==(const Vec3D &x) const
Equality test.
void abs(void)
Calculate absolute value of each component.
Definition: vec3d.hpp:187
double read_double(std::istream &is)
Readd double from stream is.
void normalize(void)
Normalize vector.
Definition: vec3d.hpp:195