xc
Plane.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 //Plane.h
22 
23 #ifndef PLANO3D_H
24 #define PLANO3D_H
25 
26 #include <cstdlib>
27 #include <cmath>
28 #include "utility/utils/misc_utils/matem.h"
29 #include "Surface3d.h"
30 #include "utility/geom/pos_vec/Pos3d.h"
31 #include "utility/geom/pos_vec/Pos3dList.h"
32 #include "../cgal_types.h"
33 #include <list>
34 
35 class Line3d;
36 class Ray3d;
37 class Segment3d;
38 class Polyline3d;
39 class Polygon3d;
40 class GeomGroup3d;
42 class Rect2d3dCooSys;
43 class Ref2d3d;
44 
45 
47 //
49 class Plane : public Surface3d
50  {
51  CGPlane_3 cgp;
52  public:
53 
54  typedef enum{AHEAD,BEHIND,INSIDE,CROSSES} polygon_classification;
55 
56  protected:
57  static polygon_classification clfpnt2clfpol(const CGAL::Oriented_side os);
58  public:
59  Plane(void);
60  explicit Plane(const CGPlane_3 &cgp);
61  Plane(const Pos3d &p1,const Pos3d &p2,const Pos3d &p3);
62  Plane(const Pos3d &o,const Vector3d &v);
63  Plane(const Pos3d &p,const Vector3d &v1,const Vector3d &v2);
64  Plane(const Line3d &r,const Pos3d &p);
65  Plane(const Segment3d &s,const Vector3d &v);
66  explicit Plane(const Polygon3d &trg);
67  explicit Plane(const GeneralEquationOfPlane &eg);
68  explicit Plane(const GeomObj3d::list_Pos3d &lp);
69  explicit Plane(const boost::python::list &);
70  template <typename InputIterator>
71  Plane(InputIterator begin,InputIterator end);
72 
73  virtual bool operator==(const Plane &) const;
74 
75  virtual GeomObj *getCopy(void) const;
76  const CGPlane_3 &ToCGAL(void) const
77  { return cgp; }
78  void ThreePoints(const Pos3d &p1,const Pos3d &p2,const Pos3d &p3);
79  virtual GEOM_FT GetMax(unsigned short int) const
80  { return NAN; }
81  virtual GEOM_FT GetMin(unsigned short int i) const
82  { return NAN; }
83  Pos3d Projection(const Pos3d &) const;
84  Vector3d Projection(const Vector3d &) const;
85  Line3d Projection(const Line3d &) const;
86  Ray3d Projection(const Ray3d &) const;
87  Segment3d Projection(const Segment3d &) const;
89  boost::python::list Projection(const boost::python::list &) const;
90  //Polygon3d Projection(const Polygon3d &) const;
91 
92  virtual Vector3d Normal(void) const;
93  Vector3d Base1(void) const;
94  Vector3d Base2(void) const;
95  Rect2d3dCooSys getCooSys(void) const;
96  Ref2d3d getRef(void) const;
97 
98  void swap(void);
99  Plane getSwap(void) const;
100 
101  bool positiveSide(const Pos3d &p) const;
102  bool negativeSide(const Pos3d &p) const;
103  int getSide(const Pos3d &p) const;
104  virtual bool In(const Pos3d &p, const GEOM_FT &tol= 0.0) const;
105 
106  CGAL::Oriented_side ClassifyPoint(const Pos3d &p) const;
107 
108  template <typename InputIterator>
109  polygon_classification ClassifyPoints(InputIterator first,InputIterator last) const;
110  polygon_classification classifyPolygon(const Polygon3d &pol) const;
111 
112  GEOM_FT PseudoDist(const Pos3d &p) const;
113  GEOM_FT PseudoDist2(const Pos3d &p) const;
114  virtual GEOM_FT dist2(const Pos3d &p) const;
115  virtual GEOM_FT dist(const Pos3d &p) const;
117  void Put(const Pos3d &p1,const Pos3d &p2,const Pos3d &p3)
118  { ThreePoints(p1,p2,p3); }
119 
121  void GeneralEquation(const GeneralEquationOfPlane &eq);
122  GEOM_FT x(const Pos2d &p) const;
123  GEOM_FT y(const Pos2d &p) const;
124  GEOM_FT z(const Pos2d &p) const;
125 
126  virtual Pos3d getCenterOfMass(void) const;
127  Pos3d Point(void) const;
129  inline virtual GEOM_FT Ix(void) const
130  { return NAN; }
132  inline virtual GEOM_FT Iy(void) const
133  { return NAN; }
135  inline virtual GEOM_FT Iz(void) const
136  { return NAN; }
137 
138  Line3d XYTrace(void) const;
139  Line3d XZTrace(void) const;
140  Line3d YZTrace(void) const;
141  Line3d getMaximumSlopeLineXY(void) const;
142  Line3d getMaximumSlopeLineXZ(void) const;
143  Line3d getMaximumSlopeLineYZ(void) const;
144 
145  bool intersects(const Plane &) const;
146  Line3d getIntersection(const Plane &) const;
147  bool intersects(const Line3d &) const;
148  Pos3d getIntersection(const Line3d &) const;
149  bool intersects(const Ray3d &) const;
150  Pos3d getIntersection(const Ray3d &) const;
151  bool intersects(const Segment3d &) const;
152  Pos3d getIntersection(const Segment3d &) const;
153  bool intersects(const Polyline3d &) const;
154  list_Pos3d getIntersection(const Polyline3d &) const;
155  bool intersects(const Polygon3d &) const;
156  Segment3d getIntersection(const Polygon3d &) const;
157 
158  GEOM_FT getAngle(const Plane &) const;
159  GEOM_FT getAngle(const Vector3d &) const;
160  GEOM_FT getAngle(const Line3d &) const;
161  GEOM_FT getAngle(const Ray3d &) const;
162  GEOM_FT getAngle(const Segment3d &) const;
163  GEOM_FT getSlopeAngleXY(void) const;
164  GEOM_FT getSlopeAngleXZ(void) const;
165  GEOM_FT getSlopeAngleYZ(void) const;
166 
168  template <typename InputIterator>
169  GEOM_FT linearLeastSquaresFitting(InputIterator begin,InputIterator end);
170 
171  friend Plane FromCGAL(const CGPlane_3 &);
172  void Print(std::ostream &os) const;
173  };
174 
175 Plane FromCGAL(const CGPlane_3 &);
176 
177 const Plane XYPlane3d(Pos3d(0,0,0),Pos3d(1,0,0),Pos3d(0,1,0));
178 const Plane XZPlane3d(Pos3d(0,0,0),Pos3d(1,0,0),Pos3d(0,0,1));
179 const Plane YZPlane3d(Pos3d(0,0,0),Pos3d(0,1,0),Pos3d(0,0,1));
180 
181 GEOM_FT angle(const Line3d &r,const Plane &);
182 GEOM_FT angle(const Plane &p,const Line3d &);
183 GEOM_FT angle(const Vector3d &,const Plane &);
184 GEOM_FT angle(const Plane &,const Vector3d &);
185 GEOM_FT angle(const Plane &,const Plane &);
186 
187 
188 Plane perpendicular(const Line3d &r, const Pos3d &);
189 
190 bool parallel(const Plane &p, const Line3d &r);
191 bool parallel(const Plane &p1, const Plane &p2);
192 
193 
194 Line3d intersection_line(const Plane &, const Plane &);
195 Pos3d intersection_point(const Plane &, const Plane &,const Plane &);
196 GeomGroup3d intersection(const Plane &p1, const Plane &p2);
197 GeomObj3d::list_Pos3d intersection(const Plane &p, const Line3d &r);
198 GeomObj3d::list_Pos3d intersection(const Line3d &r, const Plane &p);
199 GeomObj3d::list_Pos3d intersection(const Plane &p, const Ray3d &r);
200 GeomObj3d::list_Pos3d intersection(const Ray3d &r, const Plane &p);
201 GeomObj3d::list_Pos3d intersection(const Plane &p, const Segment3d &);
202 GeomObj3d::list_Pos3d intersection(const Segment3d &, const Plane &p);
203 GeomObj3d::list_Pos3d intersection(const Plane &p, const Polyline3d &r);
204 GeomObj3d::list_Pos3d intersection(const Polyline3d &r, const Plane &p);
205 Pos3d intersection_point(const Plane &p, const Line3d &r);
206 Pos3d intersection_point(const Line3d &r, const Plane &p);
207 Pos3d intersection_point(const Plane &p, const Ray3d &r);
208 Pos3d intersection_point(const Ray3d &r, const Plane &p);
209 Pos3d intersection_point(const Plane &p, const Segment3d &r);
210 Pos3d intersection_point(const Segment3d &r, const Plane &p);
211 
212 GeomObj3d::list_Pos3d intersection_points(const std::deque<Plane> &);
213 
214 template <typename InputIterator>
215 Plane::polygon_classification Plane::ClassifyPoints(InputIterator first,InputIterator last) const
216  {
217  InputIterator i= first;
218  CGAL::Oriented_side cf_pinic= ClassifyPoint(*i); i++;
219  for(;i!=last;i++)
220  {
221  const CGAL::Oriented_side cf_point= ClassifyPoint(*i);
222  if(cf_point!=cf_pinic)
223  {
224  if(cf_pinic!=CGAL::ON_ORIENTED_BOUNDARY) //Start point out of plane.
225  if(cf_point!=CGAL::ON_ORIENTED_BOUNDARY) //Point *i out of plane.
226  return CROSSES;
227  else //Point *i inside the plane.
228  continue;
229  else //Start point inside the plane.
230  if(cf_point!=CGAL::ON_ORIENTED_BOUNDARY) //Point *i out of plane.
231  {
232  cf_pinic= cf_point;
233  continue;
234  }
235  else //Point *i inside the plane.
236  continue;
237  }
238  }
239  //Si llegamos hasta aquí es que todos tienen la
240  //misma clasificación.
241  return clfpnt2clfpol(cf_pinic);
242  }
243 
245 template <typename InputIterator>
246 GEOM_FT Plane::linearLeastSquaresFitting(InputIterator begin,InputIterator end)
247  {
248  Pos3dList pointList(begin,end);
249  GEOM_FT quality(0.0);
250  cgp= pointList.linearLeastSquaresFittingPlane(quality);
251  return quality;
252  }
253 template <typename InputIterator>
254 Plane::Plane(InputIterator begin,InputIterator end)
255  : Surface3d(), cgp()
256  {
257  const size_t sz= std::distance(begin,end); // number of elements.
258  if(sz<3)
259  {
260  std::cerr << getClassName() << "::" << __FUNCTION__
261  << "; the list must contain at least three points."
262  << std::endl;
263  }
264  else if(sz==3)
265  {
266  const InputIterator i= begin;
267  const Pos3d &pi= *i;
268  InputIterator j= i;j++;
269  const Pos3d &pj= *j;
270  InputIterator k= j; k++;
271  const Pos3d &pk= *k;
272  ThreePoints(pi,pj,pk);
273  }
274  else
275  linearLeastSquaresFitting(begin,end);
276  }
277 
278 /* class Ray; */
279 
280 /* GeomObj3d::list_Pos3d intersection(const Plane &p, const Ray3d &sr); */
281 /* inline GeomObj3d::list_Pos3d intersection(const Ray3d &sr, const Plane &p) */
282 /* { return intersection(p,sr); } */
283 
284 
285 #endif
286 
Line3d XZTrace(void) const
Return the trace on the XZ plane.
Definition: Plane.cc:498
bool intersects(const Plane &) const
Returns true if intersection exists.
Definition: Plane.cc:365
Ref2d3d getRef(void) const
Return a reference frame whose XY plane corresponds to this one.
Definition: Plane.cc:166
Base class for surfaces in a three-dimensional space.
Definition: Surface3d.h:35
Line3d getMaximumSlopeLineXZ(void) const
Return the maximum slope line with respect to the XZ plane.
Definition: Plane.cc:520
Plane polygon in a 3D space.
Definition: Polygon3d.h:35
GEOM_FT y(const Pos2d &p) const
Return the y coordinate of the point of the plane such: x = p.x() z = p.y()
Definition: Plane.cc:349
Segment en tres dimensiones.
Definition: Segment3d.h:41
Base class for position lists.
Definition: PolyPos.h:35
GeneralEquationOfPlane getGeneralEquation(void) const
Returns the plane equation in general form: ax + by + cz + d = 0.
Definition: Plane.cc:315
Posición en dos dimensiones.
Definition: Pos2d.h:41
Two-dimensional rectangular coordinate system definde in a three-dimensional space.
Definition: Rect2d3dCooSys.h:36
GEOM_FT PseudoDist2(const Pos3d &p) const
Return the signed squared distance from the point.
Definition: Plane.cc:302
Vector3d Base1(void) const
Return un vector ortogonal al devuelto por Normal().
Definition: Plane.cc:145
Polyline in a three-dimensional space.
Definition: Polyline3d.h:37
Plane in a three-dimensional space.
Definition: Plane.h:49
Rect2d3dCooSys getCooSys(void) const
Return a coordinate system whose XY plane corresponds to this one.
Definition: Plane.cc:161
Line3d getIntersection(const Plane &) const
Returns (if exists) the intersection with the plane argument.
Definition: Plane.cc:369
List of 3D positions.
Definition: Pos3dList.h:36
GEOM_FT z(const Pos2d &p) const
Return the z coordinate of the point of the plane such: x = p.x() y = p.y()
Definition: Plane.cc:356
virtual Pos3d getCenterOfMass(void) const
Return the position of the centroid.
Definition: Plane.cc:320
GEOM_FT getSlopeAngleXY(void) const
Returns the slope angle with respect to the XY plane.
Definition: Plane.cc:568
static polygon_classification clfpnt2clfpol(const CGAL::Oriented_side os)
Converts the point classification to the polygon one.
Definition: Plane.cc:116
Ray in a three-dimensional space.
Definition: Ray3d.h:36
Line3d XYTrace(void) const
Return the trace on the XY plane.
Definition: Plane.cc:494
virtual std::string getClassName(void) const
Returns demangled class name.
Definition: EntityWithOwner.cc:90
Line3d getMaximumSlopeLineYZ(void) const
Return the maximum slope line with respect to the YZ plane.
Definition: Plane.cc:534
void GeneralEquation(const GeneralEquationOfPlane &eq)
Creates a Plane object from the its equation in general form: general ax + by + cz + d = 0...
Definition: Plane.cc:361
virtual GEOM_FT Iy(void) const
Moment of inertia with respect to the center of mass in local coordinates.
Definition: Plane.h:132
virtual GEOM_FT dist(const Pos3d &p) const
Return the distance from the point to this surface.
Definition: Plane.cc:290
GEOM_FT getAngle(const Plane &) const
Return the angle between this plane and the argument.
Definition: Plane.cc:548
Line3d YZTrace(void) const
Return the trace on the YZ plane.
Definition: Plane.cc:502
GEOM_FT getSlopeAngleYZ(void) const
Returns the slope angle with respect to the YZ plane.
Definition: Plane.cc:576
virtual GEOM_FT Iz(void) const
Moment of inertia with respect to the center of mass in local coordinates.
Definition: Plane.h:135
GEOM_FT PseudoDist(const Pos3d &p) const
Return the signed distance from the point.
Definition: Plane.cc:294
int getSide(const Pos3d &p) const
Return 1 if point in positive side, -1 if in negative side or 0 otherwise.
Definition: Plane.cc:265
Vector3d Base2(void) const
Return un vector ortogonal al devuelto por Normal() y al devuelto por Base1().
Definition: Plane.cc:152
virtual GEOM_FT Ix(void) const
Moment of inertia with respect to the center of mass in local coordinates.
Definition: Plane.h:129
virtual GEOM_FT GetMax(unsigned short int) const
Return the maximum value of the i-th coordinate.
Definition: Plane.h:79
Equation for a plane in general form: ax + by + cz + d = 0 where v[0]= d, v[1]= a, v[2]= b, v[3]= c.
Definition: GeneralEquationOfPlane.h:36
Pos3d Projection(const Pos3d &) const
Return the projection of the point onto this plane.
Definition: Plane.cc:176
Posición en tres dimensiones.
Definition: Pos3d.h:44
void Put(const Pos3d &p1, const Pos3d &p2, const Pos3d &p3)
Set the points that define the plane.
Definition: Plane.h:117
Line in a three-dimensional space.
Definition: Line3d.h:62
Pos3d Point(void) const
Return an arbitrary point on the plane.
Definition: Plane.cc:255
GEOM_FT x(const Pos2d &p) const
Return the x coordinate of the point of the plane such: y = p.x() z = p.y()
Definition: Plane.cc:342
virtual bool operator==(const Plane &) const
Comparison operator.
Definition: Plane.cc:101
Clase base para las entidades geométricas.
Definition: GeomObj.h:40
Group of 3D entities.
Definition: GeomGroup3d.h:36
Line3d getMaximumSlopeLineXY(void) const
Return the maximum slope line with respect to the XY plane.
Definition: Plane.cc:506
virtual GEOM_FT GetMin(unsigned short int i) const
Return the minimum value of the i-th coordinate.
Definition: Plane.h:81
virtual bool In(const Pos3d &p, const GEOM_FT &tol=0.0) const
Return true if the point is in the plane.
Definition: Plane.cc:278
GEOM_FT linearLeastSquaresFitting(const GeomObj3d::list_Pos3d &lp)
Compute the plane that best suits the point cloud.
Definition: Plane.cc:580
virtual GEOM_FT dist2(const Pos3d &p) const
Return the squared distance from the point.
Definition: Plane.cc:311
Vector en tres dimensiones.
Definition: Vector3d.h:39
Two-dimensional reference system defined in a three-dimensional space.
Definition: Ref2d3d.h:41
virtual Vector3d Normal(void) const
Return el normal vector oriented to the "positive" side.
Definition: Plane.cc:138
GEOM_FT getSlopeAngleXZ(void) const
Returns the slope angle with respect to the XZ plane.
Definition: Plane.cc:572