// Copyright (C) 2015 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 #define F(a) static_cast(a) namespace Nz { template Vector3::Vector3(T X, T Y, T Z) { Set(X, Y, Z); } template Vector3::Vector3(T X, const Vector2& vec) { Set(X, vec); } template Vector3::Vector3(T scale) { Set(scale); } template Vector3::Vector3(const T vec[3]) { Set(vec); } template Vector3::Vector3(const Vector2& vec, T Z) { Set(vec, Z); } template template Vector3::Vector3(const Vector3& vec) { Set(vec); } template Vector3::Vector3(const Vector4& vec) { Set(vec); } template T Vector3::AbsDotProduct(const Vector3& vec) const { return std::abs(x * vec.x) + std::abs(y * vec.y) + std::abs(z * vec.z); } template T Vector3::AngleBetween(const Vector3& vec) const { // sqrt(a) * sqrt(b) = sqrt(a*b) T divisor = std::sqrt(GetSquaredLength() * vec.GetSquaredLength()); #if NAZARA_MATH_SAFE if (NumberEquals(divisor, F(0.0))) { String error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif T alpha = DotProduct(vec)/divisor; return FromRadians(std::acos(Clamp(alpha, F(-1.0), F(1.0)))); } template Vector3 Vector3::CrossProduct(const Vector3& vec) const { return Vector3(y * vec.z - z * vec.y, z * vec.x - x * vec.z, x * vec.y - y * vec.x); } template T Vector3::Distance(const Vector3& vec) const { return std::sqrt(SquaredDistance(vec)); } template float Vector3::Distancef(const Vector3& vec) const { return std::sqrt(static_cast(SquaredDistance(vec))); } template T Vector3::DotProduct(const Vector3& vec) const { return x*vec.x + y*vec.y + z*vec.z; } template T Vector3::GetLength() const { return std::sqrt(GetSquaredLength()); } template float Vector3::GetLengthf() const { return std::sqrt(static_cast(GetSquaredLength())); } template Vector3 Vector3::GetNormal(T* length) const { Vector3 vec(*this); vec.Normalize(length); return vec; } template T Vector3::GetSquaredLength() const { return x*x + y*y + z*z; } template Vector3& Vector3::MakeBackward() { return Set(F(0.0), F(0.0), F(1.0)); } template Vector3& Vector3::MakeDown() { return Set(F(0.0), F(-1.0), F(0.0)); } template Vector3& Vector3::MakeForward() { return Set(F(0.0), F(0.0), F(-1.0)); } template Vector3& Vector3::MakeLeft() { return Set(F(-1.0), F(0.0), F(0.0)); } template Vector3& Vector3::MakeRight() { return Set(F(1.0), F(0.0), F(0.0)); } template Vector3& Vector3::MakeUnit() { return Set(F(1.0), F(1.0), F(1.0)); } template Vector3& Vector3::MakeUnitX() { return Set(F(1.0), F(0.0), F(0.0)); } template Vector3& Vector3::MakeUnitY() { return Set(F(0.0), F(1.0), F(0.0)); } template Vector3& Vector3::MakeUnitZ() { return Set(F(0.0), F(0.0), F(1.0)); } template Vector3& Vector3::MakeUp() { return Set(F(0.0), F(1.0), F(0.0)); } template Vector3& Vector3::MakeZero() { return Set(F(0.0), F(0.0), F(0.0)); } template Vector3& Vector3::Maximize(const Vector3& 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 Vector3& Vector3::Minimize(const Vector3& 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 Vector3& Vector3::Normalize(T* length) { T norm = GetLength(); if (norm > F(0.0)) { T invNorm = F(1.0) / norm; x *= invNorm; y *= invNorm; z *= invNorm; } if (length) *length = norm; return *this; } template Vector3& Vector3::Set(T X, T Y, T Z) { x = X; y = Y; z = Z; return *this; } template Vector3& Vector3::Set(T X, const Vector2& vec) { x = X; y = vec.x; z = vec.y; return *this; } template Vector3& Vector3::Set(T scale) { x = scale; y = scale; z = scale; return *this; } template Vector3& Vector3::Set(const T vec[3]) { std::memcpy(&x, vec, 3*sizeof(T)); return *this; } template Vector3& Vector3::Set(const Vector2& vec, T Z) { x = vec.x; y = vec.y; z = Z; return *this; } template Vector3& Vector3::Set(const Vector3& vec) { std::memcpy(this, &vec, sizeof(Vector3)); return *this; } template template Vector3& Vector3::Set(const Vector3& vec) { x = F(vec.x); y = F(vec.y); z = F(vec.z); return *this; } template Vector3& Vector3::Set(const Vector4& vec) { x = vec.x; y = vec.y; z = vec.z; return *this; } template T Vector3::SquaredDistance(const Vector3& vec) const { return (*this - vec).GetSquaredLength(); } template String Vector3::ToString() const { StringStream ss; return ss << "Vector3(" << x << ", " << y << ", " << z <<')'; } template Vector3::operator T*() { return &x; } template Vector3::operator const T*() const { return &x; } template const Vector3& Vector3::operator+() const { return *this; } template Vector3 Vector3::operator-() const { return Vector3(-x, -y, -z); } template Vector3 Vector3::operator+(const Vector3& vec) const { return Vector3(x + vec.x, y + vec.y, z + vec.z); } template Vector3 Vector3::operator-(const Vector3& vec) const { return Vector3(x - vec.x, y - vec.y, z - vec.z); } template Vector3 Vector3::operator*(const Vector3& vec) const { return Vector3(x * vec.x, y * vec.y, z * vec.z); } template Vector3 Vector3::operator*(T scale) const { return Vector3(x * scale, y * scale, z * scale); } template Vector3 Vector3::operator/(const Vector3& vec) const { #if NAZARA_MATH_SAFE if (NumberEquals(vec.x, F(0.0)) || NumberEquals(vec.y, F(0.0)) || NumberEquals(vec.z, F(0.0))) { String error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif return Vector3(x / vec.x, y / vec.y, z / vec.z); } template Vector3 Vector3::operator/(T scale) const { #if NAZARA_MATH_SAFE if (NumberEquals(scale, F(0.0))) { String error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif return Vector3(x / scale, y / scale, z / scale); } template Vector3& Vector3::operator+=(const Vector3& vec) { x += vec.x; y += vec.y; z += vec.z; return *this; } template Vector3& Vector3::operator-=(const Vector3& vec) { x -= vec.x; y -= vec.y; z -= vec.z; return *this; } template Vector3& Vector3::operator*=(const Vector3& vec) { x *= vec.x; y *= vec.y; z *= vec.z; return *this; } template Vector3& Vector3::operator*=(T scale) { x *= scale; y *= scale; z *= scale; return *this; } template Vector3& Vector3::operator/=(const Vector3& vec) { if (NumberEquals(vec.x, F(0.0)) || NumberEquals(vec.y, F(0.0)) || NumberEquals(vec.z, F(0.0))) { String error("Division by zero"); NazaraError(error); throw std::domain_error(error); } x /= vec.x; y /= vec.y; z /= vec.z; return *this; } template Vector3& Vector3::operator/=(T scale) { if (NumberEquals(scale, F(0.0))) { String error("Division by zero"); NazaraError(error); throw std::domain_error(error); } x /= scale; y /= scale; z /= scale; return *this; } template bool Vector3::operator==(const Vector3& vec) const { return NumberEquals(x, vec.x) && NumberEquals(y, vec.y) && NumberEquals(z, vec.z); } template bool Vector3::operator!=(const Vector3& vec) const { return !operator==(vec); } template bool Vector3::operator<(const Vector3& 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 Vector3::operator<=(const Vector3& 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 Vector3::operator>(const Vector3& vec) const { return !operator<=(vec); } template bool Vector3::operator>=(const Vector3& vec) const { return !operator<(vec); } template Vector3 Vector3::CrossProduct(const Vector3& vec1, const Vector3& vec2) { return vec1.CrossProduct(vec2); } template T Vector3::DotProduct(const Vector3& vec1, const Vector3& vec2) { return vec1.DotProduct(vec2); } template Vector3 Vector3::Backward() { Vector3 vector; vector.MakeBackward(); return vector; } template Vector3 Vector3::Down() { Vector3 vector; vector.MakeDown(); return vector; } template Vector3 Vector3::Forward() { Vector3 vector; vector.MakeForward(); return vector; } template Vector3 Vector3::Left() { Vector3 vector; vector.MakeLeft(); return vector; } template Vector3 Vector3::Lerp(const Vector3& from, const Vector3& to, T interpolation) { return Nz::Lerp(from, to, interpolation); } template Vector3 Vector3::Normalize(const Vector3& vec) { return vec.GetNormal(); } template Vector3 Vector3::Right() { Vector3 vector; vector.MakeRight(); return vector; } template Vector3 Vector3::Unit() { Vector3 vector; vector.MakeUnit(); return vector; } template Vector3 Vector3::UnitX() { Vector3 vector; vector.MakeUnitX(); return vector; } template Vector3 Vector3::UnitY() { Vector3 vector; vector.MakeUnitY(); return vector; } template Vector3 Vector3::UnitZ() { Vector3 vector; vector.MakeUnitZ(); return vector; } template Vector3 Vector3::Up() { Vector3 vector; vector.MakeUp(); return vector; } template Vector3 Vector3::Zero() { Vector3 vector; vector.MakeZero(); return vector; } } template std::ostream& operator<<(std::ostream& out, const Nz::Vector3& vec) { return out << vec.ToString(); } template Nz::Vector3 operator*(T scale, const Nz::Vector3& vec) { return Nz::Vector3(scale * vec.x, scale * vec.y, scale * vec.z); } template Nz::Vector3 operator/(T scale, const Nz::Vector3& vec) { #if NAZARA_MATH_SAFE if (Nz::NumberEquals(vec.x, F(0.0)) || Nz::NumberEquals(vec.y, F(0.0)) || Nz::NumberEquals(vec.z, F(0.0))) { Nz::String error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif return Nz::Vector3(scale / vec.x, scale / vec.y, scale / vec.z); } #undef F #include