// Copyright (C) 2012 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 NzVector2::NzVector2() { } template NzVector2::NzVector2(T X, T Y) { Set(X, Y); } template NzVector2::NzVector2(T scale) { Set(scale); } template NzVector2::NzVector2(T vec[2]) { Set(vec); } template template NzVector2::NzVector2(const NzVector2& vec) { Set(vec); } template T NzVector2::AbsDotProduct(const NzVector2& vec) const { return std::fabs(x * vec.x) + std::fabs(y * vec.y); } template<> inline int NzVector2::AbsDotProduct(const NzVector2& vec) const { return std::labs(x * vec.x) + std::labs(y * vec.y); } template<> inline unsigned int NzVector2::AbsDotProduct(const NzVector2& vec) const { return std::labs(x * vec.x) + std::labs(y * vec.y); } template T NzVector2::Distance(const NzVector2& vec) const { return std::sqrt(SquaredDistance(vec)); } template float NzVector2::Distancef(const NzVector2& vec) const { return std::sqrt(static_cast(SquaredDistance(vec))); } template T NzVector2::DotProduct(const NzVector2& vec) const { return x*vec.x + y*vec.y; } template NzVector2 NzVector2::GetNormal() const { NzVector2 vec(*this); vec.Normalize(); return vec; } template T NzVector2::Length() const { return std::sqrt(SquaredLength()); } template float NzVector2::Lengthf() const { return std::sqrt(static_cast(SquaredLength())); } template void NzVector2::MakeUnitX() { Set(F(1.0), F(0.0)); } template void NzVector2::MakeUnitY() { Set(F(0.0), F(1.0)); } template void NzVector2::MakeZero() { Set(F(0.0), F(0.0)); } template void NzVector2::Maximize(const NzVector2& vec) { if (vec.x > x) x = vec.x; if (vec.y > y) y = vec.y; } template void NzVector2::Minimize(const NzVector2& vec) { if (vec.x < x) x = vec.x; if (vec.y < y) y = vec.y; } template void NzVector2::Normalize() { T squaredLength = SquaredLength(); if (squaredLength-F(1.0) > std::numeric_limits::epsilon()) { T length = std::sqrt(squaredLength); x /= length; y /= length; } } template void NzVector2::Set(T X, T Y) { x = X; y = Y; } template void NzVector2::Set(T scale) { x = scale; y = scale; } template void NzVector2::Set(T vec[2]) { std::memcpy(&x, vec, 2*sizeof(T)); } template template void NzVector2::Set(const NzVector2& vec) { x = F(vec.x); y = F(vec.y); } template T NzVector2::SquaredDistance(const NzVector2& vec) const { return operator-(vec).SquaredLength(); } template T NzVector2::SquaredLength() const { return x*x + y*y; } template NzString NzVector2::ToString() const { NzStringStream ss; return ss << "Vector2(" << x << ", " << y << ')'; } template NzVector2::operator NzString() const { return ToString(); } template NzVector2::operator T*() { return &x; } template NzVector2::operator const T*() const { return &x; } template T& NzVector2::operator[](unsigned int i) { #if NAZARA_MATH_SAFE if (i >= 2) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 2)"; throw std::domain_error(ss.ToString()); } #endif return *(&x+i); } template T NzVector2::operator[](unsigned int i) const { #if NAZARA_MATH_SAFE if (i >= 2) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 2)"; throw std::domain_error(ss.ToString()); } #endif return *(&x+i); } template const NzVector2& NzVector2::operator+() const { return *this; } template NzVector2 NzVector2::operator-() const { return NzVector2(-x, -y); } template NzVector2 NzVector2::operator+(const NzVector2& vec) const { return NzVector2(x + vec.x, y + vec.y); } template NzVector2 NzVector2::operator-(const NzVector2& vec) const { return NzVector2(x - vec.x, y - vec.y); } template NzVector2 NzVector2::operator*(const NzVector2& vec) const { return NzVector2(x * vec.x, y * vec.y); } template NzVector2 NzVector2::operator*(T scale) const { return NzVector2(x * scale, y * scale); } template NzVector2 NzVector2::operator/(const NzVector2& vec) const { #if NAZARA_MATH_SAFE if (NzNumberEquals(vec.x, F(0.0)) || NzNumberEquals(vec.y, F(0.0))) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Division by zero"; throw std::domain_error(ss.ToString()); } #endif return NzVector2(x / vec.x, y / vec.y); } template NzVector2 NzVector2::operator/(T scale) const { #if NAZARA_MATH_SAFE if (NzNumberEquals(scale, F(0.0))) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Division by zero"; throw std::domain_error(ss.ToString()); } #endif return NzVector2(x / scale, y / scale); } template NzVector2& NzVector2::operator+=(const NzVector2& vec) { x += vec.x; y += vec.y; return *this; } template NzVector2& NzVector2::operator-=(const NzVector2& vec) { x -= vec.x; y -= vec.y; return *this; } template NzVector2& NzVector2::operator*=(const NzVector2& vec) { x *= vec.x; y *= vec.y; return *this; } template NzVector2& NzVector2::operator*=(T scale) { x *= scale; y *= scale; return *this; } template NzVector2& NzVector2::operator/=(const NzVector2& vec) { #if NAZARA_MATH_SAFE if (NzNumberEquals(vec.x, F(0.0)) || NzNumberEquals(vec.y, F(0.0)) || NzNumberEquals(vec.z, F(0.0))) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Division by zero"; throw std::domain_error(ss.ToString()); } #endif x /= vec.x; y /= vec.y; return *this; } template NzVector2& NzVector2::operator/=(T scale) { #if NAZARA_MATH_SAFE if (NzNumberEquals(scale, F(0.0))) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Division by zero"; throw std::domain_error(ss.ToString()); } #endif x /= scale; y /= scale; return *this; } template bool NzVector2::operator==(const NzVector2& vec) const { return NzNumberEquals(x, vec.x) && NzNumberEquals(y, vec.y); } template bool NzVector2::operator!=(const NzVector2& vec) const { return !operator==(vec); } template bool NzVector2::operator<(const NzVector2& vec) const { return x < vec.x && y < vec.y; } template bool NzVector2::operator<=(const NzVector2& vec) const { return operator<(vec) || operator==(vec); } template bool NzVector2::operator>(const NzVector2& vec) const { return !operator<=(vec); } template bool NzVector2::operator>=(const NzVector2& vec) const { return !operator<(vec); } template NzVector2 NzVector2::UnitX() { NzVector2 vector; vector.MakeUnitX(); return vector; } template NzVector2 NzVector2::UnitY() { NzVector2 vector; vector.MakeUnitY(); return vector; } template NzVector2 NzVector2::Zero() { NzVector2 vector; vector.MakeZero(); return vector; } template std::ostream& operator<<(std::ostream& out, const NzVector2& vec) { return out << vec.ToString(); } template NzVector2 operator*(T scale, const NzVector2& vec) { return NzVector2(scale * vec.x, scale * vec.y); } template NzVector2 operator/(T scale, const NzVector2& vec) { #if NAZARA_MATH_SAFE if (NzNumberEquals(vec.x, F(0.0)) || NzNumberEquals(vec.y, F(0.0)) || NzNumberEquals(vec.z, F(0.0))) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Division by zero"; throw std::domain_error(ss.ToString()); } #endif return NzVector2(scale/vec.x, scale/vec.y); } #undef F #include