HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathSphere.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 
37 #ifndef INCLUDED_IMATHSPHERE_H
38 #define INCLUDED_IMATHSPHERE_H
39 
40 //-------------------------------------
41 //
42 // A 3D sphere class template
43 //
44 //-------------------------------------
45 
46 #include "ImathVec.h"
47 #include "ImathBox.h"
48 #include "ImathLine.h"
49 #include "ImathNamespace.h"
50 
52 
53 template <class T>
54 class Sphere3
55 {
56  public:
57 
60 
61  //---------------
62  // Constructors
63  //---------------
64 
65  Sphere3() : center(0,0,0), radius(0) {}
66  Sphere3(const Vec3<T> &c, T r) : center(c), radius(r) {}
67 
68  //-------------------------------------------------------------------
69  // Utilities:
70  //
71  // s.circumscribe(b) sets center and radius of sphere s
72  // so that the s tightly encloses box b.
73  //
74  // s.intersectT (l, t) If sphere s and line l intersect, then
75  // intersectT() computes the smallest t,
76  // t >= 0, so that l(t) is a point on the
77  // sphere. intersectT() then returns true.
78  //
79  // If s and l do not intersect, intersectT()
80  // returns false.
81  //
82  // s.intersect (l, i) If sphere s and line l intersect, then
83  // intersect() calls s.intersectT(l,t) and
84  // computes i = l(t).
85  //
86  // If s and l do not intersect, intersect()
87  // returns false.
88  //
89  //-------------------------------------------------------------------
90 
91  void circumscribe(const Box<Vec3<T> > &box);
92  bool intersect(const Line3<T> &l, Vec3<T> &intersection) const;
93  bool intersectT(const Line3<T> &l, T &t) const;
94 };
95 
96 
97 //--------------------
98 // Convenient typedefs
99 //--------------------
100 
103 
104 
105 //---------------
106 // Implementation
107 //---------------
108 
109 template <class T>
111 {
112  center = T(0.5) * (box.min + box.max);
113  radius = (box.max - center).length();
114 }
115 
116 
117 template <class T>
118 bool Sphere3<T>::intersectT(const Line3<T> &line, T &t) const
119 {
120  bool doesIntersect = true;
121 
122  Vec3<T> v = line.pos - center;
123  T B = T(2.0) * (line.dir ^ v);
124  T C = (v ^ v) - (radius * radius);
125 
126  // compute discriminant
127  // if negative, there is no intersection
128 
129  T discr = B*B - T(4.0)*C;
130 
131  if (discr < 0.0)
132  {
133  // line and Sphere3 do not intersect
134 
135  doesIntersect = false;
136  }
137  else
138  {
139  // t0: (-B - sqrt(B^2 - 4AC)) / 2A (A = 1)
140 
141  T sqroot = Math<T>::sqrt(discr);
142  t = (-B - sqroot) * T(0.5);
143 
144  if (t < 0.0)
145  {
146  // no intersection, try t1: (-B + sqrt(B^2 - 4AC)) / 2A (A = 1)
147 
148  t = (-B + sqroot) * T(0.5);
149  }
150 
151  if (t < 0.0)
152  doesIntersect = false;
153  }
154 
155  return doesIntersect;
156 }
157 
158 
159 template <class T>
160 bool Sphere3<T>::intersect(const Line3<T> &line, Vec3<T> &intersection) const
161 {
162  T t;
163 
164  if (intersectT (line, t))
165  {
166  intersection = line(t);
167  return true;
168  }
169  else
170  {
171  return false;
172  }
173 }
174 
176 
177 #endif // INCLUDED_IMATHSPHERE_H
#define IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
Vec3< T > pos
Definition: ImathLine.h:59
#define IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
const GLdouble * v
Definition: glcorearb.h:836
Vec3< T > center
Definition: ImathSphere.h:58
bool intersectT(const Line3< T > &l, T &t) const
Definition: ImathSphere.h:118
Vec3< T > dir
Definition: ImathLine.h:60
Sphere3(const Vec3< T > &c, T r)
Definition: ImathSphere.h:66
bool intersect(const Line3< T > &l, Vec3< T > &intersection) const
Definition: ImathSphere.h:160
Definition: ImathBox.h:72
GLboolean r
Definition: glcorearb.h:1221
static T sqrt(T x)
Definition: ImathMath.h:115
void circumscribe(const Box< Vec3< T > > &box)
Definition: ImathSphere.h:110
Sphere3< float > Sphere3f
Definition: ImathSphere.h:101
Sphere3< double > Sphere3d
Definition: ImathSphere.h:102
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:794