]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/GeomAPI_Edge.cpp
Salome HOME
Issue #2966: Provide update of viewer on text color preference changed
[modules/shaper.git] / src / GeomAPI / GeomAPI_Edge.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include<GeomAPI_Edge.h>
21 #include<GeomAPI_Pln.h>
22 #include<GeomAPI_Pnt.h>
23 #include<GeomAPI_Circ.h>
24 #include<GeomAPI_Dir.h>
25 #include<GeomAPI_Lin.h>
26 #include<GeomAPI_Ax2.h>
27 #include<GeomAPI_Ellipse.h>
28
29 #include <BRepAdaptor_Curve.hxx>
30
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS.hxx>
34 #include <BRep_Builder.hxx>
35 #include <BRep_Tool.hxx>
36 #include <ElCLib.hxx>
37 #include <GCPnts_UniformAbscissa.hxx>
38 #include <Geom_Curve.hxx>
39 #include <Geom_Line.hxx>
40 #include <Geom_Circle.hxx>
41 #include <Geom_TrimmedCurve.hxx>
42 #include <Geom_Ellipse.hxx>
43 #include <Geom_Plane.hxx>
44 #include <GeomAPI_ExtremaCurveSurface.hxx>
45 #include <GeomAPI_IntCS.hxx>
46 #include <GeomAdaptor_Curve.hxx>
47 #include <gp_Ax1.hxx>
48 #include <gp_Pln.hxx>
49 #include <gp_Elips.hxx>
50 #include <TopExp.hxx>
51
52 #include <GCPnts_AbscissaPoint.hxx>
53
54 GeomAPI_Edge::GeomAPI_Edge()
55 {
56   TopoDS_Edge* anEdge = new TopoDS_Edge;
57
58   BRep_Builder aBuilder;
59   aBuilder.MakeEdge(*anEdge);
60
61   setImpl(anEdge);
62 }
63
64 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
65 {
66   if (!theShape->isNull() && theShape->isEdge()) {
67     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
68   }
69 }
70
71 bool GeomAPI_Edge::isSameGeometry(const std::shared_ptr<GeomAPI_Shape> theShape) const
72 {
73   if (!theShape->isEdge())
74     return false;
75   TopoDS_Edge anOwnEdge = TopoDS::Edge(impl<TopoDS_Shape>());
76   TopoDS_Edge anOtherEdge = TopoDS::Edge(theShape->impl<TopoDS_Shape>());
77
78   double aFirst, aLast;
79   Handle(Geom_Curve) anOwnCurve = BRep_Tool::Curve(anOwnEdge, aFirst, aLast);
80   Handle(Geom_Curve) anOtherCurve = BRep_Tool::Curve(anOtherEdge, aFirst, aLast);
81   return anOwnCurve == anOtherCurve;
82 }
83
84 bool GeomAPI_Edge::isLine() const
85 {
86   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
87   double aFirst, aLast;
88   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
89   if (aCurve.IsNull()) // degenerative edge
90     return false;
91   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
92     return true;
93   return false;
94 }
95
96 /// extracts a circle curve from the arbitrary curve, returns null is it is different type
97 static Handle(Geom_Circle) circ(const Handle(Geom_Curve) theCurve)
98 {
99   Handle(Geom_Circle) aResult = Handle(Geom_Circle)::DownCast(theCurve);
100   if (!aResult.IsNull())
101     return aResult;
102   // check this may be a trimmed curve that contains circle inside
103   Handle(Geom_TrimmedCurve) aTrimmed = Handle(Geom_TrimmedCurve)::DownCast(theCurve);
104   while(!aTrimmed.IsNull()) {
105     aResult = Handle(Geom_Circle)::DownCast(aTrimmed->BasisCurve());
106     if (!aResult.IsNull())
107       return aResult;
108     aTrimmed = Handle(Geom_TrimmedCurve)::DownCast(aTrimmed->BasisCurve());
109   }
110   return aResult; // null, not circle
111 }
112
113 bool GeomAPI_Edge::isCircle() const
114 {
115   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
116   double aFirst, aLast;
117   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
118   if (!circ(aCurve).IsNull()) {
119     // Check the difference of first and last parameters to be equal to the curve period
120     if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
121       return true;
122   }
123   return false;
124 }
125
126 bool GeomAPI_Edge::isArc() const
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   if (!circ(aCurve).IsNull()) {
132     // Check the difference of first and last parameters is not equal the curve period
133     if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
134       return true;
135   }
136   return false;
137 }
138
139 bool GeomAPI_Edge::isEllipse() const
140 {
141   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
142   double aFirst, aLast;
143   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
144   if (aCurve.IsNull()) // degenerative edge
145     return false;
146   if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
147     return true;
148   return false;
149 }
150
151 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
152 {
153   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
154   double aFirst, aLast;
155   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
156   gp_Pnt aPoint;
157   aCurve->D0(aFirst, aPoint);
158   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
159 }
160
161 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
162 {
163   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
164   double aFirst, aLast;
165   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
166   gp_Pnt aPoint;
167   aCurve->D0(aLast, aPoint);
168   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
169 }
170
171 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
172 {
173   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
174   double aFirst, aLast;
175   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
176   Handle(Geom_Circle) aCirc = circ(aCurve);
177   if (!aCirc.IsNull()) {
178     gp_Pnt aLoc = aCirc->Location();
179     std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
180     gp_Dir anAxis = aCirc->Axis().Direction();
181     std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
182     return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
183   }
184   return std::shared_ptr<GeomAPI_Circ>(); // not circle
185 }
186
187 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
188 {
189   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
190   double aFirst, aLast;
191   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
192   if (!aCurve.IsNull()) {
193     Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
194     if (!aElips.IsNull()) {
195       gp_Elips aGpElips = aElips->Elips();
196       std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
197       aEllipse->setImpl(new gp_Elips(aGpElips));
198       return aEllipse;
199     }
200   }
201   return std::shared_ptr<GeomAPI_Ellipse>(); // not ellipse
202 }
203
204 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
205 {
206   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
207   double aFirst, aLast;
208   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
209   if (aCurve) {
210     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
211     if (aLine) {
212       gp_Pnt aStartPnt = aLine->Value(aFirst);
213       std::shared_ptr<GeomAPI_Pnt> aStart(
214           new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
215       gp_Pnt aEndPnt = aLine->Value(aLast);
216       std::shared_ptr<GeomAPI_Pnt> aEnd(
217           new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
218       return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
219     }
220   }
221   return std::shared_ptr<GeomAPI_Lin>(); // not circle
222 }
223
224
225 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
226 {
227   if (!theEdge.get() || ! theEdge->isEdge())
228     return false;
229   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
230   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
231
232   if (aMyShape.IsNull() || aInShape.IsNull())
233     return false;
234
235   if (aMyShape.ShapeType() != aInShape.ShapeType())
236     return false;
237
238   double aMyStart, aMyEnd;
239   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
240   double aInStart, aInEnd;
241   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
242
243   // Check that end point parameters are the same
244   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
245     return false;
246
247   // Check that curves a the same type
248   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
249   GeomAdaptor_Curve aInAdaptor(aInCurve);
250   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
251     return false;
252
253   // Check that end points are equal
254   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
255   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
256   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
257   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
258
259   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
260     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
261     return false;
262
263   return true;
264 }
265
266 // LCOV_EXCL_START
267 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
268 {
269   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
270   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
271 }
272
273 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
274 {
275   double aFirst, aLast;
276   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
277   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
278   if (aCurve.IsNull())
279     return false;
280
281   double A, B, C, D;
282   thePlane->coefficients(A, B, C, D);
283   gp_Pln aPlane(A, B, C, D);
284
285   bool inPlane = false;
286   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
287     // check start and end points on the plane
288     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
289     gp_Pnt aLastPnt = aCurve->Value(aLast);
290     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
291               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
292   } else {
293     Handle(Geom_Circle) aCirc = circ(aCurve);
294     if (!aCirc.IsNull()) {
295       gp_Pnt aCenter = aCirc->Location();
296       Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
297       inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
298                 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
299     } else {
300       // three points checking
301       gp_Pnt aFirstPnt = aCurve->Value(aFirst);
302       gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
303       gp_Pnt aLastPnt = aCurve->Value(aLast);
304       inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
305                 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
306                 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
307     }
308   }
309   return inPlane;
310 }
311 // LCOV_EXCL_STOP
312
313 void GeomAPI_Edge::intersectWithPlane(const std::shared_ptr<GeomAPI_Pln> thePlane,
314                                       std::list<std::shared_ptr<GeomAPI_Pnt>>& theResult) const
315 {
316   double aFirst, aLast;
317   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
318   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
319   if (!aCurve.IsNull()) {
320     double A, B, C, D;
321     thePlane->coefficients(A, B, C, D);
322     gp_Pln aPln(A, B, C, D);
323     Handle(Geom_Plane) aPlane = new Geom_Plane(aPln);
324
325     // intersect the plane with the curve
326     GeomAPI_IntCS aIntersect;
327     aIntersect.Perform(aCurve, aPlane);
328     if (aIntersect.IsDone() && (aIntersect.NbPoints() > 0)) {
329       gp_Pnt aPnt;
330       for (int i = 1; i <= aIntersect.NbPoints(); i++) {
331         // check the parameter of intersection in the edge range
332         aIntersect.Parameters(i, A, B, C);
333         if (aCurve->IsPeriodic())
334           C = ElCLib::InPeriod(C, aFirst, aFirst + aCurve->Period());
335         if (C < aFirst - Precision::PConfusion() || C > aLast + Precision::PConfusion())
336           continue;
337
338         // obtain intersection point
339         aPnt = aIntersect.Point(i);
340         std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
341         theResult.push_back(aPntPtr);
342       }
343     }
344     else {
345       // find minimal distance between the plane and the curve
346       GeomAPI_ExtremaCurveSurface anExtrema(aCurve, aPlane);
347       double aTolerance = BRep_Tool::Tolerance(TopoDS::Edge(aShape));
348       if (anExtrema.NbExtrema() > 0 &&
349           anExtrema.LowerDistance() < aTolerance) {
350         // distance is lower than tolerance => tangent case
351         gp_Pnt aPntC, aPntS;
352         anExtrema.NearestPoints(aPntC, aPntS);
353         std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPntS.X(), aPntS.Y(), aPntS.Z()));
354         theResult.push_back(aPntPtr);
355       }
356     }
357   }
358 }
359
360 double GeomAPI_Edge::length() const
361 {
362   const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
363   BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
364   Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
365   return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
366 }
367
368 bool GeomAPI_Edge::isClosed() const
369 {
370   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
371   if (aShape.IsNull())
372     return false;
373   double aFirst, aLast;
374   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
375   if (aCurve.IsNull() || !aCurve->IsPeriodic())
376     return false;
377   aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
378
379   return fabs(aFirst - aLast) < 1.e-9;
380 }
381
382 bool GeomAPI_Edge::isDegenerated() const
383 {
384   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
385   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
386     return false;
387   return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
388 }
389
390 void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
391 {
392   TopoDS_Edge anEdge = impl<TopoDS_Edge>();
393   TopoDS_Vertex aVFirst, aVLast;
394   TopExp::Vertices(anEdge, aVFirst, aVLast);
395   BRep_Builder().UpdateVertex(aVFirst, theTolerance);
396 }
397
398 void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
399 {
400   TopoDS_Edge anEdge = impl<TopoDS_Edge>();
401   TopoDS_Vertex aVFirst, aVLast;
402   TopExp::Vertices(anEdge, aVFirst, aVLast);
403   BRep_Builder().UpdateVertex(aVLast, theTolerance);
404 }
405
406 GeomPointPtr GeomAPI_Edge::middlePoint() const
407 {
408   GeomPointPtr aMiddlePoint;
409
410   const TopoDS_Edge& anEdge = impl<TopoDS_Edge>();
411   if (anEdge.IsNull())
412     return aMiddlePoint;
413   double aFirst, aLast;
414   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
415   if (aCurve.IsNull())
416     return aMiddlePoint;
417
418   static const int NB_POINTS = 3;
419   GeomAdaptor_Curve aCurveAdaptor(aCurve, aFirst, aLast);
420   GCPnts_UniformAbscissa anAlgo(aCurveAdaptor, NB_POINTS);
421   if (anAlgo.IsDone()) {
422     gp_Pnt aPnt = aCurveAdaptor.Value(anAlgo.Parameter(2));
423     aMiddlePoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
424   }
425   return aMiddlePoint;
426 }