xc
Segment2d.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // xc utils library; general purpose classes and functions.
4 //
5 // Copyright (C) Luis C. Pérez Tato
6 //
7 // XC utils is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This software is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program.
19 // If not, see <http://www.gnu.org/licenses/>.
20 //----------------------------------------------------------------------------
21 //Segment2d.h
22 
23 #ifndef SEGMENT2D_H
24 #define SEGMENT2D_H
25 
26 #include "Linear2d.h"
27 #include "Line2d.h"
28 #include "Ray2d.h"
29 #include "../pos_vec/Pos2d.h"
30 
31 class Dir2d;
32 class VectorPos2d;
33 class Polyline2d;
34 
36 //
38 class Segment2d : public Linear2d
39  {
40  CGSegment_2 cgseg;
41  public:
42  Segment2d(void): Linear2d(),cgseg(CGPoint_2(0,0),CGPoint_2(1,0)) {}
43  explicit Segment2d(const CGSegment_2 &r)
44  : Linear2d(), cgseg(r) {}
45  Segment2d(const Pos2d &p1,const Pos2d &p2);
46 
47  virtual bool operator==(const Segment2d &) const;
48  const CGSegment_2 &ToCGAL(void) const
49  { return cgseg; }
50  virtual GeomObj *getCopy(void) const;
51  void swap(void);
52  void TwoPoints(const Pos2d &p1,const Pos2d &p2);
53  virtual GEOM_FT GetMax(unsigned short int i) const
54  { return std::max(getFromPoint()(i),getToPoint()(i)); }
55  virtual GEOM_FT GetMin(unsigned short int i) const
56  { return std::min(getFromPoint()(i),getToPoint()(i)); }
57  Line2d getSupportLine(void) const
58  { return Line2d(cgseg.supporting_line()); }
59  Pos2d getFromPoint(void) const
60  { return Pos2d(cgseg.source()); }
61  Pos2d getToPoint(void) const
62  { return Pos2d(cgseg.target()); }
63  GEOM_FT getSlope(void) const;
64  bool upwards(void) const;
65  bool downwards(void) const;
66  const Pos2d Point(const int &i) const
67  { return Pos2d(cgseg.point(i)); }
68  Pos2d PtoParametricas(const GEOM_FT &) const;
69  Pos2d getMidPoint(void) const;
70  double getParamNaturalCoord(const GEOM_FT &) const;
71  Pos2d getPointNaturalCoord(const GEOM_FT &) const;
72  inline bool EsVertical(void) const
73  { return cgseg.is_vertical(); }
74  inline bool isDegenerated(void) const
75  { return cgseg.is_degenerate(); }
76  virtual bool In(const Pos2d &, const double &tol= 0.0) const;
77  virtual GEOM_FT dist2(const Pos2d &p) const;
78  GEOM_FT dist(const Pos2d &p) const;
80  void Put(const Pos2d &p1,const Pos2d &p2)
81  { TwoPoints(p1,p2); }
82 
87  { return getSupportLine().Ordena(ptos); }
88 
89  Segment2d offset(const Vector2d &v) const;
90  Segment2d offset(const GEOM_FT &d) const;
91  Polygon2d getBufferPolygon(const GEOM_FT &d, const size_t &numVertices= 8) const;
92 
93  inline bool intersects(const Segment2d &r2) const
94  { return do_intersect(cgseg,r2.cgseg); }
95  bool intersects(const Line2d &r) const;
96  bool intersects(const Ray2d &sr) const;
97  GeomObj2d::list_Pos2d getIntersection(unsigned short int, const double &) const;
101  bool connected(const Pos2d &, const GEOM_FT &tol= 0.0) const;
102 
103  Pos2d Projection(const Pos2d &) const;
104  Vector2d Projection(const Vector2d &) const;
105 
106  Line2d Perpendicular(const Pos2d &p) const;
107  Line2d isParallel(const Pos2d &v) const;
108  Line2d getPerpendicularBisector(void) const;
109 
110  bool isParallel(const Line2d &r) const
111  { return parallel(getSupportLine(),r); }
112  bool isParallel(const Ray2d &sr) const
113  { return parallel(getSupportLine(),sr); }
114  bool isParallel(const Segment2d &r) const
115  { return parallel(getSupportLine(),r.getSupportLine()); }
116 
117  virtual GEOM_FT getLength(void) const;
118  virtual Pos2d getCenterOfMass(void) const;
119  Dir2d GetDir(void) const;
120  Vector2d VDir(void) const;
121  Vector2d Normal(void) const;
122  Vector2d GetVector(void) const;
123  virtual Vector2d getIVector(void) const;
124  virtual Vector2d getJVector(void) const;
125  GEOM_FT getAngle(const Vector2d &v) const;
126  GEOM_FT getSignedAngle(const Vector2d &v) const;
127  GEOM_FT getAngle(const Line2d &r) const;
128  GEOM_FT getAngle(const Ray2d &sr) const;
129  GEOM_FT getAngle(const Segment2d &v) const;
130  GEOM_FT getSignedAngle(const Segment2d &v) const;
131  GEOM_FT XAxisAngle(void) const;
132  GEOM_FT YAxisAngle(void) const;
133  inline GEOM_FT Azimuth(void) const
134  { return getSupportLine().Azimuth(); }
135 
137  inline virtual GEOM_FT Ix(void) const
138  { return 0.0; }
140  inline virtual GEOM_FT Iy(void) const
141  { return NAN; }
143  inline virtual GEOM_FT Pxy(void) const
144  { return NAN; }
146  inline virtual GEOM_FT Iz(void) const
147  { return NAN; }
148 
149  VectorPos2d Divide(int num_partes) const;
150  boost::python::list DividePy(int num_partes) const;
151  VectorPos2d Divide(const std::vector<double> &) const;
152  boost::python::list DividePy(const boost::python::list &) const;
153  boost::python::list split(const Pos2d &) const;
154 
155  boost::python::dict getPyDict(void) const;
156  void setPyDict(const boost::python::dict &);
157 
158  void Transform(const Trf2d &trf2d);
159 
160  void Print(std::ostream &os) const;
161  };
162 
163 inline GEOM_FT dist2(const Pos2d &p,const Segment2d &r)
164  { return r.dist2(p); }
165 inline GEOM_FT dist2(const Segment2d &r,const Pos2d &p)
166  { return dist2(p,r); }
167 GEOM_FT dist(const Pos2d &p,const Segment2d &r);
168 inline GEOM_FT dist(const Segment2d &r,const Pos2d &p)
169  { return dist(p,r); }
170 
171 GEOM_FT angle(const Segment2d &,const Vector2d &);
172 GEOM_FT angle(const Vector2d &,const Segment2d &);
173 
174 inline bool parallel(const Segment2d &sg,const Line2d &r)
175  { return sg.isParallel(r); }
176 inline bool parallel(const Line2d &r, const Segment2d &sg)
177  { return parallel(sg,r); }
178 inline bool parallel(const Segment2d &sg,const Ray2d &sr)
179  { return sg.isParallel(sr); }
180 inline bool parallel(const Ray2d &sr, const Segment2d &sg)
181  { return parallel(sg,sr); }
182 inline bool parallel(const Segment2d &r1,const Segment2d &r2)
183  { return r1.isParallel(r2); }
184 
185 inline bool intersecan(const Segment2d &sg,const Line2d &r)
186  { return sg.intersects(r); }
187 inline bool intersecan(const Line2d &r,const Segment2d &sg)
188  { return sg.intersects(r); }
189 inline bool intersecan(const Segment2d &sg,const Ray2d &sr)
190  { return sg.intersects(sr); }
191 inline bool intersecan(const Ray2d &sr,const Segment2d &sg)
192  { return sg.intersects(sr); }
193 inline bool intersecan(const Segment2d &r1,const Segment2d &r2)
194  { return r1.intersects(r2); }
195 inline GeomObj2d::list_Pos2d intersection(const Segment2d &sg,const Line2d &r)
196  { return sg.getIntersection(r); }
197 inline GeomObj2d::list_Pos2d intersection(const Line2d &r,const Segment2d &sg)
198  { return sg.getIntersection(r); }
199 inline GeomObj2d::list_Pos2d intersection(const Segment2d &sg,const Ray2d &sr)
200  { return sg.getIntersection(sr); }
201 inline GeomObj2d::list_Pos2d intersection(const Ray2d &sr,const Segment2d &sg)
202  { return sg.getIntersection(sr); }
203 inline GeomObj2d::list_Pos2d intersection(const Segment2d &sg1,const Segment2d &sg2)
204  { return sg1.getIntersection(sg2); }
205 
206 inline bool colineales(const Segment2d &sg,const Line2d &r)
207  { return colineales(sg.getSupportLine(),r); }
208 inline bool colineales(const Line2d &r,const Segment2d &sg)
209  { return colineales(sg,r); }
210 inline bool colineales(const Segment2d &sg,const Ray2d &sr)
211  { return colineales(sg.getSupportLine(),sr); }
212 inline bool colineales(const Ray2d &sr,const Segment2d &sg)
213  { return colineales(sg,sr); }
214 inline bool colineales(const Segment2d &sg1,const Segment2d &sg2)
215  { return colineales(sg1,sg2.getSupportLine()); }
216 
217 Pos2d intersection_point(const Segment2d &, const Line2d &);
218 Pos2d intersection_point(const Line2d &, const Segment2d &);
219 Pos2d intersection_point(const Segment2d &, const Segment2d &);
220 
221 inline Line2d perpendicular_bisector(const Segment2d &sg)
222  { return sg.getPerpendicularBisector(); }
223 
224 std::list<Segment2d> without_degenerated(const std::list<Segment2d> &);
225 
226 
227 std::list<Polyline2d> get_polylines(const std::list<Segment2d> &, const GEOM_FT &tol);
228 boost::python::list py_get_2d_polylines(const boost::python::list &, const GEOM_FT &tol);
229 
230 #endif
Vector2d GetVector(void) const
Return the vector from origin to the segment end point.
Definition: Segment2d.cc:119
Vector2d VDir(void) const
Return the direction vector of the segment.
Definition: Segment2d.cc:93
GEOM_FT YAxisAngle(void) const
Return the angle of the segment con el Y axis.
Definition: Segment2d.cc:152
Pos2d Projection(const Pos2d &) const
Return the orthogonal projection onto the line.
Definition: Segment2d.cc:502
Line2d getPerpendicularBisector(void) const
Return the perpendicular_bisector of the segment.
Definition: Segment2d.cc:225
Line2d isParallel(const Pos2d &v) const
Return the line parallel to the segment through p.
Definition: Segment2d.cc:179
Dirección en dos dimensiones.
Definition: Dir2d.h:36
virtual GEOM_FT dist2(const Pos2d &p) const
Return the squared distance to the segment.
Definition: Segment2d.cc:260
virtual GEOM_FT Pxy(void) const
Product of inertia.
Definition: Segment2d.h:143
Base class for position lists.
Definition: PolyPos.h:35
virtual Pos2d getCenterOfMass(void) const
Return the position of the center of mass of the segment.
Definition: Segment2d.cc:166
Posición en dos dimensiones.
Definition: Pos2d.h:41
GeomObj2d::list_Pos2d getIntersection(unsigned short int, const double &) const
Return the intersection of the line with a plane defined by the equation coord_i=cte.
Definition: Segment2d.cc:309
virtual GEOM_FT getLength(void) const
Return object length.
Definition: Segment2d.cc:162
virtual GEOM_FT Iz(void) const
Moment of inertia with respect to the center of mass en local axis.
Definition: Segment2d.h:146
Line in a two-dimensional space.
Definition: Line2d.h:61
virtual GEOM_FT Ix(void) const
Moment of inertia with respect to the center of mass en local axis.
Definition: Segment2d.h:137
void Transform(const Trf2d &trf2d)
Applies to the segment the transformation argument.
Definition: Segment2d.cc:616
virtual Vector2d getJVector(void) const
Returns a vector in the direction of the local Y axis.
Definition: Segment2d.cc:111
virtual GEOM_FT Iy(void) const
Moment of inertia with respect to the center of mass en local axis.
Definition: Segment2d.h:140
Line2d Perpendicular(const Pos2d &p) const
Return the line perpendicular to the segment through p.
Definition: Segment2d.cc:175
Base class for two-dimensional transformations.
Definition: Trf2d.h:40
bool connected(const Pos2d &, const GEOM_FT &tol=0.0) const
Return true if the segment is connected to the argument point (i.
Definition: Segment2d.cc:487
GEOM_FT getSlope(void) const
Return the angle or the line with respect to XY plane.
Definition: Segment2d.cc:77
Vector en dos dimensiones.
Definition: Vector2d.h:40
Vector de posiciones en dos dimensiones [(x1,y1),(x1,y2),...].
Definition: VectorPos2d.h:33
GEOM_FT getSignedAngle(const Vector2d &v) const
Return the angle of the segment with the vector (-PI->0->PI).
Definition: Segment2d.cc:135
double getParamNaturalCoord(const GEOM_FT &) const
Return the parametric coordinate that corresponds to the natural coordinate argument.
Definition: Segment2d.cc:294
Dir2d GetDir(void) const
Return the direction of the segment.
Definition: Segment2d.cc:89
void swap(void)
@ brief Swaps the ray orientation.
Definition: Segment2d.cc:67
Pos2d PtoParametricas(const GEOM_FT &) const
Return a point of the segment at a distance lambda from its origin.
Definition: Segment2d.cc:285
virtual GEOM_FT GetMin(unsigned short int i) const
Return the minimum value of the i-th coordinate.
Definition: Segment2d.h:55
Polígono en dos dimensiones.
Definition: Polygon2d.h:38
Segment in a two-dimensional space.
Definition: Segment2d.h:38
virtual GEOM_FT GetMax(unsigned short int i) const
Return the maximum value of the i-th coordinate.
Definition: Segment2d.h:53
virtual Vector2d getIVector(void) const
Returns a vector in the direction of the local X axis.
Definition: Segment2d.cc:102
double Azimuth(void) const
Return the angle con el Y axis.
Definition: Line2d.cc:587
Segment2d offset(const Vector2d &v) const
Return the a copy of the segment translated along the vector argument.
Definition: Segment2d.cc:184
bool upwards(void) const
Return true if the segment goes up.
Definition: Segment2d.cc:81
Polygon2d getBufferPolygon(const GEOM_FT &d, const size_t &numVertices=8) const
Return a buffer polygon around the segment.
Definition: Segment2d.cc:206
VectorPos2d Divide(int num_partes) const
Return the points that results from the segment division.
Definition: Segment2d.cc:522
GeomObj::list_Pos2d Ordena(const GeomObj::list_Pos2d &ptos) const
Return the points ordered by the value of the parameter of its projection onto the line from lowest t...
Definition: Line2d.cc:380
void setPyDict(const boost::python::dict &)
Set the values of the object members from a Python dictionary.
Definition: Segment2d.cc:607
virtual bool In(const Pos2d &, const double &tol=0.0) const
Return true if the point is on the segment.
Definition: Segment2d.cc:234
virtual bool operator==(const Segment2d &) const
Comparison operator.
Definition: Segment2d.cc:52
GEOM_FT XAxisAngle(void) const
Return the angle of the segment con el X axis.
Definition: Segment2d.cc:148
Pos2d getMidPoint(void) const
Return the mid point of the segment.
Definition: Segment2d.cc:289
Vector2d Normal(void) const
Return the direction vector of the segment.
Definition: Segment2d.cc:97
void Put(const Pos2d &p1, const Pos2d &p2)
Set the points that define the segment.
Definition: Segment2d.h:80
boost::python::list split(const Pos2d &) const
Return the two segments that result from splitting this one on the point nearest to the argument...
Definition: Segment2d.cc:588
boost::python::dict getPyDict(void) const
Return a Python dictionary with the object members values.
Definition: Segment2d.cc:598
Polyline in a two-dimensional space.
Definition: Polyline2d.h:41
GEOM_FT dist(const Pos2d &p) const
Return the distance from the point to the segment.
Definition: Segment2d.cc:281
boost::python::list DividePy(int num_partes) const
Return a Python list containing the points that results from the segment division.
Definition: Segment2d.cc:529
Clase base para las entidades geométricas.
Definition: GeomObj.h:40
bool downwards(void) const
Return true if the segment goes down.
Definition: Segment2d.cc:85
Ray in a two-dimensional space.
Definition: Ray2d.h:35
Base class for the objects involving a single dimension in a two-dimensional space.
Definition: Linear2d.h:34
GeomObj::list_Pos2d Ordena(const GeomObj::list_Pos2d &ptos) const
Returns the points ordered by the value of the parameter of its projection onto the line from lowest ...
Definition: Segment2d.h:86
Pos2d getPointNaturalCoord(const GEOM_FT &) const
Return the point of the segment with the natural coordinate being passed as parameter.
Definition: Segment2d.cc:299
GEOM_FT getAngle(const Vector2d &v) const
Return the angle of the segment with the vector (0->2PI).
Definition: Segment2d.cc:123