summaryrefslogtreecommitdiff
path: root/Runtime/Geometry/Sphere.cpp
blob: 19ddb3c4183a202172b5f2ad735981c600d479c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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 ());
}