nuclear@0: #include "vector.h" nuclear@0: #include "vmath.h" nuclear@0: nuclear@0: // ---------- Vector2 ----------- nuclear@0: nuclear@0: Vector2::Vector2(scalar_t x, scalar_t y) nuclear@0: { nuclear@0: this->x = x; nuclear@0: this->y = y; nuclear@0: } nuclear@0: nuclear@0: Vector2::Vector2(const vec2_t &vec) nuclear@0: { nuclear@0: x = vec.x; nuclear@0: y = vec.y; nuclear@0: } nuclear@0: nuclear@0: Vector2::Vector2(const Vector3 &vec) nuclear@0: { nuclear@0: x = vec.x; nuclear@0: y = vec.y; nuclear@0: } nuclear@0: nuclear@0: Vector2::Vector2(const Vector4 &vec) nuclear@0: { nuclear@0: x = vec.x; nuclear@0: y = vec.y; nuclear@0: } nuclear@0: nuclear@0: void Vector2::normalize() nuclear@0: { nuclear@0: scalar_t len = length(); nuclear@0: x /= len; nuclear@0: y /= len; nuclear@0: } nuclear@0: nuclear@0: Vector2 Vector2::normalized() const nuclear@0: { nuclear@0: scalar_t len = length(); nuclear@0: return Vector2(x / len, y / len); nuclear@0: } nuclear@0: nuclear@0: void Vector2::transform(const Matrix3x3 &mat) nuclear@0: { nuclear@0: scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2]; nuclear@0: y = mat[1][0] * x + mat[1][1] * y + mat[1][2]; nuclear@0: x = nx; nuclear@0: } nuclear@0: nuclear@0: Vector2 Vector2::transformed(const Matrix3x3 &mat) const nuclear@0: { nuclear@0: Vector2 vec; nuclear@0: vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2]; nuclear@0: vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2]; nuclear@0: return vec; nuclear@0: } nuclear@0: nuclear@0: void Vector2::rotate(scalar_t angle) nuclear@0: { nuclear@0: *this = Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y); nuclear@0: } nuclear@0: nuclear@0: Vector2 Vector2::rotated(scalar_t angle) const nuclear@0: { nuclear@0: return Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y); nuclear@0: } nuclear@0: nuclear@0: Vector2 Vector2::reflection(const Vector2 &normal) const nuclear@0: { nuclear@0: return 2.0 * dot_product(*this, normal) * normal - *this; nuclear@0: } nuclear@0: nuclear@0: Vector2 Vector2::refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const nuclear@0: { nuclear@0: // quick and dirty implementation :) nuclear@0: Vector3 v3refr = Vector3(this->x, this->y, 1.0).refraction(Vector3(this->x, this->y, 1), src_ior, dst_ior); nuclear@0: return Vector2(v3refr.x, v3refr.y); nuclear@0: } nuclear@0: nuclear@0: std::ostream &operator <<(std::ostream &out, const Vector2 &vec) nuclear@0: { nuclear@0: out << "[" << vec.x << " " << vec.y << "]"; nuclear@0: return out; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: nuclear@0: // --------- Vector3 ---------- nuclear@0: nuclear@0: Vector3::Vector3(scalar_t x, scalar_t y, scalar_t z) nuclear@0: { nuclear@0: this->x = x; nuclear@0: this->y = y; nuclear@0: this->z = z; nuclear@0: } nuclear@0: nuclear@0: Vector3::Vector3(const vec3_t &vec) nuclear@0: { nuclear@0: x = vec.x; nuclear@0: y = vec.y; nuclear@0: z = vec.z; nuclear@0: } nuclear@0: nuclear@0: Vector3::Vector3(const Vector2 &vec) nuclear@0: { nuclear@0: x = vec.x; nuclear@0: y = vec.y; nuclear@0: z = 1; nuclear@0: } nuclear@0: nuclear@0: Vector3::Vector3(const Vector4 &vec) nuclear@0: { nuclear@0: x = vec.x; nuclear@0: y = vec.y; nuclear@0: z = vec.z; nuclear@0: } nuclear@0: nuclear@0: Vector3::Vector3(const SphVector &sph) nuclear@0: { nuclear@0: *this = sph; nuclear@0: } nuclear@0: nuclear@0: Vector3 &Vector3::operator =(const SphVector &sph) nuclear@0: { nuclear@0: x = sph.r * cos(sph.theta) * sin(sph.phi); nuclear@0: z = sph.r * sin(sph.theta) * sin(sph.phi); nuclear@0: y = sph.r * cos(sph.phi); nuclear@0: return *this; nuclear@0: } nuclear@0: nuclear@0: void Vector3::normalize() nuclear@0: { nuclear@0: scalar_t len = length(); nuclear@0: x /= len; nuclear@0: y /= len; nuclear@0: z /= len; nuclear@0: } nuclear@0: nuclear@0: Vector3 Vector3::normalized() const nuclear@0: { nuclear@0: scalar_t len = length(); nuclear@0: return Vector3(x / len, y / len, z / len); nuclear@0: } nuclear@0: nuclear@0: Vector3 Vector3::reflection(const Vector3 &normal) const nuclear@0: { nuclear@0: return 2.0 * dot_product(*this, normal) * normal - *this; nuclear@0: } nuclear@0: nuclear@0: Vector3 Vector3::refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const nuclear@0: { nuclear@0: return refraction(normal, src_ior / dst_ior); nuclear@0: } nuclear@0: nuclear@0: Vector3 Vector3::refraction(const Vector3 &normal, scalar_t ior) const nuclear@0: { nuclear@0: scalar_t cos_inc = dot_product(*this, -normal); nuclear@0: nuclear@0: scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0); nuclear@0: nuclear@0: if(radical < 0.0) { // total internal reflection nuclear@0: return -reflection(normal); nuclear@0: } nuclear@0: nuclear@0: scalar_t beta = ior * cos_inc - sqrt(radical); nuclear@0: nuclear@0: return *this * ior + normal * beta; nuclear@0: } nuclear@0: nuclear@0: void Vector3::transform(const Matrix3x3 &mat) nuclear@0: { nuclear@0: scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z; nuclear@0: scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z; nuclear@0: z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z; nuclear@0: x = nx; nuclear@0: y = ny; nuclear@0: } nuclear@0: nuclear@0: Vector3 Vector3::transformed(const Matrix3x3 &mat) const nuclear@0: { nuclear@0: Vector3 vec; nuclear@0: vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z; nuclear@0: vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z; nuclear@0: vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z; nuclear@0: return vec; nuclear@0: } nuclear@0: nuclear@0: void Vector3::transform(const Matrix4x4 &mat) nuclear@0: { nuclear@0: scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3]; nuclear@0: scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3]; nuclear@0: z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3]; nuclear@0: x = nx; nuclear@0: y = ny; nuclear@0: } nuclear@0: nuclear@0: Vector3 Vector3::transformed(const Matrix4x4 &mat) const nuclear@0: { nuclear@0: Vector3 vec; nuclear@0: vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3]; nuclear@0: vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3]; nuclear@0: vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3]; nuclear@0: return vec; nuclear@0: } nuclear@0: nuclear@0: void Vector3::transform(const Quaternion &quat) nuclear@0: { nuclear@0: Quaternion vq(0.0f, *this); nuclear@0: vq = quat * vq * quat.inverse(); nuclear@0: *this = vq.v; nuclear@0: } nuclear@0: nuclear@0: Vector3 Vector3::transformed(const Quaternion &quat) const nuclear@0: { nuclear@0: Quaternion vq(0.0f, *this); nuclear@0: vq = quat * vq * quat.inverse(); nuclear@0: return vq.v; nuclear@0: } nuclear@0: nuclear@0: void Vector3::rotate(const Vector3 &euler) nuclear@0: { nuclear@0: Matrix4x4 rot; nuclear@0: rot.set_rotation(euler); nuclear@0: transform(rot); nuclear@0: } nuclear@0: nuclear@0: Vector3 Vector3::rotated(const Vector3 &euler) const nuclear@0: { nuclear@0: Matrix4x4 rot; nuclear@0: rot.set_rotation(euler); nuclear@0: return transformed(rot); nuclear@0: } nuclear@0: nuclear@0: std::ostream &operator <<(std::ostream &out, const Vector3 &vec) nuclear@0: { nuclear@0: out << "[" << vec.x << " " << vec.y << " " << vec.z << "]"; nuclear@0: return out; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: // -------------- Vector4 -------------- nuclear@0: Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w) nuclear@0: { nuclear@0: this->x = x; nuclear@0: this->y = y; nuclear@0: this->z = z; nuclear@0: this->w = w; nuclear@0: } nuclear@0: nuclear@0: Vector4::Vector4(const vec4_t &vec) nuclear@0: { nuclear@0: x = vec.x; nuclear@0: y = vec.y; nuclear@0: z = vec.z; nuclear@0: w = vec.w; nuclear@0: } nuclear@0: nuclear@0: Vector4::Vector4(const Vector2 &vec) nuclear@0: { nuclear@0: x = vec.x; nuclear@0: y = vec.y; nuclear@0: z = 1; nuclear@0: w = 1; nuclear@0: } nuclear@0: nuclear@0: Vector4::Vector4(const Vector3 &vec) nuclear@0: { nuclear@0: x = vec.x; nuclear@0: y = vec.y; nuclear@0: z = vec.z; nuclear@0: w = 1; nuclear@0: } nuclear@0: nuclear@0: void Vector4::normalize() nuclear@0: { nuclear@0: scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w); nuclear@0: x /= len; nuclear@0: y /= len; nuclear@0: z /= len; nuclear@0: w /= len; nuclear@0: } nuclear@0: nuclear@0: Vector4 Vector4::normalized() const nuclear@0: { nuclear@0: scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w); nuclear@0: return Vector4(x / len, y / len, z / len, w / len); nuclear@0: } nuclear@0: nuclear@0: void Vector4::transform(const Matrix4x4 &mat) nuclear@0: { nuclear@0: scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w; nuclear@0: scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w; nuclear@0: scalar_t nz = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w; nuclear@0: w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w; nuclear@0: x = nx; nuclear@0: y = ny; nuclear@0: z = nz; nuclear@0: } nuclear@0: nuclear@0: Vector4 Vector4::transformed(const Matrix4x4 &mat) const nuclear@0: { nuclear@0: Vector4 vec; nuclear@0: vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w; nuclear@0: vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w; nuclear@0: vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w; nuclear@0: vec.w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w; nuclear@0: return vec; nuclear@0: } nuclear@0: nuclear@0: // TODO: implement 4D vector reflection nuclear@0: Vector4 Vector4::reflection(const Vector4 &normal) const nuclear@0: { nuclear@0: return *this; nuclear@0: } nuclear@0: nuclear@0: // TODO: implement 4D vector refraction nuclear@0: Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const nuclear@0: { nuclear@0: return *this; nuclear@0: } nuclear@0: nuclear@0: std::ostream &operator <<(std::ostream &out, const Vector4 &vec) nuclear@0: { nuclear@0: out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]"; nuclear@0: return out; nuclear@0: }