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