// David Eberly, Geometric Tools, Redmond WA 98052 // Copyright (c) 1998-2020 // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt // Version: 4.0.2019.08.13 #pragma once #include #include #include namespace WwiseGTE { // Compute the smallest bounding sphere whose center is the average of // the input points. template bool GetContainer(int numPoints, Vector3 const* points, Sphere3& sphere) { sphere.center = points[0]; for (int i = 1; i < numPoints; ++i) { sphere.center += points[i]; } sphere.center /= (Real)numPoints; sphere.radius = (Real)0; for (int i = 0; i < numPoints; ++i) { Vector3 diff = points[i] - sphere.center; Real radiusSqr = Dot(diff, diff); if (radiusSqr > sphere.radius) { sphere.radius = radiusSqr; } } sphere.radius = std::sqrt(sphere.radius); return true; } template bool GetContainer(std::vector> const& points, Sphere3& sphere) { return GetContainer(static_cast(points.size()), points.data(), sphere); } // Test for containment of a point inside a sphere. template bool InContainer(Vector3 const& point, Sphere3 const& sphere) { Vector3 diff = point - sphere.center; return Length(diff) <= sphere.radius; } // Compute the smallest bounding sphere that contains the input spheres. template bool MergeContainers(Sphere3 const& sphere0, Sphere3 const& sphere1, Sphere3& merge) { Vector3 cenDiff = sphere1.center - sphere0.center; Real lenSqr = Dot(cenDiff, cenDiff); Real rDiff = sphere1.radius - sphere0.radius; Real rDiffSqr = rDiff * rDiff; if (rDiffSqr >= lenSqr) { merge = (rDiff >= (Real)0 ? sphere1 : sphere0); } else { Real length = std::sqrt(lenSqr); if (length > (Real)0) { Real coeff = (length + rDiff) / (((Real)2)*length); merge.center = sphere0.center + coeff * cenDiff; } else { merge.center = sphere0.center; } merge.radius = (Real)0.5 * (length + sphere0.radius + sphere1.radius); } return true; } }