36 #ifndef SEEN_INTERVAL_H
37 #define SEEN_INTERVAL_H
42 #include <boost/optional/optional.hpp>
62 explicit Interval() { _b[0] = _b[1] = 0; }
72 double operator[](
unsigned i)
const {
76 inline double& operator[](
unsigned i) {
return _b[i]; }
78 inline Coord min()
const {
return _b[0]; }
79 inline Coord max()
const {
return _b[1]; }
80 inline Coord extent()
const {
return _b[1] - _b[0]; }
81 inline Coord middle()
const {
return (_b[1] + _b[0]) * 0.5; }
83 inline bool isEmpty()
const {
return _b[0] == _b[1]; }
84 inline bool contains(
Coord val)
const {
return _b[0] <= val && val <= _b[1]; }
85 bool contains(
const Interval & val)
const {
return _b[0] <= val._b[0] && val._b[1] <= _b[1]; }
86 bool intersects(
const Interval & val)
const {
87 return contains(val._b[0]) || contains(val._b[1]) || val.contains(*
this);
90 inline bool operator==(
Interval other) {
return _b[0] == other._b[0] && _b[1] == other._b[1]; }
91 inline bool operator!=(
Interval other) {
return _b[0] != other._b[0] || _b[1] != other._b[1]; }
95 typedef Coord output_type;
97 return Interval(_b[0] + amnt, _b[1] + amnt);
100 return Interval(_b[0] - amnt, _b[1] - amnt);
103 _b[0] += amnt; _b[1] += amnt;
107 _b[0] -= amnt; _b[1] -= amnt;
142 void setMin(
Coord val) {
151 void setMax(
Coord val) {
160 inline void extendTo(
Coord val) {
161 if(val < _b[0]) _b[0] = val;
162 if(val > _b[1]) _b[1] = val;
168 for(
int i = 1; i < n; i++) result.extendTo(c[i]);
172 inline void expandBy(
double amnt) {
177 inline void unionWith(
const Interval & a) {
178 if(a._b[0] < _b[0]) _b[0] = a._b[0];
179 if(a._b[1] > _b[1]) _b[1] = a._b[1];
185 return Interval(a.min() + b.min(), a.max() + b.max());
187 inline Interval operator-(
const Interval & a,
const Interval & b) {
188 return Interval(a.min() - b.max(), a.max() - b.min());
190 inline Interval operator+=(Interval & a,
const Interval & b) { a = a + b;
return a; }
191 inline Interval operator-=(Interval & a,
const Interval & b) { a = a - b;
return a; }
194 inline Interval operator*(
const Interval & a,
const Interval & b) {
195 Interval res(a.min() * b.min());
196 res.extendTo(a.min() * b.max());
197 res.extendTo(a.max() * b.min());
198 res.extendTo(a.max() * b.max());
201 inline Interval operator*=(Interval & a,
const Interval & b) { a = a * b;
return a; }
215 inline Interval unify(
const Interval & a,
const Interval & b) {
216 return Interval(std::min(a.min(), b.min()),
217 std::max(a.max(), b.max()));
219 inline boost::optional<Interval> intersect(
const Interval & a,
const Interval & b) {
220 Coord u = std::max(a.min(), b.min()),
221 v = std::min(a.max(), b.max());
223 return u >= v ? boost::optional<Interval>()
224 : boost::optional<Interval>(Interval(u, v));
228 #endif //SEEN_INTERVAL_H
double Coord
Definition: coord.h:45
Definition: interval.h:56