Allolib  1.0
C++ Components For Interactive Multimedia
al_Interval.hpp
1 #ifndef INCLUDE_AL_INTERVAL_HPP
2 #define INCLUDE_AL_INTERVAL_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37 
38  File description:
39  A closed interval
40 
41  File author(s):
42  Lance Putnam, 2010, putnam.lance@gmail.com
43 */
44 
45 namespace al {
46 
48 
54 template <class T>
55 class Interval {
56  public:
57  Interval() : mMin(0), mMax(1) {}
58 
61  Interval(const T& min, const T& max) { endpoints(min, max); }
62 
63  T center() const { return (max() + min()) / T(2); }
64 
66  bool contains(const T& v) const { return v >= min() && v <= max(); }
67 
68  bool degenerate() const {
69  return min() == max();
70  }
71  T diameter() const {
72  return max() - min();
73  }
74  T size() const {
75  return diameter();
76  }
77  const T& max() const { return mMax; }
78  const T& min() const { return mMin; }
79  bool proper() const {
80  return min() != max();
81  }
82  T radius() const {
83  return diameter() / T(2);
84  }
85 
87  T toUnit(const T& v) const { return (v - min()) / diameter(); }
88 
89  template <class U>
90  bool operator==(const Interval<U>& v) {
91  return min() == v.min() && max() == v.max();
92  }
93 
94  template <class U>
95  bool operator!=(const Interval<U>& v) {
96  return !(*this == v);
97  }
98 
99  template <class U>
100  Interval& operator+=(const Interval<U>& v) {
101  endpoints(min() + v.min(), max() + v.max());
102  return *this;
103  }
104 
105  template <class U>
106  Interval& operator-=(const Interval<U>& v) {
107  endpoints(min() - v.max(), max() - v.min());
108  return *this;
109  }
110 
111  template <class U>
112  Interval& operator*=(const Interval<U>& v) {
113  T a = min() * v.min(), b = min() * v.max(), c = max() * v.min(),
114  d = max() * v.max();
115  mMin = min(min(a, b), min(c, d));
116  mMax = max(max(a, b), max(c, d));
117  return *this;
118  }
119 
120  template <class U>
121  Interval& operator/=(const Interval<U>& v) {
122  T a = min() / v.min(), b = min() / v.max(), c = max() / v.min(),
123  d = max() / v.max();
124  mMin = min(min(a, b), min(c, d));
125  mMax = max(max(a, b), max(c, d));
126  return *this;
127  }
128 
130  Interval& center(const T& v) { return centerDiameter(v, diameter()); }
131 
133  Interval& diameter(const T& v) { return centerDiameter(center(), v); }
134 
136  Interval& centerDiameter(const T& c, const T& d) {
137  mMin = c - d * T(0.5);
138  mMax = mMin + d;
139  return *this;
140  }
141 
143  Interval& endpoints(const T& min, const T& max) {
144  mMax = max;
145  mMin = min;
146  if (mMin > mMax) {
147  T t = mMin;
148  mMin = mMax;
149  mMax = t;
150  }
151  return *this;
152  }
153 
155  Interval& translate(const T& v) {
156  mMin += v;
157  mMax += v;
158  return *this;
159  }
160 
162  Interval& max(const T& v) { return endpoints(min(), v); }
163 
165  Interval& min(const T& v) { return endpoints(v, max()); }
166 
167  private:
168  T mMin, mMax;
169 
170  const T& min(const T& a, const T& b) { return a < b ? a : b; }
171  const T& max(const T& a, const T& b) { return a > b ? a : b; }
172 };
173 
174 } // namespace al
175 
176 #endif
A closed interval [min, max].
Definition: al_Interval.hpp:55
T center() const
Returns center point.
Definition: al_Interval.hpp:63
const T & min() const
Get minimum endpoint.
Definition: al_Interval.hpp:78
T toUnit(const T &v) const
Linearly map point in interval to point in the unit interval.
Definition: al_Interval.hpp:87
bool proper() const
Returns true if diameter is non-zero.
Definition: al_Interval.hpp:79
const T & max() const
Get maximum endpoint.
Definition: al_Interval.hpp:77
Interval & min(const T &v)
Set minimum endpoint.
Interval & translate(const T &v)
Translate interval by fixed amount.
Interval & endpoints(const T &min, const T &max)
Set the endpoints.
Interval & center(const T &v)
Set center point preserving diameter.
Interval & centerDiameter(const T &c, const T &d)
Set center and diameter.
Interval(const T &min, const T &max)
Definition: al_Interval.hpp:61
bool degenerate() const
Returns true if diameter is zero.
Definition: al_Interval.hpp:68
T size() const
Returns absolute difference of endpoints.
Definition: al_Interval.hpp:74
T radius() const
Returns one-half the diameter.
Definition: al_Interval.hpp:82
bool contains(const T &v) const
Returns true if value is in interval.
Definition: al_Interval.hpp:66
Interval & max(const T &v)
Set maximum endpoint.
Interval & diameter(const T &v)
Set diameter (width) preserving center.
T diameter() const
Returns absolute difference of endpoints.
Definition: al_Interval.hpp:71
Definition: al_App.hpp:23