Salome HOME
63761a0317b65c5a040d45132a313753115df0ba
[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 <TopoDS_Shape.hxx>
15 #include <TopoDS_Edge.hxx>
16 #include <TopoDS.hxx>
17 #include <BRep_Tool.hxx>
18 #include <Geom_Curve.hxx>
19 #include <Geom_Line.hxx>
20 #include <Geom_Circle.hxx>
21 #include <GeomAdaptor_Curve.hxx>
22 #include <gp_Ax1.hxx>
23 #include <gp_Pln.hxx>
24
25 GeomAPI_Edge::GeomAPI_Edge()
26   : GeomAPI_Shape()
27 {
28 }
29
30 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
31 {
32   if (!theShape->isNull() && theShape->isEdge()) {
33     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
34   }
35 }
36
37 bool GeomAPI_Edge::isLine() const
38 {
39   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
40   double aFirst, aLast;
41   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
42   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
43     return true;
44   return false;
45 }
46
47 bool GeomAPI_Edge::isCircle() 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_Circle)))
53   {
54     // Check the difference of first and last parameters to be equal to the curve period
55     if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
56       return true;
57   }
58   return false;
59 }
60
61 bool GeomAPI_Edge::isArc() const
62 {
63   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
64   double aFirst, aLast;
65   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
66   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
67   {
68     // Check the difference of first and last parameters is not equal the curve period
69     if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
70       return true;
71   }
72   return false;
73 }
74
75 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
76 {
77   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
78   double aFirst, aLast;
79   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
80   gp_Pnt aPoint;
81   aCurve->D0(aFirst, aPoint);
82   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
83 }
84
85 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
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(aLast, aPoint);
92   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
93 }
94
95 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
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   if (aCurve) {
101     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
102     if (aCirc) {
103       gp_Pnt aLoc = aCirc->Location();
104       std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
105       gp_Dir anAxis = aCirc->Axis().Direction();
106       std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
107       return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
108     }
109   }
110   return std::shared_ptr<GeomAPI_Circ>(); // not circle
111 }
112
113 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line()
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) {
119     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
120     if (aLine) {
121       gp_Pnt aStartPnt = aLine->Value(aFirst);
122       std::shared_ptr<GeomAPI_Pnt> aStart(
123           new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
124       gp_Pnt aEndPnt = aLine->Value(aLast);
125       std::shared_ptr<GeomAPI_Pnt> aEnd(
126           new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
127       return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
128     }
129   }
130   return std::shared_ptr<GeomAPI_Lin>(); // not circle
131 }
132
133
134 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
135 {
136   if (!theEdge.get() || ! theEdge->isEdge())
137     return false;
138   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
139   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
140
141   if (aMyShape.IsNull() || aInShape.IsNull())
142     return false;
143
144   if (aMyShape.ShapeType() != aInShape.ShapeType())
145     return false;
146
147   double aMyStart, aMyEnd;
148   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
149   double aInStart, aInEnd;
150   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
151
152   // Check that curves a the same type
153   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
154   GeomAdaptor_Curve aInAdaptor(aInCurve);
155   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
156     return false;
157
158   // Check that end point parameters are the same
159   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
160     return false;
161
162   // Check that end points are equal
163   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
164   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
165   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
166   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
167
168   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) || 
169     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
170     return false;
171
172   return true;
173 }
174
175 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
176 {
177   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
178   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
179 }
180
181 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
182 {
183   double aFirst, aLast;
184   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
185   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
186
187   double A, B, C, D;
188   thePlane->coefficients(A, B, C, D);
189   gp_Pln aPlane(A, B, C, D);
190
191   bool inPlane = false;
192   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
193     // check start and end points on the plane
194     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
195     gp_Pnt aLastPnt = aCurve->Value(aLast);
196     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
197               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
198   } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
199     // check the center on the plane and normals are collinear
200     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
201     gp_Pnt aCenter = aCirc->Location();
202     Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
203     inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
204               Abs(Abs(aDot) - 1.0) < Precision::Confusion();
205   } else {
206     // three points checking
207     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
208     gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
209     gp_Pnt aLastPnt = aCurve->Value(aLast);
210     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
211               aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
212               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
213   }
214   return inPlane;
215 }