1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
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
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.
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.
33 ///////////////////////////////////////////////////////////////////////////
37 #ifndef INCLUDED_IMATHSPHERE_H
38 #define INCLUDED_IMATHSPHERE_H
40 //-------------------------------------
42 // A 3D sphere class template
44 //-------------------------------------
48 #include "ImathLine.h"
64 Sphere3() : center(0,0,0), radius(0) {}
65 Sphere3(const Vec3<T> &c, T r) : center(c), radius(r) {}
67 //-------------------------------------------------------------------
70 // s.circumscribe(b) sets center and radius of sphere s
71 // so that the s tightly encloses box b.
73 // s.intersectT (l, t) If sphere s and line l intersect, then
74 // intersectT() computes the smallest t,
75 // t >= 0, so that l(t) is a point on the
76 // sphere. intersectT() then returns true.
78 // If s and l do not intersect, intersectT()
81 // s.intersect (l, i) If sphere s and line l intersect, then
82 // intersect() calls s.intersectT(l,t) and
85 // If s and l do not intersect, intersect()
88 //-------------------------------------------------------------------
90 void circumscribe(const Box<Vec3<T> > &box);
91 bool intersect(const Line3<T> &l, Vec3<T> &intersection) const;
92 bool intersectT(const Line3<T> &l, T &t) const;
96 //--------------------
97 // Convenient typedefs
98 //--------------------
100 typedef Sphere3<float> Sphere3f;
101 typedef Sphere3<double> Sphere3d;
109 void Sphere3<T>::circumscribe(const Box<Vec3<T> > &box)
111 center = T(0.5) * (box.min + box.max);
112 radius = (box.max - center).length();
117 bool Sphere3<T>::intersectT(const Line3<T> &line, T &t) const
119 bool doesIntersect = true;
121 Vec3<T> v = line.pos - center;
122 T B = 2.0 * (line.dir ^ v);
123 T C = (v ^ v) - (radius * radius);
125 // compute discriminant
126 // if negative, there is no intersection
128 T discr = B*B - 4.0*C;
132 // line and Sphere3 do not intersect
134 doesIntersect = false;
138 // t0: (-B - sqrt(B^2 - 4AC)) / 2A (A = 1)
140 T sqroot = Math<T>::sqrt(discr);
141 t = (-B - sqroot) * 0.5;
145 // no intersection, try t1: (-B + sqrt(B^2 - 4AC)) / 2A (A = 1)
147 t = (-B + sqroot) * 0.5;
151 doesIntersect = false;
154 return doesIntersect;
159 bool Sphere3<T>::intersect(const Line3<T> &line, Vec3<T> &intersection) const
163 if (intersectT (line, t))
165 intersection = line(t);