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