Salome HOME
ff6eafc601c15eca8f967fb461bcd0ab6bc317eb
[modules/shaper.git] / src / GeomAPI / GeomAPI_Face.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Face.cpp
4 // Created:     2 Dec 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "GeomAPI_Face.h"
8
9 #include "GeomAPI_Dir.h"
10 #include "GeomAPI_Pln.h"
11 #include "GeomAPI_Pnt.h"
12
13 #include <BOPTools_AlgoTools.hxx>
14 #include <BRep_Tool.hxx>
15 #include <BRepAdaptor_Surface.hxx>
16 #include <Geom_Surface.hxx>
17 #include <Geom_CylindricalSurface.hxx>
18 #include <Geom_RectangularTrimmedSurface.hxx>
19 #include <IntTools_Context.hxx>
20 #include <TopoDS.hxx>
21 #include <TopoDS_Face.hxx>
22
23 GeomAPI_Face::GeomAPI_Face()
24   : GeomAPI_Shape()
25 {
26 }
27
28 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
29 {
30   if (!theShape->isNull() && theShape->isFace()) {
31     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
32   }
33 }
34
35 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
36 {
37   if (!theFace.get())
38     return false;
39
40   if (!theFace->isFace())
41     return false;
42
43   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
44   const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
45
46   TopoDS_Face aMyFace = TopoDS::Face(aMyShape);
47   TopoDS_Face aInFace = TopoDS::Face(aInShape);
48
49   Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(aMyFace);
50   Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(aInFace);
51
52   // Check that surfaces a the same type
53   if (aMySurf->DynamicType() != aInSurf->DynamicType())
54     return false;
55
56   // Get parameters of surfaces
57   double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
58   aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
59   double aInUMin, aInUMax, aInVMin, aInVMax;
60   aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
61
62   // Check that parameters are the same
63   if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
64       fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
65       fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
66       fabs(aMyVMax - aInVMax) > Precision::PConfusion())
67     return false;
68
69   Handle(IntTools_Context) aContext = new IntTools_Context();
70   Standard_Boolean aRes = BOPTools_AlgoTools::CheckSameGeom(aMyFace, aInFace, aContext);
71
72   return aRes == Standard_True;
73 }
74
75 bool GeomAPI_Face::isCylindrical() const
76 {
77   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
78   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
79   Handle(Geom_RectangularTrimmedSurface) aTrimmed = 
80     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
81   if (!aTrimmed.IsNull())
82     aSurf = aTrimmed->BasisSurface();
83   return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
84 }
85
86 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
87 {
88   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
89   BRepAdaptor_Surface aSurfAdapt(TopoDS::Face(aShape));
90
91   if (aSurfAdapt.GetType() != GeomAbs_Plane)
92     return std::shared_ptr<GeomAPI_Pln>();
93
94   // Obtain central point
95   double aUMin, aUMax, aVMin, aVMax;
96   aUMin = aSurfAdapt.FirstUParameter();
97   aUMax = aSurfAdapt.LastUParameter();
98   aVMin = aSurfAdapt.FirstVParameter();
99   aVMax = aSurfAdapt.LastVParameter();
100   gp_Pnt aCentralPnt;
101   gp_Vec aDU, aDV;
102   aSurfAdapt.D1((aUMin+aUMax)*0.5, (aVMin+aVMax)*0.5, aCentralPnt, aDU, aDV);
103   std::shared_ptr<GeomAPI_Pnt> aCenter(
104       new GeomAPI_Pnt(aCentralPnt.X(), aCentralPnt.Y(), aCentralPnt.Z()));
105
106   // Obtain plane direction
107   gp_XYZ aNormalVec = aDU.XYZ().Crossed(aDV.XYZ());
108   if (aNormalVec.SquareModulus() < Precision::Confusion() * Precision::Confusion())
109     return std::shared_ptr<GeomAPI_Pln>();
110   std::shared_ptr<GeomAPI_Dir> aNormal(
111       new GeomAPI_Dir(aNormalVec.X(), aNormalVec.Y(), aNormalVec.Z()));
112
113   std::shared_ptr<GeomAPI_Pln> aResult(new GeomAPI_Pln(aCenter, aNormal));
114   return aResult;
115 }