DP3
Box.h
Go to the documentation of this file.
1 // Box.h: Class representing a 2-dim box
2 //
3 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
9 
10 #ifndef LOFAR_PARMDB_BOX_H
11 #define LOFAR_PARMDB_BOX_H
12 
13 #include <casacore/casa/BasicMath/Math.h>
14 
15 #include <cassert>
16 #include <utility>
17 #include <vector>
18 
19 namespace dp3 {
20 namespace parmdb {
21 
23 typedef std::pair<double, double> Point;
24 
27 class Box;
28 Box unite(const Box& lhs, const Box& rhs);
29 Box intersect(const Box& lhs, const Box& rhs);
30 
32 
36 class Box {
37  public:
39  Box() : itsStart(0, 0), itsEnd(0, 0) {}
40 
43  Box(const Point& start, const Point& end) : itsStart(start), itsEnd(end) {
44  assert(start.first <= end.first && start.second <= end.second);
45  }
46 
49  Box(const std::vector<double>&);
50 
52  Box(double x1, double x2, double y1, double y2, bool asStartEnd = false);
53 
56  bool operator==(const Box& that) const {
57  return itsStart == that.itsStart && itsEnd == that.itsEnd;
58  }
59  bool operator!=(const Box& that) const { return !operator==(that); }
61 
64  const Point& lower() const { return itsStart; }
65  const Point& upper() const { return itsEnd; }
66  double lowerX() const { return itsStart.first; }
67  double lowerY() const { return itsStart.second; }
68  double upperX() const { return itsEnd.first; }
69  double upperY() const { return itsEnd.second; }
71 
74  double widthX() const { return itsEnd.first - itsStart.first; }
75  double widthY() const { return itsEnd.second - itsStart.second; }
77 
80  bool intersects(const Box& other) const {
81  return (other.itsStart.first < itsEnd.first &&
82  !casacore::near(other.itsStart.first, itsEnd.first) &&
83  other.itsEnd.first > itsStart.first &&
84  !casacore::near(other.itsEnd.first, itsStart.first) &&
85  other.itsStart.second < itsEnd.second &&
86  !casacore::near(other.itsStart.second, itsEnd.second) &&
87  other.itsEnd.second > itsStart.second &&
88  !casacore::near(other.itsEnd.second, itsStart.second));
89  }
90 
93  bool contains(const Box& other) const {
94  return ((other.itsStart.first >= itsStart.first ||
95  casacore::near(other.itsStart.first, itsStart.first)) &&
96  (other.itsEnd.first <= itsEnd.first ||
97  casacore::near(other.itsEnd.first, itsEnd.first)) &&
98  (other.itsStart.second >= itsStart.second ||
99  casacore::near(other.itsStart.second, itsStart.second)) &&
100  (other.itsEnd.second <= itsEnd.second ||
101  casacore::near(other.itsEnd.second, itsEnd.second)));
102  }
103 
105  bool empty() const {
106  return (casacore::near(itsStart.first, itsEnd.first) ||
107  casacore::near(itsStart.second, itsEnd.second));
108  }
109 
114  Box operator&(const Box& that) const { return intersect(*this, that); }
115 
120  Box operator|(const Box& that) const { return unite(*this, that); }
121 
125  bool operator<(const Box& that) const {
126  return itsStart.second < that.itsStart.second ||
127  (itsStart.second == that.itsStart.second &&
128  itsStart.first < that.itsStart.first);
129  }
130  bool operator>(const Box& that) const {
131  return itsStart.second > that.itsStart.second ||
132  (itsStart.second == that.itsStart.second &&
133  itsStart.first > that.itsStart.first);
134  }
136 
138  void print() const;
139 
140  private:
141  Point itsStart;
142  Point itsEnd;
143 };
144 
146 
147 } // namespace parmdb
148 } // namespace dp3
149 
150 #endif
Class representing a 2-dim box.
Definition: Box.h:36
double upperX() const
Definition: Box.h:68
Box(const Point &start, const Point &end)
Definition: Box.h:43
bool contains(const Box &other) const
Definition: Box.h:93
Box(const std::vector< double > &)
Box operator&(const Box &that) const
Definition: Box.h:114
Box(double x1, double x2, double y1, double y2, bool asStartEnd=false)
Create from start/end or center/width.
double widthX() const
Definition: Box.h:74
double upperY() const
Definition: Box.h:69
Box()
Default constructor creates an empty box.
Definition: Box.h:39
bool intersects(const Box &other) const
Definition: Box.h:80
bool operator==(const Box &that) const
Definition: Box.h:56
const Point & lower() const
Definition: Box.h:64
double lowerX() const
Definition: Box.h:66
const Point & upper() const
Definition: Box.h:65
double widthY() const
Definition: Box.h:75
void print() const
Output the start and end point coordinates of the Box.
bool operator<(const Box &that) const
Definition: Box.h:125
bool operator!=(const Box &that) const
Definition: Box.h:59
bool empty() const
Check if the box is empty.
Definition: Box.h:105
Box operator|(const Box &that) const
Definition: Box.h:120
bool operator>(const Box &that) const
Definition: Box.h:130
double lowerY() const
Definition: Box.h:67
Box unite(const Box &lhs, const Box &rhs)
Box intersect(const Box &lhs, const Box &rhs)
std::pair< double, double > Point
Point: A point in a 2-D space.
Definition: Box.h:23
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53