3
* \brief Simple closed interval class
5
* Copyright 2007 Michael Sloan <mgsloan@gmail.com>
7
* Original Rect/Range code by:
8
* Lauris Kaplinski <lauris@kaplinski.com>
9
* Nathan Hurst <njh@mail.csse.monash.edu.au>
10
* bulia byak <buliabyak@users.sf.net>
11
* MenTaLguY <mental@rydia.net>
13
* This library is free software; you can redistribute it and/or
14
* modify it either under the terms of the GNU Lesser General Public
15
* License version 2.1 as published by the Free Software Foundation
16
* (the "LGPL") or, at your option, under the terms of the Mozilla
17
* Public License Version 1.1 (the "MPL"). If you do not alter this
18
* notice, a recipient may use your version of this file under either
19
* the MPL or the LGPL.
21
* You should have received a copy of the LGPL along with this library
22
* in the file COPYING-LGPL-2.1; if not, output to the Free Software
23
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
* You should have received a copy of the MPL along with this library
25
* in the file COPYING-MPL-1.1
27
* The contents of this file are subject to the Mozilla Public License
28
* Version 1.1 (the "License"); you may not use this file except in
29
* compliance with the License. You may obtain a copy of the License at
30
* http://www.mozilla.org/MPL/
32
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
33
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
34
* the specific language governing rights and limitations.
37
#ifndef LIB2GEOM_SEEN_INTERVAL_H
38
#define LIB2GEOM_SEEN_INTERVAL_H
40
#include <boost/none.hpp>
41
#include <boost/optional.hpp>
42
#include <boost/operators.hpp>
43
#include <2geom/coord.h>
44
#include <2geom/math-utils.h>
45
#include <2geom/generic-interval.h>
46
#include <2geom/int-interval.h>
51
* @brief Range of real numbers that is never empty.
53
* Intervals are closed ranges \f$[a, b]\f$, which means they include their endpoints.
54
* To use them as open ranges, you can use the interiorContains() methods.
59
: public GenericInterval<Coord>
61
typedef GenericInterval<Coord> Base;
63
/// @name Create intervals.
65
/** @brief Create an interval that contains only zero. */
67
/** @brief Create an interval that contains a single point. */
68
explicit Interval(Coord u) : Base(u) {}
69
/** @brief Create an interval that contains all points between @c u and @c v. */
70
Interval(Coord u, Coord v) : Base(u,v) {}
71
/** @brief Convert from integer interval */
72
Interval(IntInterval const &i) : Base(i.min(), i.max()) {}
73
Interval(Base const &b) : Base(b) {}
75
/** @brief Create an interval containing a range of values.
76
* The resulting interval will contain all values from the given range.
77
* The return type of iterators must be convertible to Coord. The given range
78
* must not be empty. For potentially empty ranges, see OptInterval.
79
* @param start Beginning of the range
80
* @param end End of the range
81
* @return Interval that contains all values from [start, end). */
82
template <typename InputIterator>
83
static Interval from_range(InputIterator start, InputIterator end) {
84
Interval result = Base::from_range(start, end);
87
/** @brief Create an interval from a C-style array of values it should contain. */
88
static Interval from_array(Coord const *c, unsigned n) {
89
Interval result = from_range(c, c+n);
94
/// @name Inspect contained values.
96
/// Check whether both endpoints are finite.
97
bool isFinite() const {
98
return IS_FINITE(min()) && IS_FINITE(max());
100
/** @brief Map the interval [0,1] onto this one.
101
* This method simply performs 1D linear interpolation between endpoints. */
102
Coord valueAt(Coord t) {
103
return lerp(t, min(), max());
105
/** @brief Compute a time value that maps to the given value.
106
* The supplied value does not need to be in the interval for this method to work. */
107
Coord timeAt(Coord v) {
108
return (v - min()) / extent();
110
/// Find closest time in [0,1] that maps to the given value. */
111
Coord nearestTime(Coord v) {
112
if (v <= min()) return 0;
113
if (v >= max()) return 1;
118
/// @name Test coordinates and other intervals for inclusion.
120
/** @brief Check whether the interior of the interval includes this number.
121
* Interior means all numbers in the interval except its ends. */
122
bool interiorContains(Coord val) const { return min() < val && val < max(); }
123
/** @brief Check whether the interior of the interval includes the given interval.
124
* Interior means all numbers in the interval except its ends. */
125
bool interiorContains(Interval const &val) const { return min() < val.min() && val.max() < max(); }
126
/// Check whether the number is contained in the union of the interior and the lower boundary.
127
bool lowerContains(Coord val) { return min() <= val && val < max(); }
128
/// Check whether the given interval is contained in the union of the interior and the lower boundary.
129
bool lowerContains(Interval const &val) const { return min() <= val.min() && val.max() < max(); }
130
/// Check whether the number is contained in the union of the interior and the upper boundary.
131
bool upperContains(Coord val) { return min() < val && val <= max(); }
132
/// Check whether the given interval is contained in the union of the interior and the upper boundary.
133
bool upperContains(Interval const &val) const { return min() < val.min() && val.max() <= max(); }
134
/** @brief Check whether the interiors of the intervals have any common elements.
135
* A single point in common is not considered an intersection. */
136
bool interiorIntersects(Interval const &val) const {
137
return std::max(min(), val.min()) < std::min(max(), val.max());
143
// IMPL: ScalableConcept
144
/** @brief Scale an interval */
145
Interval &operator*=(Coord s) {
149
if(s < 0) swap(_b[0], _b[1]);
152
/** @brief Scale an interval by the inverse of the specified value */
153
Interval &operator/=(Coord s) {
157
if(s < 0) swap(_b[0], _b[1]);
160
/** @brief Multiply two intervals.
161
* Product is defined as the set of points that can be obtained by multiplying
162
* any value from the second operand by any value from the first operand:
163
* \f$S = \{x \in A, y \in B: x * y\}\f$ */
164
Interval &operator*=(Interval const &o) {
165
// TODO implement properly
166
Coord mn = min(), mx = max();
167
expandTo(mn * o.min());
168
expandTo(mn * o.max());
169
expandTo(mx * o.min());
170
expandTo(mx * o.max());
173
bool operator==(IntInterval const &ii) const {
174
return min() == Coord(ii.min()) && max() == Coord(ii.max());
176
bool operator==(Interval const &other) const {
177
return Base::operator==(other);
181
/// @name Rounding to integer values
183
/** @brief Return the smallest integer interval which contains this one. */
184
IntInterval roundOutwards() const {
185
IntInterval ret(floor(min()), ceil(max()));
188
/** @brief Return the largest integer interval which is contained in this one. */
189
OptIntInterval roundInwards() const {
190
IntCoord u = ceil(min()), v = floor(max());
191
if (u > v) { OptIntInterval e; return e; }
192
IntInterval ret(u, v);
199
* @brief Range of real numbers that can be empty.
200
* @ingroup Primitives
203
: public GenericOptInterval<Coord>
205
typedef GenericOptInterval<Coord> Base;
207
/// @name Create optionally empty intervals.
209
/** @brief Create an empty interval. */
210
OptInterval() : Base() {}
211
/** @brief Wrap an existing interval. */
212
OptInterval(Interval const &a) : Base(a) {}
213
/** @brief Create an interval containing a single point. */
214
OptInterval(Coord u) : Base(u) {}
215
/** @brief Create an interval containing a range of numbers. */
216
OptInterval(Coord u, Coord v) : Base(u,v) {}
217
OptInterval(Base const &b) : Base(b) {}
219
/** @brief Promote from IntInterval. */
220
OptInterval(IntInterval const &i) : Base(Interval(i)) {}
221
/** @brief Promote from OptIntInterval. */
222
OptInterval(OptIntInterval const &i) : Base() {
223
if (i) *this = Interval(*i);
227
// functions required for Python bindings
228
inline Interval unify(Interval const &a, Interval const &b)
233
inline OptInterval intersect(Interval const &a, Interval const &b)
235
OptInterval r = a & b;
239
} // end namespace Geom
241
#endif //SEEN_INTERVAL_H
246
c-file-style:"stroustrup"
247
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
252
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :