interval.h revision 7bda77e763c0af49270427593108b66455dfd125
2N/A/**
2N/A * \file
2N/A * \brief Simple closed interval class
2N/A *//*
2N/A * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
2N/A *
2N/A * Original Rect/Range code by:
2N/A * Lauris Kaplinski <lauris@kaplinski.com>
2N/A * Nathan Hurst <njh@mail.csse.monash.edu.au>
2N/A * bulia byak <buliabyak@users.sf.net>
2N/A * MenTaLguY <mental@rydia.net>
2N/A *
2N/A * This library is free software; you can redistribute it and/or
2N/A * modify it either under the terms of the GNU Lesser General Public
2N/A * License version 2.1 as published by the Free Software Foundation
2N/A * (the "LGPL") or, at your option, under the terms of the Mozilla
2N/A * Public License Version 1.1 (the "MPL"). If you do not alter this
2N/A * notice, a recipient may use your version of this file under either
2N/A * the MPL or the LGPL.
2N/A *
2N/A * You should have received a copy of the LGPL along with this library
2N/A * in the file COPYING-LGPL-2.1; if not, output to the Free Software
2N/A * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2N/A * You should have received a copy of the MPL along with this library
2N/A * in the file COPYING-MPL-1.1
2N/A *
2N/A * The contents of this file are subject to the Mozilla Public License
2N/A * Version 1.1 (the "License"); you may not use this file except in
2N/A * compliance with the License. You may obtain a copy of the License at
2N/A * http://www.mozilla.org/MPL/
2N/A *
2N/A * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
2N/A * OF ANY KIND, either express or implied. See the LGPL or the MPL for
2N/A * the specific language governing rights and limitations.
2N/A *
2N/A */
2N/A#ifndef LIB2GEOM_SEEN_INTERVAL_H
2N/A#define LIB2GEOM_SEEN_INTERVAL_H
2N/A
2N/A#include <boost/none.hpp>
2N/A#include <boost/optional.hpp>
2N/A#include <boost/operators.hpp>
2N/A#include <2geom/coord.h>
2N/A#include <2geom/math-utils.h>
2N/A#include <2geom/generic-interval.h>
2N/A#include <2geom/int-interval.h>
2N/A
2N/Anamespace Geom {
2N/A
2N/A/**
2N/A * @brief Range of real numbers that is never empty.
2N/A *
2N/A * Intervals are closed ranges \f$[a, b]\f$, which means they include their endpoints.
2N/A * To use them as open ranges, you can use the interiorContains() methods.
2N/A *
2N/A * @ingroup Primitives
2N/A */
2N/Aclass Interval
2N/A : public GenericInterval<Coord>
2N/A{
2N/A typedef GenericInterval<Coord> Base;
2N/Apublic:
2N/A /// @name Create intervals.
2N/A /// @{
2N/A /** @brief Create an interval that contains only zero. */
2N/A Interval() {}
2N/A /** @brief Create an interval that contains a single point. */
2N/A explicit Interval(Coord u) : Base(u) {}
2N/A /** @brief Create an interval that contains all points between @c u and @c v. */
2N/A Interval(Coord u, Coord v) : Base(u,v) {}
2N/A /** @brief Convert from integer interval */
2N/A Interval(IntInterval const &i) : Base(i.min(), i.max()) {}
2N/A Interval(Base const &b) : Base(b) {}
2N/A
2N/A /** @brief Create an interval containing a range of values.
2N/A * The resulting interval will contain all values from the given range.
2N/A * The return type of iterators must be convertible to Coord. The given range
2N/A * must not be empty. For potentially empty ranges, see OptInterval.
2N/A * @param start Beginning of the range
2N/A * @param end End of the range
2N/A * @return Interval that contains all values from [start, end). */
2N/A template <typename InputIterator>
2N/A static Interval from_range(InputIterator start, InputIterator end) {
2N/A Interval result = Base::from_range(start, end);
2N/A return result;
2N/A }
2N/A /** @brief Create an interval from a C-style array of values it should contain. */
2N/A static Interval from_array(Coord const *c, unsigned n) {
Interval result = from_range(c, c+n);
return result;
}
/// @}
/// @name Inspect endpoints.
/// @{
/** @brief Access endpoints by value.
* @deprecated Use min() and max() instead */
Coord operator[](unsigned i) const { return _b[i]; }
/** @brief Access endpoints by reference.
* @deprecated Use min() and max() instead
* @todo Remove Interval index operator, which can be used to break the invariant */
Coord& operator[](unsigned i) { return _b[i]; }
bool isFinite() const {
return IS_FINITE(min()) && IS_FINITE(max());
}
/// @}
/// @name Test coordinates and other intervals for inclusion.
/// @{
/** @brief Check whether the interior of the interval includes this number.
* Interior means all numbers in the interval except its ends. */
bool interiorContains(Coord val) const { return min() < val && val < max(); }
/** @brief Check whether the interior of the interval includes the given interval.
* Interior means all numbers in the interval except its ends. */
bool interiorContains(Interval const &val) const { return min() < val.min() && val.max() < max(); }
/** @brief Check whether the interiors of the intervals have any common elements. */
bool interiorIntersects(Interval const &val) const {
return interiorContains(val.min()) || interiorContains(val.max()) || val.interiorContains(*this);
}
/// @}
/// @name Operators
/// @{
// IMPL: ScalableConcept
/** @brief Scale an interval */
Interval &operator*=(Coord s) {
_b[0] *= s;
_b[1] *= s;
if(s < 0) std::swap(_b[0], _b[1]);
return *this;
}
/** @brief Scale an interval by the inverse of the specified value */
Interval &operator/=(Coord s) {
_b[0] /= s;
_b[1] /= s;
if(s < 0) std::swap(_b[0], _b[1]);
return *this;
}
/** @brief Multiply two intervals.
* Product is defined as the set of points that can be obtained by multiplying
* any value from the second operand by any value from the first operand:
* \f$S = \{x \in A, y \in B: x * y\}\f$ */
Interval &operator*=(Interval const &o) {
// TODO implement properly
Coord mn = min(), mx = max();
expandTo(mn * o.min());
expandTo(mn * o.max());
expandTo(mx * o.min());
expandTo(mx * o.max());
return *this;
}
bool operator==(IntInterval const &ii) const {
return min() == Coord(ii.min()) && max() == Coord(ii.max());
}
bool operator==(Interval const &other) const {
return Base::operator==(other);
}
/// @}
/// @name Rounding to integer values
/// @{
/** @brief Return the smallest integer interval which contains this one. */
IntInterval roundOutwards() const {
IntInterval ret(floor(min()), ceil(max()));
return ret;
}
/** @brief Return the largest integer interval which is contained in this one. */
OptIntInterval roundInwards() const {
IntCoord u = ceil(min()), v = floor(max());
if (u > v) { OptIntInterval e; return e; }
IntInterval ret(u, v);
return ret;
}
/// @}
};
/**
* @brief Range of real numbers that can be empty.
* @ingroup Primitives
*/
class OptInterval
: public GenericOptInterval<Coord>
{
typedef GenericOptInterval<Coord> Base;
public:
/// @name Create optionally empty intervals.
/// @{
/** @brief Create an empty interval. */
OptInterval() : Base() {}
/** @brief Wrap an existing interval. */
OptInterval(Interval const &a) : Base(a) {}
/** @brief Create an interval containing a single point. */
OptInterval(Coord u) : Base(u) {}
/** @brief Create an interval containing a range of numbers. */
OptInterval(Coord u, Coord v) : Base(u,v) {}
OptInterval(Base const &b) : Base(b) {}
/** @brief Promote from IntInterval. */
OptInterval(IntInterval const &i) : Base(Interval(i)) {}
/** @brief Promote from OptIntInterval. */
OptInterval(OptIntInterval const &i) : Base() {
if (i) *this = Interval(*i);
}
};
// functions required for Python bindings
inline Interval unify(Interval const &a, Interval const &b)
{
Interval r = a | b;
return r;
}
inline OptInterval intersect(Interval const &a, Interval const &b)
{
OptInterval r = a & b;
return r;
}
} // end namespace Geom
#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:fileencoding=utf-8:textwidth=99 :