1 /*
2 * interval.h - Simple closed interval class
3 *
4 * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
5 *
6 * Original Rect/Range code by:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Nathan Hurst <njh@mail.csse.monash.edu.au>
9 * bulia byak <buliabyak@users.sf.net>
10 * MenTaLguY <mental@rydia.net>
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it either under the terms of the GNU Lesser General Public
14 * License version 2.1 as published by the Free Software Foundation
15 * (the "LGPL") or, at your option, under the terms of the Mozilla
16 * Public License Version 1.1 (the "MPL"). If you do not alter this
17 * notice, a recipient may use your version of this file under either
18 * the MPL or the LGPL.
19 *
20 * You should have received a copy of the LGPL along with this library
21 * in the file COPYING-LGPL-2.1; if not, output to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * You should have received a copy of the MPL along with this library
24 * in the file COPYING-MPL-1.1
25 *
26 * The contents of this file are subject to the Mozilla Public License
27 * Version 1.1 (the "License"); you may not use this file except in
28 * compliance with the License. You may obtain a copy of the License at
29 * http://www.mozilla.org/MPL/
30 *
31 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
32 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
33 * the specific language governing rights and limitations.
34 *
35 */
36 #ifndef SEEN_INTERVAL_H
37 #define SEEN_INTERVAL_H
39 #include <assert.h>
40 #include <2geom/coord.h>
42 #include <boost/optional/optional.hpp>
44 namespace Geom {
46 /* Although an Interval where _b[0] > _b[1] is considered empty, for proper functioning of other methods,
47 * a proper empty Interval is [+COORD_HUGE, -COORD_HUGE]. Then, expandTo(p) will set the interval to [p,p].
48 */
49 class Interval {
50 private:
51 Coord _b[2];
53 public:
54 // The default constructor creates an empty interval, that ranges from +COORD_HUGE to -COORD_HUGE.
55 // Doing an expandTo(p) on this empty interval will correctly set the whole interval to [p,p].
56 explicit Interval() { _b[0] = +COORD_HUGE; _b[1] = -COORD_HUGE; }
57 explicit Interval(Coord u) { _b[0] = _b[1] = u; }
58 /* When creating an Interval using the constructor specifying the exact range, the created interval
59 * will be [u,v] when u<=v ; and will be [v,u] when v < u !!!
60 */
61 Interval(Coord u, Coord v) {
62 if(u < v) {
63 _b[0] = u; _b[1] = v;
64 } else {
65 _b[0] = v; _b[1] = u;
66 }
67 }
69 double operator[](unsigned i) const {
70 assert(i < 2);
71 return _b[i];
72 }
73 inline double& operator[](unsigned i) { return _b[i]; } //Trust the user...
75 inline Coord min() const { return _b[0]; }
76 inline Coord max() const { return _b[1]; }
77 inline Coord extent() const { return _b[1] - _b[0]; }
78 inline Coord middle() const { return (_b[1] + _b[0]) * 0.5; }
80 inline bool isEmpty() const { return _b[0] > _b[1]; }
81 inline bool contains(Coord val) const { return _b[0] <= val && val <= _b[1]; }
82 bool contains(const Interval & val) const { return _b[0] <= val._b[0] && val._b[1] <= _b[1]; }
83 bool intersects(const Interval & val) const {
84 return contains(val._b[0]) || contains(val._b[1]) || val.contains(*this);
85 }
87 inline bool operator==(Interval other) { return _b[0] == other._b[0] && _b[1] == other._b[1]; }
88 inline bool operator!=(Interval other) { return _b[0] != other._b[0] || _b[1] != other._b[1]; }
90 //IMPL: OffsetableConcept
91 //TODO: rename output_type to something else in the concept
92 typedef Coord output_type;
93 inline Interval operator+(Coord amnt) {
94 return Interval(_b[0] + amnt, _b[1] + amnt);
95 }
96 inline Interval operator-(Coord amnt) {
97 return Interval(_b[0] - amnt, _b[1] - amnt);
98 }
99 inline Interval operator+=(Coord amnt) {
100 _b[0] += amnt; _b[1] += amnt;
101 return *this;
102 }
103 inline Interval operator-=(Coord amnt) {
104 _b[0] -= amnt; _b[1] -= amnt;
105 return *this;
106 }
108 //IMPL: ScalableConcept
109 inline Interval operator-() const { return Interval(*this); }
110 inline Interval operator*(Coord s) const { return Interval(_b[0]*s, _b[1]*s); }
111 inline Interval operator/(Coord s) const { return Interval(_b[0]/s, _b[1]/s); }
112 Interval operator*=(Coord s) {
113 if(s < 0) {
114 Coord temp = _b[0];
115 _b[0] = _b[1]*s;
116 _b[1] = temp*s;
117 } else {
118 _b[0] *= s;
119 _b[1] *= s;
120 }
121 return *this;
122 }
123 Interval operator/=(Coord s) {
124 //TODO: what about s=0?
125 if(s < 0) {
126 Coord temp = _b[0];
127 _b[0] = _b[1]/s;
128 _b[1] = temp/s;
129 } else {
130 _b[0] /= s;
131 _b[1] /= s;
132 }
133 return *this;
134 }
136 //TODO: NaN handleage for the next two?
137 //TODO: Evaluate if wrap behaviour is proper.
138 //If val > max, then rather than becoming a min==max range, it 'wraps' over
139 void setMin(Coord val) {
140 if(val > _b[1]) {
141 _b[0] = _b[1];
142 _b[1] = val;
143 } else {
144 _b[0] = val;
145 }
146 }
147 //If val < min, then rather than becoming a min==max range, it 'wraps' over
148 void setMax(Coord val) {
149 if(val < _b[0]) {
150 _b[1] = _b[0];
151 _b[0] = val;
152 } else {
153 _b[1] = val;
154 }
155 }
157 inline void extendTo(Coord val) {
158 if(val < _b[0]) _b[0] = val;
159 if(val > _b[1]) _b[1] = val; //no else, as we want to handle NaN
160 }
162 static Interval fromArray(const Coord* c, int n) {
163 assert(n > 0);
164 Interval result(c[0]);
165 for(int i = 1; i < n; i++) result.extendTo(c[i]);
166 return result;
167 }
169 inline void expandBy(double amnt) {
170 _b[0] -= amnt;
171 _b[1] += amnt;
172 }
174 inline void unionWith(const Interval & a) {
175 if(a._b[0] < _b[0]) _b[0] = a._b[0];
176 if(a._b[1] > _b[1]) _b[1] = a._b[1];
177 }
178 };
180 //IMPL: AddableConcept
181 inline Interval operator+(const Interval & a, const Interval & b) {
182 return Interval(a.min() + b.min(), a.max() + b.max());
183 }
184 inline Interval operator-(const Interval & a, const Interval & b) {
185 return Interval(a.min() - b.max(), a.max() - b.min());
186 }
187 inline Interval operator+=(Interval & a, const Interval & b) { a = a + b; return a; }
188 inline Interval operator-=(Interval & a, const Interval & b) { a = a - b; return a; }
190 //There might be impls of this based off sign checks
191 inline Interval operator*(const Interval & a, const Interval & b) {
192 Interval res(a.min() * b.min());
193 res.extendTo(a.min() * b.max());
194 res.extendTo(a.max() * b.min());
195 res.extendTo(a.max() * b.max());
196 return res;
197 }
198 inline Interval operator*=(Interval & a, const Interval & b) { a = a * b; return a; }
200 /* reinstate if useful (doesn't do the proper thing for 0 inclusion)
201 inline Interval operator/(const Interval & a, const Interval & b) {
202 Interval res(a.min() / b.min());
203 res.extendTo(a.min() / b.max());
204 res.extendTo(a.max() / b.min());
205 res.extendTo(a.max() / b.max());
206 return res;
207 }
208 inline Interval operator/=(Interval & a, const Interval & b) { a = a / b; return a; }
209 */
211 // 'union' conflicts with C keyword
212 inline Interval unify(const Interval & a, const Interval & b) {
213 return Interval(std::min(a.min(), b.min()),
214 std::max(a.max(), b.max()));
215 }
216 inline boost::optional<Interval> intersect(const Interval & a, const Interval & b) {
217 Coord u = std::max(a.min(), b.min()),
218 v = std::min(a.max(), b.max());
219 //technically >= might be incorrect, but singulars suck
220 return u >= v ? boost::optional<Interval>()
221 : boost::optional<Interval>(Interval(u, v));
222 }
224 }
225 #endif //SEEN_INTERVAL_H
227 /*
228 Local Variables:
229 mode:c++
230 c-file-style:"stroustrup"
231 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
232 indent-tabs-mode:nil
233 fill-column:99
234 End:
235 */
236 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :