vrshoot

annotate libs/vmath/vector.cc @ 2:334d17aed7de

visual studio project files
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 18:36:38 +0200
parents
children
rev   line source
nuclear@0 1 #include "vector.h"
nuclear@0 2 #include "vmath.h"
nuclear@0 3
nuclear@0 4 // ---------- Vector2 -----------
nuclear@0 5
nuclear@0 6 Vector2::Vector2(scalar_t x, scalar_t y)
nuclear@0 7 {
nuclear@0 8 this->x = x;
nuclear@0 9 this->y = y;
nuclear@0 10 }
nuclear@0 11
nuclear@0 12 Vector2::Vector2(const vec2_t &vec)
nuclear@0 13 {
nuclear@0 14 x = vec.x;
nuclear@0 15 y = vec.y;
nuclear@0 16 }
nuclear@0 17
nuclear@0 18 Vector2::Vector2(const Vector3 &vec)
nuclear@0 19 {
nuclear@0 20 x = vec.x;
nuclear@0 21 y = vec.y;
nuclear@0 22 }
nuclear@0 23
nuclear@0 24 Vector2::Vector2(const Vector4 &vec)
nuclear@0 25 {
nuclear@0 26 x = vec.x;
nuclear@0 27 y = vec.y;
nuclear@0 28 }
nuclear@0 29
nuclear@0 30 void Vector2::normalize()
nuclear@0 31 {
nuclear@0 32 scalar_t len = length();
nuclear@0 33 x /= len;
nuclear@0 34 y /= len;
nuclear@0 35 }
nuclear@0 36
nuclear@0 37 Vector2 Vector2::normalized() const
nuclear@0 38 {
nuclear@0 39 scalar_t len = length();
nuclear@0 40 return Vector2(x / len, y / len);
nuclear@0 41 }
nuclear@0 42
nuclear@0 43 void Vector2::transform(const Matrix3x3 &mat)
nuclear@0 44 {
nuclear@0 45 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2];
nuclear@0 46 y = mat[1][0] * x + mat[1][1] * y + mat[1][2];
nuclear@0 47 x = nx;
nuclear@0 48 }
nuclear@0 49
nuclear@0 50 Vector2 Vector2::transformed(const Matrix3x3 &mat) const
nuclear@0 51 {
nuclear@0 52 Vector2 vec;
nuclear@0 53 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2];
nuclear@0 54 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2];
nuclear@0 55 return vec;
nuclear@0 56 }
nuclear@0 57
nuclear@0 58 void Vector2::rotate(scalar_t angle)
nuclear@0 59 {
nuclear@0 60 *this = Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
nuclear@0 61 }
nuclear@0 62
nuclear@0 63 Vector2 Vector2::rotated(scalar_t angle) const
nuclear@0 64 {
nuclear@0 65 return Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
nuclear@0 66 }
nuclear@0 67
nuclear@0 68 Vector2 Vector2::reflection(const Vector2 &normal) const
nuclear@0 69 {
nuclear@0 70 return 2.0 * dot_product(*this, normal) * normal - *this;
nuclear@0 71 }
nuclear@0 72
nuclear@0 73 Vector2 Vector2::refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const
nuclear@0 74 {
nuclear@0 75 // quick and dirty implementation :)
nuclear@0 76 Vector3 v3refr = Vector3(this->x, this->y, 1.0).refraction(Vector3(this->x, this->y, 1), src_ior, dst_ior);
nuclear@0 77 return Vector2(v3refr.x, v3refr.y);
nuclear@0 78 }
nuclear@0 79
nuclear@0 80 std::ostream &operator <<(std::ostream &out, const Vector2 &vec)
nuclear@0 81 {
nuclear@0 82 out << "[" << vec.x << " " << vec.y << "]";
nuclear@0 83 return out;
nuclear@0 84 }
nuclear@0 85
nuclear@0 86
nuclear@0 87
nuclear@0 88 // --------- Vector3 ----------
nuclear@0 89
nuclear@0 90 Vector3::Vector3(scalar_t x, scalar_t y, scalar_t z)
nuclear@0 91 {
nuclear@0 92 this->x = x;
nuclear@0 93 this->y = y;
nuclear@0 94 this->z = z;
nuclear@0 95 }
nuclear@0 96
nuclear@0 97 Vector3::Vector3(const vec3_t &vec)
nuclear@0 98 {
nuclear@0 99 x = vec.x;
nuclear@0 100 y = vec.y;
nuclear@0 101 z = vec.z;
nuclear@0 102 }
nuclear@0 103
nuclear@0 104 Vector3::Vector3(const Vector2 &vec)
nuclear@0 105 {
nuclear@0 106 x = vec.x;
nuclear@0 107 y = vec.y;
nuclear@0 108 z = 1;
nuclear@0 109 }
nuclear@0 110
nuclear@0 111 Vector3::Vector3(const Vector4 &vec)
nuclear@0 112 {
nuclear@0 113 x = vec.x;
nuclear@0 114 y = vec.y;
nuclear@0 115 z = vec.z;
nuclear@0 116 }
nuclear@0 117
nuclear@0 118 Vector3::Vector3(const SphVector &sph)
nuclear@0 119 {
nuclear@0 120 *this = sph;
nuclear@0 121 }
nuclear@0 122
nuclear@0 123 Vector3 &Vector3::operator =(const SphVector &sph)
nuclear@0 124 {
nuclear@0 125 x = sph.r * cos(sph.theta) * sin(sph.phi);
nuclear@0 126 z = sph.r * sin(sph.theta) * sin(sph.phi);
nuclear@0 127 y = sph.r * cos(sph.phi);
nuclear@0 128 return *this;
nuclear@0 129 }
nuclear@0 130
nuclear@0 131 void Vector3::normalize()
nuclear@0 132 {
nuclear@0 133 scalar_t len = length();
nuclear@0 134 x /= len;
nuclear@0 135 y /= len;
nuclear@0 136 z /= len;
nuclear@0 137 }
nuclear@0 138
nuclear@0 139 Vector3 Vector3::normalized() const
nuclear@0 140 {
nuclear@0 141 scalar_t len = length();
nuclear@0 142 return Vector3(x / len, y / len, z / len);
nuclear@0 143 }
nuclear@0 144
nuclear@0 145 Vector3 Vector3::reflection(const Vector3 &normal) const
nuclear@0 146 {
nuclear@0 147 return 2.0 * dot_product(*this, normal) * normal - *this;
nuclear@0 148 }
nuclear@0 149
nuclear@0 150 Vector3 Vector3::refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const
nuclear@0 151 {
nuclear@0 152 return refraction(normal, src_ior / dst_ior);
nuclear@0 153 }
nuclear@0 154
nuclear@0 155 Vector3 Vector3::refraction(const Vector3 &normal, scalar_t ior) const
nuclear@0 156 {
nuclear@0 157 scalar_t cos_inc = dot_product(*this, -normal);
nuclear@0 158
nuclear@0 159 scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0);
nuclear@0 160
nuclear@0 161 if(radical < 0.0) { // total internal reflection
nuclear@0 162 return -reflection(normal);
nuclear@0 163 }
nuclear@0 164
nuclear@0 165 scalar_t beta = ior * cos_inc - sqrt(radical);
nuclear@0 166
nuclear@0 167 return *this * ior + normal * beta;
nuclear@0 168 }
nuclear@0 169
nuclear@0 170 void Vector3::transform(const Matrix3x3 &mat)
nuclear@0 171 {
nuclear@0 172 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
nuclear@0 173 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
nuclear@0 174 z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
nuclear@0 175 x = nx;
nuclear@0 176 y = ny;
nuclear@0 177 }
nuclear@0 178
nuclear@0 179 Vector3 Vector3::transformed(const Matrix3x3 &mat) const
nuclear@0 180 {
nuclear@0 181 Vector3 vec;
nuclear@0 182 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
nuclear@0 183 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
nuclear@0 184 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
nuclear@0 185 return vec;
nuclear@0 186 }
nuclear@0 187
nuclear@0 188 void Vector3::transform(const Matrix4x4 &mat)
nuclear@0 189 {
nuclear@0 190 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
nuclear@0 191 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
nuclear@0 192 z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
nuclear@0 193 x = nx;
nuclear@0 194 y = ny;
nuclear@0 195 }
nuclear@0 196
nuclear@0 197 Vector3 Vector3::transformed(const Matrix4x4 &mat) const
nuclear@0 198 {
nuclear@0 199 Vector3 vec;
nuclear@0 200 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
nuclear@0 201 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
nuclear@0 202 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
nuclear@0 203 return vec;
nuclear@0 204 }
nuclear@0 205
nuclear@0 206 void Vector3::transform(const Quaternion &quat)
nuclear@0 207 {
nuclear@0 208 Quaternion vq(0.0f, *this);
nuclear@0 209 vq = quat * vq * quat.inverse();
nuclear@0 210 *this = vq.v;
nuclear@0 211 }
nuclear@0 212
nuclear@0 213 Vector3 Vector3::transformed(const Quaternion &quat) const
nuclear@0 214 {
nuclear@0 215 Quaternion vq(0.0f, *this);
nuclear@0 216 vq = quat * vq * quat.inverse();
nuclear@0 217 return vq.v;
nuclear@0 218 }
nuclear@0 219
nuclear@0 220 void Vector3::rotate(const Vector3 &euler)
nuclear@0 221 {
nuclear@0 222 Matrix4x4 rot;
nuclear@0 223 rot.set_rotation(euler);
nuclear@0 224 transform(rot);
nuclear@0 225 }
nuclear@0 226
nuclear@0 227 Vector3 Vector3::rotated(const Vector3 &euler) const
nuclear@0 228 {
nuclear@0 229 Matrix4x4 rot;
nuclear@0 230 rot.set_rotation(euler);
nuclear@0 231 return transformed(rot);
nuclear@0 232 }
nuclear@0 233
nuclear@0 234 std::ostream &operator <<(std::ostream &out, const Vector3 &vec)
nuclear@0 235 {
nuclear@0 236 out << "[" << vec.x << " " << vec.y << " " << vec.z << "]";
nuclear@0 237 return out;
nuclear@0 238 }
nuclear@0 239
nuclear@0 240
nuclear@0 241 // -------------- Vector4 --------------
nuclear@0 242 Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w)
nuclear@0 243 {
nuclear@0 244 this->x = x;
nuclear@0 245 this->y = y;
nuclear@0 246 this->z = z;
nuclear@0 247 this->w = w;
nuclear@0 248 }
nuclear@0 249
nuclear@0 250 Vector4::Vector4(const vec4_t &vec)
nuclear@0 251 {
nuclear@0 252 x = vec.x;
nuclear@0 253 y = vec.y;
nuclear@0 254 z = vec.z;
nuclear@0 255 w = vec.w;
nuclear@0 256 }
nuclear@0 257
nuclear@0 258 Vector4::Vector4(const Vector2 &vec)
nuclear@0 259 {
nuclear@0 260 x = vec.x;
nuclear@0 261 y = vec.y;
nuclear@0 262 z = 1;
nuclear@0 263 w = 1;
nuclear@0 264 }
nuclear@0 265
nuclear@0 266 Vector4::Vector4(const Vector3 &vec)
nuclear@0 267 {
nuclear@0 268 x = vec.x;
nuclear@0 269 y = vec.y;
nuclear@0 270 z = vec.z;
nuclear@0 271 w = 1;
nuclear@0 272 }
nuclear@0 273
nuclear@0 274 void Vector4::normalize()
nuclear@0 275 {
nuclear@0 276 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
nuclear@0 277 x /= len;
nuclear@0 278 y /= len;
nuclear@0 279 z /= len;
nuclear@0 280 w /= len;
nuclear@0 281 }
nuclear@0 282
nuclear@0 283 Vector4 Vector4::normalized() const
nuclear@0 284 {
nuclear@0 285 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
nuclear@0 286 return Vector4(x / len, y / len, z / len, w / len);
nuclear@0 287 }
nuclear@0 288
nuclear@0 289 void Vector4::transform(const Matrix4x4 &mat)
nuclear@0 290 {
nuclear@0 291 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
nuclear@0 292 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
nuclear@0 293 scalar_t nz = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
nuclear@0 294 w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
nuclear@0 295 x = nx;
nuclear@0 296 y = ny;
nuclear@0 297 z = nz;
nuclear@0 298 }
nuclear@0 299
nuclear@0 300 Vector4 Vector4::transformed(const Matrix4x4 &mat) const
nuclear@0 301 {
nuclear@0 302 Vector4 vec;
nuclear@0 303 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
nuclear@0 304 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
nuclear@0 305 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
nuclear@0 306 vec.w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
nuclear@0 307 return vec;
nuclear@0 308 }
nuclear@0 309
nuclear@0 310 // TODO: implement 4D vector reflection
nuclear@0 311 Vector4 Vector4::reflection(const Vector4 &normal) const
nuclear@0 312 {
nuclear@0 313 return *this;
nuclear@0 314 }
nuclear@0 315
nuclear@0 316 // TODO: implement 4D vector refraction
nuclear@0 317 Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const
nuclear@0 318 {
nuclear@0 319 return *this;
nuclear@0 320 }
nuclear@0 321
nuclear@0 322 std::ostream &operator <<(std::ostream &out, const Vector4 &vec)
nuclear@0 323 {
nuclear@0 324 out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]";
nuclear@0 325 return out;
nuclear@0 326 }