Rectangle.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 /*
32  * Rectangle.h
33  *
34  * Created on: Sep 9, 2010
35  * Author: mpeura
36  */
37 
38 #ifndef DRAIN_RECTANGLE2_H_
39 #define DRAIN_RECTANGLE2_H_
40 
41 //#include <ostream>
42 #include <drain/UniTuple.h>
43 #include <cmath>
44 #include <vector>
45 
46 #include <drain/util/Point.h>
47 #include <drain/Type.h>
48 // using namespace std;
49 
50 
51 namespace drain {
52 
53 
54 
55 
57 
64 template <class T>
65 struct Rectangle : public drain::UniTuple<T,4> {
66 
67  Point2D<T> lowerLeft;
68  Point2D<T> upperRight;
69  // consider Range<T> x, Range<T> y,
70 
72  Rectangle(T x=0, T y=0, T x2=0, T y2=0) : lowerLeft(this->tuple(), 0), upperRight(this->tuple(), 2) {
73  this->set(x, y, x2, y2);
74  }
75 
77  Rectangle(const Rectangle & r) : lowerLeft(this->tuple(), 0), upperRight(this->tuple(), 2){
78  this->assignSequence(r);
79  };
80 
82  Rectangle(const Point2D<T> & ll, const Point2D<T> & ur) : lowerLeft(this->tuple(), 0), upperRight(this->tuple(), 2){
83  lowerLeft.setLocation(ll);
84  upperRight.setLocation(ur);
85  };
86 
87 
90  this->set(rect.tuple());
91  return *this;
92  }
93 
95  inline
96  T getWidth() const { return (this->upperRight.x - this->lowerLeft.x); };
97 
99  inline
100  T getHeight() const {
101  return (this->upperRight.y - this->lowerLeft.y);
102  };
103 
104  inline
105  T getArea() const {
106  return std::abs(getWidth()*getHeight());
107  };
108 
110  inline
111  bool empty() const {
112  return (getWidth()==0) || (getHeight()==0);
113  };
114 
115 
117  inline
118  void getCenter(drain::Point2D<T> &p) const {
119  const T divider = 2;
120  p.x = (lowerLeft.x + upperRight.x)/divider;
121  p.y = (lowerLeft.y + upperRight.y)/divider;
122  // p.x = static_cast<T>((lowerLeft.x + upperRight.x)/2.0);
123  // p.y = static_cast<T>((lowerLeft.y + upperRight.y)/2.0);
124  };
125 
126 
128 
133  bool crop(const Rectangle<T> & r);
134 
136  void extend(const Rectangle & r);
137 
139  void contract(const Rectangle & r);
140 
141 
142  inline
143  bool isInside(const T &x,const T &y) const {
144  return ((x>this->lowerLeft.x) && (x<this->upperRight.x) && (y>this->lowerLeft.y) && (y<this->upperRight.y));
145  };
146 
147  inline
148  bool isOverLapping(const Rectangle &r) const {
149  const bool xOverLap = !((r.upperRight.x < this->lowerLeft.x) || (r.lowerLeft.x > this->upperRight.x));
150  const bool yOverLap = !((r.upperRight.y < this->lowerLeft.y) || (r.lowerLeft.y > this->upperRight.y));
151  return (xOverLap && yOverLap);
152  };
153 
155  inline
156  std::vector<T> toVector() const {
157  std::vector<T> v;
159  return v;
160  }
161 
162 
163 protected:
164 
166  inline
167  bool limit(const T & lowerBound, const T & upperBound, T & x){
168 
169  if (lowerBound > upperBound){
170  return limit(upperBound, lowerBound, x);
171  //return true;
172  }
173 
174  if (x < lowerBound){
175  x = lowerBound;
176  return true;
177  }
178  else if (x > upperBound){
179  x = upperBound;
180  return true;
181  }
182 
183  return false;
184  }
185 
186 };
187 
188 
189 template <class T>
191  const Rectangle<T> bounds(*this);
192  bool result = false;
193  *this = r;
194  result |= limit(bounds.lowerLeft.x, bounds.upperRight.x, this->lowerLeft.x);
195  result |= limit(bounds.lowerLeft.x, bounds.upperRight.x, this->upperRight.x);
196  result |= limit(bounds.lowerLeft.y, bounds.upperRight.y, this->lowerLeft.y);
197  result |= limit(bounds.lowerLeft.y, bounds.upperRight.y, this->upperRight.y);
198  return result;
199 }
200 
201 
202 template <class T>
204 {
205  this->lowerLeft.x = std::min(this->lowerLeft.x,r.lowerLeft.x);
206  this->lowerLeft.y = std::min(this->lowerLeft.y,r.lowerLeft.y);
207  this->upperRight.x = std::max(this->upperRight.x,r.upperRight.x);
208  this->upperRight.y = std::max(this->upperRight.y,r.upperRight.y);
209 }
210 
211 
212 template <class T>
214 {
215  this->lowerLeft.x = std::max(this->lowerLeft.x,r.lowerLeft.x);
216  this->lowerLeft.y = std::max(this->lowerLeft.y,r.lowerLeft.y);
217  this->upperRight.x = std::min(this->upperRight.x,r.upperRight.x);
218  this->upperRight.y = std::min(this->upperRight.y,r.upperRight.y);
219 }
220 
221 
222 template <class T>
223 std::ostream & operator<<(std::ostream &ostr,const drain::Rectangle<T> &r){
224  ostr << r.lowerLeft.x << ',' << r.lowerLeft.y << ' ' << r.upperRight.x << ',' << r.upperRight.y;
225  return ostr;
226 }
227 
228 // template <class T>
229 // DRAIN_TYPENAME_DEF(Rectangle<T>, T);
230 
231 DRAIN_TYPENAME_T(Rectangle,T);
232 
233 /*
234 template <class T>
235 struct TypeName<Rectangle<T> > {
236  static const std::string & str(){
237  static const std::string name = drain::StringBuilder<>("Rectangle<", drain::TypeName<T>::str(), ">");
238  return name;
239  }
240 };
241 */
242 
243 //const std::string TypeName<tname>::name(#
244 
245 } // namespace drain
246 
247 /*
248 template <class T>
249 std::ostream & operator<<(std::ostream &ostr,const drain::Rectangle<T> &r){
250  ostr << r.lowerLeft.x << ',' << r.lowerLeft.y << ' ' << r.upperRight.x << ',' << r.upperRight.y;
251  return ostr;
252 }
253 */
254 
255 
256 
257 
258 
259 #endif /* RECTANGLE_H_ */
260 
261 // Drain
tuplebase_t & assignSequence(T &sequence, bool LENIENT=false)
Proposed for tuples only; derived classes should not shadow this.
Definition: TupleBase.h:244
T & toSequence(T &sequence) const
Copy elements to a Sequence, like stl::list, stl::set or stl::vector.
Definition: TupleBase.h:160
Tuple of N elements of type T.
Definition: UniTuple.h:65
Definition: DataSelector.cpp:1277
Definition: Point.h:48
Rectange defined through lower left and upper right coordinates.
Definition: Rectangle.h:65
Rectangle(const Point2D< T > &ll, const Point2D< T > &ur)
Constructor with corner points.
Definition: Rectangle.h:82
Rectangle(const Rectangle &r)
Copy constructor.
Definition: Rectangle.h:77
bool crop(const Rectangle< T > &r)
This becomes the intersection of r and this.
Definition: Rectangle.h:190
bool limit(const T &lowerBound, const T &upperBound, T &x)
Limits x between interval [lowerBound, upperBound].
Definition: Rectangle.h:167
bool empty() const
Return true if the area is zero.
Definition: Rectangle.h:111
T getWidth() const
Return the difference of X coordinates.
Definition: Rectangle.h:96
Rectangle(T x=0, T y=0, T x2=0, T y2=0)
Basic constructor.
Definition: Rectangle.h:72
T getHeight() const
Return the difference of Y coordinates.
Definition: Rectangle.h:100
void contract(const Rectangle &r)
The instance reduces to itse intersection with r.
Definition: Rectangle.h:213
std::vector< T > toVector() const
Write corner points to a vector [llX, llY, urX, urY].
Definition: Rectangle.h:156
void extend(const Rectangle &r)
The instance extends to its union with r.
Definition: Rectangle.h:203
Rectangle< T > & operator=(const Rectangle< T > &rect)
Assign rectangle of the same type.
Definition: Rectangle.h:89
void getCenter(drain::Point2D< T > &p) const
Return the center point.
Definition: Rectangle.h:118