~valavanisalex/ubuntu/oneiric/inkscape/inkscape_0.48.1-2ubuntu4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
 * \file
 * \brief  Simple closed interval class
 *
 * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
 *
 * Original Rect/Range code by:
 *   Lauris Kaplinski <lauris@kaplinski.com>
 *   Nathan Hurst <njh@mail.csse.monash.edu.au>
 *   bulia byak <buliabyak@users.sf.net>
 *   MenTaLguY <mental@rydia.net>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, output to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 */
#ifndef SEEN_INTERVAL_H
#define SEEN_INTERVAL_H

#include <assert.h>
#include <2geom/coord.h>

#include <boost/optional/optional.hpp>

namespace Geom {

class Interval;

/** 
 * \brief This class represents a range of numbers that is never empty.
 *
 * The endpoints are included in the range.
 */
class Interval {
private:
    Coord _b[2];

public:
    /// The default constructor creates an interval [0,0]  DO NOT RELY ON THIS, BEST NOT TO USE THIS CONSTRUCTOR
    explicit Interval() { _b[0] = 0;  _b[1] = 0; }
    explicit Interval(Coord u) { _b[0] = _b[1] = u; }
    /* When creating an Interval using the constructor specifying the exact range, the created interval
     * will be [u,v] when u<=v ; and will be [v,u] when v < u !!!
     */
    Interval(Coord u, Coord v) {
        if(u < v) {
            _b[0] = u; _b[1] = v;
        } else {
            _b[0] = v; _b[1] = u;
        }
    }
    
    double operator[](unsigned i) const {
        assert(i < 2);
        return _b[i];
    }
    inline double& operator[](unsigned i) { return _b[i]; }  //Trust the user...
    
    inline Coord min() const { return _b[0]; }
    inline Coord max() const { return _b[1]; }
    inline Coord extent() const { return _b[1] - _b[0]; }
    inline Coord middle() const { return (_b[1] + _b[0]) * 0.5; }
    
//    inline bool isEmpty() const { return _b[0] > _b[1]; }
    inline bool isSingular() const { return _b[0] == _b[1]; }
    inline bool contains(Coord val) const { return _b[0] <= val && val <= _b[1]; }
    bool contains(const Interval & val) const { return _b[0] <= val._b[0] && val._b[1] <= _b[1]; }
    bool intersects(const Interval & val) const {
        return contains(val._b[0]) || contains(val._b[1]) || val.contains(*this);
    }
    
    inline bool operator==(Interval other) const { return _b[0] == other._b[0] && _b[1] == other._b[1]; }
    inline bool operator!=(Interval other) const { return _b[0] != other._b[0] || _b[1] != other._b[1]; }
    
    //IMPL: OffsetableConcept
    //TODO: rename output_type to something else in the concept
    typedef Coord output_type;
    inline Interval operator+(Coord amnt) {
        return Interval(_b[0] + amnt, _b[1] + amnt);
    }
    inline Interval operator-(Coord amnt) {
        return Interval(_b[0] - amnt, _b[1] - amnt);
    }
    inline Interval operator+=(Coord amnt) {
        _b[0] += amnt; _b[1] += amnt;
        return *this;
    }
    inline Interval operator-=(Coord amnt) {
        _b[0] -= amnt; _b[1] -= amnt;
        return *this;
    }
    
    //IMPL: ScalableConcept
    inline Interval operator-() const { return Interval(*this); }
    inline Interval operator*(Coord s) const { return Interval(_b[0]*s, _b[1]*s); }
    inline Interval operator/(Coord s) const { return Interval(_b[0]/s, _b[1]/s); }
    Interval operator*=(Coord s) {
        if(s < 0) {
            Coord temp = _b[0];
            _b[0] = _b[1]*s;
            _b[1] = temp*s;
        } else {
            _b[0] *= s;
            _b[1] *= s;
        }
        return *this;
    }
    Interval operator/=(Coord s) {
        //TODO: what about s=0?
        if(s < 0) {
            Coord temp = _b[0];
            _b[0] = _b[1]/s;
            _b[1] = temp/s;
        } else {
            _b[0] /= s;
            _b[1] /= s;
        }
        return *this;
    }
    
