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