Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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_Pnt.h>
9 #include<GeomAPI_Circ.h>
10 #include<GeomAPI_Dir.h>
11 #include<GeomAPI_Lin.h>
12
13 #include <TopoDS_Shape.hxx>
14 #include <TopoDS_Edge.hxx>
15 #include <TopoDS.hxx>
16 #include <BRep_Tool.hxx>
17 #include <Geom_Curve.hxx>
18 #include <Geom_Line.hxx>
19 #include <Geom_Circle.hxx>
20 #include <GeomAdaptor_Curve.hxx>
21 #include <gp_Ax1.hxx>
22
23 GeomAPI_Edge::GeomAPI_Edge()
24   : GeomAPI_Shape()
25 {
26 }
27
28 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
29 {
30   if (!theShape->isNull() && theShape->isEdge()) {
31     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
32   }
33 }
34
35 bool GeomAPI_Edge::isLine() const
36 {
37   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
38   double aFirst, aLast;
39   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
40   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
41     return true;
42   return false;
43 }
44
45 bool GeomAPI_Edge::isCircle() const
46 {
47   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
48   double aFirst, aLast;
49   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
50   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
51   {
52     // Check the difference of first and last parameters to be equal to the curve period
53     if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
54       return true;
55   }
56   return false;
57 }
58
59 bool GeomAPI_Edge::isArc() const
60 {
61   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
62   double aFirst, aLast;
63   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
64   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
65   {
66     // Check the difference of first and last parameters is not equal the curve period
67     if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
68       return true;
69   }
70   return false;
71 }
72
73 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
74 {
75   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
76   double aFirst, aLast;
77   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
78   gp_Pnt aPoint;
79   aCurve->D0(aFirst, aPoint);
80   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
81 }
82
83 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
84 {
85   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
86   double aFirst, aLast;
87   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
88   gp_Pnt aPoint;
89   aCurve->D0(aLast, aPoint);
90   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
91 }
92
93 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
94 {
95   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
96   double aFirst, aLast;
97   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
98   if (aCurve) {
99     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
100     if (aCirc) {
101       gp_Pnt aLoc = aCirc->Location();
102       std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
103       gp_Dir anAxis = aCirc->Axis().Direction();
104       std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
105       return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
106     }
107   }
108   return std::shared_ptr<GeomAPI_Circ>(); // not circle
109 }
110
111 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line()
112 {
113   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
114   double aFirst, aLast;
115   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
116   if (aCurve) {
117     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
118     if (aLine) {
119       gp_Pnt aStartPnt = aLine->Value(aFirst);
120       std::shared_ptr<GeomAPI_Pnt> aStart(
121           new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
122       gp_Pnt aEndPnt = aLine->Value(aLast);
123       std::shared_ptr<GeomAPI_Pnt> aEnd(
124           new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
125       return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
126     }
127   }
128   return std::shared_ptr<GeomAPI_Lin>(); // not circle
129 }
130
131
132 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
133 {
134   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
135   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
136
137   if (aMyShape.ShapeType() != aInShape.ShapeType())
138     return false;
139
140   double aMyStart, aMyEnd;
141   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
142   double aInStart, aInEnd;
143   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
144
145   // Check that curves a the same type
146   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
147   GeomAdaptor_Curve aInAdaptor(aInCurve);
148   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
149     return false;
150
151   // Check that end point parameters are the same
152   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
153     return false;
154
155   // Check that end points are equal
156   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
157   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
158   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
159   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
160
161   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) || 
162     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
163     return false;
164
165   return true;
166 }