Salome HOME
Issue #1860: fix end lines with spaces
[modules/shaper.git] / src / GeomAPI / GeomAPI_Edge.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Edge.cpp
4 // Created:     24 Jul 2014
5 // Author:      Artem ZHIDKOV
6
7 #include<GeomAPI_Edge.h>
8 #include<GeomAPI_Pln.h>
9 #include<GeomAPI_Pnt.h>
10 #include<GeomAPI_Circ.h>
11 #include<GeomAPI_Dir.h>
12 #include<GeomAPI_Lin.h>
13
14 #include <BRepAdaptor_Curve.hxx>
15
16 #include <TopoDS_Shape.hxx>
17 #include <TopoDS_Edge.hxx>
18 #include <TopoDS.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRep_Tool.hxx>
21 #include <Geom_Curve.hxx>
22 #include <Geom_Line.hxx>
23 #include <Geom_Circle.hxx>
24 #include <GeomAdaptor_Curve.hxx>
25 #include <gp_Ax1.hxx>
26 #include <gp_Pln.hxx>
27
28 #include <GCPnts_AbscissaPoint.hxx>
29
30 GeomAPI_Edge::GeomAPI_Edge()
31 {
32   TopoDS_Edge* anEdge = new TopoDS_Edge;
33
34   BRep_Builder aBuilder;
35   aBuilder.MakeEdge(*anEdge);
36
37   setImpl(anEdge);
38 }
39
40 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
41 {
42   if (!theShape->isNull() && theShape->isEdge()) {
43     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
44   }
45 }
46
47 bool GeomAPI_Edge::isLine() const
48 {
49   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
50   double aFirst, aLast;
51   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
52   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
53     return true;
54   return false;
55 }
56
57 bool GeomAPI_Edge::isCircle() const
58 {
59   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
60   double aFirst, aLast;
61   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
62   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
63   {
64     // Check the difference of first and last parameters to be equal to the curve period
65     if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
66       return true;
67   }
68   return false;
69 }
70
71 bool GeomAPI_Edge::isArc() const
72 {
73   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
74   double aFirst, aLast;
75   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
76   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
77   {
78     // Check the difference of first and last parameters is not equal the curve period
79     if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
80       return true;
81   }
82   return false;
83 }
84
85 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
86 {
87   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
88   double aFirst, aLast;
89   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
90   gp_Pnt aPoint;
91   aCurve->D0(aFirst, aPoint);
92   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
93 }
94
95 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
96 {
97   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
98   double aFirst, aLast;
99   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
100   gp_Pnt aPoint;
101   aCurve->D0(aLast, aPoint);
102   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
103 }
104
105 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
106 {
107   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
108   double aFirst, aLast;
109   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
110   if (aCurve) {
111     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
112     if (aCirc) {
113       gp_Pnt aLoc = aCirc->Location();
114       std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
115       gp_Dir anAxis = aCirc->Axis().Direction();
116       std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
117       return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
118     }
119   }
120   return std::shared_ptr<GeomAPI_Circ>(); // not circle
121 }
122
123 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line()
124 {
125   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
126   double aFirst, aLast;
127   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
128   if (aCurve) {
129     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
130     if (aLine) {
131       gp_Pnt aStartPnt = aLine->Value(aFirst);
132       std::shared_ptr<GeomAPI_Pnt> aStart(
133           new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
134       gp_Pnt aEndPnt = aLine->Value(aLast);
135       std::shared_ptr<GeomAPI_Pnt> aEnd(
136           new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
137       return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
138     }
139   }
140   return std::shared_ptr<GeomAPI_Lin>(); // not circle
141 }
142
143
144 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
145 {
146   if (!theEdge.get() || ! theEdge->isEdge())
147     return false;
148   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
149   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
150
151   if (aMyShape.IsNull() || aInShape.IsNull())
152     return false;
153
154   if (aMyShape.ShapeType() != aInShape.ShapeType())
155     return false;
156
157   double aMyStart, aMyEnd;
158   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
159   double aInStart, aInEnd;
160   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
161
162   // Check that curves a the same type
163   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
164   GeomAdaptor_Curve aInAdaptor(aInCurve);
165   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
166     return false;
167
168   // Check that end point parameters are the same
169   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
170     return false;
171
172   // Check that end points are equal
173   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
174   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
175   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
176   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
177
178   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
179     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
180     return false;
181
182   return true;
183 }
184
185 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
186 {
187   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
188   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
189 }
190
191 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
192 {
193   double aFirst, aLast;
194   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
195   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
196
197   double A, B, C, D;
198   thePlane->coefficients(A, B, C, D);
199   gp_Pln aPlane(A, B, C, D);
200
201   bool inPlane = false;
202   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
203     // check start and end points on the plane
204     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
205     gp_Pnt aLastPnt = aCurve->Value(aLast);
206     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
207               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
208   } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
209     // check the center on the plane and normals are collinear
210     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
211     gp_Pnt aCenter = aCirc->Location();
212     Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
213     inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
214               Abs(Abs(aDot) - 1.0) < Precision::Confusion();
215   } else {
216     // three points checking
217     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
218     gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
219     gp_Pnt aLastPnt = aCurve->Value(aLast);
220     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
221               aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
222               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
223   }
224   return inPlane;
225 }
226
227 double GeomAPI_Edge::length() const
228 {
229   const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
230   BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
231   Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
232   return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
233 }