2 #ifndef vgl_box_2d_hxx_ 3 #define vgl_box_2d_hxx_ 13 # include <vcl_msvc_warnings.h> 22 min_pos_[0]=min_pos_[1]=(Type)1;
23 max_pos_[0]=max_pos_[1]=(Type)0;
28 Type
const corner2[2])
30 min_pos_[0]=max_pos_[0]=corner1[0];
31 min_pos_[1]=max_pos_[1]=corner1[1];
39 min_pos_[0]=max_pos_[0]=corner1.
x();
40 min_pos_[1]=max_pos_[1]=corner1.
y();
47 min_pos_[0]=max_pos_[0]=xmin;
48 min_pos_[1]=max_pos_[1]=ymin;
50 if (xmin > xmax || ymin > ymax) this->empty();
60 min_pos_[0]=Type(ref_point[0]-0.5*w);
61 min_pos_[1]=Type(ref_point[1]-0.5*h);
62 max_pos_[0]=Type(ref_point[0]+0.5*w);
63 max_pos_[1]=Type(ref_point[1]+0.5*h);
67 min_pos_[0]=ref_point[0];
68 min_pos_[1]=ref_point[1];
69 max_pos_[0]=ref_point[0]+w;
70 max_pos_[1]=ref_point[1]+h;
74 min_pos_[0]=ref_point[0]-w;
75 min_pos_[1]=ref_point[1]-h;
76 max_pos_[0]=ref_point[0];
77 max_pos_[1]=ref_point[1];
80 assert(!
"point_type should be one of: centre, min_pos, max_pos");
90 min_pos_[0]=Type(ref_point.
x()-0.5*w);
91 min_pos_[1]=Type(ref_point.
y()-0.5*h);
92 max_pos_[0]=Type(ref_point.
x()+0.5*w);
93 max_pos_[1]=Type(ref_point.
y()+0.5*h);
97 min_pos_[0]=ref_point.
x();
98 min_pos_[1]=ref_point.
y();
99 max_pos_[0]=ref_point.
x()+w;
100 max_pos_[1]=ref_point.
y()+h;
104 min_pos_[0]=ref_point.
x()-w;
105 min_pos_[1]=ref_point.
y()-h;
106 max_pos_[0]=ref_point.
x();
107 max_pos_[1]=ref_point.
y();
110 assert(!
"point_type should be one of: centre, min_pos, max_pos");
113 template <
class Type>
117 return Type(0.5*(min_pos_[0] + max_pos_[0]));
120 template <
class Type>
124 return Type(0.5*(min_pos_[1] + max_pos_[1]));
127 template <
class Type>
130 return (max_pos_[0] > min_pos_[0]) ? max_pos_[0] - min_pos_[0] : 0;
133 template <
class Type>
136 return (max_pos_[1] > min_pos_[1]) ? max_pos_[1] - min_pos_[1] : 0;
139 template <
class Type>
146 template <
class Type>
153 template <
class Type>
160 template <
class Type>
164 Type delta = cent_x - centroid_x();
165 min_pos_[0]= min_pos_[0] + delta;
166 max_pos_[0]= max_pos_[0] + delta;
169 template <
class Type>
173 Type delta = cent_y - centroid_y();
174 min_pos_[1]= min_pos_[1] + delta;
175 max_pos_[1]= max_pos_[1] + delta;
179 inline void set_dim_2d(T & minv, T& maxv, T spread);
185 int sum = minv + maxv;
186 sum = sum | (spread&1);
187 minv = int(std::floor((sum-spread)/2.0));
195 minv = T( (x-spread)*0.5 );
196 maxv = minv + spread;
202 template <
class Type>
212 template <
class Type>
222 template <
class Type>
226 set_dim_2d(min_pos_[0], max_pos_[0], width() + expand );
227 set_dim_2d(min_pos_[1], max_pos_[1], height() + expand );
231 template <
class Type>
235 set_dim_2d(min_pos_[0], max_pos_[0], static_cast<Type>(width()*s));
236 set_dim_2d(min_pos_[1], max_pos_[1], static_cast<Type>(height()*s));
241 template <
class Type>
244 min_pos_[0] = static_cast<Type>(min_pos_[0] * s);
245 min_pos_[1] = static_cast<Type>(min_pos_[1] * s);
246 max_pos_[0] = static_cast<Type>(max_pos_[0] * s);
247 max_pos_[1] = static_cast<Type>(max_pos_[1] * s);
250 template <
class Type>
253 min_pos_[0]=min_position[0];
254 min_pos_[1]=min_position[1];
255 if (max_pos_[0] < min_pos_[0]) {
256 max_pos_[0]=min_pos_[0];
258 if (max_pos_[1] < min_pos_[1]) {
259 max_pos_[1]=min_pos_[1];
263 template <
class Type>
266 max_pos_[0]=max_position[0];
267 max_pos_[1]=max_position[1];
268 if (max_pos_[0] < min_pos_[0])
269 min_pos_[0]=max_pos_[0];
270 if (max_pos_[1] < min_pos_[1])
271 min_pos_[1]=max_pos_[1];
274 template <
class Type>
277 min_pos_[0]=min_pt.
x();
if (max_pos_[0]<min_pos_[0]) max_pos_[0]=min_pos_[0];
278 min_pos_[1]=min_pt.
y();
if (max_pos_[1]<min_pos_[1]) max_pos_[1]=min_pos_[1];
281 template <
class Type>
284 max_pos_[0]=max_pt.
x();
if (max_pos_[0]<min_pos_[0]) min_pos_[0]=max_pos_[0];
285 max_pos_[1]=max_pt.
y();
if (max_pos_[1]<min_pos_[1]) min_pos_[1]=max_pos_[1];
288 template <
class Type>
292 return s <<
"<vgl_box_2d (empty)>";
294 return s <<
"<vgl_box_2d " 295 << min_pos_[0] <<
',' << min_pos_[1] <<
" to " 296 << max_pos_[0] <<
',' << max_pos_[1] <<
'>';
299 template <
class Type>
302 return s << min_pos_[0] <<
' ' << min_pos_[1] <<
' ' 303 << max_pos_[0] <<
' ' << max_pos_[1] <<
'\n';
306 template <
class Type>
309 return s >> min_pos_[0] >> min_pos_[1]
310 >> max_pos_[0] >> max_pos_[1];
316 template <
class Type>
321 min_pos_[0] = max_pos_[0] = p.
x();
322 min_pos_[1] = max_pos_[1] = p.
y();
326 if (p.
x() > max_pos_[0]) max_pos_[0] = p.
x();
327 if (p.
x() < min_pos_[0]) min_pos_[0] = p.
x();
328 if (p.
y() > max_pos_[1]) max_pos_[1] = p.
y();
329 if (p.
y() < min_pos_[1]) min_pos_[1] = p.
y();
337 template <
class Type>
346 template <
class Type>
349 return contains(p.
x(), p.
y());
353 template <
class Type>
362 template <
class Type>
365 min_pos_[0]=min_pos_[1]=(Type)1;
366 max_pos_[0]=max_pos_[1]=(Type)0;
370 template <
class Type>
377 template <
class Type>
383 #undef VGL_BOX_2D_INSTANTIATE 384 #define VGL_BOX_2D_INSTANTIATE(Type) \ 385 template class vgl_box_2d<Type >;\ 386 template std::istream& operator>>(std::istream&, vgl_box_2d<Type >&);\ 387 template std::ostream& operator<<(std::ostream&, vgl_box_2d<Type > const&) 389 #endif // vgl_box_2d_hxx_ void setmax_position(Type const max_position[2])
Modify top right. Bottom left only changed if necessary to avoid empty box.
void scale_about_origin(double s)
Scale width and height, keeping scaled position of origin unchanged.
a point in 2D nonhomogeneous space
Type min_x() const
Get min x.
void expand_about_centroid(Type expand)
Add to width and height, centroid unchanged.
void set_width(Type width)
Modify width, retaining centroid at current position.
vgl_point_2d< Type > max_point() const
Return upper right corner of box.
void setmin_position(Type const min_position[2])
Modify bottom left. Top right only changed if necessary to avoid empty box.
Type centroid_y() const
Get y component of centroid.
void set_max_point(vgl_point_2d< Type > const &max_pt)
Modify top right. Bottom left only changed if necessary to avoid empty box.
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
bool contains(vgl_point_2d< Type > const &p) const
Return true iff the point p is inside this box.
std::ostream & print(std::ostream &) const
Write "<vgl_box_2d x0,y0 to x1,y1>" to stream.
Type width() const
Get width of this box (= x dimension).
void set_centroid_y(Type cy)
Move box so centroid lies at cy (width and height unchanged).
void set_dim_2d(T &minv, T &maxv, T spread)
std::istream & read(std::istream &)
Read x0,y0,x1,y1 from stream.
void set_centroid_x(Type cx)
Move box so centroid lies at cx (width and height unchanged).
Represents a cartesian 2D box.
void empty()
Make the box empty.
std::ostream & write(std::ostream &) const
Write "x0 y0 x1 y1(endl)" to stream.
Contains class to represent a cartesian 2D bounding box.
vgl_point_2d< Type > centroid() const
Get the centroid point.
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
Type min_y() const
Get min y.
Type height() const
Get height of this box (= y dimension).
vgl_box_2d()
Default constructor (creates empty box).
void add(vgl_point_2d< Type > const &p)
Add a point to this box.
void set_min_point(vgl_point_2d< Type > const &min_pt)
Modify bottom left. Top right only changed if necessary to avoid empty box.
void scale_about_centroid(double s)
Scale width and height, centroid unchanged.
Represents a cartesian 2D point.
vgl_point_2d< Type > min_point() const
Return lower left corner of box.
void set_height(Type height)
Modify height, retaining centroid at current position.
Type max_y() const
Get max y.
Type max_x() const
Get max x.
bool is_empty() const
Return true if this box is empty.
Type centroid_x() const
Get x component of centroid.