LARS
LARS (Light Augmented Reality System) is an open-source framework for light-based interaction and real-time tracking in multi-robot experiments. Inspired by ARK, LARS extends the augmented reality paradigm to robotic collectives by projecting dynamic visual cues and environments onto the arena, enabling new experimental possibilities for collective robotics research, education, and outreach. LARS features integrated tracking, light projection, and modular experiment control with a user-friendly Qt GUI.
Loading...
Searching...
No Matches
rect_2d.h
Go to the documentation of this file.
1// -*-c++-*-
2
7
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 <geom/region_2d.h>
36#include <geom/size_2d.h>
37#include <geom/line_2d.h>
38#include <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
248 Rect2D & assign( const Vector2D & top_left,
249 const Size2D & size )
250 {
251 M_top_left = top_left;
252 M_size = size;
253 return *this;
254 }
255
263 const
264 Rect2D & moveCenter( const Vector2D & point )
265 {
266 M_top_left.assign( point.x - M_size.length() * 0.5,
267 point.y - M_size.width() * 0.5 );
268 return *this;
269 }
270
278 const
279 Rect2D & moveTopLeft( const Vector2D & point )
280 {
281 M_top_left = point;
282 return *this;
283 }
284
292 const
293 Rect2D & moveBottomRight( const Vector2D & point )
294 {
295 M_top_left.assign( point.x - M_size.length(),
296 point.y - M_size.width() );
297 return *this;
298 }
299
307 const
308 Rect2D & moveLeft( const double & x )
309 {
310 M_top_left.x = x;
311 return *this;
312 }
313
319 const
320 Rect2D & moveMinX( const double & x )
321 {
322 return moveLeft( x );
323 }
324
332 const
333 Rect2D & moveRight( const double & x )
334 {
335 M_top_left.x = x - M_size.length();
336 return *this;
337 }
338
344 const
345 Rect2D & moveMaxX( const double & x )
346 {
347 return moveRight( x );
348 }
349
357 const
358 Rect2D & moveTop( const double & y )
359 {
360 M_top_left.y = y;
361 return *this;
362 }
363
369 const
370 Rect2D & moveMinY( const double & y )
371 {
372 return moveTop( y );
373 }
374
382 const
383 Rect2D & moveBottom( const double & y )
384 {
385 M_top_left.y = y - M_size.width();
386 return *this;
387 }
388
394 const
395 Rect2D & moveMaxY( const double & y )
396 {
397 return moveBottom( y );
398 }
399
400
408 const
409 Rect2D & setTopLeft( const double & x,
410 const double & y );
411
418 const
419 Rect2D & setTopLeft( const Vector2D & point )
420 {
421 return setTopLeft( point.x, point.y );
422 }
423
431 const
432 Rect2D & setBottomRight( const double & x,
433 const double & y );
434
441 const
442 Rect2D & setBottomRight( const Vector2D & point )
443 {
444 return setBottomRight( point.x, point.y );
445 }
446
453 const
454 Rect2D & setLeft( const double & x );
455
461 const
462 Rect2D & setMinX( const double & x )
463 {
464 return setLeft( x );
465 }
466
473 const
474 Rect2D & setRight( const double & x );
475
481 const
482 Rect2D & setMaxX( const double & x )
483 {
484 return setRight( x );
485 }
486
493 const
494 Rect2D & setTop( const double & y );
495
501 const
502 Rect2D & setMinY( const double & y )
503 {
504 return setTop( y );
505 }
506
513 const
514 Rect2D & setBottom( const double & y );
515
521 const
522 Rect2D & setMaxY( const double & y )
523 {
524 return setBottom( y );
525 }
526
532 const
533 Rect2D & setLength( const double & length )
534 {
535 M_size.setLength( length );
536 return *this;
537 }
538
544 const
545 Rect2D & setWidth( const double & width )
546 {
547 M_size.setWidth( width );
548 return *this;
549 }
550
557 const
558 Rect2D & setSize( const double & length,
559 const double & width )
560 {
561 M_size.assign( length, width );
562 return *this;
563 }
564
570 const
572 {
573 M_size = size;
574 return *this;
575 }
576
581 bool isValid() const
582 {
583 return M_size.length() > 0.0
584 && M_size.width() > 0.0;
585 }
586
591 virtual
592 double area() const
593 {
594 return M_size.length() * M_size.width();
595 }
596
602 virtual
603 bool contains( const Vector2D & point ) const
604 {
605 return ( left() <= point.x
606 && point.x <= right()
607 && top() <= point.y
608 && point.y <= bottom() );
609 }
610
617 bool contains( const Vector2D & point,
618 const double & error_thr ) const
619 {
620 return ( left() - error_thr <= point.x
621 && point.x <= right() + error_thr
622 && top() - error_thr <= point.y
623 && point.y <= bottom() + error_thr );
624 }
625
630 const
631 double & left() const
632 {
633 return M_top_left.x;
634 }
635
640 double right() const
641 {
642 return left() + size().length();
643 }
644
649 const
650 double & top() const
651 {
652 return M_top_left.y;
653 }
654
659 double bottom() const
660 {
661 return top() + size().width();
662 }
663
668 double minX() const
669 {
670 return left();
671 }
672
677 double maxX() const
678 {
679 return right();
680 }
681
686 double minY() const
687 {
688 return top();
689 }
690
695 double maxY() const
696 {
697 return bottom();
698 }
699
704 const
705 Size2D & size() const
706 {
707 return M_size;
708 }
709
715 {
716 return Vector2D( ( left() + right() ) * 0.5,
717 ( top() + bottom() ) * 0.5 );
718 }
719
724 const
726 {
727 return M_top_left;
728 }
729
735 {
736 return Vector2D( right(), top() );
737 }
738
744 {
745 return Vector2D( left(), bottom() );
746 }
747
753 {
754 return Vector2D( right(), bottom() );
755 }
756
762 {
763 return Line2D( topLeft(), bottomLeft() );
764 }
765
771 {
772 return Line2D( topRight(), bottomRight() );
773 }
774
780 {
781 return Line2D( topLeft(), topRight() );
782 }
783
789 {
790 return Line2D( bottomLeft(), bottomRight() );
791 }
792
800 int intersection( const Line2D & line,
801 Vector2D * sol1,
802 Vector2D * sol2 ) const;
803
811 int intersection( const Ray2D & ray,
812 Vector2D * sol1,
813 Vector2D * sol2 ) const;
814
822 int intersection( const Segment2D & segment,
823 Vector2D * sol1,
824 Vector2D * sol2 ) const;
825
832 const
833 Rect2D & operator&=( const Rect2D & other );
834
842 Rect2D intersected( const Rect2D & other ) const
843 {
844 return Rect2D( *this ) &= other;
845 }
846
852 const
853 Rect2D & operator|=( const Rect2D & other );
854
861 Rect2D united( const Rect2D & other ) const
862 {
863 return Rect2D( *this ) |= other;
864 }
865};
866
873inline
874const
877 const rcsc::Rect2D & rhs )
878{
879 return rcsc::Rect2D( lhs ) &= rhs;
880}
881
887inline
888const
891 const rcsc::Rect2D & rhs )
892{
893 return rcsc::Rect2D( lhs ) |= rhs;
894}
895
896}
897
898#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
Rect2D(const Vector2D &top_left, const Vector2D &bottom_right)
constructor with 2 points.
Definition rect_2d.h:124
const Rect2D & setSize(const Size2D &size)
set a new size
Definition rect_2d.h:571
virtual bool contains(const Vector2D &point) const
check if point is within this region.
Definition rect_2d.h:603
Vector2D bottomLeft() const
get the bottom-left corner point
Definition rect_2d.h:743
const double & left() const
get the left x coordinate of this rectangle.
Definition rect_2d.h:631
const Rect2D & setWidth(const double &width)
set a new y-range
Definition rect_2d.h:545
Line2D bottomEdge() const
get the bottom edge line
Definition rect_2d.h:788
const double & top() const
get the top y coordinate of this rectangle.
Definition rect_2d.h:650
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:842
const Rect2D & setMinX(const double &x)
alias of setLeft.
Definition rect_2d.h:462
Vector2D topRight() const
get the top-right corner point
Definition rect_2d.h:734
double bottom() const
get the bottom y coordinate of this rectangle.
Definition rect_2d.h:659
double minX() const
get minimum value of x coordinate of this rectangle
Definition rect_2d.h:668
double right() const
get the right x coordinate of this rectangle.
Definition rect_2d.h:640
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:279
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:308
const Rect2D & setLength(const double &length)
set a new x-range
Definition rect_2d.h:533
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 & 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...
Definition rect_2d.cpp:71
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
virtual double area() const
get the area value of this rectangle.
Definition rect_2d.h:592
Line2D leftEdge() const
get the left edge line
Definition rect_2d.h:761
Vector2D bottomRight() const
get the bottom-right corner point
Definition rect_2d.h:752
const Rect2D & setRight(const double &x)
set the right of rectangle. the size may be changed, but the left will never be changed.
Definition rect_2d.cpp:107
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:442
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:861
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:358
const Rect2D & setMaxX(const double &x)
alias of setRight.
Definition rect_2d.h:482
Line2D topEdge() const
get the top edge line
Definition rect_2d.h:779
const Size2D & size() const
get the XY range of this rectangle
Definition rect_2d.h:705
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...
Definition rect_2d.cpp:51
double maxY() const
get maximum value of y coordinate of this rectangle
Definition rect_2d.h:695
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:419
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 & setBottom(const double &y)
set the bottom of rectangle. the size may be changed, but the top will never be changed.
Definition rect_2d.cpp:139
Rect2D(const Vector2D &top_left, const Size2D &size)
constructor with variables
Definition rect_2d.h:110
const Rect2D & setLeft(const double &x)
set the left of rectangle. the size may be changed, but the right will never be changed.
Definition rect_2d.cpp:91
const Rect2D & assign(const Vector2D &top_left, const Size2D &size)
assign new values
Definition rect_2d.h:248
double maxX() const
get maximum value of x coordinate of this rectangle
Definition rect_2d.h:677
bool contains(const Vector2D &point, const double &error_thr) const
check if point is within this region with error threshold.
Definition rect_2d.h:617
const Vector2D & topLeft() const
get the top-left corner point
Definition rect_2d.h:725
Line2D rightEdge() const
get the right edge line
Definition rect_2d.h:770
Vector2D center() const
get center point
Definition rect_2d.h:714
const Rect2D & moveMaxX(const double &x)
alias of moveRight.
Definition rect_2d.h:345
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:293
const Rect2D & moveMinX(const double &x)
alias of moveLeft.
Definition rect_2d.h:320
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:264
const Rect2D & operator&=(const Rect2D &other)
convert this rectangle to the intersection rectangle with other. If no intersection between rectangle...
Definition rect_2d.cpp:302
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:383
double minY() const
get minimum value of y coordinate of this rectangle
Definition rect_2d.h:686
bool isValid() const
check if this rectangle is valid or not.
Definition rect_2d.h:581
const Rect2D & setMinY(const double &y)
alias of setTop.
Definition rect_2d.h:502
const Rect2D & setSize(const double &length, const double &width)
set a new size
Definition rect_2d.h:558
const Rect2D & moveMaxY(const double &y)
alias of moveTop.
Definition rect_2d.h:395
const Rect2D & setTop(const double &y)
set the top of rectangle. the size may be changed, but the bottom will never be changed.
Definition rect_2d.cpp:123
const Rect2D & moveMinY(const double &y)
alias of moveTop.
Definition rect_2d.h:370
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:333
const Rect2D & setMaxY(const double &y)
alias of setBottom.
Definition rect_2d.h:522
int intersection(const Line2D &line, Vector2D *sol1, Vector2D *sol2) const
calculate intersection point with line.
Definition rect_2d.cpp:154
const Rect2D & operator|=(const Rect2D &other)
convert this rectangle to the united rectangle with other.
Definition rect_2d.cpp:335
Region2D()
accessible only from derived classes
Definition region_2d.h:49
2d segment line class
Definition segment_2d.h:46
2D size definition class
Definition size_2d.h:44
const double & length() const
get the value of X range
Definition size_2d.h:117
const Size2D & assign(const double &length, const double &width)
assign new range directly.
Definition size_2d.h:80
const double & width() const
get the value of Y range
Definition size_2d.h:127
2D point vector class
Definition vector_2d.h:47
double y
Y coordinate.
Definition vector_2d.h:65
double x
X coordinate.
Definition vector_2d.h:64
Vector2D & assign(const double &xx, const double &yy)
assign XY value directly.
Definition vector_2d.h:101
2d vector class Header File.
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:876
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:890
abstract 2D region class Header File.
2d size class Header File.