CMU462 Library Documentation  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 <ostream>
5 #include <cmath>
6 
7 namespace CMU462 {
8 
12 class Vector3D {
13  public:
14 
15  // components
16  double x, y, z;
17 
22  Vector3D() : x( 0.0 ), y( 0.0 ), z( 0.0 ) { }
23 
28  Vector3D( double x, double y, double z) : x( x ), y( y ), z( z ) { }
29 
34  Vector3D( double c ) : x( c ), y( c ), z( c ) { }
35 
40  Vector3D( const Vector3D& v ) : x( v.x ), y( v.y ), z( v.z ) { }
41 
42  // returns reference to the specified component (0-based indexing: x, y, z)
43  inline double& operator[] ( const int& index ) {
44  return ( &x )[ index ];
45  }
46 
47  // returns const reference to the specified component (0-based indexing: x, y, z)
48  inline const double& operator[] ( const int& index ) const {
49  return ( &x )[ index ];
50  }
51 
52  // negation
53  inline Vector3D operator-( void ) const {
54  return Vector3D( -x, -y, -z );
55  }
56 
57  // addition
58  inline Vector3D operator+( const Vector3D& v ) const {
59  return Vector3D( x + v.x, y + v.y, z + v.z );
60  }
61 
62  // subtraction
63  inline Vector3D operator-( const Vector3D& v ) const {
64  return Vector3D( x - v.x, y - v.y, z - v.z );
65  }
66 
67  // right scalar multiplication
68  inline Vector3D operator*( const double& c ) const {
69  return Vector3D( x * c, y * c, z * c );
70  }
71 
72  // scalar division
73  inline Vector3D operator/( const double& c ) const {
74  const double rc = 1.0/c;
75  return Vector3D( rc * x, rc * y, rc * z );
76  }
77 
78  // addition / assignment
79  inline void operator+=( const Vector3D& v ) {
80  x += v.x; y += v.y; z += v.z;
81  }
82 
83  // subtraction / assignment
84  inline void operator-=( const Vector3D& v ) {
85  x -= v.x; y -= v.y; z -= v.z;
86  }
87 
88  // scalar multiplication / assignment
89  inline void operator*=( const double& c ) {
90  x *= c; y *= c; z *= c;
91  }
92 
93  // scalar division / assignment
94  inline void operator/=( const double& c ) {
95  (*this) *= ( 1./c );
96  }
97 
101  inline double norm( void ) const {
102  return sqrt( x*x + y*y + z*z );
103  }
104 
108  inline double norm2( void ) const {
109  return x*x + y*y + z*z;
110  }
111 
115  inline Vector3D unit( void ) const {
116  double rNorm = 1. / sqrt( x*x + y*y + z*z );
117  return Vector3D( rNorm*x, rNorm*y, rNorm*z );
118  }
119 
123  inline void normalize( void ) {
124  (*this) /= norm();
125  }
126 
127 }; // class Vector3D
128 
129 // left scalar multiplication
130 inline Vector3D operator* ( const double& c, const Vector3D& v ) {
131  return Vector3D( c * v.x, c * v.y, c * v.z );
132 }
133 
134 // dot product (a.k.a. inner or scalar product)
135 inline double dot( const Vector3D& u, const Vector3D& v ) {
136  return u.x*v.x + u.y*v.y + u.z*v.z ;
137 }
138 
139 // cross product
140 inline Vector3D cross( const Vector3D& u, const Vector3D& v ) {
141  return Vector3D( u.y*v.z - u.z*v.y,
142  u.z*v.x - u.x*v.z,
143  u.x*v.y - u.y*v.x );
144 }
145 
146 // prints components
147 std::ostream& operator<<( std::ostream& os, const Vector3D& v );
148 
149 } // namespace CMU462
150 
151 #endif // CMU462_VECTOR3D_H
Definition: color.cpp:10
Defines 3D vectors.
Definition: vector3D.h:12
Vector3D unit(void) const
Returns unit vector.
Definition: vector3D.h:115
double norm(void) const
Returns Euclidean length.
Definition: vector3D.h:101
Vector3D(double x, double y, double z)
Constructor.
Definition: vector3D.h:28
double norm2(void) const
Returns Euclidean length squared.
Definition: vector3D.h:108
void normalize(void)
Divides by Euclidean length.
Definition: vector3D.h:123
Vector3D(const Vector3D &v)
Constructor.
Definition: vector3D.h:40
Vector3D()
Constructor.
Definition: vector3D.h:22
Vector3D(double c)
Constructor.
Definition: vector3D.h:34