mt
 All Classes Files Functions Enumerations Groups Pages
interval.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Adolfo Rodriguez *
3  * adolfo.rodriguez@upc.edu *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
22 
23 // HEADER GUARD
24 #ifndef MT_INTERVAL_H
25 #define MT_INTERVAL_H
26 
27 // C++ STANDARD HEADERS
28 #include <iostream>
29 #include <utility>
30 
31 // MT LIBRARY HEADERS
32 #include <mt/scalar.h>
33 
34 
36 
37 
39 
40 namespace mt
41 {
42 
43 
45 
48 
81 
82 class Interval
83 {
84 public:
85 
86 // LIFECYCLE
87 
89  Interval();
90 
92  Interval(const Scalar& s);
93 
95  Interval(const Scalar& bound1,
96  const Scalar& bound2);
97 
98  // Compiler generated copy constructor is being used
99 
100  // Compiler generated destructor is being used
101 
102 
103 // OPERATORS
104 
105  // Compiler generated assignment operator is being used
106 
107  bool operator==(const Interval& in) const;
108  bool operator!=(const Interval& in) const;
109 
110 // OPERATIONS
111 
113  Scalar center() const;
114 
116  Scalar width() const;
117 
119  Interval& widen(const Scalar& s);
120 
121 
122 // ACCESS
123 
125  Scalar getLowerBound() const;
126  Scalar& getLowerBoundRef();
127  const Scalar& getLowerBoundRef() const;
128 
130  Scalar getUpperBound() const;
131  Scalar& getUpperBoundRef();
132  const Scalar& getUpperBoundRef() const;
133 
135  void setValue(const Scalar& s);
136 
138  void setValue(const Scalar& lb,
139  const Scalar& ub);
140 
141 
142 private:
143 
144 // MEMBERS
145 
146 Scalar m_lb;
147 Scalar m_ub;
148 
149 };
150 
151 
153 
154 // OPERATORS
155 
156 Interval operator+(const Scalar& s,
157  const Interval& in);
158 
159 Interval operator+(const Interval& in1,
160  const Interval& in2);
161 
162 Interval operator-(const Scalar& s,
163  const Interval& in);
164 
165 Interval operator-(const Interval& in1,
166  const Interval& in2);
167 
168 std::ostream& operator<<(std::ostream& os,
169  const Interval& in);
170 
171 
172 // FUNCTIONS
173 
175 bool isOverlap(const Interval& in1,
176  const Interval& in2);
177 
179 bool isContained(const Scalar& s,
180  const Interval& in);
181 
183 bool isContained(const Interval& in1,
184  const Interval& in2);
185 
187 Interval widen(const Interval& in,
188  const Scalar& s);
189 
191 Interval hull(const Interval& in1,
192  const Interval& in2);
193 
194 
196 
197 // LIFECYCLE
198 
200 
201  m_lb(0.0),
202  m_ub(0.0) {}
203 
204 
205 inline Interval::Interval(const Scalar& s) :
206 
207  m_lb(s),
208  m_ub(s) {}
209 
210 
211 inline Interval::Interval(const Scalar& bound1,
212  const Scalar& bound2)
213 {
214  if (bound1 <= bound2)
215  {
216  m_lb = bound1;
217  m_ub = bound2;
218  }
219  else
220  {
221  m_lb = bound2;
222  m_ub = bound1;
223  }
224 }
225 
226 
227 // OPERATORS
228 
229 inline bool Interval::operator==(const Interval& in) const
230 {
231  return (m_lb == in.m_lb && m_ub == in.m_ub);
232 }
233 
234 
235 inline bool Interval::operator!=(const Interval& in) const
236 {
237  return std::rel_ops::operator!=(*this, in);
238 }
239 
240 
241 // OPERATIONS
242 
243 inline Scalar Interval::center() const
244 {
245  return (m_lb + m_ub) * Scalar(0.5);
246 }
247 
248 
249 inline Scalar Interval::width() const
250 {
251  return m_ub - m_lb;
252 }
253 
254 
255 inline Interval& Interval::widen(const Scalar& s)
256 {
257  m_lb -= s;
258  m_ub += s;
259  return *this;
260 }
261 
262 
263 // ACCESS
264 
265 inline Scalar Interval::getLowerBound() const
266 {
267  return m_lb;
268 }
269 
270 
271 inline Scalar& Interval::getLowerBoundRef()
272 {
273  return m_lb;
274 }
275 
276 
277 inline const Scalar& Interval::getLowerBoundRef() const
278 {
279  return m_lb;
280 }
281 
282 
283 inline Scalar Interval::getUpperBound() const
284 {
285  return m_ub;
286 }
287 
288 
289 inline Scalar& Interval::getUpperBoundRef()
290 {
291  return m_ub;
292 }
293 
294 
295 inline const Scalar& Interval::getUpperBoundRef() const
296 {
297  return m_ub;
298 }
299 
300 
301 inline void Interval::setValue(const Scalar& s)
302 {
303  m_lb = s;
304  m_ub = s;
305 }
306 
307 
308 inline void Interval::setValue(const Scalar& lb,
309  const Scalar& ub)
310 {
311  m_lb = lb;
312  m_ub = ub;
313 }
314 
316 
317 // OPERATORS
318 
319 inline Interval operator+(const Scalar& s,
320  const Interval& in)
321 {
322  return Interval(in.getLowerBound() + s,
323  in.getUpperBound() + s);
324 }
325 
326 
327 inline Interval operator+(const Interval& in1,
328  const Interval& in2)
329 {
330  return Interval(in1.getLowerBound() + in2.getLowerBound(),
331  in1.getUpperBound() + in2.getUpperBound());
332 }
333 
334 
335 inline Interval operator-(const Scalar& s,
336  const Interval& in)
337 {
338  return Interval(in.getLowerBound() - s,
339  in.getUpperBound() - s);
340 }
341 
342 
343 inline Interval operator-(const Interval& in1,
344  const Interval& in2)
345 {
346  return Interval(in1.getLowerBound() - in2.getLowerBound(),
347  in1.getUpperBound() - in2.getUpperBound());
348 }
349 
350 
351 inline std::ostream& operator<<(std::ostream& os,
352  const Interval& in)
353 {
354  return os << '[' << in.getLowerBound() << ", "
355  << in.getUpperBound() << ']';
356 }
357 
358 
359 // FUNCTIONS
360 
361 inline bool isOverlap(const Interval& in1,
362  const Interval& in2)
363 {
364  return (in1.getLowerBound() <= in2.getUpperBound()) &&
365  (in2.getLowerBound() <= in1.getUpperBound());
366 }
367 
368 
369 inline bool isContained(const Scalar& s,
370  const Interval& in)
371 {
372  return s >= in.getLowerBound() &&
373  s <= in.getUpperBound();
374 }
375 
376 
377 inline bool isContained(const Interval& in1,
378  const Interval& in2)
379 {
380  return in1.getLowerBound() >= in2.getLowerBound() &&
381  in1.getUpperBound() <= in2.getUpperBound();
382 }
383 
384 
385 inline Interval widen(const Interval& in,
386  const Scalar& s)
387 {
388  Interval in1(in);
389  return in1.widen(s);
390 }
391 
392 
393 inline Interval hull(const Interval& in1,
394  const Interval& in2)
395 {
396  return Interval(min(in1.getLowerBound(), in2.getLowerBound()),
397  max(in1.getUpperBound(), in2.getUpperBound()));
398 }
399 
400 
401 } // mt
402 
403 #endif // MT_INTERVAL_H