mSphere.h

Engine/source/math/mSphere.h

More...

Classes:

class

Detailed Description

 1
 2//-----------------------------------------------------------------------------
 3// Copyright (c) 2012 GarageGames, LLC
 4//
 5// Permission is hereby granted, free of charge, to any person obtaining a copy
 6// of this software and associated documentation files (the "Software"), to
 7// deal in the Software without restriction, including without limitation the
 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22//-----------------------------------------------------------------------------
23
24#ifndef _MSPHERE_H_
25#define _MSPHERE_H_
26
27#ifndef _MPOINT3_H_
28#include "math/mPoint3.h"
29#endif
30
31
32class SphereF
33{
34public:
35   Point3F center;
36   F32     radius;
37
38public:
39   SphereF() :radius(1.0f) {}
40   SphereF( const Point3F& in_rPosition, const F32 in_rRadius )
41    : center(in_rPosition),
42      radius(in_rRadius)
43   {
44      if ( radius < 0.0f )
45         radius = 0.0f;
46   }
47
48   bool isContained( const Point3F& in_rContain ) const;
49   bool isContained( const SphereF& in_rContain ) const;
50   bool isIntersecting( const SphereF& in_rIntersect ) const;
51   bool intersectsRay( const Point3F &start, const Point3F &end ) const;
52
53   F32 distanceTo( const Point3F &pt ) const;
54   F32 squareDistanceTo( const Point3F &pt ) const;
55};
56
57//-------------------------------------- INLINES
58//
59inline bool SphereF::isContained( const Point3F& in_rContain ) const
60{
61   F32 distSq = (center - in_rContain).lenSquared();
62
63   return (distSq <= (radius * radius));
64}
65
66inline bool SphereF::isContained( const SphereF& in_rContain ) const
67{
68   if (radius < in_rContain.radius)
69      return false;
70
71   // Since our radius is guaranteed to be >= other's, we
72   //  can dodge the sqrt() here.
73   //
74   F32 dist = (in_rContain.center - center).lenSquared();
75
76   return (dist <= ((radius - in_rContain.radius) *
77                    (radius - in_rContain.radius)));
78}
79
80inline bool SphereF::isIntersecting( const SphereF& in_rIntersect ) const
81{
82   F32 distSq = (in_rIntersect.center - center).lenSquared();
83
84   return (distSq <= ((in_rIntersect.radius + radius) *
85                      (in_rIntersect.radius + radius)));
86}
87
88inline F32 SphereF::distanceTo( const Point3F &toPt ) const
89{
90   return (center - toPt).len() - radius;
91}
92
93#endif //_SPHERE_H_
94