~jabiertxof/+junk/browup

« back to all changes in this revision

Viewing changes to 2geom/interval.h

  • Committer: Jabiertxof
  • Date: 2015-12-13 22:28:18 UTC
  • Revision ID: jtx@jtx.marker.es-20151213222818-c8pye82a6rptgcor
First commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \file
 
3
 * \brief  Simple closed interval class
 
4
 *//*
 
5
 * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
 
6
 *
 
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>
 
12
 * 
 
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.
 
20
 *
 
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
 
26
 *
 
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/
 
31
 *
 
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.
 
35
 *
 
36
 */
 
37
#ifndef LIB2GEOM_SEEN_INTERVAL_H
 
38
#define LIB2GEOM_SEEN_INTERVAL_H
 
39
 
 
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>
 
47
 
 
48
namespace Geom {
 
49
 
 
50
/** 
 
51
 * @brief Range of real numbers that is never empty.
 
52
 *
 
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.
 
55
 *
 
56
 * @ingroup Primitives
 
57
 */
 
58
class Interval
 
59
    : public GenericInterval<Coord>
 
60
{
 
61
    typedef GenericInterval<Coord> Base;
 
62
public:
 
63
    /// @name Create intervals.
 
64
    /// @{
 
65
    /** @brief Create an interval that contains only zero. */
 
66
    Interval() {}
 
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) {}
 
74
 
 
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);
 
85
        return result;
 
86
    }
 
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);
 
90
        return result;
 
91
    }
 
92
    /// @}
 
93
 
 
94
    /// @name Inspect contained values.
 
95
    /// @{
 
96
    /// Check whether both endpoints are finite.
 
97
    bool isFinite() const {
 
98
        return IS_FINITE(min()) && IS_FINITE(max());
 
99
    }
 
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());
 
104
    }
 
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();
 
109
    }
 
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;
 
114
        return timeAt(v);
 
115
    }
 
116
    /// @}
 
117
 
 
118
    /// @name Test coordinates and other intervals for inclusion.
 
119
    /// @{
 
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());
 
138
    }
 
139
    /// @}
 
140
 
 
141
    /// @name Operators
 
142
    /// @{
 
143
    // IMPL: ScalableConcept
 
144
    /** @brief Scale an interval */
 
145
    Interval &operator*=(Coord s) {
 
146
        using std::swap;
 
147
        _b[0] *= s;
 
148
        _b[1] *= s;
 
149
        if(s < 0) swap(_b[0], _b[1]);
 
150
        return *this;
 
151
    }
 
152
    /** @brief Scale an interval by the inverse of the specified value */
 
153
    Interval &operator/=(Coord s) {
 
154
        using std::swap;
 
155
        _b[0] /= s;
 
156
        _b[1] /= s;
 
157
        if(s < 0) swap(_b[0], _b[1]);
 
158
        return *this;
 
159
    }
 
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());
 
171
        return *this;
 
172
    }
 
173
    bool operator==(IntInterval const &ii) const {
 
174
        return min() == Coord(ii.min()) && max() == Coord(ii.max());
 
175
    }
 
176
    bool operator==(Interval const &other) const {
 
177
        return Base::operator==(other);
 
178
    }
 
179
    /// @}
 
180
    
 
181
    /// @name Rounding to integer values
 
182
    /// @{
 
183
    /** @brief Return the smallest integer interval which contains this one. */
 
184
    IntInterval roundOutwards() const {
 
185
        IntInterval ret(floor(min()), ceil(max()));
 
186
        return ret;
 
187
    }
 
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);
 
193
        return ret;
 
194
    }
 
195
    /// @}
 
196
};
 
197
 
 
198
/**
 
199
 * @brief Range of real numbers that can be empty.
 
200
 * @ingroup Primitives
 
201
 */
 
202
class OptInterval
 
203
    : public GenericOptInterval<Coord>
 
204
{
 
205
    typedef GenericOptInterval<Coord> Base;
 
206
public:
 
207
    /// @name Create optionally empty intervals.
 
208
    /// @{
 
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) {}
 
218
 
 
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);
 
224
    }
 
225
};
 
226
 
 
227
// functions required for Python bindings
 
228
inline Interval unify(Interval const &a, Interval const &b)
 
229
{
 
230
    Interval r = a | b;
 
231
    return r;
 
232
}
 
233
inline OptInterval intersect(Interval const &a, Interval const &b)
 
234
{
 
235
    OptInterval r = a & b;
 
236
    return r;
 
237
}
 
238
 
 
239
} // end namespace Geom
 
240
 
 
241
#endif //SEEN_INTERVAL_H
 
242
 
 
243
/*
 
244
  Local Variables:
 
245
  mode:c++
 
246
  c-file-style:"stroustrup"
 
247
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
248
  indent-tabs-mode:nil
 
249
  fill-column:99
 
250
  End:
 
251
*/
 
252
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :