// Copyright (C) 2015 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 Sphere::Sphere(T X, T Y, T Z, T Radius) { Set(X, Y, Z, Radius); } /* template Sphere::Sphere(const Circle& circle) { Set(rect); } */ template Sphere::Sphere(const Vector3& center, T Radius) { Set(center, Radius); } template Sphere::Sphere(const T sphere[4]) { Set(sphere); } template template Sphere::Sphere(const Sphere& sphere) { Set(sphere); } template bool Sphere::Contains(T X, T Y, T Z) const { return SquaredDistance(X, Y, Z) <= radius*radius; } template bool Sphere::Contains(const Box& box) const { if (box.GetMinimum().SquaredDistance(GetPosition()) <= radius * radius) { if (box.GetMaximum().SquaredDistance(GetPosition()) <= radius * radius) return true; } return false; } template bool Sphere::Contains(const Vector3& point) const { return Contains(point.x, point.y, point.z); } template T Sphere::Distance(T X, T Y, T Z) const { Vector3 distance(X-x, Y-y, Z-z); return distance.GetLength(); } template T Sphere::Distance(const Vector3& point) const { return Distance(point.x, point.y, point.z); } template Sphere& Sphere::ExtendTo(T X, T Y, T Z) { T distance = SquaredDistance(X, Y, Z); if (distance > radius*radius) radius = std::sqrt(distance); return *this; } template Sphere& Sphere::ExtendTo(const Vector3& point) { return ExtendTo(point.x, point.y, point.z); } template Vector3 Sphere::GetNegativeVertex(const Vector3& normal) const { Vector3 neg(GetPosition()); neg -= normal * radius; return neg; } template Vector3 Sphere::GetPosition() const { return Vector3(x, y, z); } template Vector3 Sphere::GetPositiveVertex(const Vector3& normal) const { Vector3 pos(GetPosition()); pos += normal * radius; return pos; } template bool Sphere::Intersect(const Box& box) const { // Arvo's algorithm. T squaredDistance = T(0.0); if (x < box.x) { T diff = x - box.x; squaredDistance += diff*diff; } else if (x > box.x + box.width) { T diff = x - (box.x + box.width); squaredDistance += diff*diff; } if (y < box.y) { T diff = y - box.y; squaredDistance += diff*diff; } else if (y > box.y + box.height) { T diff = y - (box.y + box.height); squaredDistance += diff*diff; } if (z < box.z) { T diff = z - box.z; squaredDistance += diff*diff; } else if (z > box.z + box.depth) { T diff = z - (box.z + box.depth); squaredDistance += diff*diff; } return squaredDistance <= radius * radius; } template bool Sphere::Intersect(const Sphere& sphere) const { return SquaredDistance(sphere.x, sphere.y, sphere.z) - radius*radius <= sphere.radius*sphere.radius; } template bool Sphere::IsValid() const { return radius > F(0.0); } template Sphere& Sphere::MakeZero() { x = F(0.0); y = F(0.0); z = F(0.0); radius = F(0.0); return *this; } template Sphere& Sphere::Set(T X, T Y, T Z, T Radius) { x = X; y = Y; z = Z; radius = Radius; return *this; } template Sphere& Sphere::Set(const Vector3& center, T Radius) { x = center.x; y = center.y; z = center.z; radius = Radius; return *this; } /* template Sphere& Sphere::Set(const Circle& circle) { x = circle.x; y = circle.y; z = F(0.0); radius = circle.radius; return *this; } */ template Sphere& Sphere::Set(const Sphere& sphere) { std::memcpy(this, &sphere, sizeof(Sphere)); return *this; } template Sphere& Sphere::Set(const T sphere[4]) { x = sphere[0]; y = sphere[1]; z = sphere[2]; radius = sphere[3]; return *this; } template template Sphere& Sphere::Set(const Sphere& sphere) { x = F(sphere.x); y = F(sphere.y); z = F(sphere.z); radius = F(sphere.radius); return *this; } template T Sphere::SquaredDistance(T X, T Y, T Z) const { Vector3 distance(X-x, Y-y, Z-z); return distance.GetSquaredLength(); } template T Sphere::SquaredDistance(const Vector3& point) const { return SquaredDistance(point.x, point.y, point.z); } template String Sphere::ToString() const { StringStream ss; return ss << "Sphere(" << x << ", " << y << ", " << z << "; " << radius << ')'; } template T& Sphere::operator[](unsigned int i) { #if NAZARA_MATH_SAFE if (i >= 4) { StringStream ss; ss << "Index out of range: (" << i << " >= 4)"; NazaraError(ss); throw std::domain_error(ss.ToString()); } #endif return *(&x+i); } template T Sphere::operator[](unsigned int i) const { #if NAZARA_MATH_SAFE if (i >= 4) { StringStream ss; ss << "Index out of range: (" << i << " >= 4)"; NazaraError(ss); throw std::domain_error(ss.ToString()); } #endif return *(&x+i); } template Sphere Sphere::operator*(T scalar) const { return Sphere(x, y, z, radius*scalar); } template Sphere& Sphere::operator*=(T scalar) { radius *= scalar; } template bool Sphere::operator==(const Sphere& sphere) const { return NumberEquals(x, sphere.x) && NumberEquals(y, sphere.y) && NumberEquals(z, sphere.z) && NumberEquals(radius, sphere.radius); } template bool Sphere::operator!=(const Sphere& sphere) const { return !operator==(sphere); } template Sphere Sphere::Lerp(const Sphere& from, const Sphere& to, T interpolation) { #ifdef NAZARA_DEBUG if (interpolation < F(0.0) || interpolation > F(1.0)) { NazaraError("Interpolation must be in range [0..1] (Got " + String::Number(interpolation) + ')'); return Zero(); } #endif Sphere sphere; sphere.x = Lerp(from.x, to.x, interpolation); sphere.y = Lerp(from.y, to.y, interpolation); sphere.z = Lerp(from.z, to.z, interpolation); sphere.radius = Lerp(from.radius, to.radius, interpolation); return sphere; } template Sphere Sphere::Zero() { Sphere sphere; sphere.MakeZero(); return sphere; } } template std::ostream& operator<<(std::ostream& out, const Nz::Sphere& sphere) { return out << sphere.ToString(); } #undef F #include