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
vector_2d.h
Go to the documentation of this file.
1// -*-c++-*-
2
7
8/*
9 *Copyright:
10
11 Copyright (C) Hidehisa AKIYAMA, Hiroki Shimora
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_VECTOR2D_H
33#define RCSC_GEOM_VECTOR2D_H
34
35#include <geom/angle_deg.h>
36
37#include <functional>
38#include <iostream>
39#include <cmath>
40
41namespace rcsc {
42
47class Vector2D {
48 // : public boost::addable< Vector2D >
49 // , public boost::subtractable< Vector2D >
50 // , public multipliable2< Vector2D, double >
51 // , public dividable2< Vector2D, double >
52
53public:
54
56 static const double EPSILON;
57
59 static const double ERROR_VALUE;
60
62 static const Vector2D INVALIDATED;
63
64 double x;
65 double y;
66
71 : x( 0.0 ),
72 y( 0.0 )
73 { }
74
80 Vector2D( const double & xx,
81 const double & yy )
82 : x( xx ),
83 y( yy )
84 { }
85
90 bool isValid() const
91 {
92 return ( ( x != ERROR_VALUE ) && ( y != ERROR_VALUE ) );
93 }
94
101 Vector2D & assign( const double & xx,
102 const double & yy )
103 {
104 x = xx;
105 y = yy;
106 return *this;
107 }
108
115 Vector2D & setPolar( const double & radius,
116 const AngleDeg & dir )
117 {
118 x = radius * dir.cos();
119 y = radius * dir.sin();
120 return *this;
121 }
122
127 const
129 {
130 x = ERROR_VALUE;
131 y = ERROR_VALUE;
132 return *this;
133 }
134
139 double r2() const
140 {
141 return x * x + y * y;
142 }
143
148 double r() const
149 {
150 //return std::hypot( x, y );
151 return std::sqrt( r2() );
152 }
153
158 double norm() const
159 {
160 return r();
161 }
162
167 double norm2() const
168 {
169 return r2();
170 }
171
176 double length() const
177 {
178 return r();
179 }
180
185 double length2() const
186 {
187 return r2();
188 }
189
194 AngleDeg th() const
195 {
196 return AngleDeg( AngleDeg::atan2_deg( y, x ) );
197 }
198
203 AngleDeg dir() const
204 {
205 return th();
206 }
207
212 Vector2D abs() const
213 {
214 return Vector2D( std::fabs( x ), std::fabs( y ) );
215 }
216
221 double absX() const
222 {
223 return std::fabs( x );
224 }
225
230 double absY() const
231 {
232 return std::fabs( y );
233 }
234
240 Vector2D & add( const Vector2D & v )
241 {
242 x += v.x;
243 y += v.y;
244 return *this;
245 }
246
253 Vector2D & add( const double & xx,
254 const double & yy )
255 {
256 x += xx;
257 y += yy;
258 return *this;
259 }
260
266 Vector2D & scale( const double & scalar )
267 {
268 x *= scalar;
269 y *= scalar;
270 return *this;
271 }
272
277 const
279 {
280 return *this;
281 }
282
288 {
289 return Vector2D( -x, -y );
290 }
291
297 const
299 {
300 x += v.x;
301 y += v.y;
302 return *this;
303 }
304
310 const
312 {
313 x -= v.x;
314 y -= v.y;
315 return *this;
316 }
317
323 const
324 Vector2D & operator*=( const double & scalar )
325 {
326 //x *= scalar;
327 //y *= scalar;
328 //return *this;
329 return scale( scalar );
330 }
331
337 const
338 Vector2D & operator/=( const double & scalar )
339 {
340 //if ( scalar != 0 )
341 if ( std::fabs( scalar ) > EPSILON )
342 {
343 x /= scalar;
344 y /= scalar;
345 }
346 return *this;
347 }
348
354 double dist2( const Vector2D & p ) const
355 {
356 //return ( Vector2D( *this ) -= p ).r2();
357 return ( std::pow( this->x - p.x, 2 )
358 + std::pow( this->y - p.y, 2 ) );
359 }
360
366 double dist( const Vector2D & p ) const
367 {
368 //return std::hypot( this->x - p.x,
369 // this->y - p.y );
370 return std::sqrt( dist2( p ) );
371 }
372
378 {
379 x = -x;
380 y = -y;
381 return *this;
382 }
383
389 {
390 return Vector2D( *this ).reverse();
391 }
392
398 Vector2D & setLength( const double & len )
399 {
400 double mag = this->r();
401 //if ( mag == 0 )
402 if ( mag < EPSILON )
403 {
404 return *this;
405 }
406 //return ( (*this) *= ( len / mag ) );
407 return scale( len / mag );
408 }
409
415 Vector2D setLengthVector( const double & len ) const
416 {
417 return Vector2D( *this ).setLength( len );
418 }
419
425 {
426 return setLength( 1.0 );
427 }
428
435 {
436 return Vector2D( *this ).normalize();
437 }
438
444 Vector2D & rotate( const double & deg )
445 {
446 double c = std::cos( deg * AngleDeg::DEG2RAD );
447 double s = std::sin( deg * AngleDeg::DEG2RAD );
448 return assign( this->x * c - this->y * s,
449 this->x * s + this->y * c );
450 }
451
457 Vector2D & rotate( const AngleDeg & angle )
458 {
459 return rotate( angle.degree() );
460 }
461
467 Vector2D rotatedVector( const double & deg ) const
468 {
469 return Vector2D( *this ).rotate( deg );
470 }
471
477 Vector2D rotatedVector( const AngleDeg & angle ) const
478 {
479 return Vector2D( *this ).rotate( angle.degree() );
480 }
481
488 {
489 double radius = this->r();
490 x = radius * dir.cos();
491 y = radius * dir.sin();
492 return *this;
493 }
494
500 double innerProduct( const Vector2D & v ) const
501 {
502 return this->x * v.x + this->y * v.y;
503 // == |this| * |v| * (*this - v).th().cos()
504 }
505
511 double outerProduct( const Vector2D & v ) const
512 {
513 /*---------------------*
514 * assume virtual 3D environment.
515 * calculate Z-coordinate of outer product in right hand orientation.
516 * For the time being, Input Vector's Z-coordinate is set to ZERO.
517 *---------------------*/
518 // Normal 3D outer product
519 // xn = this->y * v.z - this->z * v.y;
520 // yn = this->z * v.x - this->x * v.z;
521 // # zn = this->x * v.y - this->y * v.x;
522 return this->x * v.y - this->y * v.x;
523 // == |this| * |v| * (*this - v).th().sin()
524 }
525
531 bool equals( const Vector2D & other ) const
532 {
533 return this->x == other.x
534 && this->y == other.y;
535 }
536
542 bool equalsWeakly( const Vector2D & other ) const
543 {
544 //return dist2( other ) < EPSILON * EPSILON;
545 return std::fabs( this->x - other.x ) < EPSILON
546 && std::fabs( this->y - other.y ) < EPSILON;
547 }
548
550 // static utility
551
558 inline
559 static
560 Vector2D polar2vector( const double & mag,
561 const AngleDeg & theta )
562 {
563 return Vector2D( mag * theta.cos(), mag * theta.sin() );
564 }
565
572 inline
573 static
574 Vector2D from_polar( const double & mag,
575 const AngleDeg & theta )
576 {
577 return Vector2D( mag * theta.cos(), mag * theta.sin() );
578 }
579
586 inline
587 static
588 double inner_product( const Vector2D & v1,
589 const Vector2D & v2 )
590 {
591 return v1.innerProduct( v2 );
592 }
593
600 inline
601 static
602 double outer_product( const Vector2D & v1,
603 const Vector2D & v2 )
604 {
605 return v1.outerProduct( v2 );
606 }
607
609 // stream utility
610
616 std::ostream & print( std::ostream & os ) const
617 {
618 os << '(' << x << ", " << y << ')';
619 return os;
620 }
621
628 std::ostream & printRound( std::ostream & os,
629 const double & prec = 0.1 ) const
630 {
631 os << '(' << rint( x / prec ) * prec
632 << ", " << rint( y / prec ) * prec << ')';
633 return os;
634 }
635
637 // functors for comparison
638
643 class XCmp
644 : public std::binary_function< Vector2D, Vector2D, bool > {
645 public:
652 result_type operator()( const first_argument_type & lhs,
653 const second_argument_type & rhs ) const
654 {
655 return lhs.x < rhs.x;
656 }
657 };
658
663 class YCmp
664 : public std::binary_function< Vector2D, Vector2D, bool > {
665 public:
672 result_type operator()( const first_argument_type & lhs,
673 const second_argument_type & rhs ) const
674 {
675 return lhs.y < rhs.y;
676 }
677 };
678
683 class AbsXCmp
684 : public std::binary_function< Vector2D, Vector2D, bool > {
685 public:
692 result_type operator()( const first_argument_type & lhs,
693 const second_argument_type & rhs ) const
694 {
695 return lhs.absX() < rhs.absX();
696 }
697 };
698
703 class AbsYCmp
704 : public std::binary_function< Vector2D, Vector2D, bool > {
705 public:
712 result_type operator()( const first_argument_type & lhs,
713 const second_argument_type & rhs ) const
714 {
715 return lhs.absY() < rhs.absY();
716 }
717 };
718
723 class XYCmp
724 : public std::binary_function< Vector2D, Vector2D, bool > {
725 public:
732 result_type operator()( const first_argument_type & lhs,
733 const second_argument_type & rhs ) const
734 {
735 return ( lhs.x < rhs.x
736 ? true
737 : lhs.x > rhs.x
738 ? false
739 : lhs.y < rhs.y );
740 // if ( lhs.x < rhs.x ) return true;
741 // else if ( lhs.x > rhs.x ) return false;
742 // else return lhs.y < rhs.y;
743 }
744
745 };
746
751 class YXCmp
752 : public std::binary_function< Vector2D, Vector2D, bool > {
753 public:
760 result_type operator()( const first_argument_type & lhs,
761 const second_argument_type & rhs ) const
762 {
763 return ( lhs.y < rhs.y
764 || ( lhs.y == rhs.y && lhs.x < rhs.x ) );
765 }
766 };
767
772 class Equal
773 : public std::binary_function< Vector2D, Vector2D, bool > {
774 public:
781 result_type operator()( const first_argument_type & lhs,
782 const second_argument_type & rhs ) const
783 {
784 return lhs.equals( rhs );
785 }
786 };
787
789 // functor for region
790
795 template < typename REGION >
796 class IsWithin
797 : public std::unary_function< Vector2D, bool > {
798 private:
799 const REGION M_region;
800 public:
802 explicit
803 IsWithin( const REGION & region )
804 : M_region( region )
805 { }
806
807 result_type operator()( const argument_type & position ) const
808 {
809 return M_region.contains( position );
810 }
811 };
812};
813
814} // end of namespace
815
816
818// comparison operators
825inline
826bool
828 const rcsc::Vector2D & rhs )
829{
830 //return lhs.x == rhs.x
831 // && lhs.y == rhs.y;
832 return lhs.equals( rhs );
833}
834
841inline
842bool
844 const rcsc::Vector2D & rhs )
845{
846 return ! operator==( lhs, rhs );
847}
848
849
851// arithmetic operators
852
859inline
860const
863 const rcsc::Vector2D & rhs )
864{
865 return rcsc::Vector2D( lhs ) += rhs;
866}
867
874inline
875const
878 const rcsc::Vector2D & rhs )
879{
880 return rcsc::Vector2D( lhs ) -= rhs;
881}
882
889inline
890const
893 const double & rhs )
894{
895 return rcsc::Vector2D( lhs ) *= rhs;
896}
897
904inline
905const
908 const double & rhs )
909{
910 return rcsc::Vector2D( lhs ) /= rhs;
911}
912
916template < typename T >
917bool
918operator<( const rcsc::Vector2D & lhs,
919 const T & rhs );
920
924template < typename T >
925bool
926operator<=( const rcsc::Vector2D & lhs,
927 const T & rhs );
928
932template < typename T >
933bool
935 const T & rhs );
936
940template < typename T >
941bool
943 const T & rhs );
944
948template < typename T >
949bool
950operator<( const T & lhs,
951 const rcsc::Vector2D & rhs );
952
953
957template < typename T >
958bool
959operator<=(const T & lhs,
960 const rcsc::Vector2D & rhs );
961
962
966template < typename T >
967bool
968operator>( const T & lhs,
969 const rcsc::Vector2D & rhs );
970
974template < typename T >
975bool
976operator>=( const T & lhs,
977 const rcsc::Vector2D & rhs );
978
982template < typename T >
983bool
984operator==( const T & lhs,
985 const rcsc::Vector2D & rhs );
986
990template < typename T >
991bool
992operator!=( const T & lhs,
993 const rcsc::Vector2D & rhs );
994
995
996
998
1005inline
1006std::ostream &
1007operator<<( std::ostream & os,
1008 const rcsc::Vector2D & v )
1009{
1010 return v.print( os );
1011}
1012
1013
1014#endif
degree wrapper class Header File.
degree wrapper class
Definition angle_deg.h:45
const double & degree() const
get value of this angle
Definition angle_deg.h:133
double cos() const
calculate cosine
Definition angle_deg.h:301
static double atan2_deg(const double &y, const double &x)
static utility. calculate arc tangent value from XY
Definition angle_deg.h:495
static const double DEG2RAD
constant variable to convert DEGREE to RADIAN.
Definition angle_deg.h:67
double sin() const
calculate sine
Definition angle_deg.h:310
result_type operator()(const first_argument_type &lhs, const second_argument_type &rhs) const
functional operator.
Definition vector_2d.h:692
result_type operator()(const first_argument_type &lhs, const second_argument_type &rhs) const
functional operator.
Definition vector_2d.h:712
result_type operator()(const first_argument_type &lhs, const second_argument_type &rhs) const
functional operator.
Definition vector_2d.h:781
IsWithin(const REGION &region)
constructor
Definition vector_2d.h:803
result_type operator()(const argument_type &position) const
functional operator
Definition vector_2d.h:807
result_type operator()(const first_argument_type &lhs, const second_argument_type &rhs) const
functional operator.
Definition vector_2d.h:652
result_type operator()(const first_argument_type &lhs, const second_argument_type &rhs) const
functional operator.
Definition vector_2d.h:732
result_type operator()(const first_argument_type &lhs, const second_argument_type &rhs) const
functional operator.
Definition vector_2d.h:672
result_type operator()(const first_argument_type &lhs, const second_argument_type &rhs) const
functional operator.
Definition vector_2d.h:760
2D point vector class
Definition vector_2d.h:47
Vector2D abs() const
get new vector that XY values were set to absolute value.
Definition vector_2d.h:212
Vector2D normalizedVector() const
get new normalized vector that the length is set to 1.0 but angle is same
Definition vector_2d.h:434
static Vector2D from_polar(const double &mag, const AngleDeg &theta)
get new Vector created by POLAR value.
Definition vector_2d.h:574
std::ostream & print(std::ostream &os) const
output XY values to ostream.
Definition vector_2d.h:616
static const double ERROR_VALUE
constant error value for XY (= std::numeric_limits< doulble >::max()).
Definition vector_2d.h:59
AngleDeg th() const
get the angle of vector.
Definition vector_2d.h:194
Vector2D & rotate(const AngleDeg &angle)
rotate this vector with 'angle'.
Definition vector_2d.h:457
const Vector2D & invalidate()
invalidate this object
Definition vector_2d.h:128
Vector2D & setDir(const AngleDeg &dir)
set vector's angle to 'angle'
Definition vector_2d.h:487
Vector2D & reverse()
reverse vector components
Definition vector_2d.h:377
double r() const
get the length of vector.
Definition vector_2d.h:148
Vector2D & add(const Vector2D &v)
add vector.
Definition vector_2d.h:240
Vector2D & add(const double &xx, const double &yy)
add XY values respectively.
Definition vector_2d.h:253
double norm2() const
get the squared norm value. this method is equivalent to r2().
Definition vector_2d.h:167
Vector2D operator-() const
create reversed vector
Definition vector_2d.h:287
Vector2D & normalize()
normalize vector. length is set to 1.0.
Definition vector_2d.h:424
double dist(const Vector2D &p) const
get the distance from this to 'p'.
Definition vector_2d.h:366
Vector2D & rotate(const double &deg)
rotate this vector with 'deg'
Definition vector_2d.h:444
Vector2D setLengthVector(const double &len) const
get new vector that the length is set to 'len'
Definition vector_2d.h:415
double absY() const
get absolute y value
Definition vector_2d.h:230
static const Vector2D INVALIDATED
invalidated value vector
Definition vector_2d.h:62
static Vector2D polar2vector(const double &mag, const AngleDeg &theta)
get new Vector created by POLAR value.
Definition vector_2d.h:560
double y
Y coordinate.
Definition vector_2d.h:65
Vector2D rotatedVector(const double &deg) const
get new vector that is rotated by 'deg'.
Definition vector_2d.h:467
Vector2D rotatedVector(const AngleDeg &angle) const
get new vector that is rotated by 'angle'.
Definition vector_2d.h:477
const Vector2D & operator-=(const Vector2D &v)
subtract vector to itself
Definition vector_2d.h:311
std::ostream & printRound(std::ostream &os, const double &prec=0.1) const
output rounded XY values to ostream.
Definition vector_2d.h:628
double norm() const
get the norm value. this method is equivalent to r().
Definition vector_2d.h:158
static double inner_product(const Vector2D &v1, const Vector2D &v2)
get inner(dot) product for v1 and v2.
Definition vector_2d.h:588
Vector2D()
default constructor.
Definition vector_2d.h:70
double dist2(const Vector2D &p) const
get the squared distance from this to 'p'.
Definition vector_2d.h:354
const Vector2D & operator+() const
return this vector
Definition vector_2d.h:278
double length() const
get the length of vector. this method is equivalent to r().
Definition vector_2d.h:176
double innerProduct(const Vector2D &v) const
get inner(dot) product with 'v'.
Definition vector_2d.h:500
AngleDeg dir() const
get the angle of vector. this method is equivalent to th().
Definition vector_2d.h:203
Vector2D & scale(const double &scalar)
scale this vector
Definition vector_2d.h:266
const Vector2D & operator*=(const double &scalar)
multiplied by 'scalar'
Definition vector_2d.h:324
double x
X coordinate.
Definition vector_2d.h:64
static double outer_product(const Vector2D &v1, const Vector2D &v2)
get outer(cross) product for v1 and v2.
Definition vector_2d.h:602
static const double EPSILON
constant threshold value for calculation error
Definition vector_2d.h:56
Vector2D & setLength(const double &len)
set vector length to 'len'.
Definition vector_2d.h:398
bool isValid() const
check if this vector is valid or not.
Definition vector_2d.h:90
Vector2D & assign(const double &xx, const double &yy)
assign XY value directly.
Definition vector_2d.h:101
const Vector2D & operator+=(const Vector2D &v)
add vector to itself
Definition vector_2d.h:298
Vector2D & setPolar(const double &radius, const AngleDeg &dir)
assign XY value from POLAR value.
Definition vector_2d.h:115
double length2() const
get the squared length value. this method is equivalent to r2().
Definition vector_2d.h:185
double r2() const
get the squared length of vector.
Definition vector_2d.h:139
bool equals(const Vector2D &other) const
check if this vector is strictly same as given vector.
Definition vector_2d.h:531
double absX() const
get absolute x value
Definition vector_2d.h:221
double outerProduct(const Vector2D &v) const
get virtal outer(cross) product with 'v'.
Definition vector_2d.h:511
Vector2D reversedVector() const
get reversed vector.
Definition vector_2d.h:388
bool equalsWeakly(const Vector2D &other) const
check if this vector is weakly same as given vector.
Definition vector_2d.h:542
const Vector2D & operator/=(const double &scalar)
divided by 'scalar'.
Definition vector_2d.h:338
Vector2D(const double &xx, const double &yy)
create Vector with XY value directly.
Definition vector_2d.h:80
const rcsc::Vector2D operator-(const rcsc::Vector2D &lhs, const rcsc::Vector2D &rhs)
operator sub(T, T)
Definition vector_2d.h:877
bool operator<=(const rcsc::Vector2D &lhs, const T &rhs)
never used
const rcsc::Vector2D operator/(const rcsc::Vector2D &lhs, const double &rhs)
operator div(T, U)
Definition vector_2d.h:907
bool operator==(const rcsc::Vector2D &lhs, const rcsc::Vector2D &rhs)
check vectors are strictly same or not.
Definition vector_2d.h:827
std::ostream & operator<<(std::ostream &os, const rcsc::Vector2D &v)
stream operator
Definition vector_2d.h:1007
bool operator!=(const rcsc::Vector2D &lhs, const rcsc::Vector2D &rhs)
check vectors are strictly different or not.
Definition vector_2d.h:843
const rcsc::Vector2D operator+(const rcsc::Vector2D &lhs, const rcsc::Vector2D &rhs)
operator add(T, T)
Definition vector_2d.h:862
bool operator>(const rcsc::Vector2D &lhs, const T &rhs)
never used
bool operator<(const rcsc::Vector2D &lhs, const T &rhs)
never used
const rcsc::Vector2D operator*(const rcsc::Vector2D &lhs, const double &rhs)
operator mult(T, U)
Definition vector_2d.h:892
bool operator>=(const rcsc::Vector2D &lhs, const T &rhs)
never used