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