Salome HOME
Task 2.3: Creation of Intersection
[modules/shaper.git] / src / GeomAPI / GeomAPI_Edge.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include<GeomAPI_Edge.h>
22 #include<GeomAPI_Pln.h>
23 #include<GeomAPI_Pnt.h>
24 #include<GeomAPI_Circ.h>
25 #include<GeomAPI_Dir.h>
26 #include<GeomAPI_Lin.h>
27 #include<GeomAPI_Ax2.h>
28 #include<GeomAPI_Ellipse.h>
29
30 #include <BRepAdaptor_Curve.hxx>
31
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRep_Tool.hxx>
37 #include <Geom_Curve.hxx>
38 #include <Geom_Line.hxx>
39 #include <Geom_Circle.hxx>
40 #include <Geom_Ellipse.hxx>
41 #include <Geom_Plane.hxx>
42 #include <GeomAPI_IntCS.hxx>
43 #include <GeomAdaptor_Curve.hxx>
44 #include <gp_Ax1.hxx>
45 #include <gp_Pln.hxx>
46 #include <gp_Elips.hxx>
47 #include <TopExp.hxx>
48
49 #include <GCPnts_AbscissaPoint.hxx>
50
51 GeomAPI_Edge::GeomAPI_Edge()
52 {
53   TopoDS_Edge* anEdge = new TopoDS_Edge;
54
55   BRep_Builder aBuilder;
56   aBuilder.MakeEdge(*anEdge);
57
58   setImpl(anEdge);
59 }
60
61 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
62 {
63   if (!theShape->isNull() && theShape->isEdge()) {
64     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
65   }
66 }
67
68 bool GeomAPI_Edge::isLine() const
69 {
70   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
71   double aFirst, aLast;
72   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
73   if (aCurve.IsNull()) // degenerative edge
74     return false;
75   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
76     return true;
77   return false;
78 }
79
80 bool GeomAPI_Edge::isCircle() const
81 {
82   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
83   double aFirst, aLast;
84   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
85   if (aCurve.IsNull()) // degenerative edge
86     return false;
87   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
88   {
89     // Check the difference of first and last parameters to be equal to the curve period
90     if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
91       return true;
92   }
93   return false;
94 }
95
96 bool GeomAPI_Edge::isArc() const
97 {
98   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
99   double aFirst, aLast;
100   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
101   if (aCurve.IsNull()) // degenerative edge
102     return false;
103   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
104   {
105     // Check the difference of first and last parameters is not equal the curve period
106     if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
107       return true;
108   }
109   return false;
110 }
111
112 bool GeomAPI_Edge::isEllipse() const
113 {
114   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
115   double aFirst, aLast;
116   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
117   if (aCurve.IsNull()) // degenerative edge
118     return false;
119   if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
120     return true;
121   return false;
122 }
123
124 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
125 {
126   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
127   double aFirst, aLast;
128   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
129   gp_Pnt aPoint;
130   aCurve->D0(aFirst, aPoint);
131   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
132 }
133
134 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
135 {
136   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
137   double aFirst, aLast;
138   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
139   gp_Pnt aPoint;
140   aCurve->D0(aLast, aPoint);
141   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
142 }
143
144 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
145 {
146   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
147   double aFirst, aLast;
148   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
149   if (!aCurve.IsNull()) {
150     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
151     if (!aCirc.IsNull()) {
152       gp_Pnt aLoc = aCirc->Location();
153       std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
154       gp_Dir anAxis = aCirc->Axis().Direction();
155       std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
156       return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
157     }
158   }
159   return std::shared_ptr<GeomAPI_Circ>(); // not circle
160 }
161
162 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
163 {
164   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
165   double aFirst, aLast;
166   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
167   if (!aCurve.IsNull()) {
168     Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
169     if (!aElips.IsNull()) {
170       gp_Elips aGpElips = aElips->Elips();
171       std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
172       aEllipse->setImpl(new gp_Elips(aGpElips));
173       return aEllipse;
174     }
175   }
176   return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
177 }
178
179 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
180 {
181   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
182   double aFirst, aLast;
183   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
184   if (aCurve) {
185     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
186     if (aLine) {
187       gp_Pnt aStartPnt = aLine->Value(aFirst);
188       std::shared_ptr<GeomAPI_Pnt> aStart(
189           new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
190       gp_Pnt aEndPnt = aLine->Value(aLast);
191       std::shared_ptr<GeomAPI_Pnt> aEnd(
192           new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
193       return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
194     }
195   }
196   return std::shared_ptr<GeomAPI_Lin>(); // not circle
197 }
198
199
200 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
201 {
202   if (!theEdge.get() || ! theEdge->isEdge())
203     return false;
204   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
205   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
206
207   if (aMyShape.IsNull() || aInShape.IsNull())
208     return false;
209
210   if (aMyShape.ShapeType() != aInShape.ShapeType())
211     return false;
212
213   double aMyStart, aMyEnd;
214   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
215   double aInStart, aInEnd;
216   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
217
218   // Check that curves a the same type
219   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
220   GeomAdaptor_Curve aInAdaptor(aInCurve);
221   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
222     return false;
223
224   // Check that end point parameters are the same
225   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
226     return false;
227
228   // Check that end points are equal
229   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
230   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
231   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
232   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
233
234   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
235     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
236     return false;
237
238   return true;
239 }
240
241 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
242 {
243   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
244   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
245 }
246
247 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
248 {
249   double aFirst, aLast;
250   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
251   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
252   if (aCurve.IsNull())
253     return false;
254
255   double A, B, C, D;
256   thePlane->coefficients(A, B, C, D);
257   gp_Pln aPlane(A, B, C, D);
258
259   bool inPlane = false;
260   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
261     // check start and end points on the plane
262     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
263     gp_Pnt aLastPnt = aCurve->Value(aLast);
264     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
265               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
266   } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
267     // check the center on the plane and normals are collinear
268     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
269     gp_Pnt aCenter = aCirc->Location();
270     Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
271     inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
272               Abs(Abs(aDot) - 1.0) < Precision::Confusion();
273   } else {
274     // three points checking
275     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
276     gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
277     gp_Pnt aLastPnt = aCurve->Value(aLast);
278     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
279               aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
280               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
281   }
282   return inPlane;
283 }
284
285 std::list<std::shared_ptr<GeomAPI_Pnt>> GeomAPI_Edge::intersectWithPlane(
286   const std::shared_ptr<GeomAPI_Pln> thePlane) const
287 {
288   std::list<GeomPointPtr> aResList;
289   double aFirst, aLast;
290   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
291   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
292   if (!aCurve.IsNull()) {
293     double A, B, C, D;
294     thePlane->coefficients(A, B, C, D);
295     gp_Pln aPln(A, B, C, D);
296
297     Handle(Geom_Plane) aPlane = new Geom_Plane(aPln);
298     GeomAPI_IntCS aIntersect;
299     aIntersect.Perform(aCurve, aPlane);
300     if (aIntersect.IsDone() && (aIntersect.NbPoints() > 0)) {
301       gp_Pnt aPnt;
302       for (int i = 1; i <= aIntersect.NbPoints(); i++) {
303         aPnt = aIntersect.Point(i);
304         std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
305         aResList.push_back(aPntPtr);
306       }
307     }
308   }
309   return aResList;
310 }
311
312 double GeomAPI_Edge::length() const
313 {
314   const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
315   BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
316   Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
317   return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
318 }
319
320 bool GeomAPI_Edge::isClosed() const
321 {
322   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
323   if (aShape.IsNull())
324     return false;
325   double aFirst, aLast;
326   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
327   if (aCurve.IsNull() || !aCurve->IsPeriodic())
328     return false;
329   aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
330
331   return fabs(aFirst - aLast) < 1.e-9;
332 }
333
334 bool GeomAPI_Edge::isDegenerated() const
335 {
336   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
337   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
338     return false;
339   return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
340 }
341
342 void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
343 {
344   TopoDS_Edge anEdge = impl<TopoDS_Edge>();
345   TopoDS_Vertex aVFirst, aVLast;
346   TopExp::Vertices(anEdge, aVFirst, aVLast);
347   BRep_Builder().UpdateVertex(aVFirst, theTolerance);
348 }
349
350 void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
351 {
352   TopoDS_Edge anEdge = impl<TopoDS_Edge>();
353   TopoDS_Vertex aVFirst, aVLast;
354   TopExp::Vertices(anEdge, aVFirst, aVLast);
355   BRep_Builder().UpdateVertex(aVLast, theTolerance);
356 }