-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector3D.h
189 lines (122 loc) · 4.79 KB
/
Vector3D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#ifndef __VECTOR_3D__
#define __VECTOR_3D__
// This file contains the declaration of the class Vector3D
#include "Matrix.h"
class Normal;
class Point3D;
//----------------------------------------- class Vector3D
class Vector3D {
public:
double x, y, z;
public:
Vector3D(void); // default constructor
Vector3D(double a); // constructor
Vector3D(double _x, double _y, double _z); // constructor
Vector3D(const Vector3D& v); // copy constructor
Vector3D(const Normal& n); // constructs a vector from a Normal
Vector3D(const Point3D& p); // constructs a vector from a point
~Vector3D (void); // destructor
Vector3D& // assignment operator
operator= (const Vector3D& rhs);
Vector3D& // assign a Normal to a vector
operator= (const Normal& rhs);
Vector3D& // assign a Point3D to a vector
operator= (const Point3D& rhs);
Vector3D // unary minus
operator- (void) const;
double // length
length(void);
double // square of the length
len_squared(void);
Vector3D // multiplication by a double on the right
operator* (const double a) const;
Vector3D // division by a double
operator/ (const double a) const;
Vector3D // addition
operator+ (const Vector3D& v) const;
Vector3D& // compound addition
operator+= (const Vector3D& v);
Vector3D // subtraction
operator- (const Vector3D& v) const;
double // dot product
operator* (const Vector3D& b) const;
Vector3D // cross product
operator^ (const Vector3D& v) const;
void // convert vector to a unit vector
normalize(void);
Vector3D& // return a unit vector, and normalize the vector
hat(void);
};
// inlined member functions
// ------------------------------------------------------------------------ unary minus
// this does not change the current vector
// this allows ShadeRec objects to be declared as constant arguments in many shading
// functions that reverse the direction of a ray that's stored in the ShadeRec object
inline Vector3D
Vector3D::operator- (void) const {
return (Vector3D(-x, -y, -z));
}
// --------------------------------------------------------------------- len_squared
// the square of the length
inline double
Vector3D::len_squared(void) {
return (x * x + y * y + z * z);
}
// ----------------------------------------------------------------------- operator*
// multiplication by a double on the right
inline Vector3D
Vector3D::operator* (const double a) const {
return (Vector3D(x * a, y * a, z * a));
}
// ----------------------------------------------------------------------- operator/
// division by a double
inline Vector3D
Vector3D::operator/ (const double a) const {
return (Vector3D(x / a, y / a, z / a));
}
// ----------------------------------------------------------------------- operator+
// addition
inline Vector3D
Vector3D::operator+ (const Vector3D& v) const {
return (Vector3D(x + v.x, y + v.y, z + v.z));
}
// ----------------------------------------------------------------------- operator-
// subtraction
inline Vector3D
Vector3D::operator- (const Vector3D& v) const {
return (Vector3D(x - v.x, y - v.y, z - v.z));
}
// ----------------------------------------------------------------------- operator*
// dot product
inline double
Vector3D::operator* (const Vector3D& v) const {
return (x * v.x + y * v.y + z * v.z);
}
// ----------------------------------------------------------------------- operator^
// cross product
inline Vector3D
Vector3D::operator^ (const Vector3D& v) const {
return (Vector3D(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x));
}
// --------------------------------------------------------------------- operator+=
// compound addition
inline Vector3D&
Vector3D::operator+= (const Vector3D& v) {
x += v.x; y += v.y; z += v.z;
return (*this);
}
// inlined non-member function
// ----------------------------------------------------------------------- operator*
// multiplication by a double on the left
Vector3D
operator* (const double a, const Vector3D& v);
inline Vector3D
operator* (const double a, const Vector3D& v) {
return (Vector3D(a * v.x, a * v.y, a * v.z));
}
// non-inlined non-member function
// ----------------------------------------------------------------------- operator*
// multiplication by a matrix on the left
Vector3D
operator* (const Matrix& mat, const Vector3D& v);
#endif