1 #ifndef CMU462_VECTOR3D_H
2 #define CMU462_VECTOR3D_H
28 Vector3D(
double x,
double y,
double z) : x( x ), y( y ), z( z ) { }
34 Vector3D(
double c ) : x( c ), y( c ), z( c ) { }
43 inline double& operator[] (
const int& index ) {
44 return ( &x )[ index ];
48 inline const double& operator[] (
const int& index )
const {
49 return ( &x )[ index ];
53 inline Vector3D operator-(
void )
const {
59 return Vector3D( x + v.x, y + v.y, z + v.z );
64 return Vector3D( x - v.x, y - v.y, z - v.z );
68 inline Vector3D operator*(
const double& c )
const {
69 return Vector3D( x * c, y * c, z * c );
73 inline Vector3D operator/(
const double& c )
const {
74 const double rc = 1.0/c;
75 return Vector3D( rc * x, rc * y, rc * z );
79 inline void operator+=(
const Vector3D& v ) {
80 x += v.x; y += v.y; z += v.z;
84 inline void operator-=(
const Vector3D& v ) {
85 x -= v.x; y -= v.y; z -= v.z;
89 inline void operator*=(
const double& c ) {
90 x *= c; y *= c; z *= c;
94 inline void operator/=(
const double& c ) {
101 inline double norm(
void )
const {
102 return sqrt( x*x + y*y + z*z );
109 return x*x + y*y + z*z;
116 double rNorm = 1. / sqrt( x*x + y*y + z*z );
117 return Vector3D( rNorm*x, rNorm*y, rNorm*z );
130 inline Vector3D operator* (
const double& c,
const Vector3D& v ) {
131 return Vector3D( c * v.x, c * v.y, c * v.z );
135 inline double dot(
const Vector3D& u,
const Vector3D& v ) {
136 return u.x*v.x + u.y*v.y + u.z*v.z ;
140 inline Vector3D cross(
const Vector3D& u,
const Vector3D& v ) {
141 return Vector3D( u.y*v.z - u.z*v.y,
147 std::ostream& operator<<( std::ostream& os,
const Vector3D& v );
151 #endif // CMU462_VECTOR3D_H
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