Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
interval.h
Go to the documentation of this file.
1/*
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/operators.hpp>
42#include <2geom/coord.h>
43#include <2geom/math-utils.h>
45#include <2geom/int-interval.h>
46
47namespace Geom {
48
58 : public GenericInterval<Coord>
59{
61public:
64
65 constexpr Interval() = default;
67 explicit constexpr Interval(Coord u) : Base(u) {}
69 constexpr Interval(Coord u, Coord v) : Base(u, v) {}
71 constexpr Interval(IntInterval const &i) : Base(i.min(), i.max()) {}
72 constexpr Interval(Base const &b) : Base(b) {}
73
81 template <typename InputIterator>
82 static Interval from_range(InputIterator start, InputIterator end) {
83 return Base::from_range(start, end);
84 }
86 static Interval from_array(Coord const *c, unsigned n) {
87 return Base::from_array(c, n);
88 }
90
94 bool isFinite() const {
95 return std::isfinite(min()) && std::isfinite(max());
96 }
99 constexpr Coord valueAt(Coord t) const {
100 return lerp(t, min(), max());
101 }
104 constexpr Coord timeAt(Coord v) const {
105 return (v - min()) / extent();
106 }
108 constexpr Coord nearestTime(Coord v) const {
109 if (v <= min()) return 0;
110 if (v >= max()) return 1;
111 return timeAt(v);
112 }
114
117
119 constexpr bool interiorContains(Coord val) const { return min() < val && val < max(); }
122 constexpr bool interiorContains(Interval const &val) const { return min() < val.min() && val.max() < max(); }
124 constexpr bool lowerContains(Coord val) const { return min() <= val && val < max(); }
126 constexpr bool lowerContains(Interval const &val) const { return min() <= val.min() && val.max() < max(); }
128 constexpr bool upperContains(Coord val) { return min() < val && val <= max(); }
130 constexpr bool upperContains(Interval const &val) const { return min() < val.min() && val.max() <= max(); }
133 constexpr bool interiorIntersects(Interval const &val) const {
134 return std::max(min(), val.min()) < std::min(max(), val.max());
135 }
137
140 // IMPL: ScalableConcept
142 constexpr Interval &operator*=(Coord s) {
143 using std::swap;
144 _b[0] *= s;
145 _b[1] *= s;
146 if (s < 0) swap(_b[0], _b[1]);
147 return *this;
148 }
150 constexpr Interval &operator/=(Coord s) {
151 using std::swap;
152 _b[0] /= s;
153 _b[1] /= s;
154 if (s < 0) swap(_b[0], _b[1]);
155 return *this;
156 }
161 constexpr Interval &operator*=(Interval const &o) {
162 // TODO implement properly
163 Coord mn = min(), mx = max();
164 expandTo(mn * o.min());
165 expandTo(mn * o.max());
166 expandTo(mx * o.min());
167 expandTo(mx * o.max());
168 return *this;
169 }
170 constexpr bool operator==(IntInterval const &ii) const {
171 return min() == Coord(ii.min()) && max() == Coord(ii.max());
172 }
173 constexpr bool operator==(Interval const &other) const {
174 return Base::operator==(other);
175 }
177
180
182 return IntInterval(floor(min()), ceil(max()));
183 }
186 IntCoord u = ceil(min()), v = floor(max());
187 if (u > v) return {};
188 return IntInterval(u, v);
189 }
191};
192
198 : public GenericOptInterval<Coord>
199{
201public:
202 using Base::Base;
203 using Base::operator==;
204 using Base::operator!=;
205
206 constexpr OptInterval(Base const &b) : Base(b) {}
207
209 constexpr OptInterval(IntInterval const &i) : Base(Interval(i)) {}
211 constexpr OptInterval(OptIntInterval const &i) {
212 if (i) *this = Interval(*i);
213 }
214};
215
216// functions required for Python bindings
217inline Interval unify(Interval const &a, Interval const &b) {
218 return a | b;
219}
220inline OptInterval intersect(Interval const &a, Interval const &b) {
221 return a & b;
222}
223
224} // namespace Geom
225
226// Structured binding support
227template <> struct std::tuple_size<Geom::Interval> : std::integral_constant<size_t, 2> {};
228template <size_t I> struct std::tuple_element<I, Geom::Interval> { using type = Geom::Coord; };
229
230// Hash support
231template <> struct std::hash<Geom::Interval> : std::hash<Geom::GenericInterval<Geom::Coord>> {};
232
233#endif // LIB2GEOM_SEEN_INTERVAL_H
234
235/*
236 Local Variables:
237 mode:c++
238 c-file-style:"stroustrup"
239 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
240 indent-tabs-mode:nil
241 fill-column:99
242 End:
243*/
244// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
A range of numbers which is never empty.
constexpr Coord extent() const
static CInterval from_range(InputIterator start, InputIterator end)
Create an interval containing a range of values.
static CInterval from_array(Coord const *c, unsigned n)
Create an interval from a C-style array of values it should contain.
constexpr bool operator==(CInterval const &other) const
Test for interval equality.
constexpr void expandTo(Coord val)
Extend the interval to include the given number.
constexpr Coord min() const
constexpr Coord max() const
A range of numbers that can be empty.
std::optional< CInterval > Base
Range of real numbers that is never empty.
Definition interval.h:59
constexpr bool operator==(IntInterval const &ii) const
Definition interval.h:170
constexpr Coord nearestTime(Coord v) const
Find closest time in [0,1] that maps to the given value. *‍/.
Definition interval.h:108
constexpr bool upperContains(Interval const &val) const
Check whether the given interval is contained in the union of the interior and the upper boundary.
Definition interval.h:130
constexpr Interval & operator*=(Coord s)
Scale an interval.
Definition interval.h:142
constexpr bool interiorContains(Coord val) const
Check whether the interior of the interval includes this number.
Definition interval.h:119
constexpr Coord valueAt(Coord t) const
Map the interval [0,1] onto this one.
Definition interval.h:99
constexpr Interval(Base const &b)
Definition interval.h:72
constexpr bool lowerContains(Coord val) const
Check whether the number is contained in the union of the interior and the lower boundary.
Definition interval.h:124
constexpr Coord timeAt(Coord v) const
Compute a time value that maps to the given value.
Definition interval.h:104
constexpr Interval(Coord u, Coord v)
Create an interval that contains all points between u and v.
Definition interval.h:69
constexpr bool lowerContains(Interval const &val) const
Check whether the given interval is contained in the union of the interior and the lower boundary.
Definition interval.h:126
OptIntInterval roundInwards() const
Return the largest integer interval which is contained in this one.
Definition interval.h:185
constexpr Interval & operator*=(Interval const &o)
Multiply two intervals.
Definition interval.h:161
constexpr Interval(Coord u)
Create an interval that contains a single point.
Definition interval.h:67
constexpr bool operator==(Interval const &other) const
Definition interval.h:173
constexpr Interval & operator/=(Coord s)
Scale an interval by the inverse of the specified value.
Definition interval.h:150
constexpr Interval(IntInterval const &i)
Convert from integer interval.
Definition interval.h:71
constexpr Interval()=default
Create an interval that contains only zero.
constexpr bool interiorContains(Interval const &val) const
Check whether the interior of the interval includes the given interval.
Definition interval.h:122
IntInterval roundOutwards() const
Return the smallest integer interval which contains this one.
Definition interval.h:181
constexpr bool interiorIntersects(Interval const &val) const
Check whether the interiors of the intervals have any common elements.
Definition interval.h:133
static Interval from_array(Coord const *c, unsigned n)
Create an interval from a C-style array of values it should contain.
Definition interval.h:86
bool isFinite() const
Definition interval.h:94
constexpr bool upperContains(Coord val)
Check whether the number is contained in the union of the interior and the upper boundary.
Definition interval.h:128
static Interval from_range(InputIterator start, InputIterator end)
Create an interval containing a range of values.
Definition interval.h:82
Range of real numbers that can be empty.
Definition interval.h:199
constexpr OptInterval(OptIntInterval const &i)
Promote from OptIntInterval.
Definition interval.h:211
constexpr OptInterval(IntInterval const &i)
Promote from IntInterval.
Definition interval.h:209
constexpr OptInterval(Base const &b)
Definition interval.h:206
Integral and real coordinate types and some basic utilities.
double c[8][4]
Closed interval of generic values.
constexpr Coord lerp(Coord t, Coord a, Coord b)
Numerically stable linear interpolation.
Definition coord.h:97
int IntCoord
Type used for integral coordinates.
Definition coord.h:80
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
GenericInterval< IntCoord > IntInterval
Range of integers that is never empty.
Definition forward.h:51
auto floor(Geom::Rect const &rect)
Definition geom.h:131
Closed interval of integer values.
Low level math functions and compatibility wrappers.
Geom::Point start
Geom::Point end
Various utility functions.
Definition affine.h:22
IntRect unify(IntRect const &a, IntRect const &b)
Definition int-rect.h:55
std::vector< Point > intersect(const xAx &C1, const xAx &C2)
Definition conicsec.cpp:361