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
angle_deg.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_ANGLEDEG_H
33#define RCSC_GEOM_ANGLEDEG_H
34
35#include <functional>
36#include <iostream>
37#include <cmath>
38
39namespace rcsc {
40
45class AngleDeg {
46 // : public boost::addable< AngleDeg >
47 // , public boost::addable2< AngleDeg, double >
48 // , public boost::addable2_left< double, AngleDeg >
49 // , public boost::subtractable< AngleDeg >
50 // , public boost::subtractable2< AngleDeg, double >
51 // , public boost::subtractable2_left< double, AngleDeg >
52 // , public boost::multipliable2< AngleDeg, double >
53 // , public boost::dividable2< AngleDeg, double >
54private:
56 double M_degree;
57
58public:
60 static const double EPSILON;
61
63 static const double PI;
65 static const double TWO_PI;
67 static const double DEG2RAD;
69 static const double RAD2DEG;
70
73 : M_degree( 0.0 )
74 { }
75
82 AngleDeg( const double & deg )
83 : M_degree( deg )
84 {
85 normalize();
86 }
87
93 const
94 AngleDeg & operator=( const double & deg )
95 {
96 M_degree = deg;
97 return normalize();
98 }
99
100private:
105 const
106 AngleDeg & normalize()
107 {
108 if ( M_degree < -360.0 || 360.0 < M_degree )
109 {
110 M_degree = std::fmod( M_degree, 360.0 );
111 }
112
113 if ( M_degree < -180.0 )
114 {
115 M_degree += 360.0;
116 }
117
118 if ( M_degree > 180.0 )
119 {
120 M_degree -= 360.0;
121 }
122
123 return *this;
124 }
125
126public:
127
132 const
133 double & degree() const
134 {
135 return M_degree;
136 }
137
142 double abs() const
143 {
144 return std::fabs( degree() );
145 }
146
150 double radian() const
151 {
152 return degree() * DEG2RAD;
153 }
154
155 /*
156 operator double() const
157 {
158 return degree();
159 }
160 */
161
167 {
168 return AngleDeg( - degree() );
169 }
170
176 const
177 AngleDeg & operator+=( const AngleDeg & angle )
178 {
179 M_degree += angle.degree();
180 return normalize();
181 }
182
188 const
189 AngleDeg & operator+=( const double & deg )
190 {
191 M_degree += deg;
192 return normalize();
193 }
194
200 const
201 AngleDeg & operator-=( const AngleDeg & angle )
202 {
203 M_degree -= angle.degree();
204 return normalize();
205 }
206
212 const
213 AngleDeg & operator-=( const double & deg )
214 {
215 M_degree -= deg;
216 return normalize();
217 }
218
224 const
225 AngleDeg & operator*=( const double & scalar )
226 {
227 M_degree *= scalar;
228 return normalize();
229 }
230
236 const
237 AngleDeg & operator/=( const double & scalar )
238 {
239 if ( std::fabs( scalar ) < EPSILON )
240 {
241 return * this;
242 }
243 M_degree /= scalar;
244 return normalize();
245 }
246
248
253 bool isLeftOf( const AngleDeg & angle ) const
254 {
255 //return (*this - angle).degree() < 0.0;
256 double diff = angle.degree() - this->degree();
257 return ( ( 0.0 < diff && diff < 180.0 )
258 || diff < -180.0 );
259 }
260
265 bool isLeftEqualOf( const AngleDeg & angle ) const
266 {
267 //return (*this - angle).degree() <= 0.0;
268 double diff = angle.degree() - this->degree();
269 return ( ( 0.0 <= diff && diff < 180.0 )
270 || diff < -180.0 );
271 }
272
277 bool isRightOf( const AngleDeg & angle ) const
278 {
279 //return (*this - angle).degree() > 0.0;
280 double diff = this->degree() - angle.degree();
281 return ( ( 0.0 < diff && diff < 180.0 )
282 || diff < -180.0 );
283 }
284
289 bool isRightEqualOf( const AngleDeg & angle ) const
290 {
291 //return (*this - angle).degree() >= 0.0;
292 double diff = this->degree() - angle.degree();
293 return ( ( 0.0 <= diff && diff < 180.0 )
294 || diff < -180.0 );
295 }
296
301 double cos() const
302 {
303 return std::cos( degree() * DEG2RAD );
304 }
305
310 double sin() const
311 {
312 return std::sin( degree() * DEG2RAD );
313 }
314
319 double tan() const
320 {
321 return std::tan( degree() * DEG2RAD );
322 }
323
330 bool isWithin( const AngleDeg & left,
331 const AngleDeg & right ) const;
332
339 void sinMinMax( const double & angle_err,
340 double * minsin,
341 double * maxsin ) const;
342
349 void cosMinMax( const double & angle_err,
350 double * mincos,
351 double * maxcos ) const;
352
354 // static utility method concerned with angle operation
355
361 inline
362 static
363 double normalize_angle( double dir )
364 {
365 if ( dir < -360.0 || 360.0 < dir )
366 {
367 dir = std::fmod( dir, 360.0 );
368 }
369
370 if ( dir < -180.0 )
371 {
372 dir += 360.0;
373 }
374
375 if ( dir > 180.0 )
376 {
377 dir -= 360.0;
378 }
379
380 return dir;
381 }
382
388 inline
389 static
390 double rad2deg( const double & rad )
391 {
392 return rad * RAD2DEG;
393 }
394
400 inline
401 static
402 double deg2rad( const double & deg )
403 {
404 return deg * DEG2RAD;
405 }
406
412 inline
413 static
414 double cos_deg( const double & deg )
415 {
416 return std::cos( deg2rad( deg ) );
417 }
418
424 inline
425 static
426 double sin_deg( const double & deg )
427 {
428 return std::sin( deg2rad( deg ) );
429 }
430
436 inline
437 static
438 double tan_deg( const double & deg )
439 {
440 return std::tan( deg2rad( deg ) );
441 }
442
448 inline
449 static
450 double acos_deg( const double & cosine )
451 {
452 return ( cosine >= 1.0
453 ? 0.0
454 : ( cosine <= -1.0
455 ? 180.0
456 : rad2deg( std::acos( cosine ) ) ) );
457 }
458
464 inline
465 static
466 double asin_deg( const double & sine )
467 {
468 return ( sine >= 1.0
469 ? 90.0
470 : ( sine <= -1.0
471 ? -90.0
472 : rad2deg( std::asin( sine ) ) ) );
473 }
474
480 inline
481 static
482 double atan_deg( const double & tangent )
483 {
484 return rad2deg( std::atan( tangent ) );
485 }
486
493 inline
494 static
495 double atan2_deg( const double & y,
496 const double & x )
497 {
498 return ( ( x == 0.0 && y == 0.0 )
499 ? 0.0
500 : rad2deg( std::atan2( y, x ) ) );
501 }
502
511 static
512 AngleDeg bisect( const AngleDeg & left,
513 const AngleDeg & right );
514
516
522 std::ostream & print( std::ostream & os ) const
523 {
524 return os << degree();
525 }
526
533 std::ostream & printRound( std::ostream & os,
534 const double & step = 0.1 ) const
535 {
536 return os << rint( degree() / step ) * step;
537 }
538
540
545 : public std::binary_function< AngleDeg, AngleDeg, bool > {
546 public:
548 result_type operator()( const first_argument_type & lhs,
549 const second_argument_type & rhs ) const
550 {
551 return lhs.degree() < rhs.degree();
552 }
553 };
554
555};
556
557
559
565 : public std::unary_function< AngleDeg, bool > {
566 const AngleDeg M_left;
567 const AngleDeg M_right;
568public:
570 AngleIsWithin( const AngleDeg & left,
571 const AngleDeg & right )
572 : M_left( left )
573 , M_right( right )
574 { }
575
577 result_type operator()( const argument_type & angle ) const
578 {
579 return angle.isWithin( M_left, M_right );
580 }
581};
582
583
584} // end of namespace
585
586
588// arith operators
589
590/*-------------------------------------------------------------------*/
597inline
598const
601 const rcsc::AngleDeg & rhs )
602{
603 return rcsc::AngleDeg( lhs ) += rhs;
604}
605
606/*-------------------------------------------------------------------*/
613inline
614const
617 const double & rhs )
618{
619 return rcsc::AngleDeg( lhs ) += rhs;
620}
621
622/*-------------------------------------------------------------------*/
629inline
630const
632operator+( const double & lhs,
633 const rcsc::AngleDeg & rhs )
634{
635 return rcsc::AngleDeg( rhs ) += lhs;
636}
637
638/*-------------------------------------------------------------------*/
645inline
646const
649 const rcsc::AngleDeg & rhs )
650{
651 return rcsc::AngleDeg( lhs ) -= rhs;
652}
653
654/*-------------------------------------------------------------------*/
661inline
662const
665 const double & rhs )
666{
667 return rcsc::AngleDeg( lhs ) -= rhs;
668}
669
670/*-------------------------------------------------------------------*/
677inline
678const
680operator-( const double & lhs,
681 const rcsc::AngleDeg & rhs )
682{
683 return rcsc::AngleDeg( lhs ) -= rhs;
684}
685
687// predicate operator
688
689/*-------------------------------------------------------------------*/
696inline
697bool
699 const rcsc::AngleDeg & rhs )
700{
701 return std::fabs( lhs.degree() - rhs.degree() ) > rcsc::AngleDeg::EPSILON;
702}
703
704/*-------------------------------------------------------------------*/
711inline
712bool
714 const double & rhs )
715{
716 return std::fabs( lhs.degree() - rhs ) > rcsc::AngleDeg::EPSILON;
717}
718
719/*-------------------------------------------------------------------*/
726inline
727bool
728operator!=( const double & lhs,
729 const rcsc::AngleDeg & rhs )
730{
731 return std::fabs( lhs - rhs.degree() ) > rcsc::AngleDeg::EPSILON;
732}
733
734/*-------------------------------------------------------------------*/
741inline
742bool
744 const rcsc::AngleDeg & rhs )
745{
746 return std::fabs( lhs.degree() - rhs.degree() ) < rcsc::AngleDeg::EPSILON;
747}
748
749/*-------------------------------------------------------------------*/
756inline
757bool
759 const double & rhs )
760{
761 return std::fabs( lhs.degree() - rhs ) < rcsc::AngleDeg::EPSILON;
762}
763
764/*-------------------------------------------------------------------*/
771inline
772bool
773operator==( const double & lhs,
774 const rcsc::AngleDeg & rhs )
775{
776 return std::fabs( lhs - rhs.degree() ) < rcsc::AngleDeg::EPSILON;
777}
778
779
780/*-------------------------------------------------------------------*/
787inline
788std::ostream &
789operator<<( std::ostream & os,
790 const rcsc::AngleDeg & a )
791{
792 return a.print( os );
793}
794
795#endif
bool operator==(const rcsc::AngleDeg &lhs, const rcsc::AngleDeg &rhs)
predicate operator ==
Definition angle_deg.h:743
std::ostream & operator<<(std::ostream &os, const rcsc::AngleDeg &a)
output to ostream
Definition angle_deg.h:789
const rcsc::AngleDeg operator+(const rcsc::AngleDeg &lhs, const rcsc::AngleDeg &rhs)
operator add(T, T)
Definition angle_deg.h:600
bool operator!=(const rcsc::AngleDeg &lhs, const rcsc::AngleDeg &rhs)
predicate operator !=
Definition angle_deg.h:698
const rcsc::AngleDeg operator-(const rcsc::AngleDeg &lhs, const rcsc::AngleDeg &rhs)
operator sub(T, T)
Definition angle_deg.h:648
predicate function object. this compares two angles by degree value
Definition angle_deg.h:545
result_type operator()(const first_argument_type &lhs, const second_argument_type &rhs) const
operator method
Definition angle_deg.h:548
degree wrapper class
Definition angle_deg.h:45
static double cos_deg(const double &deg)
static utility. calculate cosine value for degree angle
Definition angle_deg.h:414
double tan() const
calculate tarngetn
Definition angle_deg.h:319
double radian() const
get RADIAN value.
Definition angle_deg.h:150
bool isLeftEqualOf(const AngleDeg &angle) const
check if this angle is left or equal of 'angle'
Definition angle_deg.h:265
const double & degree() const
get value of this angle
Definition angle_deg.h:133
static double deg2rad(const double &deg)
static utility. convert degree to radian
Definition angle_deg.h:402
double cos() const
calculate cosine
Definition angle_deg.h:301
static double atan_deg(const double &tangent)
static utility. calculate arc tangent value
Definition angle_deg.h:482
static double rad2deg(const double &rad)
static utility. convert radian to degree
Definition angle_deg.h:390
const AngleDeg & operator+=(const AngleDeg &angle)
operator += with AngleDeg
Definition angle_deg.h:177
const AngleDeg & operator+=(const double &deg)
operator += with double
Definition angle_deg.h:189
static double sin_deg(const double &deg)
static utility. calculate sine value for degree angle
Definition angle_deg.h:426
AngleDeg(const double &deg)
constructor with value.
Definition angle_deg.h:82
bool isRightOf(const AngleDeg &angle) const
check if this angle is right of 'angle'
Definition angle_deg.h:277
const AngleDeg & operator*=(const double &scalar)
operator *=
Definition angle_deg.h:225
static const double PI
pi valur
Definition angle_deg.h:63
const AngleDeg & operator-=(const AngleDeg &angle)
operator -= with AngleDeg
Definition angle_deg.h:201
static double atan2_deg(const double &y, const double &x)
static utility. calculate arc tangent value from XY
Definition angle_deg.h:495
const AngleDeg & operator=(const double &deg)
operator substitution.
Definition angle_deg.h:94
static double normalize_angle(double dir)
static utility. normalize angle
Definition angle_deg.h:363
static const double DEG2RAD
constant variable to convert DEGREE to RADIAN.
Definition angle_deg.h:67
AngleDeg operator-() const
get new AngleDeg multiplyed by -1.
Definition angle_deg.h:166
static double asin_deg(const double &sine)
static utility. calculate arc sine value
Definition angle_deg.h:466
static const double TWO_PI
2*pi valur
Definition angle_deg.h:65
bool isRightEqualOf(const AngleDeg &angle) const
check if this angle is right or equal of 'angle'
Definition angle_deg.h:289
std::ostream & print(std::ostream &os) const
output value to ostream
Definition angle_deg.h:522
static const double RAD2DEG
constant variable to convert RADIAN to DEGREE.
Definition angle_deg.h:69
AngleDeg()
default constructor.
Definition angle_deg.h:72
std::ostream & printRound(std::ostream &os, const double &step=0.1) const
output rounded value to ostream
Definition angle_deg.h:533
void cosMinMax(const double &angle_err, double *mincos, double *maxcos) const
calculate min/max cosine value of angle with angle error.
Definition angle_deg.cpp:135
static const double EPSILON
epsilon value
Definition angle_deg.h:60
static AngleDeg bisect(const AngleDeg &left, const AngleDeg &right)
static utility that returns bisect angle of [left, right].
Definition angle_deg.cpp:177
static double tan_deg(const double &deg)
static utility. calculate tangent value for degree angle
Definition angle_deg.h:438
const AngleDeg & operator/=(const double &scalar)
operator /=
Definition angle_deg.h:237
double sin() const
calculate sine
Definition angle_deg.h:310
double abs() const
get absolute value of this angle
Definition angle_deg.h:142
void sinMinMax(const double &angle_err, double *minsin, double *maxsin) const
calculate min/max sine value with angle error.
Definition angle_deg.cpp:90
bool isLeftOf(const AngleDeg &angle) const
check if this angle is left of 'angle'
Definition angle_deg.h:253
const AngleDeg & operator-=(const double &deg)
operator -= with double
Definition angle_deg.h:213
bool isWithin(const AngleDeg &left, const AngleDeg &right) const
check if this angle is within [left, right] (turn clockwise).
Definition angle_deg.cpp:60
static double acos_deg(const double &cosine)
static utility. calculate arc cosine value
Definition angle_deg.h:450
AngleIsWithin(const AngleDeg &left, const AngleDeg &right)
constructor
Definition angle_deg.h:570
result_type operator()(const argument_type &angle) const
operator method
Definition angle_deg.h:577