// Copyright (C) 2012 Rémi Bèges - Jérôme Leclercq // This file is part of the "Nazara Engine - Mathematics module" // For conditions of distribution and use, see copyright notice in Config.hpp #include #include #include #include #include #include #include #define F(a) static_cast(a) template NzVector3::NzVector3(T X, T Y, T Z) { Set(X, Y, Z); } template NzVector3::NzVector3(T scale) { Set(scale); } template NzVector3::NzVector3(const T vec[3]) { Set(vec); } template NzVector3::NzVector3(const NzVector2& vec, T Z) { Set(vec, Z); } template template NzVector3::NzVector3(const NzVector3& vec) { Set(vec); } template T NzVector3::AbsDotProduct(const NzVector3& vec) const { return std::fabs(x * vec.x) + std::fabs(y * vec.y) + std::fabs(z * vec.z); } template<> inline int NzVector3::AbsDotProduct(const NzVector3& vec) const { return std::labs(x * vec.x) + std::labs(y * vec.y) + std::labs(z * vec.z); } template<> inline unsigned int NzVector3::AbsDotProduct(const NzVector3& vec) const { return std::labs(x * vec.x) + std::labs(y * vec.y) + std::labs(z * vec.z); } template NzVector3 NzVector3::CrossProduct(const NzVector3& vec) const { return NzVector3(y * vec.z - z * vec.y, z * vec.x - x * vec.z, x * vec.y - y * vec.x); } template T NzVector3::Distance(const NzVector3& vec) const { return std::sqrt(SquaredDistance(vec)); } template float NzVector3::Distancef(const NzVector3& vec) const { return std::sqrt(static_cast(SquaredDistance())); } template T NzVector3::DotProduct(const NzVector3& vec) const { return x*vec.x + y*vec.y + z*vec.z; } template NzVector3 NzVector3::GetNormal() const { NzVector3 vec(*this); vec.Normalize(); return vec; } template T NzVector3::Length() const { return std::sqrt(SquaredLength()); } template float NzVector3::Lengthf() const { return std::sqrt(static_cast(SquaredLength())); } template NzVector3& NzVector3::MakeForward() { return Set(F(0.0), F(0.0), F(-1.0)); } template NzVector3& NzVector3::MakeLeft() { return Set(F(-1.0), F(0.0), F(0.0)); } template NzVector3& NzVector3::MakeUnitX() { return Set(F(1.0), F(0.0), F(0.0)); } template NzVector3& NzVector3::MakeUnitY() { return Set(F(0.0), F(1.0), F(0.0)); } template NzVector3& NzVector3::MakeUnitZ() { return Set(F(0.0), F(0.0), F(1.0)); } template NzVector3& NzVector3::MakeUp() { return Set(F(0.0), F(1.0), F(0.0)); } template NzVector3& NzVector3::MakeZero() { return Set(F(0.0), F(0.0), F(0.0)); } template NzVector3& NzVector3::Maximize(const NzVector3& vec) { if (vec.x > x) x = vec.x; if (vec.y > y) y = vec.y; if (vec.z > z) z = vec.z; return *this; } template NzVector3& NzVector3::Minimize(const NzVector3& vec) { if (vec.x < x) x = vec.x; if (vec.y < y) y = vec.y; if (vec.z < z) z = vec.z; return *this; } template NzVector3& NzVector3::Normalize(T* length) { T norm = std::sqrt(SquaredLength()); T invNorm = F(1.0) / norm; x *= invNorm; y *= invNorm; z *= invNorm; if (length) *length = norm; return *this; } template NzVector3& NzVector3::Set(T X, T Y, T Z) { x = X; y = Y; z = Z; return *this; } template NzVector3& NzVector3::Set(T scale) { x = scale; y = scale; z = scale; return *this; } template NzVector3& NzVector3::Set(const T vec[3]) { std::memcpy(&x, vec, 3*sizeof(T)); return *this; } template NzVector3& NzVector3::Set(const NzVector2& vec, T Z) { x = vec.x; y = vec.y; z = Z; return *this; } template template NzVector3& NzVector3::Set(const NzVector3& vec) { x = F(vec.x); y = F(vec.y); z = F(vec.z); return *this; } template T NzVector3::SquaredDistance(const NzVector3& vec) const { return operator-(vec).SquaredLength(); } template T NzVector3::SquaredLength() const { return x*x + y*y + z*z; } template NzString NzVector3::ToString() const { NzStringStream ss; return ss << "Vector3(" << x << ", " << y << ", " << z <<')'; } template NzVector3::operator NzString() const { return ToString(); } template NzVector3::operator T*() { return &x; } template NzVector3::operator const T*() const { return &x; } template T& NzVector3::operator[](unsigned int i) { #if NAZARA_MATH_SAFE if (i >= 3) { NzStringStream ss; ss << "Index out of range: (" << i << " >= 3)"; NazaraError(ss); throw std::out_of_range(ss.ToString()); } #endif return *(&x+i); } template T NzVector3::operator[](unsigned int i) const { #if NAZARA_MATH_SAFE if (i >= 3) { NzStringStream ss; ss << "Index out of range: (" << i << " >= 3)"; NazaraError(ss); throw std::out_of_range(ss.ToString()); } #endif return *(&x+i); } template const NzVector3& NzVector3::operator+() const { return *this; } template NzVector3 NzVector3::operator-() const { return NzVector3(-x, -y, -z); } template NzVector3 NzVector3::operator+(const NzVector3& vec) const { return NzVector3(x + vec.x, y + vec.y, z + vec.z); } template NzVector3 NzVector3::operator-(const NzVector3& vec) const { return NzVector3(x - vec.x, y - vec.y, z - vec.z); } template NzVector3 NzVector3::operator*(const NzVector3& vec) const { return NzVector3(x * vec.x, y * vec.y, z * vec.z); } template NzVector3 NzVector3::operator*(T scale) const { return NzVector3(x * scale, y * scale, z * scale); } template NzVector3 NzVector3::operator/(const NzVector3& vec) const { #if NAZARA_MATH_SAFE if (NzNumberEquals(vec.x, F(0.0)) || NzNumberEquals(vec.y, F(0.0)) || NzNumberEquals(vec.z, F(0.0))) { NzString error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif return NzVector3(x / vec.x, y / vec.y, z / vec.z); } template NzVector3 NzVector3::operator/(T scale) const { #if NAZARA_MATH_SAFE if (NzNumberEquals(scale, F(0.0))) { NzString error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif return NzVector3(x / scale, y / scale, z / scale); } template NzVector3& NzVector3::operator+=(const NzVector3& vec) { x += vec.x; y += vec.y; z += vec.z; return *this; } template NzVector3& NzVector3::operator-=(const NzVector3& vec) { x -= vec.x; y -= vec.y; z -= vec.z; return *this; } template NzVector3& NzVector3::operator*=(const NzVector3& vec) { x *= vec.x; y *= vec.y; z *= vec.z; return *this; } template NzVector3& NzVector3::operator*=(T scale) { x *= scale; y *= scale; z *= scale; return *this; } template NzVector3& NzVector3::operator/=(const NzVector3& vec) { if (NzNumberEquals(vec.x, F(0.0)) || NzNumberEquals(vec.y, F(0.0)) || NzNumberEquals(vec.z, F(0.0))) { NzString error("Division by zero"); NazaraError(error); throw std::domain_error(error); } x /= vec.x; y /= vec.y; z /= vec.z; return *this; } template NzVector3& NzVector3::operator/=(T scale) { if (NzNumberEquals(scale, F(0.0))) { NzString error("Division by zero"); NazaraError(error); throw std::domain_error(error); } x /= scale; y /= scale; z /= scale; return *this; } template bool NzVector3::operator==(const NzVector3& vec) const { return NzNumberEquals(x, vec.x) && NzNumberEquals(y, vec.y) && NzNumberEquals(z, vec.z); } template bool NzVector3::operator!=(const NzVector3& vec) const { return !operator==(vec); } template bool NzVector3::operator<(const NzVector3& vec) const { if (x == vec.x) { if (y < vec.y) return z < vec.z; else return y < vec.y; } else return x < vec.x; } template bool NzVector3::operator<=(const NzVector3& vec) const { if (x == vec.x) { if (y < vec.y) return z <= vec.z; else return y < vec.y; } else return x < vec.x; } template bool NzVector3::operator>(const NzVector3& vec) const { return !operator<=(vec); } template bool NzVector3::operator>=(const NzVector3& vec) const { return !operator<(vec); } template NzVector3 NzVector3::CrossProduct(const NzVector3& vec1, const NzVector3& vec2) { return vec1.CrossProduct(vec2); } template T NzVector3::DotProduct(const NzVector3& vec1, const NzVector3& vec2) { return vec1.DotProduct(vec2); } template NzVector3 NzVector3::Forward() { NzVector3 vector; vector.MakeForward(); return vector; } template NzVector3 NzVector3::Left() { NzVector3 vector; vector.MakeLeft(); return vector; } template NzVector3 NzVector3::Lerp(const NzVector3& from, const NzVector3& to, T interpolation) { return NzLerp(from, to, interpolation); } template NzVector3 NzVector3::Normalize(const NzVector3& vec) { return vec.GetNormal(); } template NzVector3 NzVector3::UnitX() { NzVector3 vector; vector.MakeUnitX(); return vector; } template NzVector3 NzVector3::UnitY() { NzVector3 vector; vector.MakeUnitY(); return vector; } template NzVector3 NzVector3::UnitZ() { NzVector3 vector; vector.MakeUnitZ(); return vector; } template NzVector3 NzVector3::Up() { NzVector3 vector; vector.MakeUp(); return vector; } template NzVector3 NzVector3::Zero() { NzVector3 vector; vector.MakeZero(); return vector; } template std::ostream& operator<<(std::ostream& out, const NzVector3& vec) { return out << vec.ToString(); } template NzVector3 operator*(T scale, const NzVector3& vec) { return NzVector3(scale * vec.x, scale * vec.y, scale * vec.z); } template NzVector3 operator/(T scale, const NzVector3& vec) { #if NAZARA_MATH_SAFE if (NzNumberEquals(vec.x, F(0.0)) || NzNumberEquals(vec.y, F(0.0)) || NzNumberEquals(vec.z, F(0.0))) { NzString error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif return NzVector3(scale / vec.x, scale / vec.y, scale / vec.z); } #undef F #include