Visualization Library 2.0.0-b3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Sphere.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Sphere_INCLUDE_ONCE
33 #define Sphere_INCLUDE_ONCE
34 
35 #include <vlCore/AABB.hpp>
36 
37 namespace vl
38 {
39 //-----------------------------------------------------------------------------
40 // Sphere
41 //-----------------------------------------------------------------------------
44  {
45  public:
47  Sphere(): mRadius(-1) { }
48 
50  Sphere(const vec3& center, real radius): mCenter(center), mRadius(radius) {}
51 
53  Sphere(const AABB& aabb) { *this = aabb; }
54 
56  void setNull() { mRadius =-1.0f; mCenter = vec3(0,0,0); }
57 
59  bool isNull() const { return mRadius < 0.0f; }
60 
62  bool isPoint() const { return mRadius == 0.0f; }
63 
65  void setCenter(const vec3& center) { mCenter = center; }
66 
68  const vec3& center() const { return mCenter; }
69 
71  void setRadius( real radius ) { mRadius = radius; }
72 
74  real radius() const { return mRadius; }
75 
77  bool includes(const Sphere& other) const
78  {
79  if (isNull())
80  return false;
81  else
82  if (other.isNull())
83  return true;
84  else
85  {
86  real distance = (center() - other.center()).length();
87  return radius() >= distance + other.radius();
88  }
89  }
90 
92  bool operator==(const Sphere& other) const
93  {
94  return mCenter == other.mCenter && mRadius == other.mRadius;
95  }
96 
98  bool operator!=(const Sphere& other) const
99  {
100  return !operator==(other);
101  }
102 
104  Sphere& operator=(const AABB& aabb)
105  {
106  if (aabb.isNull())
107  setNull();
108  else
109  {
110  mCenter = aabb.center();
111  mRadius = (aabb.minCorner() - aabb.maxCorner()).length() / (real)2.0;
112  }
113  return *this;
114  }
115 
117  Sphere operator+(const Sphere& other)
118  {
119  Sphere t = *this;
120  return t += other;
121  }
122 
124  const Sphere& operator+=(const Sphere& other)
125  {
126  if (this->isNull())
127  *this = other;
128  else
129  if (other.includes(*this))
130  {
131  *this = other;
132  }
133  else
134  if (!other.isNull() && !this->includes(other))
135  {
136  vec3 v = other.center() - this->center();
137  if (v.isNull())
138  {
139  // the center remains the same
140  // sets the maximum radius
141  setRadius( radius() > other.radius() ? radius() : other.radius() );
142  }
143  else
144  {
145  v.normalize();
146  vec3 p0 = this->center() - v * this->radius();
147  vec3 p1 = other.center() + v * other.radius();
148  setCenter( (p0 + p1)*(real)0.5 );
149  setRadius( (p0 - p1).length()*(real)0.5 );
150  }
151  }
152 
153  return *this;
154  }
155 
157  void transformed(Sphere& out, const mat4& mat) const
158  {
159  out.setNull();
160  if ( !isNull() )
161  {
162  out.mCenter = mat * center();
163  // vec3 p = center() + vec3( (real)0.577350269189625840, (real)0.577350269189625840, (real)0.577350269189625840 ) * radius();
164  // p = mat * p;
165  // p = p - out.center();
166  // out.setRadius(p.length());
167  vec3 p0 = center() + vec3(radius(),0,0);
168  vec3 p1 = center() + vec3(0,radius(),0);
169  vec3 p2 = center() + vec3(0,0,radius());
170  p0 = mat * p0;
171  p1 = mat * p1;
172  p2 = mat * p2;
173  real d0 = (p0 - out.mCenter).lengthSquared();
174  real d1 = (p1 - out.mCenter).lengthSquared();
175  real d2 = (p2 - out.mCenter).lengthSquared();
176  out.mRadius = ::sqrt( d0>d1 ? (d0>d2?d0:d2) : (d1>d2?d1:d2) );
177  }
178  }
179 
181  Sphere transformed(const mat4& mat) const
182  {
183  Sphere sphere;
184  transformed(sphere, mat);
185  return sphere;
186  }
187 
188  protected:
190  real mRadius;
191  };
192 }
193 
194 #endif
const Vector3 & normalize(T_Scalar *len=NULL)
Definition: Vector3.hpp:227
T sqrt(T a)
Definition: glsl_math.hpp:592
bool operator!=(const Sphere &other) const
Returns true if two spheres are not identical.
Definition: Sphere.hpp:98
real mRadius
Definition: Sphere.hpp:190
bool isNull() const
Returns true if the AABB is null.
Definition: AABB.hpp:60
const vec3 & center() const
Returns the center of the sphere.
Definition: Sphere.hpp:68
const Sphere & operator+=(const Sphere &other)
Enlarges the sphere to contain the specified sphere.
Definition: Sphere.hpp:124
vec3 center() const
Returns the center of the AABB.
Definition: AABB.cpp:184
Sphere operator+(const Sphere &other)
Returns a sphere that contains the two specified spheres.
Definition: Sphere.hpp:117
Visualization Library main namespace.
real radius() const
Returns the radius of the sphere.
Definition: Sphere.hpp:74
T distance(T p0, T p1)
Definition: glsl_math.hpp:1098
bool isPoint() const
Returns true if the sphere as radius == 0.
Definition: Sphere.hpp:62
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
const vec3 & maxCorner() const
Returns the corner of the AABB with the maximum x y z coordinates.
Definition: AABB.hpp:193
Sphere transformed(const mat4 &mat) const
Returns a sphere that contains the original sphere transformed by the given matrix.
Definition: Sphere.hpp:181
void setRadius(real radius)
Sets the radius of the sphere.
Definition: Sphere.hpp:71
The Sphere class defines a sphere using a center and a radius using vl::real precision.
Definition: Sphere.hpp:43
const vec3 & minCorner() const
Returns the corner of the AABB with the minimum x y z coordinates.
Definition: AABB.hpp:190
Sphere(const AABB &aabb)
Copy-constructor.
Definition: Sphere.hpp:53
vec3 mCenter
Definition: Sphere.hpp:189
void setNull()
Sets the sphere as null.
Definition: Sphere.hpp:56
fvec3 vec3
Defined as: &#39;typedef fvec3 vec3&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector3.hpp:269
bool operator==(const ref< T1 > &o1, const ref< T2 > &o2)
Definition: Object.hpp:144
bool includes(const Sphere &other) const
Returns true if a sphere contains the specified sphere.
Definition: Sphere.hpp:77
T length(T v)
Definition: glsl_math.hpp:1084
bool isNull() const
Definition: Vector3.hpp:226
Sphere()
Constructor: creates a null sphere.
Definition: Sphere.hpp:47
void transformed(Sphere &out, const mat4 &mat) const
Returns a sphere that contains the original sphere transformed by the given matrix.
Definition: Sphere.hpp:157
bool isNull() const
Returns true if the sphere is null, ie, if radius is < 0.
Definition: Sphere.hpp:59
Sphere(const vec3 &center, real radius)
Constructor: creates a sphere with the given center and radius.
Definition: Sphere.hpp:50
bool operator==(const Sphere &other) const
Returns true if two spheres are identical.
Definition: Sphere.hpp:92
void setCenter(const vec3 &center)
Sets the center of the sphere.
Definition: Sphere.hpp:65
Sphere & operator=(const AABB &aabb)
Constructs a sphere that contains the specified AABB.
Definition: Sphere.hpp:104