goat3d

view libs/vmath/vector.cc @ 40:a5c5cec3cb88

- added mesh attribute and face append functions - added Int4 constructor - continued the blender exporter - fixed a bug in clean_filename which made it produce unterminated strings - renamed clean_filename to goat3d_clean_filename and made it extern - added call to goat3d_clean_filename in the mesh XML export code to cleanup ctm filenames
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 13 Oct 2013 10:14:19 +0300
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 Vector3::Vector3(const SphVector &sph)
119 {
120 *this = sph;
121 }
123 Vector3 &Vector3::operator =(const SphVector &sph)
124 {
125 x = sph.r * cos(sph.theta) * sin(sph.phi);
126 z = sph.r * sin(sph.theta) * sin(sph.phi);
127 y = sph.r * cos(sph.phi);
128 return *this;
129 }
131 void Vector3::normalize()
132 {
133 scalar_t len = length();
134 x /= len;
135 y /= len;
136 z /= len;
137 }
139 Vector3 Vector3::normalized() const
140 {
141 scalar_t len = length();
142 return Vector3(x / len, y / len, z / len);
143 }
145 Vector3 Vector3::reflection(const Vector3 &normal) const
146 {
147 return 2.0 * dot_product(*this, normal) * normal - *this;
148 }
150 Vector3 Vector3::refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const
151 {
152 return refraction(normal, src_ior / dst_ior);
153 }
155 Vector3 Vector3::refraction(const Vector3 &normal, scalar_t ior) const
156 {
157 scalar_t cos_inc = dot_product(*this, -normal);
159 scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0);
161 if(radical < 0.0) { // total internal reflection
162 return -reflection(normal);
163 }
165 scalar_t beta = ior * cos_inc - sqrt(radical);
167 return *this * ior + normal * beta;
168 }
170 void Vector3::transform(const Matrix3x3 &mat)
171 {
172 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
173 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
174 z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
175 x = nx;
176 y = ny;
177 }
179 Vector3 Vector3::transformed(const Matrix3x3 &mat) const
180 {
181 Vector3 vec;
182 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
183 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
184 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
185 return vec;
186 }
188 void Vector3::transform(const Matrix4x4 &mat)
189 {
190 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
191 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
192 z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
193 x = nx;
194 y = ny;
195 }
197 Vector3 Vector3::transformed(const Matrix4x4 &mat) const
198 {
199 Vector3 vec;
200 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
201 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
202 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
203 return vec;
204 }
206 void Vector3::transform(const Quaternion &quat)
207 {
208 Quaternion vq(0.0f, *this);
209 vq = quat * vq * quat.inverse();
210 *this = vq.v;
211 }
213 Vector3 Vector3::transformed(const Quaternion &quat) const
214 {
215 Quaternion vq(0.0f, *this);
216 vq = quat * vq * quat.inverse();
217 return vq.v;
218 }
220 void Vector3::rotate(const Vector3 &euler)
221 {
222 Matrix4x4 rot;
223 rot.set_rotation(euler);
224 transform(rot);
225 }
227 Vector3 Vector3::rotated(const Vector3 &euler) const
228 {
229 Matrix4x4 rot;
230 rot.set_rotation(euler);
231 return transformed(rot);
232 }
234 std::ostream &operator <<(std::ostream &out, const Vector3 &vec)
235 {
236 out << "[" << vec.x << " " << vec.y << " " << vec.z << "]";
237 return out;
238 }
241 // -------------- Vector4 --------------
242 Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w)
243 {
244 this->x = x;
245 this->y = y;
246 this->z = z;
247 this->w = w;
248 }
250 Vector4::Vector4(const vec4_t &vec)
251 {
252 x = vec.x;
253 y = vec.y;
254 z = vec.z;
255 w = vec.w;
256 }
258 Vector4::Vector4(const Vector2 &vec)
259 {
260 x = vec.x;
261 y = vec.y;
262 z = 1;
263 w = 1;
264 }
266 Vector4::Vector4(const Vector3 &vec)
267 {
268 x = vec.x;
269 y = vec.y;
270 z = vec.z;
271 w = 1;
272 }
274 void Vector4::normalize()
275 {
276 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
277 x /= len;
278 y /= len;
279 z /= len;
280 w /= len;
281 }
283 Vector4 Vector4::normalized() const
284 {
285 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
286 return Vector4(x / len, y / len, z / len, w / len);
287 }
289 void Vector4::transform(const Matrix4x4 &mat)
290 {
291 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
292 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
293 scalar_t nz = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
294 w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
295 x = nx;
296 y = ny;
297 z = nz;
298 }
300 Vector4 Vector4::transformed(const Matrix4x4 &mat) const
301 {
302 Vector4 vec;
303 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
304 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
305 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
306 vec.w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
307 return vec;
308 }
310 // TODO: implement 4D vector reflection
311 Vector4 Vector4::reflection(const Vector4 &normal) const
312 {
313 return *this;
314 }
316 // TODO: implement 4D vector refraction
317 Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const
318 {
319 return *this;
320 }
322 std::ostream &operator <<(std::ostream &out, const Vector4 &vec)
323 {
324 out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]";
325 return out;
326 }