LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
geoarea.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "cad/const.h"
4 
5 #include "geocoordinate.h"
6 #include "geovector.h"
7 #include "geobase.h"
8 #include "cad/base/visitor.h"
9 
10 namespace lc {
11  namespace geo {
15  class Area : public Base, virtual public Visitable {
16  public:
24  explicit Area(const Coordinate& coordA, const Coordinate& coordB) :
25  _minP(Coordinate(std::min(coordA.x(), coordB.x()), std::min(coordA.y(), coordB.y()), std::min(coordA.z(), coordB.z()))),
26  _maxP(Coordinate(std::max(coordA.x(), coordB.x()), std::max(coordA.y(), coordB.y()), std::max(coordA.z(), coordB.z()))) {
27  if (coordA.x() != coordB.x() && coordA.y() != coordB.y() && coordA.z() != coordB.z()) {
28  throw "Points describe a volume, not a area.";
29  }
30 
31  }
32 
33  explicit Area() : _minP(0., 0.), _maxP(0., 0.) {
34  }
35 
43  explicit Area(const Coordinate& coord, double width, double height) : Area(coord, Coordinate(coord.x() + width, coord.y() + height)) {
44  }
45 
46  //explicit Area(const Area &a) : _minP(a._minP), _maxP(a._maxP) {}
47 
48  inline Area& operator = (const Area& other) {
49  if (this != &other) {
50  _minP = other._minP;
51  _maxP = other._maxP;
52  }
53 
54  return *this;
55  }
56 
60  inline const Coordinate minP() const {
61  return _minP;
62  }
63 
67  inline const Coordinate maxP() const {
68  return _maxP;
69  }
70 
76  inline double width() const {
77  return _maxP.x() - _minP.x();
78  }
79 
85  inline double height() const {
86  return _maxP.y() - _minP.y();
87  }
88 
94  inline bool inArea(const Coordinate& point, double tolerance = 0.) const {
95  return (point.x() >= _minP.x() - tolerance && point.x() <= _maxP.x() + tolerance && point.y() >= _minP.y() - tolerance && point.y() <= _maxP.y() + tolerance);
96  }
97 
104  inline bool inArea(const Area& area) const {
105  return _minP.x() >= area._minP.x() && _minP.y() >= area._minP.y() && _maxP.x() <= area._maxP.x() && _maxP.y() <= area._maxP.y();
106  }
107 
114  inline bool overlaps(const Area& otherArea) const {
115  if (otherArea._maxP.x() < _minP.x() || otherArea._minP.x() > _maxP.x() || otherArea._maxP.y() < _minP.y() || otherArea._minP.y() > _maxP.y()) {
116  return false;
117  } else {
118  return true;
119  }
120  }
121 
128  inline short numCornersInside(const Area& otherArea) const {
129  short pointsInside = 0;
130 
131  if (otherArea.inArea(_minP)) {
132  pointsInside++;
133  }
134 
135  if (otherArea.inArea(_maxP)) {
136  pointsInside++;
137  }
138 
139  if (otherArea.inArea(Coordinate(_minP.x(), _maxP.y()))) {
140  pointsInside++;
141  }
142 
143  if (otherArea.inArea(Coordinate(_maxP.x(), _minP.y()))) {
144  pointsInside++;
145  }
146 
147  return pointsInside;
148  }
149 
156  inline Area merge(const Area& other) const {
157  return Area(
158  Coordinate(std::min(other.minP().x(), this->minP().x()), std::min(other.minP().y(), this->minP().y())),
159  Coordinate(std::max(other.maxP().x(), this->maxP().x()), std::max(other.maxP().y(), this->maxP().y())));
160  }
161 
168  inline Area merge(const Coordinate& other) const {
169  return Area(
170  Coordinate(std::min(other.x(), this->minP().x()), std::min(other.y(), this->minP().y())),
171  Coordinate(std::max(other.x(), this->maxP().x()), std::max(other.y(), this->maxP().y())));
172  }
173 
181  inline Area intersection(const Area& other, double tolerance = 0.) const {
182  Area ret(
183  Coordinate(std::max(other.minP().x(), this->minP().x()), std::max(other.minP().y(), this->minP().y())),
184  Coordinate(std::min(other.maxP().x(), this->maxP().x()), std::min(other.maxP().y(), this->maxP().y())));
185 
186  if (ret.width() < tolerance || ret.height() < tolerance) {
187  return Area();
188  }
189 
190  return ret;
191  }
192 
198  inline Vector top() const {
199  return Vector(Coordinate(_minP.x(), _maxP.y()), Coordinate(_maxP.x(), _maxP.y()));
200  }
201 
207  inline Vector left() const {
208  return Vector(Coordinate(_minP.x(), _minP.y()), Coordinate(_minP.x(), _maxP.y()));
209  }
210 
216  inline Vector bottom() const {
217  return Vector(Coordinate(_minP.x(), _minP.y()), Coordinate(_maxP.x(), _minP.y()));
218  }
219 
223  inline Area increaseBy (double increaseBy) const {
226  }
232  inline bool operator==(const Area& ar) const {
233  return this->minP() == ar.minP() && this->maxP() == ar.maxP();
234  }
235 
236 
242  inline Vector right() const {
243  return Vector(Coordinate(_maxP.x(), _minP.y()), Coordinate(_maxP.x(), _maxP.y()));
244  }
245 
246  virtual void accept(GeoEntityVisitor &v) const override { v.visit(*this); }
247 
248  private:
249  friend std::ostream& operator<<(std::ostream& os, const Area& area) {
250  os << "Area(" << area.minP() << " " << area.maxP() << ")";
251  return os;
252  }
253 
254  private:
257  };
258  }
259 }
const Coordinate minP() const
Definition: geoarea.h:60
double x() const
Returns x of Coordinate.
Definition: geocoordinate.h:26
Vector right() const
right vector of this area
Definition: geoarea.h:242
Area increaseBy(double increaseBy) const
Definition: geoarea.h:223
Coordinate _minP
Definition: geoarea.h:255
Area intersection(const Area &other, double tolerance=0.) const
merge two area's and expand if required to largest containing area
Definition: geoarea.h:181
Area merge(const Coordinate &other) const
merge two area's and expand if required to largest containing area
Definition: geoarea.h:168
double y() const
Returns y of Coordinate.
Definition: geocoordinate.h:34
friend std::ostream & operator<<(std::ostream &os, const Area &area)
Definition: geoarea.h:249
bool operator==(const Area &ar) const
checks for the equality of Area
Definition: geoarea.h:232
Area merge(const Area &other) const
merge two area's and expand if required to largest containing area
Definition: geoarea.h:156
Area(const Coordinate &coord, double width, double height)
Area given at a coordinate with a given width and height.
Definition: geoarea.h:43
Definition: cadentity.h:12
double z() const
Returns z of Coordinate.
Definition: geocoordinate.h:42
virtual void accept(GeoEntityVisitor &v) const override
Definition: geoarea.h:246
Coordinate _maxP
Definition: geoarea.h:256
Vector top() const
top vector of this area
Definition: geoarea.h:198
Area & operator=(const Area &other)
Definition: geoarea.h:48
bool inArea(const Area &area) const
inArea test if this object's fits fully in area
Definition: geoarea.h:104
short numCornersInside(const Area &otherArea) const
numCornersInside count the number of corners this object has in otherArea
Definition: geoarea.h:128
Vector bottom() const
bottom vector of this area
Definition: geoarea.h:216
const Coordinate maxP() const
Definition: geoarea.h:67
double height() const
height Returns the height of this area
Definition: geoarea.h:85
bool inArea(const Coordinate &point, double tolerance=0.) const
Test of a specific point lies within area.
Definition: geoarea.h:94
Vector left() const
left vector for this area
Definition: geoarea.h:207
double width() const
width Returns the width of this area
Definition: geoarea.h:76
bool overlaps(const Area &otherArea) const
overlaps returns true if any overlap is happening between the two area's, even if otherArea fits with...
Definition: geoarea.h:114
Area(const Coordinate &coordA, const Coordinate &coordB)
Definition: geoarea.h:24