1 #ifndef CMU462_VECTOR3D_H
2 #define CMU462_VECTOR3D_H
30 Vector3D(
double x,
double y,
double z) : x( x ), y( y ), z( z ) { }
36 Vector3D(
double c ) : x( c ), y( c ), z( c ) { }
45 inline double& operator[] (
const int& index ) {
46 return ( &x )[ index ];
50 inline const double& operator[] (
const int& index )
const {
51 return ( &x )[ index ];
54 inline bool operator==(
const Vector3D& v)
const {
55 return v.x == x && v.y == y && v.z == z;
59 inline Vector3D operator-(
void )
const {
65 return Vector3D( x + v.x, y + v.y, z + v.z );
70 return Vector3D( x - v.x, y - v.y, z - v.z );
74 inline Vector3D operator*(
const double& c )
const {
75 return Vector3D( x * c, y * c, z * c );
79 inline Vector3D operator/(
const double& c )
const {
80 const double rc = 1.0/c;
81 return Vector3D( rc * x, rc * y, rc * z );
85 inline void operator+=(
const Vector3D& v ) {
86 x += v.x; y += v.y; z += v.z;
90 inline void operator-=(
const Vector3D& v ) {
91 x -= v.x; y -= v.y; z -= v.z;
95 inline void operator*=(
const double& c ) {
96 x *= c; y *= c; z *= c;
100 inline void operator/=(
const double& c ) {
107 inline double norm(
void )
const {
108 return sqrt( x*x + y*y + z*z );
115 return x*x + y*y + z*z;
122 double rNorm = 1. / sqrt( x*x + y*y + z*z );
123 return Vector3D( rNorm*x, rNorm*y, rNorm*z );
136 inline Vector3D operator* (
const double& c,
const Vector3D& v ) {
137 return Vector3D( c * v.x, c * v.y, c * v.z );
141 inline double dot(
const Vector3D& u,
const Vector3D& v ) {
142 return u.x*v.x + u.y*v.y + u.z*v.z ;
146 inline Vector3D cross(
const Vector3D& u,
const Vector3D& v ) {
147 return Vector3D( u.y*v.z - u.z*v.y,
153 std::ostream& operator<<( std::ostream& os,
const Vector3D& v );
157 #endif // CMU462_VECTOR3D_H
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