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