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 
79  Vec4D( const class Vec3D &vec );
80 
81  Vec4D( std::istream &s ) {
82  p[0] = read_double( s );
83  p[1] = read_double( s );
84  p[2] = read_double( s );
85  p[3] = read_double( s );
86  }
87  ~Vec4D() {}
88 
89  double &operator[]( int i ) { return( p[i] ); }
90  const double &operator[]( int i ) const { return( p[i] ); }
91  double &operator()( int i ) { return( p[i] ); }
92  const double &operator()( int i ) const { return( p[i] ); }
93 
99  Vec4D operator+( const Vec4D &vec ) const {
100  return( Vec4D( p[0] + vec[0],
101  p[1] + vec[1],
102  p[2] + vec[2],
103  (p[2] == vec[2] ? 0.0 : 1.0) ) );
104  }
105 
111  Vec4D operator-( const Vec4D &vec ) const {
112  return( Vec4D( p[0] - vec[0],
113  p[1] - vec[1],
114  p[2] - vec[2],
115  (p[2] == vec[2] ? 0.0 : 1.0) ) );
116  }
117 
123  Vec4D &operator+=( const Vec4D &vec ) {
124  p[0] += vec[0];
125  p[1] += vec[1];
126  p[2] += vec[2];
127  return( *this );
128  }
129 
134  double operator*( const Vec4D &vec ) const {
135  return( p[0] * vec[0] +
136  p[1] * vec[1] +
137  p[2] * vec[2] );
138  }
139 
144  Vec4D operator*( double x ) const {
145  return( Vec4D( x*p[0], x*p[1], x*p[2], p[3] ) );
146  }
147 
152  Vec4D &operator*=( double x ) {
153  p[0] *= x;
154  p[1] *= x;
155  p[2] *= x;
156  return( *this );
157  }
158 
163  Vec4D &operator/=( double x ) {
164  double div = 1.0/x;
165  p[0] *= div;
166  p[1] *= div;
167  p[2] *= div;
168  return( *this );
169  }
170 
175  bool operator!=( const Vec4D &x ) {
176  if( p[0] != x.p[0] || p[1] != x.p[1] || p[2] != x.p[2] || p[3] != x.p[3] )
177  return( true );
178  return( false );
179  }
180 
185  bool operator==( const Vec4D &x ) {
186  if( p[0] == x.p[0] && p[1] == x.p[1] && p[2] == x.p[2] && p[3] == x.p[3] )
187  return( true );
188  return( false );
189  }
190 
193  Vec4D &operator=( const Vec4D &x ) {
194  p[0] = x[0];
195  p[1] = x[1];
196  p[2] = x[2];
197  p[3] = x[3];
198  return( *this );
199  }
200 
206  void homogenize() {
207  double inv_w = 1.0/p[3];
208  p[0] *= inv_w;
209  p[1] *= inv_w;
210  p[2] *= inv_w;
211  p[3] = 1.0;
212  }
213 
218  void normalize() {
219  double inv_norm = 1.0/sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] );
220  p[0] *= inv_norm;
221  p[1] *= inv_norm;
222  p[2] *= inv_norm;
223  p[3] = 0.0;
224  }
225 
230  double norm2() const {
231  return( sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ) );
232  }
233 
238  double ssqr() const {
239  return( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] );
240  }
241 
242  void save( std::ostream &s ) const {
243  write_double( s, p[0] );
244  write_double( s, p[1] );
245  write_double( s, p[2] );
246  write_double( s, p[3] );
247  }
248 
253  friend Vec4D cross( const Vec4D &vec1, const Vec4D &vec2 );
254 
257  friend double norm2( const Vec4D &vec );
258 
263  friend Vec4D operator*( double x, const Vec4D &vec );
264 
267  friend std::ostream &operator<<( std::ostream &os, const Vec4D &vec );
268 };
269 
270 
271 inline double norm2( const Vec4D &vec ) {
272  return( vec.norm2() );
273 }
274 
275 inline Vec4D cross( const Vec4D &vec1, const Vec4D &vec2 ) {
276  return( Vec4D( vec1[1] * vec2[2] - vec1[2] * vec2[1],
277  vec1[2] * vec2[0] - vec1[0] * vec2[2],
278  vec1[0] * vec2[1] - vec1[1] * vec2[0],
279  0.0 ) );
280 }
281 
282 
283 inline Vec4D operator*( double x, const Vec4D &vec )
284 {
285  return( Vec4D( x*vec[0], x*vec[1], x*vec[2], vec[3] ) );
286 }
287 
288 
289 inline std::ostream &operator<<( std::ostream &os, const Vec4D &vec )
290 {
291  os << std::setw(12) << to_string(vec[0]).substr(0,12) << " ";
292  os << std::setw(12) << to_string(vec[1]).substr(0,12) << " ";
293  os << std::setw(12) << to_string(vec[2]).substr(0,12) << " ";
294  os << std::setw(12) << to_string(vec[3]).substr(0,12);
295  return( os );
296 }
297 
298 
299 #endif
300 
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
316 
317 
318 
Vec4D operator+(const Vec4D &vec) const
Addition.
Definition: vec4d.hpp:99
void homogenize()
Homogenize vector.
Definition: vec4d.hpp:206
Vec4D operator*(double x) const
Vector scaling.
Definition: vec4d.hpp:144
Vec3D cross(const Vec3D &vec1, const Vec3D &vec2)
Definition: vec3d.hpp:265
const double & operator()(int i) const
Definition: vec4d.hpp:92
Vec4D(double x)
Definition: vec4d.hpp:74
Vec4D(double x, double y, double z)
Definition: vec4d.hpp:76
double norm2(const Vec3D &vec)
Definition: vec3d.hpp:260
double & operator()(int i)
Definition: vec4d.hpp:91
double & operator[](int i)
Definition: vec4d.hpp:89
Vec4D & operator/=(double x)
Vector scaling with divisor.
Definition: vec4d.hpp:163
Bindary file writing and reading tools.
double operator*(const Vec4D &vec) const
Dot product.
Definition: vec4d.hpp:134
std::string to_string(const T &t)
Function for converting a type to string.
Definition: error.hpp:62
~Vec4D()
Definition: vec4d.hpp:87
const double & operator[](int i) const
Definition: vec4d.hpp:90
Three dimensional vectors.
Vec4D(std::istream &s)
Definition: vec4d.hpp:81
double ssqr() const
Returns square of 2-norm of vector.
Definition: vec4d.hpp:238
friend Vec4D cross(const Vec4D &vec1, const Vec4D &vec2)
Cross product.
Definition: vec4d.hpp:275
void write_double(std::ostream &os, double value)
Write double value into stream os.
bool operator!=(const Vec4D &x)
Inequality test.
Definition: vec4d.hpp:175
double norm2() const
Returns 2-norm of vector.
Definition: vec4d.hpp:230
Vec4D & operator=(const Vec4D &x)
Assignment.
Definition: vec4d.hpp:193
Vec4D & operator+=(const Vec4D &vec)
Accumulation.
Definition: vec4d.hpp:123
void normalize()
Normalize vector.
Definition: vec4d.hpp:218
Vec4D(double x, double y, double z, double w)
Definition: vec4d.hpp:77
Color operator*(double x, const Color &c)
Definition: color.hpp:116
bool operator==(const Vec4D &x)
Equality test.
Definition: vec4d.hpp:185
Homogenous vector for three dimensional space.
Definition: vec4d.hpp:67
Error classes and handling
Vec4D & operator*=(double x)
Vector scaling.
Definition: vec4d.hpp:152
Vec4D()
Definition: vec4d.hpp:73
Vec4D operator-(const Vec4D &vec) const
Difference.
Definition: vec4d.hpp:111
std::ostream & operator<<(std::ostream &os, const Color &c)
Definition: color.hpp:122
friend std::ostream & operator<<(std::ostream &os, const Vec4D &vec)
Outputting to stream.
Definition: vec4d.hpp:289
Three dimensional vector.
Definition: vec3d.hpp:58
void save(std::ostream &s) const
Definition: vec4d.hpp:242
double read_double(std::istream &is)
Readd double from stream is.
Vec4D(double x, double y)
Definition: vec4d.hpp:75