OGLplus (0.52.0) a C++ wrapper for OpenGL

sphere.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_MATH_SPHERE_1310040710_HPP
14 #define OGLPLUS_MATH_SPHERE_1310040710_HPP
15 
16 #include <oglplus/math/vector.hpp>
17 
18 #include <cassert>
19 #include <type_traits>
20 
21 namespace oglplus {
22 
24 
28 template <typename T>
29 class Sphere
30 {
31 private:
32  Vector<T, 3> _center;
33  T _radius;
34 public:
36  Sphere(void)
37  : _center(T(0))
38  , _radius(T(1))
39  { }
40 
42 
45  Sphere(T x, T y, T z, T r)
46  : _center(x, y, z)
47  , _radius(r)
48  {
49  assert(_radius >= T(0));
50  }
51 
53 
56  Sphere(const Vector<T, 3>& position, T radius)
57  : _center(position)
58  , _radius(radius)
59  {
60  assert(_radius >= T(0));
61  }
62 
64  template <typename U>
65  Sphere(const Sphere<U>& that)
66  : _center(that.Center())
67  , _radius(that.Radius())
68  {
69  assert(_radius >= T(0));
70  }
71 
73  const Vector<T, 3>& Center(void) const
74  {
75  return _center;
76  }
77 
79  void Center(const Vector<T, 3>& position)
80  {
81  _center = position;
82  }
83 
85  void Translate(const Vector<T, 3>& offset)
86  {
87  _center += offset;
88  }
89 
91  void Transform(const Matrix<T, 4, 4>& matrix)
92  {
93  _center = (matrix * Vector<T, 4>(_center, 1)).xyz();
94  }
95 
97  bool Degenerate(void) const
98  {
99  return _radius == T(0);
100  }
101 
103 
106  T Radius(void) const
107  {
108  return _radius;
109  }
110 
112 
115  void Radius(T radius)
116  {
117  assert(_radius >= T(0));
118  _radius = radius;
119  }
120 
122 
125  void Grow(T amount)
126  {
127  assert(amount >= T(0));
128  _radius += amount;
129  }
130 
132 
135  void Shrink(T amount)
136  {
137  assert(amount >= T(0));
138  assert(amount <= Radius());
139  _radius -= amount;
140  }
141 
143 
146  void Scale(T amount)
147  {
148  assert(amount >= T(0));
149  _radius *= amount;
150  }
151 
153 
156  T Diameter(void) const
157  {
158  return 2*Radius();
159  }
160 
161 #if OGLPLUS_DOCUMENTATION_ONLY
162  friend bool Intersecting(const Sphere& a, const Sphere& b);
164 #endif
165 
167  bool IntersectsWith(const Sphere& that) const
168  {
169  T d = this->_radius + that._radius;
170  return Distance(this->_center, that._center) < d;
171  }
172 };
173 
174 #if OGLPLUS_DOCUMENTATION_ONLY || defined(GL_FLOAT)
175 typedef Sphere<GLfloat> Spheref;
177 #endif
178 
179 template <typename T>
180 bool Intersecting(const Sphere<T>& a, const Sphere<T>& b)
181 {
182  return a.IntersectsWith(b);
183 }
184 
185 } // namespace oglplus
186 
187 #endif // include guard
Definition: vector.hpp:14
bool IntersectsWith(const Sphere &that) const
Returns true if this sphere intersects with that sphere.
Definition: sphere.hpp:167
void Translate(const Vector< T, 3 > &offset)
Translates the sphere to a new position by offset.
Definition: sphere.hpp:85
void Scale(T amount)
Scales the sphere by the specified amount.
Definition: sphere.hpp:146
T Radius(void) const
Returns the radius of the sphere.
Definition: sphere.hpp:106
Sphere(const Sphere< U > &that)
Copy construction from angles using different underlying type.
Definition: sphere.hpp:65
T Diameter(void) const
Returns the diameter of the sphere.
Definition: sphere.hpp:156
friend bool Intersecting(const Sphere &a, const Sphere &b)
Returns true if spheres a and b are intersecting.
Definition: sphere.hpp:180
bool Degenerate(void) const
Returns true if the sphere is degenerate (has zero radius)
Definition: sphere.hpp:97
Sphere(const Vector< T, 3 > &position, T radius)
Constructs a sphere with the specified radius at the position.
Definition: sphere.hpp:56
void Grow(T amount)
Grows the sphere by the specified amount.
Definition: sphere.hpp:125
void Shrink(T amount)
Shrinks the sphere by the specified amount.
Definition: sphere.hpp:135
Sphere(void)
Constructs a unit sphere at the origin.
Definition: sphere.hpp:36
Sphere< GLfloat > Spheref
Instantiation of Sphere using GL floating-point as underlying type.
Definition: sphere.hpp:176
void Radius(T radius)
Sets the radius of the sphere.
Definition: sphere.hpp:115
const Vector< T, 3 > & Center(void) const
Returns the position of the center of the sphere.
Definition: sphere.hpp:73
Class implementing sphere-related functionality.
Definition: sphere.hpp:29
void Center(const Vector< T, 3 > &position)
Sets the position of the sphere.
Definition: sphere.hpp:79
Definition: vector.hpp:14
void Transform(const Matrix< T, 4, 4 > &matrix)
Transforms the spheres origin by the specified matrix.
Definition: sphere.hpp:91
Sphere(T x, T y, T z, T r)
Constructs a sphere at the specified position and the specified radius.
Definition: sphere.hpp:45
A vector class.

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).