Salome HOME
01c98124c438de5cc5fd096a8412f0a68d49526d
[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 <ElCLib.hxx>
38 #include <GCPnts_UniformAbscissa.hxx>
39 #include <Geom_Curve.hxx>
40 #include <Geom_Line.hxx>
41 #include <Geom_Circle.hxx>
42 #include <Geom_Ellipse.hxx>
43 #include <Geom_Plane.hxx>
44 #include <GeomAPI_IntCS.hxx>
45 #include <GeomAdaptor_Curve.hxx>
46 #include <gp_Ax1.hxx>
47 #include <gp_Pln.hxx>
48 #include <gp_Elips.hxx>
49 #include <TopExp.hxx>
50
51 #include <GCPnts_AbscissaPoint.hxx>
52
53 GeomAPI_Edge::GeomAPI_Edge()
54 {
55   TopoDS_Edge* anEdge = new TopoDS_Edge;
56
57   BRep_Builder aBuilder;
58   aBuilder.MakeEdge(*anEdge);
59
60   setImpl(anEdge);
61 }
62
63 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
64 {
65   if (!theShape->isNull() && theShape->isEdge()) {
66     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
67   }
68 }
69
70 bool GeomAPI_Edge::isLine() const
71 {
72   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
73   double aFirst, aLast;
74   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
75   if (aCurve.IsNull()) // degenerative edge
76     return false;
77   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
78     return true;
79   return false;
80 }
81
82 bool GeomAPI_Edge::isCircle() const
83 {
84   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
85   double aFirst, aLast;
86   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
87   if (aCurve.IsNull()) // degenerative edge
88     return false;
89   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
90   {
91     // Check the difference of first and last parameters to be equal to the curve period
92     if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
93       return true;
94   }
95   return false;
96 }
97
98 bool GeomAPI_Edge::isArc() const
99 {
100   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
101   double aFirst, aLast;
102   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
103   if (aCurve.IsNull()) // degenerative edge
104     return false;
105   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
106   {
107     // Check the difference of first and last parameters is not equal the curve period
108     if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
109       return true;
110   }
111   return false;
112 }
113
114 bool GeomAPI_Edge::isEllipse() const
115 {
116   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
117   double aFirst, aLast;
118   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
119   if (aCurve.IsNull()) // degenerative edge
120     return false;
121   if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
122     return true;
123   return false;
124 }
125
126 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
127 {
128   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
129   double aFirst, aLast;
130   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
131   gp_Pnt aPoint;
132   aCurve->D0(aFirst, aPoint);
133   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
134 }
135
136 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
137 {
138   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
139   double aFirst, aLast;
140   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
141   gp_Pnt aPoint;
142   aCurve->D0(aLast, aPoint);
143   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
144 }
145
146 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
147 {
148   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
149   double aFirst, aLast;
150   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
151   if (!aCurve.IsNull()) {
152     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
153     if (!aCirc.IsNull()) {
154       gp_Pnt aLoc = aCirc->Location();
155       std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
156       gp_Dir anAxis = aCirc->Axis().Direction();
157       std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
158       return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
159     }
160   }
161   return std::shared_ptr<GeomAPI_Circ>(); // not circle
162 }
163
164 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
165 {
166   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
167   double aFirst, aLast;
168   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
169   if (!aCurve.IsNull()) {
170     Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
171     if (!aElips.IsNull()) {
172       gp_Elips aGpElips = aElips->Elips();
173       std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
174       aEllipse->setImpl(new gp_Elips(aGpElips));
175       return aEllipse;
176     }
177   }
178   return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
179 }
180
181 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
182 {
183   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
184   double aFirst, aLast;
185   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
186   if (aCurve) {
187     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
188     if (aLine) {
189       gp_Pnt aStartPnt = aLine->Value(aFirst);
190       std::shared_ptr<GeomAPI_Pnt> aStart(
191           new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
192       gp_Pnt aEndPnt = aLine->Value(aLast);
193       std::shared_ptr<GeomAPI_Pnt> aEnd(
194           new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
195       return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
196     }
197   }
198   return std::shared_ptr<GeomAPI_Lin>(); // not circle
199 }
200
201
202 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
203 {
204   if (!theEdge.get() || ! theEdge->isEdge())
205     return false;
206   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
207   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
208
209   if (aMyShape.IsNull() || aInShape.IsNull())
210     return false;
211
212   if (aMyShape.ShapeType() != aInShape.ShapeType())
213     return false;
214
215   double aMyStart, aMyEnd;
216   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
217   double aInStart, aInEnd;
218   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
219
220   // Check that end point parameters are the same
221   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
222     return false;
223
224   // Check that curves a the same type
225   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
226   GeomAdaptor_Curve aInAdaptor(aInCurve);
227   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
228     return false;
229
230   // Check that end points are equal
231   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
232   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
233   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
234   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
235
236   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
237     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
238     return false;
239
240   return true;
241 }
242
243 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
244 {
245   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
246   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
247 }
248
249 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
250 {
251   double aFirst, aLast;
252   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
253   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
254   if (aCurve.IsNull())
255     return false;
256
257   double A, B, C, D;
258   thePlane->coefficients(A, B, C, D);
259   gp_Pln aPlane(A, B, C, D);
260
261   bool inPlane = false;
262   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
263     // check start and end points on the plane
264     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
265     gp_Pnt aLastPnt = aCurve->Value(aLast);
266     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
267               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
268   } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
269     // check the center on the plane and normals are collinear
270     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
271     gp_Pnt aCenter = aCirc->Location();
272     Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
273     inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
274               Abs(Abs(aDot) - 1.0) < Precision::Confusion();
275   } else {
276     // three points checking
277     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
278     gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
279     gp_Pnt aLastPnt = aCurve->Value(aLast);
280     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
281               aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
282               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
283   }
284   return inPlane;
285 }
286
287 void GeomAPI_Edge::intersectWithPlane(const std::shared_ptr<GeomAPI_Pln> thePlane,
288                                       std::list<std::shared_ptr<GeomAPI_Pnt>>& theResult) const
289 {
290   double aFirst, aLast;
291   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
292   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
293   if (!aCurve.IsNull()) {
294     double A, B, C, D;
295     thePlane->coefficients(A, B, C, D);
296     gp_Pln aPln(A, B, C, D);
297
298     Handle(Geom_Plane) aPlane = new Geom_Plane(aPln);
299     GeomAPI_IntCS aIntersect;
300     aIntersect.Perform(aCurve, aPlane);
301     if (aIntersect.IsDone() && (aIntersect.NbPoints() > 0)) {
302       gp_Pnt aPnt;
303       for (int i = 1; i <= aIntersect.NbPoints(); i++) {
304         // check the parameter of intersection in the edge range
305         aIntersect.Parameters(i, A, B, C);
306         if (aCurve->IsPeriodic())
307           C = ElCLib::InPeriod(C, aFirst, aFirst + aCurve->Period());
308         if (C < aFirst - Precision::PConfusion() || C > aLast + Precision::PConfusion())
309           continue;
310
311         // obtain intersection point
312         aPnt = aIntersect.Point(i);
313         std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
314         theResult.push_back(aPntPtr);
315       }
316     }
317   }
318 }
319
320 double GeomAPI_Edge::length() const
321 {
322   const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
323   BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
324   Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
325   return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
326 }
327
328 bool GeomAPI_Edge::isClosed() const
329 {
330   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
331   if (aShape.IsNull())
332     return false;
333   double aFirst, aLast;
334   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
335   if (aCurve.IsNull() || !aCurve->IsPeriodic())
336     return false;
337   aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
338
339   return fabs(aFirst - aLast) < 1.e-9;
340 }
341
342 bool GeomAPI_Edge::isDegenerated() const
343 {
344   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
345   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
346     return false;
347   return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
348 }
349
350 void GeomAPI_Edge::setFirstPointTolerance(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(aVFirst, theTolerance);
356 }
357
358 void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
359 {
360   TopoDS_Edge anEdge = impl<TopoDS_Edge>();
361   TopoDS_Vertex aVFirst, aVLast;
362   TopExp::Vertices(anEdge, aVFirst, aVLast);
363   BRep_Builder().UpdateVertex(aVLast, theTolerance);
364 }
365
366 GeomPointPtr GeomAPI_Edge::middlePoint() const
367 {
368   GeomPointPtr aMiddlePoint;
369
370   const TopoDS_Edge& anEdge = impl<TopoDS_Edge>();
371   if (anEdge.IsNull())
372     return aMiddlePoint;
373   double aFirst, aLast;
374   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
375   if (aCurve.IsNull())
376     return aMiddlePoint;
377
378   static const int NB_POINTS = 3;
379   GeomAdaptor_Curve aCurveAdaptor(aCurve, aFirst, aLast);
380   GCPnts_UniformAbscissa anAlgo(aCurveAdaptor, NB_POINTS);
381   if (anAlgo.IsDone()) {
382     gp_Pnt aPnt = aCurveAdaptor.Value(anAlgo.Parameter(2));
383     aMiddlePoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
384   }
385   return aMiddlePoint;
386 }