Rectangle.hh
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <boost/range/algorithm/max_element.hpp>
7 #include <boost/range/algorithm/min_element.hpp>
8 #include <boost/range/algorithm_ext/is_sorted.hpp>
9 #include <boost/range/algorithm_ext/erase.hpp>
10 
11 #include "boca/math/Vector2.hh"
12 #include "boca/math/Range.hh"
13 
14 namespace boca
15 {
16 
21 template<typename Value>
22 class Rectangle
23 {
24 
25 public:
26 
27  Rectangle() {}
28 
30  x_(x),
31  y_(y)
32  {}
33 
35  x_( {min.X(), max.X()}),
36  y_( {min.Y(), max.Y()})
37  {}
38 
39  Rectangle(Vector2<Value> min, Value width, Value height) :
40  x_( {min.X(), min.X() + width}),
41  y_( {min.Y(), min.Y() + height})
42  {}
43 
44  Rectangle(Value x_min, Value x_max, Value y_min, Value y_max) :
45  x_( {x_min, x_max}),
46  y_( {y_min, y_max})
47  {}
48 
49  void SetX(Value const& min, Value const& max) {
50  x_.Set(min, max);
51  }
52 
53  void SetY(Value const& min, Value const& max) {
54  y_.Set(min, max);
55  }
56 
57  void SetX(Range<Value> const& x) {
58  x_ = x;
59  }
60 
61  void SetY(Range<Value> const& y) {
62  y_ = y;
63  }
64 
65  void SetX(std::pair<Value, Value> const& x) {
66  x_.Set(x);
67  }
68 
69  void SetY(std::pair<Value, Value> const& y) {
70  y_.Set(y);
71  }
72 
73  void ResetY() {
74  y_ = Range<Value>();
75  }
76 
77  void ResetX() {
78  x_ = Range<Value>();
79  }
80 
81  void SetXMin(Value x_min) {
82  x_.SetMin(x_min);
83  }
84 
85  void SetXMax(Value x_max) {
86  x_.SetMax(x_max);
87  }
88 
89  void SetYMin(Value y_min) {
90  y_.SetMin(y_min);
91  }
92 
93  void SetYMax(Value y_max) {
94  y_.SetMax(y_max);
95  }
96 
97  void Widen(Rectangle<Value> const& range) {
98  x_.Widen(range.Horizontal());
99  y_.Widen(range.Vertical());
100  }
101 
102  void WidenX(Range<Value> const& x) {
103  x_.Widen(x);
104  }
105 
106  void WidenXMin(Value x_min) {
107  x_.WidenMin(x_min);
108  }
109 
110  void WidenXMax(Value x_max) {
111  x_.WidenMax(x_max);
112  }
113 
114  void WidenY(Range<Value> const& y) {
115  y_.Widen(y);
116  }
117 
118  void WidenYMin(Value y_min) {
119  y_.WidenMin(y_min);
120  }
121 
122  void WidenYMax(Value y_max) {
123  y_.WidenMax(y_max);
124  }
125 
126  Value XMin() const {
127  return x_.Min();
128  }
129 
130  Value XMax() const {
131  return x_.Max();
132  }
133 
134  Value YMin() const {
135  return y_.Min();
136  }
137 
138  Value YMax() const {
139  return y_.Max();
140  }
141 
142  Vector2<Value> Min() const {
143  return {x_.Min(), y_.Min()};
144  }
145 
146  Vector2<Value> Max() const {
147  return {x_.Max(), y_.Max()};
148  }
149 
151  return x_;
152  }
153 
155  return y_;
156  }
157 
158  Range<Value> const& Horizontal() const {
159  return x_;
160  }
161 
162  Range<Value> const& Vertical() const {
163  return y_;
164  }
165 
166  Value Width() const {
167  return x_.Length();
168  }
169 
170  Value Height() const {
171  return y_.Length();
172  }
173 
174  template <typename Value2>
175  void WidenY(Range<Value> const& bound_x, std::vector<Value2> const& xs1, std::vector<Value2> const& ys1) {
176 // y_.Widen({0.001,1});
177  // std::cout << "bound min " << bound_x.Min() << " bound max " << bound_x.Max() << '\n';
178  auto xs = xs1;
179  boost::remove_erase(xs, 0);
180  auto ys = ys1;
181  boost::remove_erase(ys, 0);
182  auto bound = Range<int>{};
183  if (boost::range::is_sorted(xs, Smaller<Value2>())) {
184 // std::cout << "smaller" << '\n';
185  bound.SetMin(boost::range::lower_bound(xs, Value2(bound_x.Min())) - xs.begin());
186  bound.SetMax(boost::range::upper_bound(xs, Value2(bound_x.Max())) - xs.begin());
187  } else if (boost::range::is_sorted(xs, Larger<Value2>())) {
188 // std::cout << "larger" << '\n';
189  bound.SetMin(boost::range::lower_bound(xs, Value2(bound_x.Max()), Larger<Value2>()) - xs.begin());
190  bound.SetMax(boost::range::upper_bound(xs, Value2(bound_x.Min()), Larger<Value2>()) - xs.begin());
191  } else {
192 // std::cout << "not sorted" << '\n';
193  bound.SetMin(0);
194  bound.SetMax(xs.size());
195  }
196 // std::cout << "min_x " << bound.Min() << " max_x " << bound.Max() << '\n';
197  auto bound_y = Range<Value2>{};
198  auto min = std::min_element(ys.begin() + bound.Min(), ys.begin() + bound.Max());
199 // , SmallerButNonZero<Value2>());
200  bound_y.SetMin(*min);
201  bound_y.SetMax(*std::max_element(ys.begin() + bound.Min(), ys.begin() + bound.Max()));
202 // std::cout << "min " << bound_y.Min() << " max " << bound_y.Max() << '\n';
203  y_.Widen(bound_y);
204  }
205 
206  template <typename Value2>
207  void WidenX(Range<Value> const& y, std::vector<Value2> const& xs, std::vector<Value2> const& ys) {
208  auto min_y = boost::range::upper_bound(ys, Value2(y.Min())) - ys.begin();
209  auto max_y = boost::range::lower_bound(ys, Value2(y.Max())) - ys.begin();
210  auto min = std::min_element(xs.begin() + min_y, xs.begin() + max_y, SmallerButNonZero<Value2>());
211  auto max = std::max_element(xs.begin() + min_y, xs.begin() + max_y);
212  x_.Widen( {std::min(*min, *max), std::max(*min, *max)});
213  }
214 
218  friend auto &operator<<(std::ostream &stream, Rectangle const &rectangle)
219  {
220  stream << rectangle.x_ << rectangle.y_;
221  return stream;
222  }
223 
224 private:
225 
226  template <typename Value_2_>
227  std::function<bool(Value_2_ value_1, Value_2_ value_2)> Smaller() {
228  return [](Value_2_ value_1, Value_2_ value_2) {
229  return value_1 < value_2;
230  };
231  }
232 
233  template <typename Value_2_>
234  std::function<bool(Value_2_ value_1, Value_2_ value_2)> SmallerButNonZero() {
235  return [](Value_2_ value_1, Value_2_ value_2) {
236  return value_1 != 0 ? value_1 < value_2 : value_1 > value_2;
237  };
238  }
239 
240  template <typename Value_2_>
241  std::function<bool(Value_2_ value_1, Value_2_ value_2)> Larger() {
242  return [](Value_2_ value_1, Value_2_ value_2) {
243  return value_1 > value_2;
244  };
245  }
246 
247  Range<Value> x_;
248  Range<Value> y_;
249 
250 };
251 
252 }
253 
Rectangle(Range< Value > x, Range< Value > y)
Definition: Rectangle.hh:29
Value_ Min() const
Definition: Range.hh:91
Range< Value > & Vertical()
Definition: Rectangle.hh:154
void SetXMin(Value x_min)
Definition: Rectangle.hh:81
void SetYMax(Value y_max)
Definition: Rectangle.hh:93
void WidenY(Range< Value > const &y)
Definition: Rectangle.hh:114
Range< Value > const & Horizontal() const
Definition: Rectangle.hh:158
friend auto & operator<<(std::ostream &stream, Rectangle const &rectangle)
Output stream operator.
Definition: Rectangle.hh:218
Value Height() const
Definition: Rectangle.hh:170
Rectangle of two Ranges.
Definition: Rectangle.hh:22
void WidenY(Range< Value > const &bound_x, std::vector< Value2 > const &xs1, std::vector< Value2 > const &ys1)
Definition: Rectangle.hh:175
void WidenXMin(Value x_min)
Definition: Rectangle.hh:106
Value_ Max() const
Definition: Range.hh:95
Value YMax() const
Definition: Rectangle.hh:138
void WidenYMax(Value y_max)
Definition: Rectangle.hh:122
Rectangle(Value x_min, Value x_max, Value y_min, Value y_max)
Definition: Rectangle.hh:44
void SetY(std::pair< Value, Value > const &y)
Definition: Rectangle.hh:69
void Widen(Rectangle< Value > const &range)
Definition: Rectangle.hh:97
Rectangle()
Definition: Rectangle.hh:27
Range< Value > & Horizontal()
Definition: Rectangle.hh:150
void SetY(Range< Value > const &y)
Definition: Rectangle.hh:61
void SetYMin(Value y_min)
Definition: Rectangle.hh:89
Range< Value > const & Vertical() const
Definition: Rectangle.hh:162
void WidenYMin(Value y_min)
Definition: Rectangle.hh:118
Value_ Length() const
Definition: Range.hh:122
Value Width() const
Definition: Rectangle.hh:166
void ResetY()
Definition: Rectangle.hh:73
void Widen(Range< Value_ > const &bound)
Definition: Range.hh:70
Boosted Collider Analysis.
Definition: Analysis.hh:15
void WidenXMax(Value x_max)
Definition: Rectangle.hh:110
void SetY(Value const &min, Value const &max)
Definition: Rectangle.hh:53
void WidenX(Range< Value > const &y, std::vector< Value2 > const &xs, std::vector< Value2 > const &ys)
Definition: Rectangle.hh:207
void SetMin(Value_ min)
Definition: Range.hh:48
void SetX(Value const &min, Value const &max)
Definition: Rectangle.hh:49
void SetXMax(Value x_max)
Definition: Rectangle.hh:85
constexpr Value_ const & X() const
Getter for X.
Definition: Vector2.hh:150
Vector2< Value > Min() const
Definition: Rectangle.hh:142
Rectangle(Vector2< Value > min, Value width, Value height)
Definition: Rectangle.hh:39
Value max(Value const &value_1, Value const &value_2)
Maximal value.
Definition: Units.hh:260
void SetX(std::pair< Value, Value > const &x)
Definition: Rectangle.hh:65
Value XMin() const
Definition: Rectangle.hh:126
void WidenX(Range< Value > const &x)
Definition: Rectangle.hh:102
Vector2< Value > Max() const
Definition: Rectangle.hh:146
void ResetX()
Definition: Rectangle.hh:77
Two dimensional Vector.
Definition: PseudoJet.hh:23
Rectangle(Vector2< Value > min, Vector2< Value > max)
Definition: Rectangle.hh:34
void SetMax(Value_ max)
Definition: Range.hh:53
void WidenMin(Value_ min)
Definition: Range.hh:80
Value YMin() const
Definition: Rectangle.hh:134
void WidenMax(Value_ max)
Definition: Range.hh:86
void SetX(Range< Value > const &x)
Definition: Rectangle.hh:57
void Set(Value_ min, Value_ max)
Definition: Range.hh:58
Value XMax() const
Definition: Rectangle.hh:130