CMU462 Library  1.0
15-462/15-662: Computer Graphics (Fall 2015)
vector4D.h
1 #ifndef CMU462_VECTOR4D_H
2 #define CMU462_VECTOR4D_H
3 
4 #include "CMU462.h"
5 #include "vector3D.h"
6 
7 #include <ostream>
8 #include <cmath>
9 
10 namespace CMU462 {
11 
15 class Vector4D {
16  public:
17 
18  // components
19  double x, y, z, w;
20 
25  Vector4D() : x( 0.0 ), y( 0.0 ), z( 0.0 ), w( 0.0 ) { }
26 
31  Vector4D( double x, double y, double z, double w) : x( x ), y( y ), z( z ), w( w ) { }
32 
37  Vector4D( double x, double y, double z) : x( x ), y( y ), z( z ), w( 0.0 ) { }
38 
39 
44  Vector4D( double c ) : x( c ), y( c ), z( c ), w ( c ) { }
45 
50  Vector4D( const Vector4D& v ) : x( v.x ), y( v.y ), z( v.z ), w( v.w ) { }
51 
56  Vector4D( const Vector3D& v ) : x( v.x ), y( v.y ), z( v.z ), w( 0.0 ) { }
57 
62  Vector4D( const Vector3D& v, double w ) : x( v.x ), y( v.y ), z( v.z ), w( w ) { }
63 
64  // returns reference to the specified component (0-based indexing: x, y, z)
65  inline double& operator[] ( const int& index ) {
66  return ( &x )[ index ];
67  }
68 
69  // returns const reference to the specified component (0-based indexing: x, y, z)
70  inline const double& operator[] ( const int& index ) const {
71  return ( &x )[ index ];
72  }
73 
74  // negation
75  inline Vector4D operator-( void ) const {
76  return Vector4D( -x, -y, -z, -w);
77  }
78 
79  // addition
80  inline Vector4D operator+( const Vector4D& v ) const {
81  return Vector4D( x + v.x, y + v.y, z + v.z, w + v.w);
82  }
83 
84  // subtraction
85  inline Vector4D operator-( const Vector4D& v ) const {
86  return Vector4D( x - v.x, y - v.y, z - v.z, w - v.w );
87  }
88 
89  // right scalar multiplication
90  inline Vector4D operator*( const double& c ) const {
91  return Vector4D( x * c, y * c, z * c, w * c );
92  }
93 
94  // scalar division
95  inline Vector4D operator/( const double& c ) const {
96  const double rc = 1.0/c;
97  return Vector4D( rc * x, rc * y, rc * z, rc * w );
98  }
99 
100  // addition / assignment
101  inline void operator+=( const Vector4D& v ) {
102  x += v.x; y += v.y; z += v.z; z += v.w;
103  }
104 
105  // subtraction / assignment
106  inline void operator-=( const Vector4D& v ) {
107  x -= v.x; y -= v.y; z -= v.z; w -= v.w;
108  }
109 
110  // scalar multiplication / assignment
111  inline void operator*=( const double& c ) {
112  x *= c; y *= c; z *= c; w *= c;
113  }
114 
115  // scalar division / assignment
116  inline void operator/=( const double& c ) {
117  (*this) *= ( 1./c );
118  }
119 
123  inline double norm( void ) const {
124  return sqrt( x*x + y*y + z*z + w*w );
125  }
126 
130  inline double norm2( void ) const {
131  return x*x + y*y + z*z + w*w;
132  }
133 
137  inline Vector4D unit( void ) const {
138  double rNorm = 1. / sqrt( x*x + y*y + z*z + w*w);
139  return Vector4D( rNorm*x, rNorm*y, rNorm*z );
140  }
141 
146  inline void normalize( void ) {
147  (*this) /= norm();
148  }
149 
153  Vector3D to3D();
154 
159 
160 }; // class Vector4D
161 
162 // left scalar multiplication
163 inline Vector4D operator* ( const double& c, const Vector4D& v ) {
164  return Vector4D( c * v.x, c * v.y, c * v.z, c*v.w );
165 }
166 
167 // dot product (a.k.a. inner or scalar product)
168 inline double dot( const Vector4D& u, const Vector4D& v ) {
169  return u.x*v.x + u.y*v.y + u.z*v.z + u.w*v.w;;
170 }
171 
172 // prints components
173 std::ostream& operator<<( std::ostream& os, const Vector4D& v );
174 
175 } // namespace CMU462
176 
177 #endif // CMU462_VECTOR3D_H
Vector4D(double x, double y, double z, double w)
Constructor.
Definition: vector4D.h:31
Vector3D to3D()
Converts this vector to a 3D vector ignoring the w component.
Definition: vector4D.cpp:10
Vector3D projectTo3D()
Converts this vector to a 3D vector by dividing x, y, and z by w.
Definition: vector4D.cpp:14
Vector4D unit(void) const
Returns unit vector.
Definition: vector4D.h:137
Definition: CMU462.h:8
Defines 4D standard vectors.
Definition: vector4D.h:15
Defines 3D vectors.
Definition: vector3D.h:14
double norm(void) const
Returns Euclidean distance metric extended to 4 dimensions.
Definition: vector4D.h:123
Vector4D(const Vector4D &v)
Constructor.
Definition: vector4D.h:50
Vector4D(double x, double y, double z)
Constructor.
Definition: vector4D.h:37
double norm2(void) const
Returns Euclidean length squared.
Definition: vector4D.h:130
Vector4D(const Vector3D &v, double w)
Constructor.
Definition: vector4D.h:62
Vector4D(const Vector3D &v)
Constructor.
Definition: vector4D.h:56
Vector4D(double c)
Constructor.
Definition: vector4D.h:44
Vector4D()
Constructor.
Definition: vector4D.h:25
void normalize(void)
Divides by Euclidean length.
Definition: vector4D.h:146