summaryrefslogtreecommitdiff
path: root/Runtime/Geometry/Sphere.h
blob: e19e2f54dbd141e02e6fdb567480d61eda5af151 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef SPHERE_H
#define SPHERE_H

#include "Runtime/Serialize/SerializeUtility.h"
#include "Runtime/Math/Vector3.h"
#include <algorithm>
#include "Runtime/Modules/ExportModules.h"

class Sphere
{
	Vector3f	m_Center;
	float		m_Radius;
	
	public:
	
	DECLARE_SERIALIZE (Sphere)
	
	Sphere () {}
	Sphere (const Vector3f& p0, float r)				{Set (p0, r);}
	
	void Set (const Vector3f& p0)						{m_Center = p0;	m_Radius = 0;}
	void Set (const Vector3f& p0, float r)				{m_Center = p0;	m_Radius = r;}
	
	void Set (const Vector3f& p0, const Vector3f& p1);
	
	void Set (const Vector3f* inVertices, UInt32 inHowmany);
	
	Vector3f& GetCenter () {return m_Center;}
	const Vector3f& GetCenter ()const  {return m_Center;}
	
	float& GetRadius () {return m_Radius;}
	const float& GetRadius ()const {return m_Radius;}
	
	bool IsInside (const Sphere& inSphere)const;
};

float EXPORT_COREMODULE CalculateSqrDistance (const Vector3f& p, const Sphere& s);
bool Intersect (const Sphere& inSphere0, const Sphere& inSphere1);


inline void Sphere::Set (const Vector3f& p0, const Vector3f& p1)
{
	Vector3f dhalf = (p1 - p0) * 0.5;

	m_Center = dhalf + p0;
	m_Radius = Magnitude (dhalf);
}

inline bool Sphere::IsInside (const Sphere& inSphere)const
{
	float sqrDist = SqrMagnitude (GetCenter () - inSphere.GetCenter ());
	if (Sqr (GetRadius ()) > sqrDist + Sqr (inSphere.GetRadius ()))
		return true;
	else
		return false;
}

inline bool Intersect (const Sphere& inSphere0, const Sphere& inSphere1)
{
	float sqrDist = SqrMagnitude (inSphere0.GetCenter () - inSphere1.GetCenter ());
	if (Sqr (inSphere0.GetRadius () + inSphere1.GetRadius ()) > sqrDist)
		return true;
	else
		return false;
}

inline float CalculateSqrDistance (const Vector3f& p, const Sphere& s)
{
	return std::max (0.0F, SqrMagnitude (p - s.GetCenter ()) - Sqr (s.GetRadius ()));
}

template<class TransferFunction> inline
void Sphere::Transfer (TransferFunction& transfer)
{
	TRANSFER (m_Center);
	TRANSFER (m_Radius);
}

#endif