rev |
line source |
nuclear@1
|
1 #include "vector.h"
|
nuclear@1
|
2 #include "vmath.h"
|
nuclear@1
|
3
|
nuclear@1
|
4 // ---------- Vector2 -----------
|
nuclear@1
|
5
|
nuclear@1
|
6 Vector2::Vector2(scalar_t x, scalar_t y)
|
nuclear@1
|
7 {
|
nuclear@1
|
8 this->x = x;
|
nuclear@1
|
9 this->y = y;
|
nuclear@1
|
10 }
|
nuclear@1
|
11
|
nuclear@1
|
12 Vector2::Vector2(const vec2_t &vec)
|
nuclear@1
|
13 {
|
nuclear@1
|
14 x = vec.x;
|
nuclear@1
|
15 y = vec.y;
|
nuclear@1
|
16 }
|
nuclear@1
|
17
|
nuclear@1
|
18 Vector2::Vector2(const Vector3 &vec)
|
nuclear@1
|
19 {
|
nuclear@1
|
20 x = vec.x;
|
nuclear@1
|
21 y = vec.y;
|
nuclear@1
|
22 }
|
nuclear@1
|
23
|
nuclear@1
|
24 Vector2::Vector2(const Vector4 &vec)
|
nuclear@1
|
25 {
|
nuclear@1
|
26 x = vec.x;
|
nuclear@1
|
27 y = vec.y;
|
nuclear@1
|
28 }
|
nuclear@1
|
29
|
nuclear@1
|
30 void Vector2::normalize()
|
nuclear@1
|
31 {
|
nuclear@1
|
32 scalar_t len = length();
|
nuclear@1
|
33 x /= len;
|
nuclear@1
|
34 y /= len;
|
nuclear@1
|
35 }
|
nuclear@1
|
36
|
nuclear@1
|
37 Vector2 Vector2::normalized() const
|
nuclear@1
|
38 {
|
nuclear@1
|
39 scalar_t len = length();
|
nuclear@1
|
40 return Vector2(x / len, y / len);
|
nuclear@1
|
41 }
|
nuclear@1
|
42
|
nuclear@1
|
43 void Vector2::transform(const Matrix3x3 &mat)
|
nuclear@1
|
44 {
|
nuclear@1
|
45 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2];
|
nuclear@1
|
46 y = mat[1][0] * x + mat[1][1] * y + mat[1][2];
|
nuclear@1
|
47 x = nx;
|
nuclear@1
|
48 }
|
nuclear@1
|
49
|
nuclear@1
|
50 Vector2 Vector2::transformed(const Matrix3x3 &mat) const
|
nuclear@1
|
51 {
|
nuclear@1
|
52 Vector2 vec;
|
nuclear@1
|
53 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2];
|
nuclear@1
|
54 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2];
|
nuclear@1
|
55 return vec;
|
nuclear@1
|
56 }
|
nuclear@1
|
57
|
nuclear@1
|
58 void Vector2::rotate(scalar_t angle)
|
nuclear@1
|
59 {
|
nuclear@1
|
60 *this = Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
|
nuclear@1
|
61 }
|
nuclear@1
|
62
|
nuclear@1
|
63 Vector2 Vector2::rotated(scalar_t angle) const
|
nuclear@1
|
64 {
|
nuclear@1
|
65 return Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
|
nuclear@1
|
66 }
|
nuclear@1
|
67
|
nuclear@1
|
68 Vector2 Vector2::reflection(const Vector2 &normal) const
|
nuclear@1
|
69 {
|
nuclear@1
|
70 return 2.0 * dot_product(*this, normal) * normal - *this;
|
nuclear@1
|
71 }
|
nuclear@1
|
72
|
nuclear@1
|
73 Vector2 Vector2::refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const
|
nuclear@1
|
74 {
|
nuclear@1
|
75 // quick and dirty implementation :)
|
nuclear@1
|
76 Vector3 v3refr = Vector3(this->x, this->y, 1.0).refraction(Vector3(this->x, this->y, 1), src_ior, dst_ior);
|
nuclear@1
|
77 return Vector2(v3refr.x, v3refr.y);
|
nuclear@1
|
78 }
|
nuclear@1
|
79
|
nuclear@1
|
80 std::ostream &operator <<(std::ostream &out, const Vector2 &vec)
|
nuclear@1
|
81 {
|
nuclear@1
|
82 out << "[" << vec.x << " " << vec.y << "]";
|
nuclear@1
|
83 return out;
|
nuclear@1
|
84 }
|
nuclear@1
|
85
|
nuclear@1
|
86
|
nuclear@1
|
87
|
nuclear@1
|
88 // --------- Vector3 ----------
|
nuclear@1
|
89
|
nuclear@1
|
90 Vector3::Vector3(scalar_t x, scalar_t y, scalar_t z)
|
nuclear@1
|
91 {
|
nuclear@1
|
92 this->x = x;
|
nuclear@1
|
93 this->y = y;
|
nuclear@1
|
94 this->z = z;
|
nuclear@1
|
95 }
|
nuclear@1
|
96
|
nuclear@1
|
97 Vector3::Vector3(const vec3_t &vec)
|
nuclear@1
|
98 {
|
nuclear@1
|
99 x = vec.x;
|
nuclear@1
|
100 y = vec.y;
|
nuclear@1
|
101 z = vec.z;
|
nuclear@1
|
102 }
|
nuclear@1
|
103
|
nuclear@1
|
104 Vector3::Vector3(const Vector2 &vec)
|
nuclear@1
|
105 {
|
nuclear@1
|
106 x = vec.x;
|
nuclear@1
|
107 y = vec.y;
|
nuclear@1
|
108 z = 1;
|
nuclear@1
|
109 }
|
nuclear@1
|
110
|
nuclear@1
|
111 Vector3::Vector3(const Vector4 &vec)
|
nuclear@1
|
112 {
|
nuclear@1
|
113 x = vec.x;
|
nuclear@1
|
114 y = vec.y;
|
nuclear@1
|
115 z = vec.z;
|
nuclear@1
|
116 }
|
nuclear@1
|
117
|
nuclear@1
|
118 Vector3::Vector3(const SphVector &sph)
|
nuclear@1
|
119 {
|
nuclear@1
|
120 *this = sph;
|
nuclear@1
|
121 }
|
nuclear@1
|
122
|
nuclear@1
|
123 Vector3 &Vector3::operator =(const SphVector &sph)
|
nuclear@1
|
124 {
|
nuclear@1
|
125 x = sph.r * cos(sph.theta) * sin(sph.phi);
|
nuclear@1
|
126 z = sph.r * sin(sph.theta) * sin(sph.phi);
|
nuclear@1
|
127 y = sph.r * cos(sph.phi);
|
nuclear@1
|
128 return *this;
|
nuclear@1
|
129 }
|
nuclear@1
|
130
|
nuclear@1
|
131 void Vector3::normalize()
|
nuclear@1
|
132 {
|
nuclear@1
|
133 scalar_t len = length();
|
nuclear@1
|
134 x /= len;
|
nuclear@1
|
135 y /= len;
|
nuclear@1
|
136 z /= len;
|
nuclear@1
|
137 }
|
nuclear@1
|
138
|
nuclear@1
|
139 Vector3 Vector3::normalized() const
|
nuclear@1
|
140 {
|
nuclear@1
|
141 scalar_t len = length();
|
nuclear@1
|
142 return Vector3(x / len, y / len, z / len);
|
nuclear@1
|
143 }
|
nuclear@1
|
144
|
nuclear@1
|
145 Vector3 Vector3::reflection(const Vector3 &normal) const
|
nuclear@1
|
146 {
|
nuclear@1
|
147 return 2.0 * dot_product(*this, normal) * normal - *this;
|
nuclear@1
|
148 }
|
nuclear@1
|
149
|
nuclear@1
|
150 Vector3 Vector3::refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const
|
nuclear@1
|
151 {
|
nuclear@1
|
152 return refraction(normal, src_ior / dst_ior);
|
nuclear@1
|
153 }
|
nuclear@1
|
154
|
nuclear@1
|
155 Vector3 Vector3::refraction(const Vector3 &normal, scalar_t ior) const
|
nuclear@1
|
156 {
|
nuclear@1
|
157 scalar_t cos_inc = dot_product(*this, -normal);
|
nuclear@1
|
158
|
nuclear@1
|
159 scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0);
|
nuclear@1
|
160
|
nuclear@1
|
161 if(radical < 0.0) { // total internal reflection
|
nuclear@1
|
162 return -reflection(normal);
|
nuclear@1
|
163 }
|
nuclear@1
|
164
|
nuclear@1
|
165 scalar_t beta = ior * cos_inc - sqrt(radical);
|
nuclear@1
|
166
|
nuclear@1
|
167 return *this * ior + normal * beta;
|
nuclear@1
|
168 }
|
nuclear@1
|
169
|
nuclear@1
|
170 void Vector3::transform(const Matrix3x3 &mat)
|
nuclear@1
|
171 {
|
nuclear@1
|
172 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
|
nuclear@1
|
173 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
|
nuclear@1
|
174 z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
|
nuclear@1
|
175 x = nx;
|
nuclear@1
|
176 y = ny;
|
nuclear@1
|
177 }
|
nuclear@1
|
178
|
nuclear@1
|
179 Vector3 Vector3::transformed(const Matrix3x3 &mat) const
|
nuclear@1
|
180 {
|
nuclear@1
|
181 Vector3 vec;
|
nuclear@1
|
182 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
|
nuclear@1
|
183 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
|
nuclear@1
|
184 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
|
nuclear@1
|
185 return vec;
|
nuclear@1
|
186 }
|
nuclear@1
|
187
|
nuclear@1
|
188 void Vector3::transform(const Matrix4x4 &mat)
|
nuclear@1
|
189 {
|
nuclear@1
|
190 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
|
nuclear@1
|
191 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
|
nuclear@1
|
192 z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
|
nuclear@1
|
193 x = nx;
|
nuclear@1
|
194 y = ny;
|
nuclear@1
|
195 }
|
nuclear@1
|
196
|
nuclear@1
|
197 Vector3 Vector3::transformed(const Matrix4x4 &mat) const
|
nuclear@1
|
198 {
|
nuclear@1
|
199 Vector3 vec;
|
nuclear@1
|
200 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
|
nuclear@1
|
201 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
|
nuclear@1
|
202 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
|
nuclear@1
|
203 return vec;
|
nuclear@1
|
204 }
|
nuclear@1
|
205
|
nuclear@1
|
206 void Vector3::transform(const Quaternion &quat)
|
nuclear@1
|
207 {
|
nuclear@1
|
208 Quaternion vq(0.0f, *this);
|
nuclear@1
|
209 vq = quat * vq * quat.inverse();
|
nuclear@1
|
210 *this = vq.v;
|
nuclear@1
|
211 }
|
nuclear@1
|
212
|
nuclear@1
|
213 Vector3 Vector3::transformed(const Quaternion &quat) const
|
nuclear@1
|
214 {
|
nuclear@1
|
215 Quaternion vq(0.0f, *this);
|
nuclear@1
|
216 vq = quat * vq * quat.inverse();
|
nuclear@1
|
217 return vq.v;
|
nuclear@1
|
218 }
|
nuclear@1
|
219
|
nuclear@1
|
220 void Vector3::rotate(const Vector3 &euler)
|
nuclear@1
|
221 {
|
nuclear@1
|
222 Matrix4x4 rot;
|
nuclear@1
|
223 rot.set_rotation(euler);
|
nuclear@1
|
224 transform(rot);
|
nuclear@1
|
225 }
|
nuclear@1
|
226
|
nuclear@1
|
227 Vector3 Vector3::rotated(const Vector3 &euler) const
|
nuclear@1
|
228 {
|
nuclear@1
|
229 Matrix4x4 rot;
|
nuclear@1
|
230 rot.set_rotation(euler);
|
nuclear@1
|
231 return transformed(rot);
|
nuclear@1
|
232 }
|
nuclear@1
|
233
|
nuclear@1
|
234 std::ostream &operator <<(std::ostream &out, const Vector3 &vec)
|
nuclear@1
|
235 {
|
nuclear@1
|
236 out << "[" << vec.x << " " << vec.y << " " << vec.z << "]";
|
nuclear@1
|
237 return out;
|
nuclear@1
|
238 }
|
nuclear@1
|
239
|
nuclear@1
|
240
|
nuclear@1
|
241 // -------------- Vector4 --------------
|
nuclear@1
|
242 Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w)
|
nuclear@1
|
243 {
|
nuclear@1
|
244 this->x = x;
|
nuclear@1
|
245 this->y = y;
|
nuclear@1
|
246 this->z = z;
|
nuclear@1
|
247 this->w = w;
|
nuclear@1
|
248 }
|
nuclear@1
|
249
|
nuclear@1
|
250 Vector4::Vector4(const vec4_t &vec)
|
nuclear@1
|
251 {
|
nuclear@1
|
252 x = vec.x;
|
nuclear@1
|
253 y = vec.y;
|
nuclear@1
|
254 z = vec.z;
|
nuclear@1
|
255 w = vec.w;
|
nuclear@1
|
256 }
|
nuclear@1
|
257
|
nuclear@1
|
258 Vector4::Vector4(const Vector2 &vec)
|
nuclear@1
|
259 {
|
nuclear@1
|
260 x = vec.x;
|
nuclear@1
|
261 y = vec.y;
|
nuclear@1
|
262 z = 1;
|
nuclear@1
|
263 w = 1;
|
nuclear@1
|
264 }
|
nuclear@1
|
265
|
nuclear@1
|
266 Vector4::Vector4(const Vector3 &vec)
|
nuclear@1
|
267 {
|
nuclear@1
|
268 x = vec.x;
|
nuclear@1
|
269 y = vec.y;
|
nuclear@1
|
270 z = vec.z;
|
nuclear@1
|
271 w = 1;
|
nuclear@1
|
272 }
|
nuclear@1
|
273
|
nuclear@1
|
274 void Vector4::normalize()
|
nuclear@1
|
275 {
|
nuclear@1
|
276 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
|
nuclear@1
|
277 x /= len;
|
nuclear@1
|
278 y /= len;
|
nuclear@1
|
279 z /= len;
|
nuclear@1
|
280 w /= len;
|
nuclear@1
|
281 }
|
nuclear@1
|
282
|
nuclear@1
|
283 Vector4 Vector4::normalized() const
|
nuclear@1
|
284 {
|
nuclear@1
|
285 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
|
nuclear@1
|
286 return Vector4(x / len, y / len, z / len, w / len);
|
nuclear@1
|
287 }
|
nuclear@1
|
288
|
nuclear@1
|
289 void Vector4::transform(const Matrix4x4 &mat)
|
nuclear@1
|
290 {
|
nuclear@1
|
291 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
|
nuclear@1
|
292 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
|
nuclear@1
|
293 scalar_t nz = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
|
nuclear@1
|
294 w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
|
nuclear@1
|
295 x = nx;
|
nuclear@1
|
296 y = ny;
|
nuclear@1
|
297 z = nz;
|
nuclear@1
|
298 }
|
nuclear@1
|
299
|
nuclear@1
|
300 Vector4 Vector4::transformed(const Matrix4x4 &mat) const
|
nuclear@1
|
301 {
|
nuclear@1
|
302 Vector4 vec;
|
nuclear@1
|
303 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
|
nuclear@1
|
304 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
|
nuclear@1
|
305 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
|
nuclear@1
|
306 vec.w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
|
nuclear@1
|
307 return vec;
|
nuclear@1
|
308 }
|
nuclear@1
|
309
|
nuclear@1
|
310 // TODO: implement 4D vector reflection
|
nuclear@1
|
311 Vector4 Vector4::reflection(const Vector4 &normal) const
|
nuclear@1
|
312 {
|
nuclear@1
|
313 return *this;
|
nuclear@1
|
314 }
|
nuclear@1
|
315
|
nuclear@1
|
316 // TODO: implement 4D vector refraction
|
nuclear@1
|
317 Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const
|
nuclear@1
|
318 {
|
nuclear@1
|
319 return *this;
|
nuclear@1
|
320 }
|
nuclear@1
|
321
|
nuclear@1
|
322 std::ostream &operator <<(std::ostream &out, const Vector4 &vec)
|
nuclear@1
|
323 {
|
nuclear@1
|
324 out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]";
|
nuclear@1
|
325 return out;
|
nuclear@1
|
326 }
|