Fréchet View  1.6.0
A Tool for Exploring Fréchet Distance Algorithms
interval.h
Go to the documentation of this file.
1 #ifndef INTERVAL_H
2 #define INTERVAL_H
3 
4 #include <numeric.h>
5 #include <QDebug>
6 
7 namespace frechet { namespace data {
8 
31 class Interval {
32 private:
34  double _lower,_upper;
35 public:
37  static const Interval UNIT;
39  static const Interval INVALID;
40 public:
44  Interval() : _lower(NAN),_upper(NAN) {}
50  Interval(double lower, double upper) : _lower(lower), _upper(upper) {}
55  Interval(const Interval& that) : _lower(that._lower), _upper(that._upper) {}
61  Interval(const Interval& that,double shift) : _lower(that._lower+shift), _upper(that._upper+shift) {}
62 
67  double size() const { return _upper-_lower; }
68 
74  bool valid() const { return !std::isnan(_lower) && !std::isnan(_upper); }
79  bool empty() const { return _lower > _upper; }
83  operator bool () const { return valid() && !empty(); }
87  bool operator! () const { return !valid() || empty(); }
88 
92  double lower() const { return _lower; }
96  double upper() const { return _upper; }
97 
101  double& lower() { return _lower; }
105  double& upper() { return _upper; }
111  Interval& setLower(double value) { _lower=value; return *this; }
117  Interval& setUpper(double value) { _upper=value; return *this; }
121  double mid() const {
122  return (_lower+_upper)/2;
123  }
124 
130  Interval& operator= (const Interval& that) {
131  return assign(that._lower,that._upper);
132  }
137  inline static bool equals(double a, double b) {
138  if (std::isnan(a))
139  return std::isnan(b);
140  else
141  return a==b;
142  }
143 
149  bool operator== (const Interval& that) const {
150  return equals(_lower,that._lower) && equals(_upper,that._upper);
151  //return (_lower==that._lower) && (_upper==that._upper);
152  }
159  bool equals(const Interval& that, double precision) const {
160  return (abs(_lower-that._lower) <= precision) && ((abs(_upper-that._upper) <= precision));
161  }
167  bool operator!= (const Interval& that) const {
168  return ! operator==(that);
169  }
170 
171  /*
172  * */
178  if (empty())
179  return Interval(_upper,_lower);
180  else
181  return *this;
182  }
188  if (empty())
189  return assign(_upper,_lower);
190  else
191  return* this;
192  }
198  _lower = _upper = NAN;
199  return *this;
200  }
206  bool contains(double x) const {
207  return (x >= _lower) && (x <= _upper);
208  }
209 
216  bool contains(double x, double precision) const {
217  return (x >= (_lower-precision))
218  && (x <= (_upper+precision));
219  }
225  bool contains(const Interval& that) const {
226  return (that._lower >= _lower) && (that._upper <= _upper);
227  }
228 
234  bool intersects(const Interval& that) const {
235  return (that._upper >= _lower) && (that._lower <= _upper);
236  }
237 
243  bool intersects_proper(const Interval& that) const {
244  return (that._upper > _lower) && (that._lower < _upper);
245  }
246 
252  bool bordersTo(const Interval& that) const {
253  return (that._upper == _lower) || (that._lower == _upper);
254  }
260  double mapToUnitInterval(double x) const {
261  // map to Unit
262  if (size()==0.0)
263  return (x-lower());
264  else
265  return (x-lower()) / size();
266  }
267 
273  double mapFromUnitInterval(double x) const {
274  return lower() + x*size();
275  }
276 
283  double mapTo(double x, const Interval& other) const {
284  return other.mapFromUnitInterval(mapToUnitInterval(x));
285  }
286 
293  double mapFrom(double x, const Interval& other) const {
294  return mapFromUnitInterval(other.mapToUnitInterval(x));
295  }
296 
301  Interval operator+ (double offset) const {
302  return Interval(_lower+offset,_upper+offset);
303  }
308  Interval& operator+= (double offset) {
309  return assign(_lower+offset,_upper+offset);
310  }
311 
316  Interval operator- (double offset) const {
317  return Interval(_lower-offset,_upper-offset);
318  }
323  Interval& operator-= (double offset) {
324  return assign(_lower-offset,_upper-offset);
325  }
330  Interval operator* (double factor) const {
331  return Interval(_lower*factor,_upper*factor);
332  }
337  Interval& operator*= (double factor) {
338  return assign(_lower*factor,_upper*factor);
339  }
340 
345  Interval operator/ (double factor) const {
346  return Interval(_lower/factor,_upper/factor);
347  }
352  Interval& operator/= (double factor) {
353  return assign(_lower/factor,_upper/factor);
354  }
355 
361  Interval operator+ (const Interval& that) const {
362  return Interval(
363  numeric::min(_lower,that._lower),
364  numeric::max(_upper,that._upper));
365  }
366 
372  Interval& operator+= (const Interval& that) {
373  return assign(
374  numeric::min(_lower,that._lower),
375  numeric::max(_upper,that._upper));
376  }
377 
383  Interval operator- (const Interval& that) const {
384  return Interval(
385  numeric::max(_lower,that._upper),
386  numeric::min(_upper,that._lower));
387  }
388 
395  return assign(
396  numeric::max(_lower,that._upper),
397  numeric::min(_upper,that._lower));
398  }
399 
405  Interval operator& (const Interval& that) const {
406  return Interval(
407  numeric::max(_lower,that._lower),
408  numeric::min(_upper,that._upper));
409  }
410 
416  Interval& operator&= (const Interval& that) {
417  return assign(
418  numeric::max(_lower,that._lower),
419  numeric::min(_upper,that._upper));
420  }
421 
425  Interval& union_lf(const Interval& that);
426 
427 private:
434  Interval& assign(double lower, double upper) {
435  _lower = lower;
436  _upper = upper;
437  return *this;
438  }
439  // sketch for a lock-free union operator
441  static void assign_min_lf(volatile double* value, double update_value);
443  static void assign_max_lf(volatile double* value, double update_value);
445  static int64_t compare_and_swap(volatile int64_t* value, int64_t old_value, int64_t update_value);
446 };
447 
448 
457 {
458 public:
461 
465  IntervalPair() : H(), V() {}
471  IntervalPair(const Interval& ah, const Interval& av)
472  : H(ah),V(av)
473  { }
474 
480  QRectF boundingRect() const
481  {
482  QRectF r;
483  r.setCoords(H.lower(),V.lower(), H.upper(),V.upper());
484  return r;
485  }
491  bool operator== (const IntervalPair& that) {
492  return H==that.H && V==that.V;
493  }
499  bool operator!= (const IntervalPair& that) {
500  return H!=that.H || V!=that.V;
501  }
508  {
509  H += that.H;
510  V += that.V;
511  return *this;
512  }
513 
519  {
520  H.clear();
521  V.clear();
522  return *this;
523  }
530  IntervalPair translated(double i, double j) const
531  {
532  return IntervalPair(H+i, V+j);
533  }
534 };
535 
537 std::ostream& operator <<(std::ostream& stream, const frechet::data::Interval& ival);
539 std::ostream& operator <<(std::ostream& stream, const frechet::data::IntervalPair& ival);
540 
541 } } // namespace frechet::data
542 
544 QDebug operator<< (QDebug debug, const frechet::data::Interval& ival);
546 QDebug operator<< (QDebug debug, const frechet::data::IntervalPair& ival);
547 
548 #endif // INTERVAL_H
Interval(const Interval &that, double shift)
Copy constructor with shift offset.
Definition: interval.h:61
QDebug operator<<(QDebug debug, const frechet::data::Interval &ival)
operator for printing debug info to a QDebug stream
Definition: interval.cpp:89
static bool equals(double a, double b)
equality comparator
Definition: interval.h:137
bool contains(const Interval &that) const
containment test (closed interval, bounds inclusive)
Definition: interval.h:225
bool operator!() const
Definition: interval.h:87
double mapTo(double x, const Interval &other) const
re-map a value to another interval
Definition: interval.h:283
Interval & setLower(double value)
update lower bound
Definition: interval.h:111
IntervalPair & clear()
clear both intervals (assigning NAN as boundary values)
Definition: interval.h:518
Interval operator &(const Interval &that) const
intersection operator
Definition: interval.h:405
static const Interval INVALID
an invalid interval (contains NAN values)
Definition: interval.h:39
Interval H
horizontal interval
Definition: interval.h:459
global definitions for all algorithms.
Interval(const Interval &that)
Copy constructor.
Definition: interval.h:55
double mid() const
Definition: interval.h:121
bool intersects(const Interval &that) const
intersection test (closed interval, bounds inclusive)
Definition: interval.h:234
Interval operator/(double factor) const
divide by a factor
Definition: interval.h:345
bool intersects_proper(const Interval &that) const
intersection test (open interval, bounds exclusive)
Definition: interval.h:243
Interval & operator-=(double offset)
shift the interval by a fixed offset
Definition: interval.h:323
Interval & operator=(const Interval &that)
assignment operator
Definition: interval.h:130
Interval & union_lf(const Interval &that)
lock-free union operator; not used and not tested
Definition: interval.cpp:19
double _lower
lower and upper border of the interval
Definition: interval.h:34
Interval()
Default constructor. Creates an invalid Interval.
Definition: interval.h:44
a pair of horizonal / vertical intervals.
Definition: interval.h:456
double mapFrom(double x, const Interval &other) const
re-map a value from another interval to this interval
Definition: interval.h:293
bool bordersTo(const Interval &that) const
adjacency test
Definition: interval.h:252
bool empty() const
empty test
Definition: interval.h:79
bool operator!=(const Interval &that) const
non-equality comparator
Definition: interval.h:167
Interval & clear()
make this an invalid interval
Definition: interval.h:197
double upper() const
Definition: interval.h:96
std::ostream & operator<<(std::ostream &stream, const frechet::data::Interval &ival)
operator for printing debug info to a std::ostream
Definition: interval.cpp:79
bool equals(const Interval &that, double precision) const
equalality comparator with tolerance
Definition: interval.h:159
Interval V
vertical interval
Definition: interval.h:460
static void assign_max_lf(volatile double *value, double update_value)
lock-free assigment; not used and not tested
Definition: interval.cpp:46
Interval normalized() const
normalized
Definition: interval.h:177
Interval operator-(double offset) const
shift the interval by a fixed offset
Definition: interval.h:316
double mapFromUnitInterval(double x) const
re-map a value from the unit interval to this interval
Definition: interval.h:273
IntervalPair & operator+=(const IntervalPair &that)
union operator
Definition: interval.h:507
double size() const
size of the interval (upper - lower).
Definition: interval.h:67
bool operator==(const IntervalPair &that)
equality comparator
Definition: interval.h:491
bool contains(double x, double precision) const
containment test with tolerance (assumes closed interval, bounds inclusive)
Definition: interval.h:216
Interval & operator&=(const Interval &that)
intersection operator
Definition: interval.h:416
double lower() const
Definition: interval.h:92
Interval & operator+=(double offset)
shift the interval by a fixed offset
Definition: interval.h:308
double min(double a, double b)
minimum function with checks for NAN
Definition: numeric.h:222
Interval & normalize()
make sure that lower <= upper, swapping bounds, if necessary
Definition: interval.h:187
Interval & setUpper(double value)
update upper bound
Definition: interval.h:117
Interval operator *(double factor) const
multiply by a factor
Definition: interval.h:330
static void assign_min_lf(volatile double *value, double update_value)
lock-free assigment; not used and not tested
Definition: interval.cpp:28
Interval(double lower, double upper)
Constructor from boundary values.
Definition: interval.h:50
static const Interval UNIT
the unit interval [0,1]
Definition: interval.h:37
Interval & operator/=(double factor)
multiply by a factor
Definition: interval.h:352
IntervalPair()
empty constructor; creates a pair of invalid intervals
Definition: interval.h:465
double mapToUnitInterval(double x) const
re-map a value from this interval to the unit interval
Definition: interval.h:260
static int64_t compare_and_swap(volatile int64_t *value, int64_t old_value, int64_t update_value)
lock-free compare-and-swap; not used and not tested
Definition: interval.cpp:64
bool contains(double x) const
containment test (assumes closed interval, bounds inclusive)
Definition: interval.h:206
Interval & operator *=(double factor)
multiply by a factor
Definition: interval.h:337
IntervalPair translated(double i, double j) const
translate both intervals
Definition: interval.h:530
Interval operator+(double offset) const
shift the interval by a fixed offset
Definition: interval.h:301
bool operator!=(const IntervalPair &that)
non-equality comparator
Definition: interval.h:499
Interval & assign(double lower, double upper)
assign new bounds
Definition: interval.h:434
an interval of two double values.
Definition: interval.h:31
Number abs(const Number &x)
abs() function template for arbitrary numerical types
Definition: numeric.h:91
bool valid() const
validity test
Definition: interval.h:74
IntervalPair(const Interval &ah, const Interval &av)
constructor with intervals
Definition: interval.h:471
double max(double a, double b)
maximum function with checks for NAN
Definition: numeric.h:233
QRectF boundingRect() const
calculate the bounding rectangle, i.e. a rectangle that is defined by the horizontal and vertical int...
Definition: interval.h:480
bool operator==(const Interval &that) const
equality comparator
Definition: interval.h:149