1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2004, 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 ///////////////////////////////////////////////////////////////////////////
36 #ifndef INCLUDED_IMATHBOX_H
37 #define INCLUDED_IMATHBOX_H
39 //-------------------------------------------------------------------
41 // class Imath::Box<class T>
42 // --------------------------------
44 // This class imposes the following requirements on its
47 // 1) The class T must implement these operators:
49 // with the signature (T,T) and the expected
50 // return values for a numeric type.
52 // 2) The class T must implement operator=
53 // with the signature (T,float and/or double)
55 // 3) The class T must have a constructor which takes
56 // a float (and/or double) for use in initializing the box.
58 // 4) The class T must have a function T::dimensions()
59 // which returns the number of dimensions in the class
60 // (since its assumed its a vector) -- preferably, this
61 // returns a constant expression.
63 //-------------------------------------------------------------------
75 //-------------------------
76 // Data Members are public
77 //-------------------------
82 //-----------------------------------------------------
83 // Constructors - an "empty" box is created by default
84 //-----------------------------------------------------
88 Box(const T& minT, const T& maxT);
90 //--------------------
92 //--------------------
94 bool operator == (const Box<T> &src) const;
95 bool operator != (const Box<T> &src) const;
102 void extendBy(const T& point);
103 void extendBy(const Box<T>& box);
105 //---------------------------------------------------
106 // Query functions - these compute results each time
107 //---------------------------------------------------
111 bool intersects(const T &point) const;
112 bool intersects(const Box<T> &box) const;
114 unsigned int majorAxis() const;
120 bool isEmpty() const;
121 bool hasVolume() const;
125 //--------------------
126 // Convenient typedefs
127 //--------------------
130 typedef Box <V2s> Box2s;
131 typedef Box <V2i> Box2i;
132 typedef Box <V2f> Box2f;
133 typedef Box <V2d> Box2d;
134 typedef Box <V3s> Box3s;
135 typedef Box <V3i> Box3i;
136 typedef Box <V3f> Box3f;
137 typedef Box <V3d> Box3d;
152 inline Box<T>::Box(const T& point)
159 inline Box<T>::Box(const T& minV, const T& maxV)
167 Box<T>::operator == (const Box<T> &src) const
169 return (min == src.min && max == src.max);
174 Box<T>::operator != (const Box<T> &src) const
176 return (min != src.min || max != src.max);
180 inline void Box<T>::makeEmpty()
182 min = T(T::baseTypeMax());
183 max = T(T::baseTypeMin());
187 inline void Box<T>::extendBy(const T& point)
189 for (unsigned int i=0; i<min.dimensions(); i++)
191 if ( point[i] < min[i] ) min[i] = point[i];
192 if ( point[i] > max[i] ) max[i] = point[i];
197 inline void Box<T>::extendBy(const Box<T>& box)
199 for (unsigned int i=0; i<min.dimensions(); i++)
201 if ( box.min[i] < min[i] ) min[i] = box.min[i];
202 if ( box.max[i] > max[i] ) max[i] = box.max[i];
207 inline bool Box<T>::intersects(const T& point) const
209 for (unsigned int i=0; i<min.dimensions(); i++)
211 if (point[i] < min[i] || point[i] > max[i]) return false;
217 inline bool Box<T>::intersects(const Box<T>& box) const
219 for (unsigned int i=0; i<min.dimensions(); i++)
221 if (box.max[i] < min[i] || box.min[i] > max[i]) return false;
227 inline T Box<T>::size() const
236 inline T Box<T>::center() const
242 inline bool Box<T>::isEmpty() const
244 for (unsigned int i=0; i<min.dimensions(); i++)
246 if (max[i] < min[i]) return true;
252 inline bool Box<T>::hasVolume() const
254 for (unsigned int i=0; i<min.dimensions(); i++)
256 if (max[i] <= min[i]) return false;
262 inline unsigned int Box<T>::majorAxis() const
264 unsigned int major = 0;
267 for (unsigned int i=1; i<min.dimensions(); i++)
269 if ( s[i] > s[major] ) major = i;