HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathInterval.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_IMATHINTERVAL_H
38 #define INCLUDED_IMATHINTERVAL_H
39 
40 
41 //-------------------------------------------------------------------
42 //
43 // class Imath::Interval<class T>
44 // --------------------------------
45 //
46 // An Interval has a min and a max and some miscellaneous
47 // functions. It is basically a Box<T> that allows T to be
48 // a scalar.
49 //
50 //-------------------------------------------------------------------
51 
52 #include "ImathVec.h"
53 #include "ImathNamespace.h"
54 
56 
57 
58 template <class T>
59 class Interval
60 {
61  public:
62 
63  //-------------------------
64  // Data Members are public
65  //-------------------------
66 
67  T min;
68  T max;
69 
70  //-----------------------------------------------------
71  // Constructors - an "empty" Interval is created by default
72  //-----------------------------------------------------
73 
74  Interval();
75  Interval(const T& point);
76  Interval(const T& minT, const T& maxT);
77 
78  //--------------------------------
79  // Operators: we get != from STL
80  //--------------------------------
81 
82  bool operator == (const Interval<T> &src) const;
83 
84  //------------------
85  // Interval manipulation
86  //------------------
87 
88  void makeEmpty();
89  void extendBy(const T& point);
90  void extendBy(const Interval<T>& interval);
91 
92  //---------------------------------------------------
93  // Query functions - these compute results each time
94  //---------------------------------------------------
95 
96  T size() const;
97  T center() const;
98  bool intersects(const T &point) const;
99  bool intersects(const Interval<T> &interval) const;
100 
101  //----------------
102  // Classification
103  //----------------
104 
105  bool hasVolume() const;
106  bool isEmpty() const;
107 };
108 
109 
110 //--------------------
111 // Convenient typedefs
112 //--------------------
113 
114 
119 
120 //----------------
121 // Implementation
122 //----------------
123 
124 
125 template <class T>
127 {
128  makeEmpty();
129 }
130 
131 template <class T>
132 inline Interval<T>::Interval(const T& point)
133 {
134  min = point;
135  max = point;
136 }
137 
138 template <class T>
139 inline Interval<T>::Interval(const T& minV, const T& maxV)
140 {
141  min = minV;
142  max = maxV;
143 }
144 
145 template <class T>
146 inline bool
148 {
149  return (min == src.min && max == src.max);
150 }
151 
152 template <class T>
153 inline void
155 {
156  min = limits<T>::max();
157  max = limits<T>::min();
158 }
159 
160 template <class T>
161 inline void
162 Interval<T>::extendBy(const T& point)
163 {
164  if ( point < min )
165  min = point;
166 
167  if ( point > max )
168  max = point;
169 }
170 
171 template <class T>
172 inline void
174 {
175  if ( interval.min < min )
176  min = interval.min;
177 
178  if ( interval.max > max )
179  max = interval.max;
180 }
181 
182 template <class T>
183 inline bool
184 Interval<T>::intersects(const T& point) const
185 {
186  return point >= min && point <= max;
187 }
188 
189 template <class T>
190 inline bool
191 Interval<T>::intersects(const Interval<T>& interval) const
192 {
193  return interval.max >= min && interval.min <= max;
194 }
195 
196 template <class T>
197 inline T
199 {
200  return max-min;
201 }
202 
203 template <class T>
204 inline T
206 {
207  return (max+min)/2;
208 }
209 
210 template <class T>
211 inline bool
213 {
214  return max < min;
215 }
216 
217 template <class T>
218 inline bool Interval<T>::hasVolume() const
219 {
220  return max > min;
221 }
222 
223 
225 
226 #endif // INCLUDED_IMATHINTERVAL_H
#define IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
bool isEmpty() const
#define IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
static T min()
static T max()
bool intersects(const T &point) const
void extendBy(const T &point)
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:133
Interval< double > Intervald
Interval< int > Intervali
T center() const
T size() const
bool operator==(const Interval< T > &src) const
Interval< short > Intervals
Interval< float > Intervalf
void makeEmpty()
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:129
bool hasVolume() const
GLenum src
Definition: glcorearb.h:1792