LIBRCSC Docs
Documentation for HELIOS's BASE LIBRCSC library for RoboCup 2D Simulation League.
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
vector_2d.h
Go to the documentation of this file.
1// -*-c++-*-
2
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 <rcsc/geom/angle_deg.h>
36
37#include <iostream>
38#include <cmath>
39
40namespace rcsc {
41
46class Vector2D {
47 // : public boost::addable< Vector2D >
48 // , public boost::subtractable< Vector2D >
49 // , public multipliable2< Vector2D, double >
50 // , public dividable2< Vector2D, double >
51
52public:
53
55 static const double EPSILON;
56
58 static const double ERROR_VALUE;
59
61 static const Vector2D INVALIDATED;
62
63 double x;
64 double y;
65
70 : x( 0.0 ),
71 y( 0.0 )
72 { }
73
79 Vector2D( const double xx,
80 const double yy )
81 : x( xx ),
82 y( yy )
83 { }
84
89 bool isValid() const
90 {
91 return ( ( x != ERROR_VALUE ) && ( y != ERROR_VALUE ) );
92 }
93
100 Vector2D & assign( const double xx,
101 const double yy )
102 {
103 x = xx;
104 y = yy;
105 return *this;
106 }
107
114 Vector2D & setPolar( const double radius,
115 const AngleDeg & dir )
116 {
117 x = radius * dir.cos();
118 y = radius * dir.sin();
119 return *this;
120 }
121
127 {
128 x = ERROR_VALUE;
129 y = ERROR_VALUE;
130 return *this;
131 }
132
137 double r2() const
138 {
139 return x * x + y * y;
140 }
141
146 double r() const
147 {
148 //return std::hypot( x, y );
149 return std::sqrt( r2() );
150 }
151
156 double norm() const
157 {
158 return r();
159 }
160
165 double norm2() const
166 {
167 return r2();
168 }
169
174 double length() const
175 {
176 return r();
177 }
178
183 double length2() const
184 {
185 return r2();
186 }
187
192 AngleDeg th() const
193 {
194 return AngleDeg( AngleDeg::atan2_deg( y, x ) );
195 }
196
201 AngleDeg dir() const
202 {
203 return th();
204 }
205
210 Vector2D abs() const
211 {
212 return Vector2D( std::fabs( x ), std::fabs( y ) );
213 }
214
219 double absX() const
220 {
221 return std::fabs( x );
222 }
223
228 double absY() const
229 {
230 return std::fabs( y );
231 }
232
238 Vector2D & add( const Vector2D & v )
239 {
240 x += v.x;
241 y += v.y;
242 return *this;
243 }
244
251 Vector2D & add( const double xx,
252 const double yy )
253 {
254 x += xx;
255 y += yy;
256 return *this;
257 }
258
264 Vector2D & scale( const double scalar )
265 {
266 x *= scalar;
267 y *= scalar;
268 return *this;
269 }
270
275 const Vector2D & operator+() const
276 {
277 return *this;
278 }
279
285 {
286 return Vector2D( -x, -y );
287 }
288
294 const Vector2D & operator+=( const Vector2D & v )
295 {
296 x += v.x;
297 y += v.y;
298 return *this;
299 }
300
306 const Vector2D & operator-=( const Vector2D & v )
307 {
308 x -= v.x;
309 y -= v.y;
310 return *this;
311 }
312
318 const Vector2D & operator*=( const double scalar )
319 {
320 //x *= scalar;
321 //y *= scalar;
322 //return *this;
323 return scale( scalar );
324 }
325
331 const Vector2D & operator/=( const double scalar )
332 {
333 //if ( scalar != 0 )
334 if ( std::fabs( scalar ) > EPSILON )
335 {
336 x /= scalar;
337 y /= scalar;
338 }
339 return *this;
340 }
341
347 double dist2( const Vector2D & p ) const
348 {
349 //return ( Vector2D( *this ) -= p ).r2();
350 return ( std::pow( this->x - p.x, 2 )
351 + std::pow( this->y - p.y, 2 ) );
352 }
353
359 double dist( const Vector2D & p ) const
360 {
361 //return std::hypot( this->x - p.x,
362 // this->y - p.y );
363 return std::sqrt( dist2( p ) );
364 }
365
372 double dist2( const double xx,
373 const double yy ) const
374 {
375 return ( std::pow( this->x - xx, 2 )
376 + std::pow( this->y - yy, 2 ) );
377 }
378
385 double dist( const double xx,
386 const double yy ) const
387 {
388 //return std::hypot( this->x - xx,
389 // this->y - yy );
390 return std::sqrt( dist2( xx, yy ) );
391 }
392
398 {
399 x = -x;
400 y = -y;
401 return *this;
402 }
403
409 {
410 return Vector2D( *this ).reverse();
411 }
412
418 Vector2D & setLength( const double len )
419 {
420 double mag = this->r();
421 //if ( mag == 0 )
422 if ( mag < EPSILON )
423 {
424 return *this;
425 }
426 //return ( (*this) *= ( len / mag ) );
427 return scale( len / mag );
428 }
429
435 Vector2D setLengthVector( const double len ) const
436 {
437 return Vector2D( *this ).setLength( len );
438 }
439
445 {
446 return setLength( 1.0 );
447 }
448
455 {
456 return Vector2D( *this ).normalize();
457 }
458
464 Vector2D & rotate( const double deg )
465 {
466 double c = std::cos( deg * AngleDeg::DEG2RAD );
467 double s = std::sin( deg * AngleDeg::DEG2RAD );
468 return assign( this->x * c - this->y * s,
469 this->x * s + this->y * c );
470 }
471
477 Vector2D & rotate( const AngleDeg & angle )
478 {
479 return rotate( angle.degree() );
480 }
481
487 Vector2D rotatedVector( const double deg ) const
488 {
489 return Vector2D( *this ).rotate( deg );
490 }
491
497 Vector2D rotatedVector( const AngleDeg & angle ) const
498 {
499 return Vector2D( *this ).rotate( angle.degree() );
500 }
501
508 {
509 double radius = this->r();
510 x = radius * dir.cos();
511 y = radius * dir.sin();
512 return *this;
513 }
514
520 double innerProduct( const Vector2D & v ) const
521 {
522 return this->x * v.x + this->y * v.y;
523 // == |this| * |v| * (*this - v).th().cos()
524 }
525
531 double outerProduct( const Vector2D & v ) const
532 {
533 /*---------------------*
534 * assume virtual 3D environment.
535 * calculate Z-coordinate of outer product in right hand orientation.
536 * For the time being, Input Vector's Z-coordinate is set to ZERO.
537 *---------------------*/
538 // Normal 3D outer product
539 // xn = this->y * v.z - this->z * v.y;
540 // yn = this->z * v.x - this->x * v.z;
541 // # zn = this->x * v.y - this->y * v.x;
542 return this->x * v.y - this->y * v.x;
543 // == |this| * |v| * (*this - v).th().sin()
544 }
545
551 bool equals( const Vector2D & other ) const
552 {
553 return this->x == other.x
554 && this->y == other.y;
555 }
556
562 bool equalsWeakly( const Vector2D & other ) const
563 {
564 //return dist2( other ) < EPSILON * EPSILON;
565 return std::fabs( this->x - other.x ) < EPSILON
566 && std::fabs( this->y - other.y ) < EPSILON;
567 }
568
570 // static utility
571
578 inline
579 static
580 Vector2D polar2vector( const double mag,
581 const AngleDeg & theta )
582 {
583 return Vector2D( mag * theta.cos(), mag * theta.sin() );
584 }
585
592 inline
593 static
594 Vector2D from_polar( const double mag,
595 const AngleDeg & theta )
596 {
597 return Vector2D( mag * theta.cos(), mag * theta.sin() );
598 }
599
606 inline
607 static
608 double inner_product( const Vector2D & v1,
609 const Vector2D & v2 )
610 {
611 return v1.innerProduct( v2 );
612 }
613
620 inline
621 static
622 double outer_product( const Vector2D & v1,
623 const Vector2D & v2 )
624 {
625 return v1.outerProduct( v2 );
626 }
627
629 // stream utility
630
636 std::ostream & print( std::ostream & os ) const
637 {
638 os << '(' << x << ", " << y << ')';
639 return os;
640 }
641
648 std::ostream & printRound( std::ostream & os,
649 const double prec = 0.1 ) const
650 {
651 os << '(' << rint( x / prec ) * prec
652 << ", " << rint( y / prec ) * prec << ')';
653 return os;
654 }
655
657 // functors for comparison
658
663 struct XCmp {
670 bool operator()( const Vector2D & lhs,
671 const Vector2D & rhs ) const
672 {
673 return lhs.x < rhs.x;
674 }
675 };
676
681 struct YCmp {
688 bool operator()( const Vector2D & lhs,
689 const Vector2D & rhs ) const
690 {
691 return lhs.y < rhs.y;
692 }
693 };
694
699 struct AbsXCmp {
706 bool operator()( const Vector2D & lhs,
707 const Vector2D & rhs ) const
708 {
709 return lhs.absX() < rhs.absX();
710 }
711 };
712
717 struct AbsYCmp {
724 bool operator()( const Vector2D & lhs,
725 const Vector2D & rhs ) const
726 {
727 return lhs.absY() < rhs.absY();
728 }
729 };
730
735 struct XYCmp {
742 bool operator()( const Vector2D & lhs,
743 const Vector2D & rhs ) const
744 {
745 return ( lhs.x < rhs.x
746 ? true
747 : lhs.x > rhs.x
748 ? false
749 : lhs.y < rhs.y );
750 }
751
752 };
753
758 struct YXCmp {
765 bool operator()( const Vector2D & lhs,
766 const Vector2D & rhs ) const
767 {
768 return ( lhs.y < rhs.y
769 || ( lhs.y == rhs.y && lhs.x < rhs.x ) );
770 }
771 };
772
773};
774
775} // end of namespace
776
777
779// comparison operators
786inline
787bool
789 const rcsc::Vector2D & rhs )
790{
791 //return lhs.x == rhs.x
792 // && lhs.y == rhs.y;
793 return lhs.equals( rhs );
794}
795
802inline
803bool
805 const rcsc::Vector2D & rhs )
806{
807 return ! operator==( lhs, rhs );
808}
809
810
812// arithmetic operators
813
820inline
821const
824 const rcsc::Vector2D & rhs )
825{
826 return rcsc::Vector2D( lhs ) += rhs;
827}
828
835inline
836const
839 const rcsc::Vector2D & rhs )
840{
841 return rcsc::Vector2D( lhs ) -= rhs;
842}
843
850inline
851const
854 const double rhs )
855{
856 return rcsc::Vector2D( lhs ) *= rhs;
857}
858
865inline
866const
869 const double rhs )
870{
871 return rcsc::Vector2D( lhs ) /= rhs;
872}
873
877template < typename T >
878bool
879operator<( const rcsc::Vector2D & lhs,
880 const T & rhs );
881
885template < typename T >
886bool
887operator<=( const rcsc::Vector2D & lhs,
888 const T & rhs );
889
893template < typename T >
894bool
896 const T & rhs );
897
901template < typename T >
902bool
904 const T & rhs );
905
909template < typename T >
910bool
911operator<( const T & lhs,
912 const rcsc::Vector2D & rhs );
913
914
918template < typename T >
919bool
920operator<=(const T & lhs,
921 const rcsc::Vector2D & rhs );
922
923
927template < typename T >
928bool
929operator>( const T & lhs,
930 const rcsc::Vector2D & rhs );
931
935template < typename T >
936bool
937operator>=( const T & lhs,
938 const rcsc::Vector2D & rhs );
939
943template < typename T >
944bool
945operator==( const T & lhs,
946 const rcsc::Vector2D & rhs );
947
951template < typename T >
952bool
953operator!=( const T & lhs,
954 const rcsc::Vector2D & rhs );
955
956
957
959
966inline
967std::ostream &
968operator<<( std::ostream & os,
969 const rcsc::Vector2D & v )
970{
971 return v.print( os );
972}
973
974
975#endif
degree wrapper class Header File.
degree wrapper class
Definition: angle_deg.h:45
double cos() const
calculate cosine
Definition: angle_deg.h:292
double degree() const
get value of this angle
Definition: angle_deg.h:130
static const double DEG2RAD
constant variable to convert DEGREE to RADIAN.
Definition: angle_deg.h:67
static double atan2_deg(const double y, const double x)
static utility. calculate arc tangent value from XY
Definition: angle_deg.h:486
double sin() const
calculate sine
Definition: angle_deg.h:301
2D point vector class
Definition: vector_2d.h:46
Vector2D abs() const
get new vector that XY values were set to absolute value.
Definition: vector_2d.h:210
Vector2D normalizedVector() const
get new normalized vector that the length is set to 1.0 but angle is same
Definition: vector_2d.h:454
double dist2(const double xx, const double yy) const
get the squared distance from this to (xx,yy).
Definition: vector_2d.h:372
Vector2D & scale(const double scalar)
scale this vector
Definition: vector_2d.h:264
Vector2D & setLength(const double len)
set vector length to 'len'.
Definition: vector_2d.h:418
std::ostream & print(std::ostream &os) const
output XY values to ostream.
Definition: vector_2d.h:636
AngleDeg th() const
get the angle of vector.
Definition: vector_2d.h:192
Vector2D & rotate(const AngleDeg &angle)
rotate this vector with 'angle'.
Definition: vector_2d.h:477
const Vector2D & invalidate()
invalidate this object
Definition: vector_2d.h:126
Vector2D & assign(const double xx, const double yy)
assign XY value directly.
Definition: vector_2d.h:100
Vector2D & setDir(const AngleDeg &dir)
set vector's angle to 'angle'
Definition: vector_2d.h:507
Vector2D rotatedVector(const double deg) const
get new vector that is rotated by 'deg'.
Definition: vector_2d.h:487
static const Vector2D INVALIDATED
invalidated value vector
Definition: vector_2d.h:61
Vector2D & reverse()
reverse vector components
Definition: vector_2d.h:397
double r() const
get the length of vector.
Definition: vector_2d.h:146
Vector2D & add(const Vector2D &v)
add vector.
Definition: vector_2d.h:238
static Vector2D from_polar(const double mag, const AngleDeg &theta)
get new Vector created by POLAR value.
Definition: vector_2d.h:594
Vector2D & setPolar(const double radius, const AngleDeg &dir)
assign XY value from POLAR value.
Definition: vector_2d.h:114
double norm2() const
get the squared norm value. this method is equivalent to r2().
Definition: vector_2d.h:165
Vector2D operator-() const
create reversed vector
Definition: vector_2d.h:284
Vector2D & normalize()
normalize vector. length is set to 1.0.
Definition: vector_2d.h:444
double dist(const Vector2D &p) const
get the distance from this to 'p'.
Definition: vector_2d.h:359
Vector2D(const double xx, const double yy)
create Vector with XY value directly.
Definition: vector_2d.h:79
Vector2D setLengthVector(const double len) const
get new vector that the length is set to 'len'
Definition: vector_2d.h:435
const Vector2D & operator/=(const double scalar)
divided by 'scalar'.
Definition: vector_2d.h:331
static Vector2D polar2vector(const double mag, const AngleDeg &theta)
get new Vector created by POLAR value.
Definition: vector_2d.h:580
double absY() const
get absolute y value
Definition: vector_2d.h:228
double y
Y coordinate.
Definition: vector_2d.h:64
Vector2D rotatedVector(const AngleDeg &angle) const
get new vector that is rotated by 'angle'.
Definition: vector_2d.h:497
const Vector2D & operator-=(const Vector2D &v)
subtract vector to itself
Definition: vector_2d.h:306
double norm() const
get the norm value. this method is equivalent to r().
Definition: vector_2d.h:156
static double inner_product(const Vector2D &v1, const Vector2D &v2)
get inner(dot) product for v1 and v2.
Definition: vector_2d.h:608
Vector2D()
default constructor.
Definition: vector_2d.h:69
double dist2(const Vector2D &p) const
get the squared distance from this to 'p'.
Definition: vector_2d.h:347
Vector2D & add(const double xx, const double yy)
add XY values respectively.
Definition: vector_2d.h:251
const Vector2D & operator+() const
return this vector
Definition: vector_2d.h:275
double length() const
get the length of vector. this method is equivalent to r().
Definition: vector_2d.h:174
static const double EPSILON
constant threshold value for calculation error
Definition: vector_2d.h:55
double innerProduct(const Vector2D &v) const
get inner(dot) product with 'v'.
Definition: vector_2d.h:520
AngleDeg dir() const
get the angle of vector. this method is equivalent to th().
Definition: vector_2d.h:201
const Vector2D & operator*=(const double scalar)
multiplied by 'scalar'
Definition: vector_2d.h:318
double x
X coordinate.
Definition: vector_2d.h:63
double dist(const double xx, const double yy) const
get the distance from this to (xx, yy).
Definition: vector_2d.h:385
static const double ERROR_VALUE
constant error value for XY (= std::numeric_limits< doulble >::max()).
Definition: vector_2d.h:58
static double outer_product(const Vector2D &v1, const Vector2D &v2)
get outer(cross) product for v1 and v2.
Definition: vector_2d.h:622
std::ostream & printRound(std::ostream &os, const double prec=0.1) const
output rounded XY values to ostream.
Definition: vector_2d.h:648
bool isValid() const
check if this vector is valid or not.
Definition: vector_2d.h:89
const Vector2D & operator+=(const Vector2D &v)
add vector to itself
Definition: vector_2d.h:294
double length2() const
get the squared length value. this method is equivalent to r2().
Definition: vector_2d.h:183
double r2() const
get the squared length of vector.
Definition: vector_2d.h:137
bool equals(const Vector2D &other) const
check if this vector is strictly same as given vector.
Definition: vector_2d.h:551
double absX() const
get absolute x value
Definition: vector_2d.h:219
double outerProduct(const Vector2D &v) const
get virtal outer(cross) product with 'v'.
Definition: vector_2d.h:531
Vector2D reversedVector() const
get reversed vector.
Definition: vector_2d.h:408
bool equalsWeakly(const Vector2D &other) const
check if this vector is weakly same as given vector.
Definition: vector_2d.h:562
Vector2D & rotate(const double deg)
rotate this vector with 'deg'
Definition: vector_2d.h:464
comparison predicate for absolute X value.
Definition: vector_2d.h:699
bool operator()(const Vector2D &lhs, const Vector2D &rhs) const
functional operator.
Definition: vector_2d.h:706
comparison predicate for absolute Y value.
Definition: vector_2d.h:717
bool operator()(const Vector2D &lhs, const Vector2D &rhs) const
functional operator.
Definition: vector_2d.h:724
comparison predicate for X value.
Definition: vector_2d.h:663
bool operator()(const Vector2D &lhs, const Vector2D &rhs) const
functional operator.
Definition: vector_2d.h:670
comparison predicate for XY value (X -> Y order).
Definition: vector_2d.h:735
bool operator()(const Vector2D &lhs, const Vector2D &rhs) const
functional operator.
Definition: vector_2d.h:742
comparison predicate for Y value.
Definition: vector_2d.h:681
bool operator()(const Vector2D &lhs, const Vector2D &rhs) const
functional operator.
Definition: vector_2d.h:688
comparison predicatio for XY value (Y -> X order)
Definition: vector_2d.h:758
bool operator()(const Vector2D &lhs, const Vector2D &rhs) const
functional operator.
Definition: vector_2d.h:765
const rcsc::Vector2D operator-(const rcsc::Vector2D &lhs, const rcsc::Vector2D &rhs)
operator sub(T, T)
Definition: vector_2d.h:838
bool operator<=(const rcsc::Vector2D &lhs, const T &rhs)
never used
bool operator==(const rcsc::Vector2D &lhs, const rcsc::Vector2D &rhs)
check vectors are strictly same or not.
Definition: vector_2d.h:788
std::ostream & operator<<(std::ostream &os, const rcsc::Vector2D &v)
stream operator
Definition: vector_2d.h:968
const rcsc::Vector2D operator/(const rcsc::Vector2D &lhs, const double rhs)
operator div(T, U)
Definition: vector_2d.h:868
bool operator!=(const rcsc::Vector2D &lhs, const rcsc::Vector2D &rhs)
check vectors are strictly different or not.
Definition: vector_2d.h:804
const rcsc::Vector2D operator+(const rcsc::Vector2D &lhs, const rcsc::Vector2D &rhs)
operator add(T, T)
Definition: vector_2d.h:823
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:853
bool operator>=(const rcsc::Vector2D &lhs, const T &rhs)
never used