    //TODO: NaN handleage for the next two?
    //TODO: Evaluate if wrap behaviour is proper.
    //If val > max, then rather than becoming a min==max range, it 'wraps' over
    void setMin(Coord val) {
        if(val > _b[1]) {
            _b[0] = _b[1];
            _b[1] = val;
        } else {
            _b[0] = val;
        }
    }
    //If val < min, then rather than becoming a min==max range, it 'wraps' over
    void setMax(Coord val) {
        if(val < _b[0]) {
            _b[1] = _b[0];
            _b[0] = val;
        } else {
            _b[1] = val;
        }
    }
    
    inline void extendTo(Coord val) {
       if(val < _b[0]) _b[0] = val;
       if(val > _b[1]) _b[1] = val;  //no else, as we want to handle NaN
    }
    
    static Interval fromArray(const Coord* c, int n) {
        assert(n > 0);
        Interval result(c[0]);
        for(int i = 1; i < n; i++) result.extendTo(c[i]);
        return result;
    }
    
    /** When this would create an empty interval, the interval will be the centerpoint of the old range only.
     */
    inline void expandBy(double amnt) {
        _b[0] -= amnt;
        _b[1] += amnt;
        if (_b[0] > _b[1]) {
            Coord halfway = (_b[0]+_b[1])/2;
            _b[0] = _b[1] = halfway;
        }
    }
    
    inline void unionWith(const Interval & a) {
        if(a._b[0] < _b[0]) _b[0] = a._b[0];
        if(a._b[1] > _b[1]) _b[1] = a._b[1];
    }
};

//IMPL: AddableConcept
inline Interval operator+(const Interval & a, const Interval & b) {
    return Interval(a.min() + b.min(), a.max() + b.max());
}
inline Interval operator-(const Interval & a, const Interval & b) {
    return Interval(a.min() - b.max(), a.max() - b.min());
}
inline Interval operator+=(Interval & a, const Interval & b) { a = a + b; return a; }
inline Interval operator-=(Interval & a, const Interval & b) { a = a - b; return a; }

//There might be impls of this based off sign checks
inline Interval operator*(const Interval & a, const Interval & b) {
    Interval res(a.min() * b.min());
    res.extendTo(a.min() * b.max());
    res.extendTo(a.max() * b.min());
    res.extendTo(a.max() * b.max());
    return res;
}
inline Interval operator*=(Interval & a, const Interval & b) { a = a * b; return a; }

/* reinstate if useful (doesn't do the proper thing for 0 inclusion)
inline Interval operator/(const Interval & a, const Interval & b) {
    Interval res(a.min() / b.min());
    res.extendTo(a.min() / b.max());
    res.extendTo(a.max() / b.min());
    res.extendTo(a.max() / b.max());
    return res;
}
inline Interval operator/=(Interval & a, const Interval & b) { a = a / b; return a; }
*/

// 'union' conflicts with C keyword
inline Interval unify(const Interval & a, const Interval & b) {
    return Interval(std::min(a.min(), b.min()),
                    std::max(a.max(), b.max()));
}

/**
 * \brief OptInterval is an Interval that can be empty.
 */
class OptInterval : public boost::optional<Interval> {
public:
    OptInterval() : boost::optional<Interval>() {};
    OptInterval(Interval const &a) : boost::optional<Interval>(a) {};
    OptInterval(Coord u) : boost::optional<Interval>(Interval(u)) {};
    OptInterval(Coord u, Coord v) : boost::optional<Interval>(Interval(u,v)) {};

    /**
     * Check whether this OptInterval is empty or not.
     */
    inline bool isEmpty() { return (*this == false); };
    
    /**
     * If \c this is empty, copy argument \c a. Otherwise, union with it (and do nothing when \c a is empty)
     */
    inline void unionWith(const OptInterval & a) {
        if (a) {
            if (*this) { // check that we are not empty
                (*this)->unionWith(*a);
            } else {
                *this = a;
            }
        }
    }
};

inline OptInterval intersect(const Interval & a, const Interval & b) {
    Coord u = std::max(a.min(), b.min()),
          v = std::min(a.max(), b.max());
    //technically >= might be incorrect, but singulars suck
    return u > v ? OptInterval()
                  : OptInterval(Interval(u, v));
}

#ifdef _GLIBCXX_IOSTREAM
inline std::ostream &operator<< (std::ostream &os, 
                                 const Geom::Interval &I) {
    os << "Interval("<<I[0] << ", "<<I[1] << ")";
    return os;
}
#endif

}
#endif //SEEN_INTERVAL_H

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :