3dphotoshoot
view libs/vmath/vector.cc @ 10:c71c477521ca
converting to GLES2 and C++
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 31 May 2015 00:40:26 +0300 |
parents | |
children |
line source
1 /*
2 libvmath - a vector math library
3 Copyright (C) 2004-2015 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "vector.h"
19 #include "vmath.h"
21 // ---------- Vector2 -----------
23 Vector2::Vector2(scalar_t x, scalar_t y)
24 {
25 this->x = x;
26 this->y = y;
27 }
29 Vector2::Vector2(const vec2_t &vec)
30 {
31 x = vec.x;
32 y = vec.y;
33 }
35 Vector2::Vector2(const Vector3 &vec)
36 {
37 x = vec.x;
38 y = vec.y;
39 }
41 Vector2::Vector2(const Vector4 &vec)
42 {
43 x = vec.x;
44 y = vec.y;
45 }
47 void Vector2::normalize()
48 {
49 scalar_t len = length();
50 x /= len;
51 y /= len;
52 }
54 Vector2 Vector2::normalized() const
55 {
56 scalar_t len = length();
57 return Vector2(x / len, y / len);
58 }
60 void Vector2::transform(const Matrix3x3 &mat)
61 {
62 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2];
63 y = mat[1][0] * x + mat[1][1] * y + mat[1][2];
64 x = nx;
65 }
67 Vector2 Vector2::transformed(const Matrix3x3 &mat) const
68 {
69 Vector2 vec;
70 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2];
71 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2];
72 return vec;
73 }
75 void Vector2::rotate(scalar_t angle)
76 {
77 *this = Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
78 }
80 Vector2 Vector2::rotated(scalar_t angle) const
81 {
82 return Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
83 }
85 Vector2 Vector2::reflection(const Vector2 &normal) const
86 {
87 return 2.0 * dot_product(*this, normal) * normal - *this;
88 }
90 Vector2 Vector2::refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const
91 {
92 // quick and dirty implementation :)
93 Vector3 v3refr = Vector3(this->x, this->y, 1.0).refraction(Vector3(this->x, this->y, 1), src_ior, dst_ior);
94 return Vector2(v3refr.x, v3refr.y);
95 }
97 /*
98 std::ostream &operator <<(std::ostream &out, const Vector2 &vec)
99 {
100 out << "[" << vec.x << " " << vec.y << "]";
101 return out;
102 }
103 */
106 // --------- Vector3 ----------
108 Vector3::Vector3(scalar_t x, scalar_t y, scalar_t z)
109 {
110 this->x = x;
111 this->y = y;
112 this->z = z;
113 }
115 Vector3::Vector3(const vec3_t &vec)
116 {
117 x = vec.x;
118 y = vec.y;
119 z = vec.z;
120 }
122 Vector3::Vector3(const Vector2 &vec)
123 {
124 x = vec.x;
125 y = vec.y;
126 z = 1;
127 }
129 Vector3::Vector3(const Vector4 &vec)
130 {
131 x = vec.x;
132 y = vec.y;
133 z = vec.z;
134 }
136 Vector3::Vector3(const SphVector &sph)
137 {
138 *this = sph;
139 }
141 Vector3 &Vector3::operator =(const SphVector &sph)
142 {
143 x = sph.r * cos(sph.theta) * sin(sph.phi);
144 z = sph.r * sin(sph.theta) * sin(sph.phi);
145 y = sph.r * cos(sph.phi);
146 return *this;
147 }
149 void Vector3::normalize()
150 {
151 scalar_t len = length();
152 x /= len;
153 y /= len;
154 z /= len;
155 }
157 Vector3 Vector3::normalized() const
158 {
159 scalar_t len = length();
160 return Vector3(x / len, y / len, z / len);
161 }
163 Vector3 Vector3::reflection(const Vector3 &normal) const
164 {
165 return 2.0 * dot_product(*this, normal) * normal - *this;
166 }
168 Vector3 Vector3::refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const
169 {
170 return refraction(normal, src_ior / dst_ior);
171 }
173 Vector3 Vector3::refraction(const Vector3 &normal, scalar_t ior) const
174 {
175 scalar_t cos_inc = dot_product(*this, -normal);
177 scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0);
179 if(radical < 0.0) { // total internal reflection
180 return -reflection(normal);
181 }
183 scalar_t beta = ior * cos_inc - sqrt(radical);
185 return *this * ior + normal * beta;
186 }
188 void Vector3::transform(const Matrix3x3 &mat)
189 {
190 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
191 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
192 z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
193 x = nx;
194 y = ny;
195 }
197 Vector3 Vector3::transformed(const Matrix3x3 &mat) const
198 {
199 Vector3 vec;
200 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
201 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
202 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
203 return vec;
204 }
206 void Vector3::transform(const Matrix4x4 &mat)
207 {
208 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
209 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
210 z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
211 x = nx;
212 y = ny;
213 }
215 Vector3 Vector3::transformed(const Matrix4x4 &mat) const
216 {
217 Vector3 vec;
218 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
219 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
220 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
221 return vec;
222 }
224 void Vector3::transform(const Quaternion &quat)
225 {
226 Quaternion vq(0.0f, *this);
227 vq = quat * vq * quat.inverse();
228 *this = vq.v;
229 }
231 Vector3 Vector3::transformed(const Quaternion &quat) const
232 {
233 Quaternion vq(0.0f, *this);
234 vq = quat * vq * quat.inverse();
235 return vq.v;
236 }
238 void Vector3::rotate(const Vector3 &euler)
239 {
240 Matrix4x4 rot;
241 rot.set_rotation(euler);
242 transform(rot);
243 }
245 Vector3 Vector3::rotated(const Vector3 &euler) const
246 {
247 Matrix4x4 rot;
248 rot.set_rotation(euler);
249 return transformed(rot);
250 }
252 /*
253 std::ostream &operator <<(std::ostream &out, const Vector3 &vec)
254 {
255 out << "[" << vec.x << " " << vec.y << " " << vec.z << "]";
256 return out;
257 }
258 */
261 // -------------- Vector4 --------------
262 Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w)
263 {
264 this->x = x;
265 this->y = y;
266 this->z = z;
267 this->w = w;
268 }
270 Vector4::Vector4(const vec4_t &vec)
271 {
272 x = vec.x;
273 y = vec.y;
274 z = vec.z;
275 w = vec.w;
276 }
278 Vector4::Vector4(const Vector2 &vec)
279 {
280 x = vec.x;
281 y = vec.y;
282 z = 1;
283 w = 1;
284 }
286 Vector4::Vector4(const Vector3 &vec)
287 {
288 x = vec.x;
289 y = vec.y;
290 z = vec.z;
291 w = 1;
292 }
294 void Vector4::normalize()
295 {
296 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
297 x /= len;
298 y /= len;
299 z /= len;
300 w /= len;
301 }
303 Vector4 Vector4::normalized() const
304 {
305 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
306 return Vector4(x / len, y / len, z / len, w / len);
307 }
309 void Vector4::transform(const Matrix4x4 &mat)
310 {
311 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
312 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
313 scalar_t nz = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
314 w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
315 x = nx;
316 y = ny;
317 z = nz;
318 }
320 Vector4 Vector4::transformed(const Matrix4x4 &mat) const
321 {
322 Vector4 vec;
323 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
324 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
325 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
326 vec.w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
327 return vec;
328 }
330 // TODO: implement 4D vector reflection
331 Vector4 Vector4::reflection(const Vector4 &normal) const
332 {
333 return *this;
334 }
336 // TODO: implement 4D vector refraction
337 Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const
338 {
339 return *this;
340 }
342 /*
343 std::ostream &operator <<(std::ostream &out, const Vector4 &vec)
344 {
345 out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]";
346 return out;
347 }
348 */