summaryrefslogtreecommitdiff
path: root/Runtime/Geometry/Sphere.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-08-14 22:50:43 +0800
committerchai <chaifix@163.com>2019-08-14 22:50:43 +0800
commit15740faf9fe9fe4be08965098bbf2947e096aeeb (patch)
treea730ec236656cc8cab5b13f088adfaed6bb218fb /Runtime/Geometry/Sphere.cpp
+Unity Runtime codeHEADmaster
Diffstat (limited to 'Runtime/Geometry/Sphere.cpp')
-rw-r--r--Runtime/Geometry/Sphere.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/Runtime/Geometry/Sphere.cpp b/Runtime/Geometry/Sphere.cpp
new file mode 100644
index 0000000..19ddb3c
--- /dev/null
+++ b/Runtime/Geometry/Sphere.cpp
@@ -0,0 +1,57 @@
+#include "UnityPrefix.h"
+#include "Sphere.h"
+
+Sphere MergeSpheres (const Sphere& inSphere0, const Sphere& inSphere1);
+float MergeSpheresRadius (const Sphere& inSphere0, const Sphere& inSphere1);
+
+void Sphere::Set (const Vector3f* inVertices, UInt32 inHowmany)
+{
+ m_Radius = 0.0F;
+ m_Center = Vector3f::zero;
+ UInt32 i;
+ for (i=0;i<inHowmany;i++)
+ m_Radius = std::max (m_Radius, SqrMagnitude (inVertices[i]));
+ m_Radius = sqrt (m_Radius);
+}
+
+
+Sphere MergeSpheres (const Sphere& inSphere0, const Sphere& inSphere1)
+{
+ Vector3f kCDiff = inSphere1.GetCenter() - inSphere0.GetCenter();
+ float fLSqr = SqrMagnitude (kCDiff);
+ float fRDiff = inSphere1.GetRadius() - inSphere0.GetRadius();
+
+ if (fRDiff*fRDiff >= fLSqr)
+ return fRDiff >= 0.0 ? inSphere1 : inSphere0;
+
+ float fLength = sqrt (fLSqr);
+ const float fTolerance = 1.0e-06f;
+ Sphere kSphere;
+
+ if (fLength > fTolerance)
+ {
+ float fCoeff = (fLength + fRDiff) / (2.0 * fLength);
+ kSphere.GetCenter () = inSphere0.GetCenter () + fCoeff * kCDiff;
+ }
+ else
+ {
+ kSphere.GetCenter () = inSphere0.GetCenter ();
+ }
+
+ kSphere.GetRadius () = 0.5F * (fLength + inSphere0.GetRadius () + inSphere1.GetRadius ());
+
+ return kSphere;
+}
+
+float MergeSpheresRadius (const Sphere& inSphere0, const Sphere& inSphere1)
+{
+ Vector3f kCDiff = inSphere1.GetCenter() - inSphere0.GetCenter();
+ float fLSqr = SqrMagnitude (kCDiff);
+ float fRDiff = inSphere1.GetRadius () - inSphere0.GetRadius();
+
+ if (fRDiff*fRDiff >= fLSqr)
+ return fRDiff >= 0.0 ? inSphere1.GetRadius () : inSphere0.GetRadius ();
+
+ float fLength = sqrt (fLSqr);
+ return 0.5F * (fLength + inSphere0.GetRadius () + inSphere1.GetRadius ());
+}