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
segment_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_SEGMENT2D_H
33#define RCSC_GEOM_SEGMENT2D_H
34
35#include <geom/line_2d.h>
36#include <geom/vector_2d.h>
37
38#include <cmath>
39
40namespace rcsc {
41
46class Segment2D {
47private:
48
49 static const double EPSILON;
50 static const double CALC_ERROR;
51
52 Vector2D M_origin;
53 Vector2D M_terminal;
54
56 Segment2D();
57
58 bool checkIntersectsOnLine( const Vector2D & p ) const;
59
60public:
67 const Vector2D & terminal )
68 : M_origin( origin ),
69 M_terminal( terminal )
70 { }
71
79 Segment2D( const double & origin_x,
80 const double & origin_y,
81 const double & terminal_x,
82 const double & terminal_y )
83 : M_origin( origin_x, origin_y ),
84 M_terminal( terminal_x, terminal_y )
85 { }
86
94 const double & length,
95 const AngleDeg & dir )
96 : M_origin( origin ),
97 M_terminal( origin + Vector2D::from_polar( length, dir ) )
98 { }
99
106 const
107 Segment2D & assign( const Vector2D & origin,
108 const Vector2D & terminal )
109 {
110 M_origin = origin;
111 M_terminal = terminal;
112 return *this;
113 }
114
123 const
124 Segment2D & assign( const double & origin_x,
125 const double & origin_y,
126 const double & terminal_x,
127 const double & terminal_y )
128 {
129 M_origin.assign( origin_x, origin_y );
130 M_terminal.assign( terminal_x, terminal_y );
131 return *this;
132 }
133
141 const
142 Segment2D & assign( const Vector2D & origin,
143 const double & length,
144 const AngleDeg & dir )
145 {
146 M_origin = origin;
147 M_terminal = origin + Vector2D::from_polar( length, dir );
148 return *this;
149 }
150
156 bool isValid() const
157 {
158 return ! origin().equalsWeakly( terminal() );
159 }
160
165 const
166 Vector2D & origin() const
167 {
168 return M_origin;
169 }
170
175 const
177 {
178 return M_terminal;
179 }
180
185 Line2D line() const
186 {
187 return Line2D( origin(), terminal() );
188 }
189
194 double length() const
195 {
196 return origin().dist( terminal() );
197 }
198
204 {
205 return ( terminal() - origin() ).th();
206 }
207
208
209
214 const
215 Segment2D & swap()
216 {
217 // std::swap( M_origin, M_terminal );
218 Vector2D tmp = M_origin;
219 M_origin = M_terminal;
220 M_terminal = tmp;
221 return *this;
222 }
223
228 const
229 Segment2D & reverse()
230 {
231 return swap();
232 }
233
238 Segment2D reversedSegment() const
239 {
240 return Segment2D( *this ).reverse();
241 }
242
251
257 bool contains( const Vector2D & p ) const
258 {
259 return ( ( p.x - origin().x ) * ( p.x - terminal().x ) <= CALC_ERROR //EPSILON
260 && ( p.y - origin().y ) * ( p.y - terminal().y ) <= CALC_ERROR); //EPSILON );
261 }
262
268 bool equals( const Segment2D & other ) const
269 {
270 return this->origin().equals( other.origin() )
271 && this->terminal().equals( other.terminal() );
272 }
273
279 bool equalsWeakly( const Segment2D & other ) const
280 {
281 return this->origin().equalsWeakly( other.origin() )
282 && this->terminal().equalsWeakly( other.terminal() );
283 }
284
285
292 Vector2D projection( const Vector2D & p ) const;
293
301 Vector2D intersection( const Segment2D & other,
302 const bool allow_end_point ) const;
303
310 Vector2D intersection( const Line2D & l ) const;
311
317 bool existIntersection( const Segment2D & other ) const;
318
324 bool intersects( const Segment2D & other ) const
325 {
326 return existIntersection( other );
327 }
328
336 bool existIntersectionExceptEndpoint( const Segment2D & other ) const;
337
345 bool intersectsExceptEndpoint( const Segment2D & other ) const
346 {
347 return existIntersectionExceptEndpoint( other );
348 }
349
355 bool existIntersection( const Line2D & l ) const;
356
362 bool intersects( const Line2D & l ) const
363 {
364 return existIntersection( l );
365 }
366
373 Vector2D nearestPoint( const Vector2D & p ) const;
374
380 double dist( const Vector2D & p ) const;
381
387 double dist( const Segment2D & seg ) const;
388
394 double farthestDist( const Vector2D & p ) const;
395
401 bool onSegment( const Vector2D & p ) const;
402
408 bool onSegmentWeakly( const Vector2D & p ) const;
409
415 std::ostream & print( std::ostream & os ) const
416 {
417 os << '[' << origin() << '-' << terminal() << ']';
418 return os;
419 }
420
421};
422
423}
424
425#endif
degree wrapper class
Definition angle_deg.h:45
2d straight line class
Definition line_2d.h:47
static Line2D perpendicular_bisector(const Vector2D &p1, const Vector2D &p2)
make perpendicular bisector line from twt points
Definition line_2d.cpp:69
2d segment line class
Definition segment_2d.h:46
const Segment2D & reverse()
swap segment edge point. This method is equivalent to swap(), provided for convenience.
Definition segment_2d.h:229
Segment2D(const Vector2D &origin, const double &length, const AngleDeg &dir)
construct using origin, direction and length
Definition segment_2d.h:93
Vector2D projection(const Vector2D &p) const
calculates projection point from p
Definition segment_2d.cpp:52
double dist(const Vector2D &p) const
get minimum distance between this segment and point
Definition segment_2d.cpp:312
double farthestDist(const Vector2D &p) const
get maximum distance between this segment and point
Definition segment_2d.cpp:366
bool contains(const Vector2D &p) const
check if the point is within the rectangle defined by this segment as a diagonal line.
Definition segment_2d.h:257
const Vector2D & terminal() const
get 2nd point of segment edge
Definition segment_2d.h:176
bool intersects(const Line2D &l) const
check if this line segment intersects with target line.
Definition segment_2d.h:362
bool isValid() const
check if this line segment is valid or not. origin's coodinates value have to be different from termi...
Definition segment_2d.h:156
const Segment2D & swap()
swap segment edge point
Definition segment_2d.h:215
bool onSegment(const Vector2D &p) const
strictly check if point is on segment or not
Definition segment_2d.cpp:377
double length() const
get the length of this segment
Definition segment_2d.h:194
Vector2D nearestPoint(const Vector2D &p) const
get a point on segment where distance of point is minimal.
Definition segment_2d.cpp:273
Segment2D reversedSegment() const
get the reversed line segment.
Definition segment_2d.h:238
bool existIntersection(const Segment2D &other) const
check if segments cross each other or not.
Definition segment_2d.cpp:190
AngleDeg direction() const
get the direction angle of this line segment
Definition segment_2d.h:203
bool onSegmentWeakly(const Vector2D &p) const
weakly check if point is on segment or not
Definition segment_2d.cpp:389
bool equals(const Segment2D &other) const
check if this line segment has completely same value as input line segment.
Definition segment_2d.h:268
Line2D perpendicularBisector() const
make perpendicular bisector line from segment points
Definition segment_2d.h:247
bool intersectsExceptEndpoint(const Segment2D &other) const
check if segments intersect each other on non terminal point.
Definition segment_2d.h:345
Line2D line() const
get line generated from segment
Definition segment_2d.h:185
std::ostream & print(std::ostream &os) const
print data to an output stream.
Definition segment_2d.h:415
Segment2D(const double &origin_x, const double &origin_y, const double &terminal_x, const double &terminal_y)
construct directly using raw coordinate values
Definition segment_2d.h:79
bool existIntersectionExceptEndpoint(const Segment2D &other) const
check if segments intersect each other on non terminal point.
Definition segment_2d.cpp:169
Segment2D(const Vector2D &origin, const Vector2D &terminal)
construct from 2 points
Definition segment_2d.h:66
Vector2D intersection(const Segment2D &other, const bool allow_end_point) const
check & get the intersection point with other line segment
Definition segment_2d.cpp:92
const Vector2D & origin() const
get 1st point of segment edge
Definition segment_2d.h:166
bool intersects(const Segment2D &other) const
check if segments cross each other or not.
Definition segment_2d.h:324
const Segment2D & assign(const double &origin_x, const double &origin_y, const double &terminal_x, const double &terminal_y)
construct directly using raw coordinate values
Definition segment_2d.h:124
const Segment2D & assign(const Vector2D &origin, const Vector2D &terminal)
construct from 2 points
Definition segment_2d.h:107
bool equalsWeakly(const Segment2D &other) const
check if this line segment has weakly same value as input line segment.
Definition segment_2d.h:279
const Segment2D & assign(const Vector2D &origin, const double &length, const AngleDeg &dir)
construct using origin, direction and length
Definition segment_2d.h:142
2D point vector class
Definition vector_2d.h:47
static Vector2D from_polar(const double &mag, const AngleDeg &theta)
get new Vector created by POLAR value.
Definition vector_2d.h:574
double dist(const Vector2D &p) const
get the distance from this to 'p'.
Definition vector_2d.h:366
double y
Y coordinate.
Definition vector_2d.h:65
double x
X coordinate.
Definition vector_2d.h:64
bool equals(const Vector2D &other) const
check if this vector is strictly same as given vector.
Definition vector_2d.h:531
bool equalsWeakly(const Vector2D &other) const
check if this vector is weakly same as given vector.
Definition vector_2d.h:542
2d vector class Header File.
2D straight line Header File.