CMU462 Library  1.0
15-462/15-662: Computer Graphics (Fall 2015)
vector3D.h
1 #ifndef CMU462_VECTOR3D_H
2 #define CMU462_VECTOR3D_H
3 
4 #include "CMU462.h"
5 
6 #include <ostream>
7 #include <cmath>
8 
9 namespace CMU462 {
10 
14 class Vector3D {
15  public:
16 
17  // components
18  double x, y, z;
19 
24  Vector3D() : x( 0.0 ), y( 0.0 ), z( 0.0 ) { }
25 
30  Vector3D( double x, double y, double z) : x( x ), y( y ), z( z ) { }
31 
36  Vector3D( double c ) : x( c ), y( c ), z( c ) { }
37 
42  Vector3D( const Vector3D& v ) : x( v.x ), y( v.y ), z( v.z ) { }
43 
44  // returns reference to the specified component (0-based indexing: x, y, z)
45  inline double& operator[] ( const int& index ) {
46  return ( &x )[ index ];
47  }
48 
49  // returns const reference to the specified component (0-based indexing: x, y, z)
50  inline const double& operator[] ( const int& index ) const {
51  return ( &x )[ index ];
52  }
53 
54  inline bool operator==( const Vector3D& v) const {
55  return v.x == x && v.y == y && v.z == z;
56  }
57 
58  // negation
59  inline Vector3D operator-( void ) const {
60  return Vector3D( -x, -y, -z );
61  }
62 
63  // addition
64  inline Vector3D operator+( const Vector3D& v ) const {
65  return Vector3D( x + v.x, y + v.y, z + v.z );
66  }
67 
68  // subtraction
69  inline Vector3D operator-( const Vector3D& v ) const {
70  return Vector3D( x - v.x, y - v.y, z - v.z );
71  }
72 
73  // right scalar multiplication
74  inline Vector3D operator*( const double& c ) const {
75  return Vector3D( x * c, y * c, z * c );
76  }
77 
78  // scalar division
79  inline Vector3D operator/( const double& c ) const {
80  const double rc = 1.0/c;
81  return Vector3D( rc * x, rc * y, rc * z );
82  }
83 
84  // addition / assignment
85  inline void operator+=( const Vector3D& v ) {
86  x += v.x; y += v.y; z += v.z;
87  }
88 
89  // subtraction / assignment
90  inline void operator-=( const Vector3D& v ) {
91  x -= v.x; y -= v.y; z -= v.z;
92  }
93 
94  // scalar multiplication / assignment
95  inline void operator*=( const double& c ) {
96  x *= c; y *= c; z *= c;
97  }
98 
99  // scalar division / assignment
100  inline void operator/=( const double& c ) {
101  (*this) *= ( 1./c );
102  }
103 
107  inline double norm( void ) const {
108  return sqrt( x*x + y*y + z*z );
109  }
110 
114  inline double norm2( void ) const {
115  return x*x + y*y + z*z;
116  }
117 
121  inline Vector3D unit( void ) const {
122  double rNorm = 1. / sqrt( x*x + y*y + z*z );
123  return Vector3D( rNorm*x, rNorm*y, rNorm*z );
124  }
125 
129  inline void normalize( void ) {
130  (*this) /= norm();
131  }
132 
133 }; // class Vector3D
134 
135 // left scalar multiplication
136 inline Vector3D operator* ( const double& c, const Vector3D& v ) {
137  return Vector3D( c * v.x, c * v.y, c * v.z );
138 }
139 
140 // dot product (a.k.a. inner or scalar product)
141 inline double dot( const Vector3D& u, const Vector3D& v ) {
142  return u.x*v.x + u.y*v.y + u.z*v.z ;
143 }
144 
145 // cross product
146 inline Vector3D cross( const Vector3D& u, const Vector3D& v ) {
147  return Vector3D( u.y*v.z - u.z*v.y,
148  u.z*v.x - u.x*v.z,
149  u.x*v.y - u.y*v.x );
150 }
151 
152 // prints components
153 std::ostream& operator<<( std::ostream& os, const Vector3D& v );
154 
155 } // namespace CMU462
156 
157 #endif // CMU462_VECTOR3D_H
Definition: CMU462.h:8
Defines 3D vectors.
Definition: vector3D.h:14
Vector3D unit(void) const
Returns unit vector.
Definition: vector3D.h:121
double norm(void) const
Returns Euclidean length.
Definition: vector3D.h:107
Vector3D(double x, double y, double z)
Constructor.
Definition: vector3D.h:30
double norm2(void) const
Returns Euclidean length squared.
Definition: vector3D.h:114
void normalize(void)
Divides by Euclidean length.
Definition: vector3D.h:129
Vector3D(const Vector3D &v)
Constructor.
Definition: vector3D.h:42
Vector3D()
Constructor.
Definition: vector3D.h:24
Vector3D(double c)
Constructor.
Definition: vector3D.h:36