vrshoot
diff libs/vmath/vector.cc @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/vmath/vector.cc Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,326 @@ 1.4 +#include "vector.h" 1.5 +#include "vmath.h" 1.6 + 1.7 +// ---------- Vector2 ----------- 1.8 + 1.9 +Vector2::Vector2(scalar_t x, scalar_t y) 1.10 +{ 1.11 + this->x = x; 1.12 + this->y = y; 1.13 +} 1.14 + 1.15 +Vector2::Vector2(const vec2_t &vec) 1.16 +{ 1.17 + x = vec.x; 1.18 + y = vec.y; 1.19 +} 1.20 + 1.21 +Vector2::Vector2(const Vector3 &vec) 1.22 +{ 1.23 + x = vec.x; 1.24 + y = vec.y; 1.25 +} 1.26 + 1.27 +Vector2::Vector2(const Vector4 &vec) 1.28 +{ 1.29 + x = vec.x; 1.30 + y = vec.y; 1.31 +} 1.32 + 1.33 +void Vector2::normalize() 1.34 +{ 1.35 + scalar_t len = length(); 1.36 + x /= len; 1.37 + y /= len; 1.38 +} 1.39 + 1.40 +Vector2 Vector2::normalized() const 1.41 +{ 1.42 + scalar_t len = length(); 1.43 + return Vector2(x / len, y / len); 1.44 +} 1.45 + 1.46 +void Vector2::transform(const Matrix3x3 &mat) 1.47 +{ 1.48 + scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2]; 1.49 + y = mat[1][0] * x + mat[1][1] * y + mat[1][2]; 1.50 + x = nx; 1.51 +} 1.52 + 1.53 +Vector2 Vector2::transformed(const Matrix3x3 &mat) const 1.54 +{ 1.55 + Vector2 vec; 1.56 + vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2]; 1.57 + vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2]; 1.58 + return vec; 1.59 +} 1.60 + 1.61 +void Vector2::rotate(scalar_t angle) 1.62 +{ 1.63 + *this = Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y); 1.64 +} 1.65 + 1.66 +Vector2 Vector2::rotated(scalar_t angle) const 1.67 +{ 1.68 + return Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y); 1.69 +} 1.70 + 1.71 +Vector2 Vector2::reflection(const Vector2 &normal) const 1.72 +{ 1.73 + return 2.0 * dot_product(*this, normal) * normal - *this; 1.74 +} 1.75 + 1.76 +Vector2 Vector2::refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const 1.77 +{ 1.78 + // quick and dirty implementation :) 1.79 + Vector3 v3refr = Vector3(this->x, this->y, 1.0).refraction(Vector3(this->x, this->y, 1), src_ior, dst_ior); 1.80 + return Vector2(v3refr.x, v3refr.y); 1.81 +} 1.82 + 1.83 +std::ostream &operator <<(std::ostream &out, const Vector2 &vec) 1.84 +{ 1.85 + out << "[" << vec.x << " " << vec.y << "]"; 1.86 + return out; 1.87 +} 1.88 + 1.89 + 1.90 + 1.91 +// --------- Vector3 ---------- 1.92 + 1.93 +Vector3::Vector3(scalar_t x, scalar_t y, scalar_t z) 1.94 +{ 1.95 + this->x = x; 1.96 + this->y = y; 1.97 + this->z = z; 1.98 +} 1.99 + 1.100 +Vector3::Vector3(const vec3_t &vec) 1.101 +{ 1.102 + x = vec.x; 1.103 + y = vec.y; 1.104 + z = vec.z; 1.105 +} 1.106 + 1.107 +Vector3::Vector3(const Vector2 &vec) 1.108 +{ 1.109 + x = vec.x; 1.110 + y = vec.y; 1.111 + z = 1; 1.112 +} 1.113 + 1.114 +Vector3::Vector3(const Vector4 &vec) 1.115 +{ 1.116 + x = vec.x; 1.117 + y = vec.y; 1.118 + z = vec.z; 1.119 +} 1.120 + 1.121 +Vector3::Vector3(const SphVector &sph) 1.122 +{ 1.123 + *this = sph; 1.124 +} 1.125 + 1.126 +Vector3 &Vector3::operator =(const SphVector &sph) 1.127 +{ 1.128 + x = sph.r * cos(sph.theta) * sin(sph.phi); 1.129 + z = sph.r * sin(sph.theta) * sin(sph.phi); 1.130 + y = sph.r * cos(sph.phi); 1.131 + return *this; 1.132 +} 1.133 + 1.134 +void Vector3::normalize() 1.135 +{ 1.136 + scalar_t len = length(); 1.137 + x /= len; 1.138 + y /= len; 1.139 + z /= len; 1.140 +} 1.141 + 1.142 +Vector3 Vector3::normalized() const 1.143 +{ 1.144 + scalar_t len = length(); 1.145 + return Vector3(x / len, y / len, z / len); 1.146 +} 1.147 + 1.148 +Vector3 Vector3::reflection(const Vector3 &normal) const 1.149 +{ 1.150 + return 2.0 * dot_product(*this, normal) * normal - *this; 1.151 +} 1.152 + 1.153 +Vector3 Vector3::refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const 1.154 +{ 1.155 + return refraction(normal, src_ior / dst_ior); 1.156 +} 1.157 + 1.158 +Vector3 Vector3::refraction(const Vector3 &normal, scalar_t ior) const 1.159 +{ 1.160 + scalar_t cos_inc = dot_product(*this, -normal); 1.161 + 1.162 + scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0); 1.163 + 1.164 + if(radical < 0.0) { // total internal reflection 1.165 + return -reflection(normal); 1.166 + } 1.167 + 1.168 + scalar_t beta = ior * cos_inc - sqrt(radical); 1.169 + 1.170 + return *this * ior + normal * beta; 1.171 +} 1.172 + 1.173 +void Vector3::transform(const Matrix3x3 &mat) 1.174 +{ 1.175 + scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z; 1.176 + scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z; 1.177 + z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z; 1.178 + x = nx; 1.179 + y = ny; 1.180 +} 1.181 + 1.182 +Vector3 Vector3::transformed(const Matrix3x3 &mat) const 1.183 +{ 1.184 + Vector3 vec; 1.185 + vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z; 1.186 + vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z; 1.187 + vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z; 1.188 + return vec; 1.189 +} 1.190 + 1.191 +void Vector3::transform(const Matrix4x4 &mat) 1.192 +{ 1.193 + scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3]; 1.194 + scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3]; 1.195 + z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3]; 1.196 + x = nx; 1.197 + y = ny; 1.198 +} 1.199 + 1.200 +Vector3 Vector3::transformed(const Matrix4x4 &mat) const 1.201 +{ 1.202 + Vector3 vec; 1.203 + vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3]; 1.204 + vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3]; 1.205 + vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3]; 1.206 + return vec; 1.207 +} 1.208 + 1.209 +void Vector3::transform(const Quaternion &quat) 1.210 +{ 1.211 + Quaternion vq(0.0f, *this); 1.212 + vq = quat * vq * quat.inverse(); 1.213 + *this = vq.v; 1.214 +} 1.215 + 1.216 +Vector3 Vector3::transformed(const Quaternion &quat) const 1.217 +{ 1.218 + Quaternion vq(0.0f, *this); 1.219 + vq = quat * vq * quat.inverse(); 1.220 + return vq.v; 1.221 +} 1.222 + 1.223 +void Vector3::rotate(const Vector3 &euler) 1.224 +{ 1.225 + Matrix4x4 rot; 1.226 + rot.set_rotation(euler); 1.227 + transform(rot); 1.228 +} 1.229 + 1.230 +Vector3 Vector3::rotated(const Vector3 &euler) const 1.231 +{ 1.232 + Matrix4x4 rot; 1.233 + rot.set_rotation(euler); 1.234 + return transformed(rot); 1.235 +} 1.236 + 1.237 +std::ostream &operator <<(std::ostream &out, const Vector3 &vec) 1.238 +{ 1.239 + out << "[" << vec.x << " " << vec.y << " " << vec.z << "]"; 1.240 + return out; 1.241 +} 1.242 + 1.243 + 1.244 +// -------------- Vector4 -------------- 1.245 +Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w) 1.246 +{ 1.247 + this->x = x; 1.248 + this->y = y; 1.249 + this->z = z; 1.250 + this->w = w; 1.251 +} 1.252 + 1.253 +Vector4::Vector4(const vec4_t &vec) 1.254 +{ 1.255 + x = vec.x; 1.256 + y = vec.y; 1.257 + z = vec.z; 1.258 + w = vec.w; 1.259 +} 1.260 + 1.261 +Vector4::Vector4(const Vector2 &vec) 1.262 +{ 1.263 + x = vec.x; 1.264 + y = vec.y; 1.265 + z = 1; 1.266 + w = 1; 1.267 +} 1.268 + 1.269 +Vector4::Vector4(const Vector3 &vec) 1.270 +{ 1.271 + x = vec.x; 1.272 + y = vec.y; 1.273 + z = vec.z; 1.274 + w = 1; 1.275 +} 1.276 + 1.277 +void Vector4::normalize() 1.278 +{ 1.279 + scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w); 1.280 + x /= len; 1.281 + y /= len; 1.282 + z /= len; 1.283 + w /= len; 1.284 +} 1.285 + 1.286 +Vector4 Vector4::normalized() const 1.287 +{ 1.288 + scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w); 1.289 + return Vector4(x / len, y / len, z / len, w / len); 1.290 +} 1.291 + 1.292 +void Vector4::transform(const Matrix4x4 &mat) 1.293 +{ 1.294 + scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w; 1.295 + scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w; 1.296 + scalar_t nz = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w; 1.297 + w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w; 1.298 + x = nx; 1.299 + y = ny; 1.300 + z = nz; 1.301 +} 1.302 + 1.303 +Vector4 Vector4::transformed(const Matrix4x4 &mat) const 1.304 +{ 1.305 + Vector4 vec; 1.306 + vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w; 1.307 + vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w; 1.308 + vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w; 1.309 + vec.w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w; 1.310 + return vec; 1.311 +} 1.312 + 1.313 +// TODO: implement 4D vector reflection 1.314 +Vector4 Vector4::reflection(const Vector4 &normal) const 1.315 +{ 1.316 + return *this; 1.317 +} 1.318 + 1.319 +// TODO: implement 4D vector refraction 1.320 +Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const 1.321 +{ 1.322 + return *this; 1.323 +} 1.324 + 1.325 +std::ostream &operator <<(std::ostream &out, const Vector4 &vec) 1.326 +{ 1.327 + out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]"; 1.328 + return out; 1.329 +}