gpuray_glsl

view vmath/vector.cc @ 0:f234630e38ff

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