2 This file is part of XRay, a photorealistic 3D rendering library.
3 Copyright (C) 2005 John Tsiombikas
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.
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.
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
22 * @author John Tsiombikas
31 // ---------- Vector2 -----------
33 Vector2::Vector2(scalar_t x, scalar_t y) {
38 Vector2::Vector2(const Vector3 &vec) {
43 Vector2::Vector2(const Vector4 &vec) {
48 void Vector2::normalize() {
49 scalar_t len = length();
54 Vector2 Vector2::normalized() const {
55 scalar_t len = length();
56 return Vector2(x / len, y / len);
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];
65 Vector2 Vector2::transformed(const Matrix3x3 &mat) const {
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];
72 void Vector2::rotate(scalar_t angle) {
73 *this = Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
76 Vector2 Vector2::rotated(scalar_t angle) const {
77 return Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
80 Vector2 Vector2::reflection(const Vector2 &normal) const {
81 return 2.0 * dot_product(*this, normal) * normal - *this;
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);
90 std::ostream &operator <<(std::ostream &out, const Vector2 &vec) {
91 out << "[" << vec.x << " " << vec.y << "]";
97 // --------- Vector3 ----------
99 Vector3::Vector3(scalar_t x, scalar_t y, scalar_t z) {
105 Vector3::Vector3(const Vector2 &vec) {
111 Vector3::Vector3(const Vector4 &vec) {
117 Vector3::Vector3(const SphVector &sph) {
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);
128 void Vector3::normalize() {
129 scalar_t len = length();
135 Vector3 Vector3::normalized() const {
136 scalar_t len = length();
137 return Vector3(x / len, y / len, z / len);
140 Vector3 Vector3::reflection(const Vector3 &normal) const {
141 return -(2.0 * dot_product(*this, normal) * normal - *this);
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;
148 scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0);
150 if(radical < 0.0) { // total internal reflection
151 return reflection(normal);
154 scalar_t beta = ior * cos_inc - sqrt(radical);
156 return *this * ior + normal * beta;
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;
167 Vector3 Vector3::transformed(const Matrix3x3 &mat) const {
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;
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];
183 Vector3 Vector3::transformed(const Matrix4x4 &mat) const {
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];
191 void Vector3::transform(const Quaternion &quat) {
192 Quaternion vq(0.0f, *this);
193 vq = quat * vq * quat.inverse();
197 Vector3 Vector3::transformed(const Quaternion &quat) const {
198 Quaternion vq(0.0f, *this);
199 vq = quat * vq * quat.inverse();
203 void Vector3::rotate(const Vector3 &euler) {
205 rot.set_rotation(euler);
209 Vector3 Vector3::rotated(const Vector3 &euler) const {
211 rot.set_rotation(euler);
212 return transformed(rot);
216 std::ostream &operator <<(std::ostream &out, const Vector3 &vec) {
217 out << "[" << vec.x << " " << vec.y << " " << vec.z << "]";
223 // -------------- Vector4 --------------
224 Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w) {
231 Vector4::Vector4(const Vector2 &vec) {
238 Vector4::Vector4(const Vector3 &vec) {
245 void Vector4::normalize() {
246 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
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);
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;
268 Vector4 Vector4::transformed(const Matrix4x4 &mat) const {
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;
277 // TODO: implement 4D vector reflection
278 Vector4 Vector4::reflection(const Vector4 &normal) const {
282 // TODO: implement 4D vector refraction
283 Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const {
287 std::ostream &operator <<(std::ostream &out, const Vector4 &vec) {
288 out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]";