added summerhack
[summerhack] / tools / curve_draw / vmath / vector.cc
1 /*
2 This file is part of XRay, a photorealistic 3D rendering library.
3 Copyright (C) 2005 John Tsiombikas
4
5 XRay is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 XRay 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with XRay; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 /**
21  * @file vector.cxx
22  * @author John Tsiombikas
23  * @date 28 June 2005
24  *
25  * Vector math.
26  */
27
28 #include "vector.h"
29 #include "sphvec.h"
30
31 // ---------- Vector2 -----------
32
33 Vector2::Vector2(scalar_t x, scalar_t y) {
34         this->x = x;
35         this->y = y;
36 }
37
38 Vector2::Vector2(const Vector3 &vec) {
39         x = vec.x;
40         y = vec.y;
41 }
42
43 Vector2::Vector2(const Vector4 &vec) {
44         x = vec.x;
45         y = vec.y;
46 }
47
48 void Vector2::normalize() {
49         scalar_t len = length();
50         x /= len;
51         y /= len;
52 }
53
54 Vector2 Vector2::normalized() const {   
55         scalar_t len = length();
56         return Vector2(x / len, y / len);
57 }
58
59 void Vector2::transform(const Matrix3x3 &mat) {
60         scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2];
61         y = mat[1][0] * x + mat[1][1] * y + mat[1][2];
62         x = nx;
63 }
64
65 Vector2 Vector2::transformed(const Matrix3x3 &mat) const {
66         Vector2 vec;
67         vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2];
68         vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2];
69         return vec;
70 }
71
72 void Vector2::rotate(scalar_t angle) {
73         *this = Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
74 }
75
76 Vector2 Vector2::rotated(scalar_t angle) const {
77         return Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
78 }
79         
80 Vector2 Vector2::reflection(const Vector2 &normal) const {
81         return 2.0 * dot_product(*this, normal) * normal - *this;
82 }
83
84 Vector2 Vector2::refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const {
85         // quick and dirty implementation :)
86         Vector3 v3refr = Vector3(this->x, this->y, 1.0).refraction(Vector3(this->x, this->y, 1), src_ior, dst_ior);
87         return Vector2(v3refr.x, v3refr.y);
88 }
89
90 std::ostream &operator <<(std::ostream &out, const Vector2 &vec) {
91         out << "[" << vec.x << " " << vec.y << "]";
92         return out;
93 }
94
95
96
97 // --------- Vector3 ----------
98
99 Vector3::Vector3(scalar_t x, scalar_t y, scalar_t z) {
100         this->x = x;
101         this->y = y;
102         this->z = z;
103 }
104
105 Vector3::Vector3(const Vector2 &vec) {
106         x = vec.x;
107         y = vec.y;
108         z = 1;
109 }
110
111 Vector3::Vector3(const Vector4 &vec) {
112         x = vec.x;
113         y = vec.y;
114         z = vec.z;
115 }
116
117 Vector3::Vector3(const SphVector &sph) {
118         *this = sph;
119 }
120
121 Vector3 &Vector3::operator =(const SphVector &sph) {
122         x = sph.r * cos(sph.theta) * sin(sph.phi);
123         z = sph.r * sin(sph.theta) * sin(sph.phi);
124         y = sph.r * cos(sph.phi);
125         return *this;
126 }
127
128 void Vector3::normalize() {
129         scalar_t len = length();
130         x /= len;
131         y /= len;
132         z /= len;       
133 }
134
135 Vector3 Vector3::normalized() const {
136         scalar_t len = length();
137         return Vector3(x / len, y / len, z / len);
138 }
139
140 Vector3 Vector3::reflection(const Vector3 &normal) const {
141         return -(2.0 * dot_product(*this, normal) * normal - *this);
142 }
143
144 Vector3 Vector3::refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const {
145         scalar_t cos_inc = dot_product(*this, -normal);
146         scalar_t ior = src_ior / dst_ior;
147
148         scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0);
149
150         if(radical < 0.0) {             // total internal reflection
151                 return reflection(normal);
152         }
153
154         scalar_t beta = ior * cos_inc - sqrt(radical);
155
156         return *this * ior + normal * beta;
157 }
158
159 void Vector3::transform(const Matrix3x3 &mat) {
160         scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
161         scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
162         z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
163         x = nx;
164         y = ny;
165 }
166
167 Vector3 Vector3::transformed(const Matrix3x3 &mat) const {
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 }
174
175 void Vector3::transform(const Matrix4x4 &mat) {
176         scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
177         scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
178         z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
179         x = nx;
180         y = ny;
181 }
182
183 Vector3 Vector3::transformed(const Matrix4x4 &mat) const {
184         Vector3 vec;
185         vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
186         vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
187         vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
188         return vec;
189 }
190
191 void Vector3::transform(const Quaternion &quat) {
192         Quaternion vq(0.0f, *this);
193         vq = quat * vq * quat.inverse();
194         *this = vq.v;
195 }
196
197 Vector3 Vector3::transformed(const Quaternion &quat) const {
198         Quaternion vq(0.0f, *this);
199         vq = quat * vq * quat.inverse();
200         return vq.v;
201 }
202
203 void Vector3::rotate(const Vector3 &euler) {
204         Matrix4x4 rot;
205         rot.set_rotation(euler);
206         transform(rot);
207 }
208
209 Vector3 Vector3::rotated(const Vector3 &euler) const {
210         Matrix4x4 rot;
211         rot.set_rotation(euler);
212         return transformed(rot);
213 }
214
215
216 std::ostream &operator <<(std::ostream &out, const Vector3 &vec) {
217         out << "[" << vec.x << " " << vec.y << " " << vec.z << "]";
218         return out;
219 }
220
221
222
223 // -------------- Vector4 --------------
224 Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w) {
225         this->x = x;
226         this->y = y;
227         this->z = z;
228         this->w = w;
229 }
230
231 Vector4::Vector4(const Vector2 &vec) {
232         x = vec.x;
233         y = vec.y;
234         z = 1;
235         w = 1;
236 }
237
238 Vector4::Vector4(const Vector3 &vec) {
239         x = vec.x;
240         y = vec.y;
241         z = vec.z;
242         w = 1;
243 }
244
245 void Vector4::normalize() {
246         scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
247         x /= len;
248         y /= len;
249         z /= len;
250         w /= len;
251 }
252
253 Vector4 Vector4::normalized() const {
254         scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
255         return Vector4(x / len, y / len, z / len, w / len);
256 }
257
258 void Vector4::transform(const Matrix4x4 &mat) {
259         scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
260         scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
261         scalar_t nz = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
262         w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
263         x = nx;
264         y = ny;
265         z = nz;
266 }
267
268 Vector4 Vector4::transformed(const Matrix4x4 &mat) const {
269         Vector4 vec;
270         vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
271         vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
272         vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
273         vec.w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
274         return vec;
275 }
276
277 // TODO: implement 4D vector reflection
278 Vector4 Vector4::reflection(const Vector4 &normal) const {
279         return *this;
280 }
281
282 // TODO: implement 4D vector refraction
283 Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const {
284         return *this;
285 }
286
287 std::ostream &operator <<(std::ostream &out, const Vector4 &vec) {
288         out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]";
289         return out;
290 }