Path: blob/master/3rdparty/openexr/Imath/ImathSphere.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536#ifndef INCLUDED_IMATHSPHERE_H37#define INCLUDED_IMATHSPHERE_H3839//-------------------------------------40//41// A 3D sphere class template42//43//-------------------------------------4445#include "ImathVec.h"46#include "ImathBox.h"47#include "ImathLine.h"4849namespace Imath {5051template <class T>52class Sphere353{54public:5556Vec3<T> center;57T radius;5859//---------------60// Constructors61//---------------6263Sphere3() : center(0,0,0), radius(0) {}64Sphere3(const Vec3<T> &c, T r) : center(c), radius(r) {}6566//-------------------------------------------------------------------67// Utilities:68//69// s.circumscribe(b) sets center and radius of sphere s70// so that the s tightly encloses box b.71//72// s.intersectT (l, t) If sphere s and line l intersect, then73// intersectT() computes the smallest t,74// t >= 0, so that l(t) is a point on the75// sphere. intersectT() then returns true.76//77// If s and l do not intersect, intersectT()78// returns false.79//80// s.intersect (l, i) If sphere s and line l intersect, then81// intersect() calls s.intersectT(l,t) and82// computes i = l(t).83//84// If s and l do not intersect, intersect()85// returns false.86//87//-------------------------------------------------------------------8889void circumscribe(const Box<Vec3<T> > &box);90bool intersect(const Line3<T> &l, Vec3<T> &intersection) const;91bool intersectT(const Line3<T> &l, T &t) const;92};939495//--------------------96// Convenient typedefs97//--------------------9899typedef Sphere3<float> Sphere3f;100typedef Sphere3<double> Sphere3d;101102103//---------------104// Implementation105//---------------106107template <class T>108void Sphere3<T>::circumscribe(const Box<Vec3<T> > &box)109{110center = T(0.5) * (box.min + box.max);111radius = (box.max - center).length();112}113114115template <class T>116bool Sphere3<T>::intersectT(const Line3<T> &line, T &t) const117{118bool doesIntersect = true;119120Vec3<T> v = line.pos - center;121T B = T(2.0) * (line.dir ^ v);122T C = (v ^ v) - (radius * radius);123124// compute discriminant125// if negative, there is no intersection126127T discr = B*B - T(4.0)*C;128129if (discr < 0.0)130{131// line and Sphere3 do not intersect132133doesIntersect = false;134}135else136{137// t0: (-B - sqrt(B^2 - 4AC)) / 2A (A = 1)138139T sqroot = Math<T>::sqrt(discr);140t = (-B - sqroot) * T(0.5);141142if (t < 0.0)143{144// no intersection, try t1: (-B + sqrt(B^2 - 4AC)) / 2A (A = 1)145146t = (-B + sqroot) * T(0.5);147}148149if (t < 0.0)150doesIntersect = false;151}152153return doesIntersect;154}155156157template <class T>158bool Sphere3<T>::intersect(const Line3<T> &line, Vec3<T> &intersection) const159{160T t;161162if (intersectT (line, t))163{164intersection = line(t);165return true;166}167else168{169return false;170}171}172173174} //namespace Imath175176#endif177178179