gpuray_glsl

annotate vmath/vector.cc @ 3:297dbc5080c4

cone intersection
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 09 Nov 2014 20:13:33 +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 void Vector3::normalize()
nuclear@0 119 {
nuclear@0 120 scalar_t len = length();
nuclear@0 121 x /= len;
nuclear@0 122 y /= len;
nuclear@0 123 z /= len;
nuclear@0 124 }
nuclear@0 125
nuclear@0 126 Vector3 Vector3::normalized() const
nuclear@0 127 {
nuclear@0 128 scalar_t len = length();
nuclear@0 129 return Vector3(x / len, y / len, z / len);
nuclear@0 130 }
nuclear@0 131
nuclear@0 132 Vector3 Vector3::reflection(const Vector3 &normal) const
nuclear@0 133 {
nuclear@0 134 return 2.0 * dot_product(*this, normal) * normal - *this;
nuclear@0 135 }
nuclear@0 136
nuclear@0 137 Vector3 Vector3::refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const
nuclear@0 138 {
nuclear@0 139 return refraction(normal, src_ior / dst_ior);
nuclear@0 140 }
nuclear@0 141
nuclear@0 142 Vector3 Vector3::refraction(const Vector3 &normal, scalar_t ior) const
nuclear@0 143 {
nuclear@0 144 scalar_t cos_inc = dot_product(*this, -normal);
nuclear@0 145
nuclear@0 146 scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0);
nuclear@0 147
nuclear@0 148 if(radical < 0.0) { // total internal reflection
nuclear@0 149 return -reflection(normal);
nuclear@0 150 }
nuclear@0 151
nuclear@0 152 scalar_t beta = ior * cos_inc - sqrt(radical);
nuclear@0 153
nuclear@0 154 return *this * ior + normal * beta;
nuclear@0 155 }
nuclear@0 156
nuclear@0 157 void Vector3::transform(const Matrix3x3 &mat)
nuclear@0 158 {
nuclear@0 159 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
nuclear@0 160 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
nuclear@0 161 z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
nuclear@0 162 x = nx;
nuclear@0 163 y = ny;
nuclear@0 164 }
nuclear@0 165
nuclear@0 166 Vector3 Vector3::transformed(const Matrix3x3 &mat) const
nuclear@0 167 {
nuclear@0 168 Vector3 vec;
nuclear@0 169 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
nuclear@0 170 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
nuclear@0 171 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
nuclear@0 172 return vec;
nuclear@0 173 }
nuclear@0 174
nuclear@0 175 void Vector3::transform(const Matrix4x4 &mat)
nuclear@0 176 {
nuclear@0 177 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
nuclear@0 178 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
nuclear@0 179 z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
nuclear@0 180 x = nx;
nuclear@0 181 y = ny;
nuclear@0 182 }
nuclear@0 183
nuclear@0 184 Vector3 Vector3::transformed(const Matrix4x4 &mat) const
nuclear@0 185 {
nuclear@0 186 Vector3 vec;
nuclear@0 187 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
nuclear@0 188 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
nuclear@0 189 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
nuclear@0 190 return vec;
nuclear@0 191 }
nuclear@0 192
nuclear@0 193 void Vector3::transform(const Quaternion &quat)
nuclear@0 194 {
nuclear@0 195 Quaternion vq(0.0f, *this);
nuclear@0 196 vq = quat * vq * quat.inverse();
nuclear@0 197 *this = vq.v;
nuclear@0 198 }
nuclear@0 199
nuclear@0 200 Vector3 Vector3::transformed(const Quaternion &quat) const
nuclear@0 201 {
nuclear@0 202 Quaternion vq(0.0f, *this);
nuclear@0 203 vq = quat * vq * quat.inverse();
nuclear@0 204 return vq.v;
nuclear@0 205 }
nuclear@0 206
nuclear@0 207 void Vector3::rotate(const Vector3 &euler)
nuclear@0 208 {
nuclear@0 209 Matrix4x4 rot;
nuclear@0 210 rot.set_rotation(euler);
nuclear@0 211 transform(rot);
nuclear@0 212 }
nuclear@0 213
nuclear@0 214 Vector3 Vector3::rotated(const Vector3 &euler) const
nuclear@0 215 {
nuclear@0 216 Matrix4x4 rot;
nuclear@0 217 rot.set_rotation(euler);
nuclear@0 218 return transformed(rot);
nuclear@0 219 }
nuclear@0 220
nuclear@0 221 std::ostream &operator <<(std::ostream &out, const Vector3 &vec)
nuclear@0 222 {
nuclear@0 223 out << "[" << vec.x << " " << vec.y << " " << vec.z << "]";
nuclear@0 224 return out;
nuclear@0 225 }
nuclear@0 226
nuclear@0 227
nuclear@0 228 // -------------- Vector4 --------------
nuclear@0 229 Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w)
nuclear@0 230 {
nuclear@0 231 this->x = x;
nuclear@0 232 this->y = y;
nuclear@0 233 this->z = z;
nuclear@0 234 this->w = w;
nuclear@0 235 }
nuclear@0 236
nuclear@0 237 Vector4::Vector4(const vec4_t &vec)
nuclear@0 238 {
nuclear@0 239 x = vec.x;
nuclear@0 240 y = vec.y;
nuclear@0 241 z = vec.z;
nuclear@0 242 w = vec.w;
nuclear@0 243 }
nuclear@0 244
nuclear@0 245 Vector4::Vector4(const Vector2 &vec)
nuclear@0 246 {
nuclear@0 247 x = vec.x;
nuclear@0 248 y = vec.y;
nuclear@0 249 z = 1;
nuclear@0 250 w = 1;
nuclear@0 251 }
nuclear@0 252
nuclear@0 253 Vector4::Vector4(const Vector3 &vec)
nuclear@0 254 {
nuclear@0 255 x = vec.x;
nuclear@0 256 y = vec.y;
nuclear@0 257 z = vec.z;
nuclear@0 258 w = 1;
nuclear@0 259 }
nuclear@0 260
nuclear@0 261 void Vector4::normalize()
nuclear@0 262 {
nuclear@0 263 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
nuclear@0 264 x /= len;
nuclear@0 265 y /= len;
nuclear@0 266 z /= len;
nuclear@0 267 w /= len;
nuclear@0 268 }
nuclear@0 269
nuclear@0 270 Vector4 Vector4::normalized() const
nuclear@0 271 {
nuclear@0 272 scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
nuclear@0 273 return Vector4(x / len, y / len, z / len, w / len);
nuclear@0 274 }
nuclear@0 275
nuclear@0 276 void Vector4::transform(const Matrix4x4 &mat)
nuclear@0 277 {
nuclear@0 278 scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
nuclear@0 279 scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
nuclear@0 280 scalar_t nz = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
nuclear@0 281 w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
nuclear@0 282 x = nx;
nuclear@0 283 y = ny;
nuclear@0 284 z = nz;
nuclear@0 285 }
nuclear@0 286
nuclear@0 287 Vector4 Vector4::transformed(const Matrix4x4 &mat) const
nuclear@0 288 {
nuclear@0 289 Vector4 vec;
nuclear@0 290 vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
nuclear@0 291 vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
nuclear@0 292 vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
nuclear@0 293 vec.w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
nuclear@0 294 return vec;
nuclear@0 295 }
nuclear@0 296
nuclear@0 297 // TODO: implement 4D vector reflection
nuclear@0 298 Vector4 Vector4::reflection(const Vector4 &normal) const
nuclear@0 299 {
nuclear@0 300 return *this;
nuclear@0 301 }
nuclear@0 302
nuclear@0 303 // TODO: implement 4D vector refraction
nuclear@0 304 Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const
nuclear@0 305 {
nuclear@0 306 return *this;
nuclear@0 307 }
nuclear@0 308
nuclear@0 309 std::ostream &operator <<(std::ostream &out, const Vector4 &vec)
nuclear@0 310 {
nuclear@0 311 out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]";
nuclear@0 312 return out;
nuclear@0 313 }