LIBRCSC Docs
Documentation for HELIOS's BASE LIBRCSC library for RoboCup 2D Simulation League.
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
segment_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_SEGMENT2D_H
33#define RCSC_GEOM_SEGMENT2D_H
34
35#include <rcsc/geom/line_2d.h>
36#include <rcsc/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() = delete;
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 Segment2D & assign( const Vector2D & origin,
107 const Vector2D & terminal )
108 {
109 M_origin = origin;
110 M_terminal = terminal;
111 return *this;
112 }
113
122 const Segment2D & assign( const double origin_x,
123 const double origin_y,
124 const double terminal_x,
125 const double terminal_y )
126 {
127 M_origin.assign( origin_x, origin_y );
128 M_terminal.assign( terminal_x, terminal_y );
129 return *this;
130 }
131
139 const Segment2D & assign( const Vector2D & origin,
140 const double length,
141 const AngleDeg & dir )
142 {
143 M_origin = origin;
144 M_terminal = origin + Vector2D::from_polar( length, dir );
145 return *this;
146 }
147
153 bool isValid() const
154 {
155 return ! origin().equalsWeakly( terminal() );
156 }
157
162 const Vector2D & origin() const
163 {
164 return M_origin;
165 }
166
171 const Vector2D & terminal() const
172 {
173 return M_terminal;
174 }
175
180 Line2D line() const
181 {
182 return Line2D( origin(), terminal() );
183 }
184
189 double length() const
190 {
191 return origin().dist( terminal() );
192 }
193
199 {
200 return ( terminal() - origin() ).th();
201 }
202
203
204
209 const Segment2D & swap()
210 {
211 // std::swap( M_origin, M_terminal );
212 Vector2D tmp = M_origin;
213 M_origin = M_terminal;
214 M_terminal = tmp;
215 return *this;
216 }
217
223 {
224 return swap();
225 }
226
232 {
233 return Segment2D( *this ).reverse();
234 }
235
241 {
243 }
244
250 bool contains( const Vector2D & p ) const
251 {
252 return ( ( p.x - origin().x ) * ( p.x - terminal().x ) <= CALC_ERROR //EPSILON
253 && ( p.y - origin().y ) * ( p.y - terminal().y ) <= CALC_ERROR ); //EPSILON );
254 }
255
261 bool equals( const Segment2D & other ) const
262 {
263 return this->origin().equals( other.origin() )
264 && this->terminal().equals( other.terminal() );
265 }
266
272 bool equalsWeakly( const Segment2D & other ) const
273 {
274 return this->origin().equalsWeakly( other.origin() )
275 && this->terminal().equalsWeakly( other.terminal() );
276 }
277
278
285 Vector2D projection( const Vector2D & p ) const;
286
295 const bool allow_end_point ) const;
296
303 Vector2D intersection( const Line2D & l ) const;
304
310 bool existIntersection( const Segment2D & other ) const;
311
317 bool intersects( const Segment2D & other ) const
318 {
319 return existIntersection( other );
320 }
321
329 bool existIntersectionExceptEndpoint( const Segment2D & other ) const;
330
338 bool intersectsExceptEndpoint( const Segment2D & other ) const
339 {
340 return existIntersectionExceptEndpoint( other );
341 }
342
348 bool existIntersection( const Line2D & l ) const;
349
355 bool intersects( const Line2D & l ) const
356 {
357 return existIntersection( l );
358 }
359
366 Vector2D nearestPoint( const Vector2D & p ) const;
367
373 double dist( const Vector2D & p ) const;
374
380 double dist( const Segment2D & seg ) const;
381
387 double farthestDist( const Vector2D & p ) const;
388
394 bool onSegment( const Vector2D & p ) const;
395
401 bool onSegmentWeakly( const Vector2D & p ) const;
402
408 std::ostream & print( std::ostream & os ) const
409 {
410 os << '[' << origin() << '-' << terminal() << ']';
411 return os;
412 }
413
414};
415
416}
417
418#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
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:222
Vector2D projection(const Vector2D &p) const
calculates projection point from p
bool existIntersection(const Line2D &l) const
check if this line segment intersects with target line.
double dist(const Vector2D &p) const
get minimum distance between this segment and point
double farthestDist(const Vector2D &p) const
get maximum distance between this segment and point
const Segment2D & assign(const Vector2D &origin, const double length, const AngleDeg &dir)
construct using origin, direction and length
Definition: segment_2d.h:139
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:250
const Vector2D & terminal() const
get 2nd point of segment edge
Definition: segment_2d.h:171
bool intersects(const Line2D &l) const
check if this line segment intersects with target line.
Definition: segment_2d.h:355
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:153
const Segment2D & swap()
swap segment edge point
Definition: segment_2d.h:209
bool onSegment(const Vector2D &p) const
strictly check if point is on segment or not
double length() const
get the length of this segment
Definition: segment_2d.h:189
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:122
Vector2D nearestPoint(const Vector2D &p) const
get a point on segment where distance of point is minimal.
Segment2D reversedSegment() const
get the reversed line segment.
Definition: segment_2d.h:231
bool existIntersection(const Segment2D &other) const
check if segments cross each other or not.
AngleDeg direction() const
get the direction angle of this line segment
Definition: segment_2d.h:198
bool onSegmentWeakly(const Vector2D &p) const
weakly check if point is on segment or not
bool equals(const Segment2D &other) const
check if this line segment has completely same value as input line segment.
Definition: segment_2d.h:261
Line2D perpendicularBisector() const
make perpendicular bisector line from segment points
Definition: segment_2d.h:240
bool intersectsExceptEndpoint(const Segment2D &other) const
check if segments intersect each other on non terminal point.
Definition: segment_2d.h:338
Line2D line() const
get line generated from segment
Definition: segment_2d.h:180
std::ostream & print(std::ostream &os) const
print data to an output stream.
Definition: segment_2d.h:408
Segment2D(const Vector2D &origin, const double length, const AngleDeg &dir)
construct using origin, direction and length
Definition: segment_2d.h:93
bool existIntersectionExceptEndpoint(const Segment2D &other) const
check if segments intersect each other on non terminal point.
Segment2D(const Vector2D &origin, const Vector2D &terminal)
construct from 2 points
Definition: segment_2d.h:66
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
double dist(const Segment2D &seg) const
get minimum distance between 2 segments
Vector2D intersection(const Segment2D &other, const bool allow_end_point) const
check & get the intersection point with other line segment
const Vector2D & origin() const
get 1st point of segment edge
Definition: segment_2d.h:162
bool intersects(const Segment2D &other) const
check if segments cross each other or not.
Definition: segment_2d.h:317
const Segment2D & assign(const Vector2D &origin, const Vector2D &terminal)
construct from 2 points
Definition: segment_2d.h:106
bool equalsWeakly(const Segment2D &other) const
check if this line segment has weakly same value as input line segment.
Definition: segment_2d.h:272
Vector2D intersection(const Line2D &l) const
check & get the intersection point with other line
2D point vector class
Definition: vector_2d.h:46
Vector2D & assign(const double xx, const double yy)
assign XY value directly.
Definition: vector_2d.h:100
static Vector2D from_polar(const double mag, const AngleDeg &theta)
get new Vector created by POLAR value.
Definition: vector_2d.h:594
double dist(const Vector2D &p) const
get the distance from this to 'p'.
Definition: vector_2d.h:359
double y
Y coordinate.
Definition: vector_2d.h:64
double x
X coordinate.
Definition: vector_2d.h:63
bool equals(const Vector2D &other) const
check if this vector is strictly same as given vector.
Definition: vector_2d.h:551
bool equalsWeakly(const Vector2D &other) const
check if this vector is weakly same as given vector.
Definition: vector_2d.h:562
2D straight line Header File.
2d vector class Header File.