2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
4 This file is part of the n3dmath2 library.
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.
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.
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
21 #include "n3dmath2.hpp"
23 // ---------- Vector2 -----------
25 Vector2::Vector2(scalar_t x, scalar_t y) {
30 Vector2::Vector2(const Vector3 &vec) {
35 Vector2::Vector2(const Vector4 &vec) {
40 void Vector2::normalize() {
41 scalar_t len = length();
46 Vector2 Vector2::normalized() const {
47 scalar_t len = length();
48 return Vector2(x / len, y / len);
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];
57 Vector2 Vector2::transformed(const Matrix3x3 &mat) const {
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];
64 void Vector2::rotate(scalar_t angle) {
65 *this = Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
68 Vector2 Vector2::rotated(scalar_t angle) const {
69 return Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
72 Vector2 Vector2::reflection(const Vector2 &normal) const {
73 return 2.0 * dot_product(*this, normal) * normal - *this;
76 Vector2 Vector2::refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const {
80 std::ostream &operator <<(std::ostream &out, const Vector2 &vec) {
81 out << "[" << vec.x << " " << vec.y << "]";
87 // --------- Vector3 ----------
89 Vector3::Vector3(scalar_t x, scalar_t y, scalar_t z) {
95 Vector3::Vector3(const Vector2 &vec) {
101 Vector3::Vector3(const Vector4 &vec) {
107 Vector3::Vector3(const SphVector &sph) {
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);
118 void Vector3::normalize() {
119 scalar_t len = length();
125 Vector3 Vector3::normalized() const {
126 scalar_t len = length();
127 return Vector3(x / len, y / len, z / len);
130 Vector3 Vector3::reflection(const Vector3 &normal) const {
131 return -(2.0 * dot_product(*this, normal) * normal - *this);
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;
138 scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0);
140 if(radical < 0.0) { // total internal reflection
141 return reflection(normal);
144 scalar_t beta = ior * cos_inc - sqrt(radical);
146 return *this * ior + normal * beta;
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;
157 Vector3 Vector3::transformed(const Matrix3x3 &mat) const {
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;
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];
173 Vector3 Vector3::transformed(const Matrix4x4 &mat) const {
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];
181 void Vector3::transform(const Quaternion &quat) {
182 Quaternion vq(0.0f, *this);
183 vq = quat * vq * quat.inverse();
187 Vector3 Vector3::transformed(const Quaternion &quat) const {
188 Quaternion vq(0.0f, *this);
189 vq = quat * vq * quat.inverse();
193 void Vector3::rotate(const Vector3 &euler) {
195 rot.set_rotation(euler);
199 Vector3 Vector3::rotated(const Vector3 &euler) const {
201 rot.set_rotation(euler);
202 return transformed(rot);
205 std::ostream &operator <<(std::ostream &out, const Vector3 &vec) {
206 out << "[" << vec.x << " " << vec.y << " " << vec.z << "]";
212 // -------------- Vector4 --------------
213 Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w) {
220 Vector4::Vector4(const Vector2 &vec) {
227 Vector4::Vector4(const Vector3 &vec) {
234 void Vector4::normalize() {
235 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
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);
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;
257 Vector4 Vector4::transformed(const Matrix4x4 &mat) const {
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;
266 // TODO: implement 4D vector reflection
267 Vector4 Vector4::reflection(const Vector4 &normal) const {
271 // TODO: implement 4D vector refraction
272 Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const {
276 std::ostream &operator <<(std::ostream &out, const Vector4 &vec) {
277 out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]";