Salome HOME
New feature "Placement" was implemented
[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 <Geom_Surface.hxx>
15 #include <Geom_Plane.hxx>
16
17 GeomAPI_Face::GeomAPI_Face()
18   : GeomAPI_Shape()
19 {
20 }
21
22 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
23 {
24   if (!theShape->isNull() && theShape->isFace()) {
25     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
26   }
27 }
28
29 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
30 {
31   if (!theFace->isFace())
32     return false;
33
34   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
35   const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
36
37   Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(TopoDS::Face(aMyShape));
38   Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(TopoDS::Face(aInShape));
39
40   // Check that surfaces a the same type
41   if (aMySurf->DynamicType() != aInSurf->DynamicType())
42     return false;
43
44   // Get parameters of surfaces
45   double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
46   aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
47   double aInUMin, aInUMax, aInVMin, aInVMax;
48   aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
49
50   // Check that parameters are the same
51   if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
52       fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
53       fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
54       fabs(aMyVMax - aInVMax) > Precision::PConfusion())
55     return false;
56
57   return true;
58 }
59
60 bool GeomAPI_Face::isPlanar() const
61 {
62   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
63   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
64   if (aSurf->IsKind(STANDARD_TYPE(Geom_Plane)))
65     return true;
66   return false;
67 }
68
69 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
70 {
71   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
72   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
73
74   if (!aSurf->IsKind(STANDARD_TYPE(Geom_Plane)))
75     return std::shared_ptr<GeomAPI_Pln>();
76
77   // Obtain central point
78   double aUMin, aUMax, aVMin, aVMax;
79   aSurf->Bounds(aUMin, aUMax, aVMin, aVMax);
80   gp_Pnt aCentralPnt;
81   gp_Vec aDU, aDV;
82   aSurf->D1((aUMin+aUMax)*0.5, (aVMin+aVMax)*0.5, aCentralPnt, aDU, aDV);
83   std::shared_ptr<GeomAPI_Pnt> aCenter(
84       new GeomAPI_Pnt(aCentralPnt.X(), aCentralPnt.Y(), aCentralPnt.Z()));
85
86   // Obtain plane direction
87   gp_XYZ aNormalVec = aDU.XYZ().Crossed(aDV.XYZ());
88   if (aNormalVec.SquareModulus() < Precision::Confusion() * Precision::Confusion())
89     return std::shared_ptr<GeomAPI_Pln>();
90   std::shared_ptr<GeomAPI_Dir> aNormal(
91       new GeomAPI_Dir(aNormalVec.X(), aNormalVec.Y(), aNormalVec.Z()));
92
93   std::shared_ptr<GeomAPI_Pln> aResult(new GeomAPI_Pln(aCenter, aNormal));
94   return aResult;
95 }