LIBRCSC Docs
Documentation for HELIOS's BASE LIBRCSC library for RoboCup 2D Simulation League.
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
rect_2d.h
Go to the documentation of this file.
1// -*-c++-*-
2
8/*
9 *Copyright:
10
11 Copyright (C) Hidehisa Akiyama
12
13 This code is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27 *EndCopyright:
28 */
29
31
32#ifndef RCSC_GEOM_RECT2D_H
33#define RCSC_GEOM_RECT2D_H
34
35#include <rcsc/geom/region_2d.h>
36#include <rcsc/geom/size_2d.h>
37#include <rcsc/geom/line_2d.h>
38#include <rcsc/geom/vector_2d.h>
39
40namespace rcsc {
41
42class Ray2D;
43class Segment2D;
44
58class Rect2D
59 : public Region2D {
60private:
62 Vector2D M_top_left;
63
65 Size2D M_size;
66
67public:
72 : M_top_left( 0.0, 0.0 )
73 , M_size( 0.0, 0.0 )
74 { }
75private:
83 Rect2D( const double left_x,
84 const double top_y,
85 const double length,
86 const double width )
87 : M_top_left( left_x, top_y )
88 , M_size( length, width )
89 { }
90
97 Rect2D( const Vector2D & top_left,
98 const double length,
99 const double width )
100 : M_top_left( top_left )
101 , M_size( length, width )
102 { }
103
104public:
110 Rect2D( const Vector2D & top_left,
111 const Size2D & size )
112 : M_top_left( top_left )
113 , M_size( size )
114 { }
115
124 Rect2D( const Vector2D & top_left,
125 const Vector2D & bottom_right )
126 : M_top_left( top_left )
127 , M_size( bottom_right.x - top_left.x,
128 bottom_right.y - top_left.y )
129 {
130 if ( bottom_right.x - top_left.x < 0.0 )
131 {
132 M_top_left.x = bottom_right.x;
133 }
134 if ( bottom_right.y - top_left.y < 0.0 )
135 {
136 M_top_left.y = bottom_right.y;
137 }
138 }
139
146 static
148 const double length,
149 const double width )
150 {
151 return Rect2D( center.x - length*0.5,
152 center.y - width*0.5,
153 length,
154 width );
155 }
156
164 static
165 Rect2D from_center( const double center_x,
166 const double center_y,
167 const double length,
168 const double width )
169 {
170 return Rect2D( center_x - length*0.5,
171 center_y - width*0.5,
172 length,
173 width );
174 }
175
181 static
182 Rect2D from_corners( const Vector2D & top_left,
183 const Vector2D & bottom_right )
184 {
185 return Rect2D( top_left, bottom_right );
186 }
187
195 static
196 Rect2D from_corners( const double l,
197 const double t,
198 const double r,
199 const double b )
200 {
201 return Rect2D( Vector2D( l, t ), Vector2D( r, b ) );
202 }
203
204private:
212 const
213 Rect2D & assign( const double left_x,
214 const double top_y,
215 const double length,
216 const double width )
217 {
218 M_top_left.assign( left_x, top_y );
219 M_size.assign( length, width );
220 return *this;
221 }
222
230 const
231 Rect2D & assign( const Vector2D & top_left,
232 const double length,
233 const double width )
234 {
235 M_top_left = top_left;
236 M_size.assign( length, width );
237 return *this;
238 }
239
240public:
247 const Rect2D & assign( const Vector2D & top_left,
248 const Size2D & size )
249 {
250 M_top_left = top_left;
251 M_size = size;
252 return *this;
253 }
254
262 const Rect2D & moveCenter( const Vector2D & point )
263 {
264 M_top_left.assign( point.x - M_size.length() * 0.5,
265 point.y - M_size.width() * 0.5 );
266 return *this;
267 }
268
276 const Rect2D & moveTopLeft( const Vector2D & point )
277 {
278 M_top_left = point;
279 return *this;
280 }
281
289 const Rect2D & moveBottomRight( const Vector2D & point )
290 {
291 M_top_left.assign( point.x - M_size.length(),
292 point.y - M_size.width() );
293 return *this;
294 }
295
303 const Rect2D & moveLeft( const double x )
304 {
305 M_top_left.x = x;
306 return *this;
307 }
308
314 const Rect2D & moveMinX( const double x )
315 {
316 return moveLeft( x );
317 }
318
326 const Rect2D & moveRight( const double x )
327 {
328 M_top_left.x = x - M_size.length();
329 return *this;
330 }
331
337 const Rect2D & moveMaxX( const double x )
338 {
339 return moveRight( x );
340 }
341
349 const Rect2D & moveTop( const double y )
350 {
351 M_top_left.y = y;
352 return *this;
353 }
354
360 const Rect2D & moveMinY( const double y )
361 {
362 return moveTop( y );
363 }
364
372 const Rect2D & moveBottom( const double y )
373 {
374 M_top_left.y = y - M_size.width();
375 return *this;
376 }
377
383 const Rect2D & moveMaxY( const double y )
384 {
385 return moveBottom( y );
386 }
387
388
396 const Rect2D & setTopLeft( const double x,
397 const double y );
398
405 const Rect2D & setTopLeft( const Vector2D & point )
406 {
407 return setTopLeft( point.x, point.y );
408 }
409
417 const Rect2D & setBottomRight( const double x,
418 const double y );
419
426 const Rect2D & setBottomRight( const Vector2D & point )
427 {
428 return setBottomRight( point.x, point.y );
429 }
430
437 const Rect2D & setLeft( const double x );
438
444 const Rect2D & setMinX( const double x )
445 {
446 return setLeft( x );
447 }
448
455 const Rect2D & setRight( const double x );
456
462 const Rect2D & setMaxX( const double x )
463 {
464 return setRight( x );
465 }
466
473 const Rect2D & setTop( const double y );
474
480 const Rect2D & setMinY( const double y )
481 {
482 return setTop( y );
483 }
484
491 const Rect2D & setBottom( const double y );
492
498 const Rect2D & setMaxY( const double y )
499 {
500 return setBottom( y );
501 }
502
508 const Rect2D & setLength( const double length )
509 {
510 M_size.setLength( length );
511 return *this;
512 }
513
519 const Rect2D & setWidth( const double width )
520 {
521 M_size.setWidth( width );
522 return *this;
523 }
524
531 const Rect2D & setSize( const double length,
532 const double width )
533 {
534 M_size.assign( length, width );
535 return *this;
536 }
537
543 const Rect2D & setSize( const Size2D & size )
544 {
545 M_size = size;
546 return *this;
547 }
548
553 bool isValid() const
554 {
555 return M_size.length() > 0.0
556 && M_size.width() > 0.0;
557 }
558
563 virtual
564 double area() const
565 {
566 return M_size.length() * M_size.width();
567 }
568
574 virtual
575 bool contains( const Vector2D & point ) const
576 {
577 return ( left() <= point.x
578 && point.x <= right()
579 && top() <= point.y
580 && point.y <= bottom() );
581 }
582
589 bool contains( const Vector2D & point,
590 const double error_thr ) const
591 {
592 return ( left() - error_thr <= point.x
593 && point.x <= right() + error_thr
594 && top() - error_thr <= point.y
595 && point.y <= bottom() + error_thr );
596 }
597
602 double left() const
603 {
604 return M_top_left.x;
605 }
606
611 double right() const
612 {
613 return left() + size().length();
614 }
615
620 double top() const
621 {
622 return M_top_left.y;
623 }
624
629 double bottom() const
630 {
631 return top() + size().width();
632 }
633
638 double minX() const
639 {
640 return left();
641 }
642
647 double maxX() const
648 {
649 return right();
650 }
651
656 double minY() const
657 {
658 return top();
659 }
660
665 double maxY() const
666 {
667 return bottom();
668 }
669
674 const Size2D & size() const
675 {
676 return M_size;
677 }
678
684 {
685 return Vector2D( ( left() + right() ) * 0.5,
686 ( top() + bottom() ) * 0.5 );
687 }
688
693 const Vector2D & topLeft() const
694 {
695 return M_top_left;
696 }
697
703 {
704 return Vector2D( right(), top() );
705 }
706
712 {
713 return Vector2D( left(), bottom() );
714 }
715
721 {
722 return Vector2D( right(), bottom() );
723 }
724
730 {
731 return Line2D( topLeft(), bottomLeft() );
732 }
733
739 {
740 return Line2D( topRight(), bottomRight() );
741 }
742
748 {
749 return Line2D( topLeft(), topRight() );
750 }
751
757 {
758 return Line2D( bottomLeft(), bottomRight() );
759 }
760
768 int intersection( const Line2D & line,
769 Vector2D * sol1,
770 Vector2D * sol2 ) const;
771
779 int intersection( const Ray2D & ray,
780 Vector2D * sol1,
781 Vector2D * sol2 ) const;
782
790 int intersection( const Segment2D & segment,
791 Vector2D * sol1,
792 Vector2D * sol2 ) const;
793
800 const Rect2D & operator&=( const Rect2D & other );
801
809 Rect2D intersected( const Rect2D & other ) const
810 {
811 return Rect2D( *this ) &= other;
812 }
813
819 const Rect2D & operator|=( const Rect2D & other );
820
827 Rect2D united( const Rect2D & other ) const
828 {
829 return Rect2D( *this ) |= other;
830 }
831};
832
839inline
840const
843 const rcsc::Rect2D & rhs )
844{
845 return rcsc::Rect2D( lhs ) &= rhs;
846}
847
853inline
854const
857 const rcsc::Rect2D & rhs )
858{
859 return rcsc::Rect2D( lhs ) |= rhs;
860}
861
862}
863
864#endif
2d straight line class
Definition: line_2d.h:47
2D ray line class
Definition: ray_2d.h:44
2D rectangle regin class.
Definition: rect_2d.h:59
static Rect2D from_corners(const Vector2D &top_left, const Vector2D &bottom_right)
create rectangle with 2 corner points. just call one of constructor.
Definition: rect_2d.h:182
const Rect2D & setTopLeft(const double x, const double y)
set the top-left corner of the rectangle. the size may be changed, but the bottom-right corner will n...
Rect2D(const Vector2D &top_left, const Vector2D &bottom_right)
constructor with 2 points.
Definition: rect_2d.h:124
const Rect2D & setSize(const double length, const double width)
set a new size
Definition: rect_2d.h:531
int intersection(const Ray2D &ray, Vector2D *sol1, Vector2D *sol2) const
calculate intersection point with ray.
const Rect2D & setMaxY(const double y)
alias of setBottom.
Definition: rect_2d.h:498
const Rect2D & setSize(const Size2D &size)
set a new size
Definition: rect_2d.h:543
virtual bool contains(const Vector2D &point) const
check if point is within this region.
Definition: rect_2d.h:575
const Rect2D & setMinY(const double y)
alias of setTop.
Definition: rect_2d.h:480
Vector2D bottomLeft() const
get the bottom-left corner point
Definition: rect_2d.h:711
const Rect2D & moveMinY(const double y)
alias of moveTop.
Definition: rect_2d.h:360
Line2D bottomEdge() const
get the bottom edge line
Definition: rect_2d.h:756
Rect2D intersected(const Rect2D &other) const
get the intersected rectangle of this rectangle and the other rectangle. This method is equivalent to...
Definition: rect_2d.h:809
int intersection(const Segment2D &segment, Vector2D *sol1, Vector2D *sol2) const
calculate intersection point with line segment.
const Rect2D & setRight(const double x)
set the right of rectangle. the size may be changed, but the left will never be changed.
Vector2D topRight() const
get the top-right corner point
Definition: rect_2d.h:702
double bottom() const
get the bottom y coordinate of this rectangle.
Definition: rect_2d.h:629
static Rect2D from_center(const Vector2D &center, const double length, const double width)
create rectangle with center point and size.
Definition: rect_2d.h:147
static Rect2D from_center(const double center_x, const double center_y, const double length, const double width)
create rectangle with center point and size.
Definition: rect_2d.h:165
const Rect2D & setBottom(const double y)
set the bottom of rectangle. the size may be changed, but the top will never be changed.
double left() const
get the left x coordinate of this rectangle.
Definition: rect_2d.h:602
double minX() const
get minimum value of x coordinate of this rectangle
Definition: rect_2d.h:638
double right() const
get the right x coordinate of this rectangle.
Definition: rect_2d.h:611
const Rect2D & moveTopLeft(const Vector2D &point)
move the rectangle. the top-left coner is set to the given position. the size is unchanged.
Definition: rect_2d.h:276
virtual double area() const
get the area value of this rectangle.
Definition: rect_2d.h:564
const Rect2D & setMinX(const double x)
alias of setLeft.
Definition: rect_2d.h:444
const Rect2D & moveMaxX(const double x)
alias of moveRight.
Definition: rect_2d.h:337
Line2D leftEdge() const
get the left edge line
Definition: rect_2d.h:729
Vector2D bottomRight() const
get the bottom-right corner point
Definition: rect_2d.h:720
const Rect2D & setBottomRight(const double x, const double y)
set the bottom-right corner of the rectangle. the size may be changed, but the top-left corner will n...
const Rect2D & setBottomRight(const Vector2D &point)
set the bottom-right corner of the rectangle. the size may be changed, but the top-left corner will n...
Definition: rect_2d.h:426
Rect2D united(const Rect2D &other) const
get the united rectangle of this rectangle and the other rectangle. This method is equivalent to oper...
Definition: rect_2d.h:827
const Rect2D & setLength(const double length)
set a new x-range
Definition: rect_2d.h:508
const Rect2D & moveMaxY(const double y)
alias of moveTop.
Definition: rect_2d.h:383
Line2D topEdge() const
get the top edge line
Definition: rect_2d.h:747
const Size2D & size() const
get the XY range of this rectangle
Definition: rect_2d.h:674
double maxY() const
get maximum value of y coordinate of this rectangle
Definition: rect_2d.h:665
Rect2D()
default constructor creates a zero area rectanble at (0,0)
Definition: rect_2d.h:71
const Rect2D & setTopLeft(const Vector2D &point)
set the top-left corner of the rectangle. the size may be changed, but the bottom-right corner will n...
Definition: rect_2d.h:405
const Rect2D & setTop(const double y)
set the top of rectangle. the size may be changed, but the bottom will never be changed.
const Rect2D & setLeft(const double x)
set the left of rectangle. the size may be changed, but the right will never be changed.
const Rect2D & moveRight(const double x)
move the rectangle. the right line is set to the given value. the size is unchanged.
Definition: rect_2d.h:326
const Rect2D & setMaxX(const double x)
alias of setRight.
Definition: rect_2d.h:462
Rect2D(const Vector2D &top_left, const Size2D &size)
constructor with variables
Definition: rect_2d.h:110
const Rect2D & assign(const Vector2D &top_left, const Size2D &size)
assign new values
Definition: rect_2d.h:247
const Rect2D & moveMinX(const double x)
alias of moveLeft.
Definition: rect_2d.h:314
double maxX() const
get maximum value of x coordinate of this rectangle
Definition: rect_2d.h:647
const Rect2D & moveBottom(const double y)
move the rectangle. the top line is set to the given value. the size is unchanged.
Definition: rect_2d.h:372
const Vector2D & topLeft() const
get the top-left corner point
Definition: rect_2d.h:693
Line2D rightEdge() const
get the right edge line
Definition: rect_2d.h:738
const Rect2D & moveLeft(const double x)
move the rectangle. the left line is set to the given position. the size is unchanged.
Definition: rect_2d.h:303
Vector2D center() const
get center point
Definition: rect_2d.h:683
static Rect2D from_corners(const double l, const double t, const double r, const double b)
create rectangle with 2 corner points. just call one of constructor.
Definition: rect_2d.h:196
const Rect2D & moveBottomRight(const Vector2D &point)
move the rectangle. the bottom-right coner is set to the given position. the size is unchanged.
Definition: rect_2d.h:289
const Rect2D & moveCenter(const Vector2D &point)
move the rectangle. the center point is set to the given position. the size is unchanged.
Definition: rect_2d.h:262
const Rect2D & operator&=(const Rect2D &other)
convert this rectangle to the intersection rectangle with other. If no intersection between rectangle...
bool contains(const Vector2D &point, const double error_thr) const
check if point is within this region with error threshold.
Definition: rect_2d.h:589
double minY() const
get minimum value of y coordinate of this rectangle
Definition: rect_2d.h:656
const Rect2D & setWidth(const double width)
set a new y-range
Definition: rect_2d.h:519
const Rect2D & moveTop(const double y)
move the rectangle. the top line is set to the given value. the size is unchanged.
Definition: rect_2d.h:349
bool isValid() const
check if this rectangle is valid or not.
Definition: rect_2d.h:553
double top() const
get the top y coordinate of this rectangle.
Definition: rect_2d.h:620
int intersection(const Line2D &line, Vector2D *sol1, Vector2D *sol2) const
calculate intersection point with line.
const Rect2D & operator|=(const Rect2D &other)
convert this rectangle to the united rectangle with other.
abstract 2D region class
Definition: region_2d.h:43
2d segment line class
Definition: segment_2d.h:46
2D size definition class
Definition: size_2d.h:44
const Size2D & assign(const double length, const double width)
assign new range directly.
Definition: size_2d.h:79
double length() const
get the value of X range
Definition: size_2d.h:113
const Size2D & setWidth(const double width)
set new Y range
Definition: size_2d.h:103
double width() const
get the value of Y range
Definition: size_2d.h:122
const Size2D & setLength(const double length)
set new X range
Definition: size_2d.h:92
2D point vector class
Definition: vector_2d.h:46
Vector2D & assign(const double xx, const double yy)
assign XY value directly.
Definition: vector_2d.h:100
double y
Y coordinate.
Definition: vector_2d.h:64
double x
X coordinate.
Definition: vector_2d.h:63
2D straight line Header File.
const rcsc::Rect2D operator&(const rcsc::Rect2D &lhs, const rcsc::Rect2D &rhs)
get the intersected rectangle of this rectangle and the other rectangle. If no intersection between r...
Definition: rect_2d.h:842
const rcsc::Rect2D operator|(const rcsc::Rect2D &lhs, const rcsc::Rect2D &rhs)
get the united rectangle of this rectangle and the other rectangle.
Definition: rect_2d.h:856
abstract 2D region class Header File.
2d size class Header File.
2d vector class Header File.