Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / GeomAPI / GeomAPI_Face.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAPI_Face.h"
22
23 #include "GeomAPI_Dir.h"
24 #include "GeomAPI_Pln.h"
25 #include "GeomAPI_Pnt.h"
26
27 #include <BOPTools_AlgoTools.hxx>
28 #include <BRep_Tool.hxx>
29 #include <BRepAdaptor_Surface.hxx>
30 #include <Geom_Surface.hxx>
31 #include <Geom_CylindricalSurface.hxx>
32 #include <Geom_RectangularTrimmedSurface.hxx>
33 #include <GeomLib_IsPlanarSurface.hxx>
34 #include <IntTools_Context.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Face.hxx>
37
38 GeomAPI_Face::GeomAPI_Face()
39   : GeomAPI_Shape()
40 {
41 }
42
43 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
44 {
45   if (!theShape->isNull() && theShape->isFace()) {
46     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
47   }
48 }
49
50 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
51 {
52   if (!theFace.get())
53     return false;
54
55   if (!theFace->isFace())
56     return false;
57
58   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
59   const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
60
61   TopoDS_Face aMyFace = TopoDS::Face(aMyShape);
62   TopoDS_Face aInFace = TopoDS::Face(aInShape);
63
64   Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(aMyFace);
65   Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(aInFace);
66
67   // Check that surfaces a the same type
68   if (aMySurf->DynamicType() != aInSurf->DynamicType())
69     return false;
70
71   // Get parameters of surfaces
72   double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
73   aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
74   double aInUMin, aInUMax, aInVMin, aInVMax;
75   aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
76
77   // Check that parameters are the same
78   if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
79       fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
80       fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
81       fabs(aMyVMax - aInVMax) > Precision::PConfusion())
82     return false;
83
84   Handle(IntTools_Context) aContext = new IntTools_Context();
85   // Double check needed bacause BOPTools_AlgoTools::CheckSameGeom not very smart.
86   Standard_Boolean aRes = BOPTools_AlgoTools::CheckSameGeom(aMyFace, aInFace, aContext)
87     && BOPTools_AlgoTools::CheckSameGeom(aInFace, aMyFace, aContext);
88
89   return aRes == Standard_True;
90 }
91
92 bool GeomAPI_Face::isCylindrical() const
93 {
94   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
95   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
96   Handle(Geom_RectangularTrimmedSurface) aTrimmed =
97     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
98   if (!aTrimmed.IsNull())
99     aSurf = aTrimmed->BasisSurface();
100   return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
101 }
102
103 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
104 {
105   std::shared_ptr<GeomAPI_Pln> aResult;
106   TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
107   if (aShape.IsNull())
108     return aResult;  // null shape
109   if (aShape.ShapeType() != TopAbs_FACE)
110     return aResult;  // not face
111   TopoDS_Face aFace = TopoDS::Face(aShape);
112   if (aFace.IsNull())
113     return aResult;  // not face
114   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
115   if (aSurf.IsNull())
116     return aResult;  // no surface
117   GeomLib_IsPlanarSurface isPlanar(aSurf);
118   if(!isPlanar.IsPlanar()) {
119     return aResult;
120   }
121   gp_Pln aPln = isPlanar.Plan();
122   double aA, aB, aC, aD;
123   aPln.Coefficients(aA, aB, aC, aD);
124   if (aFace.Orientation() == TopAbs_REVERSED) {
125     aA = -aA;
126     aB = -aB;
127     aC = -aC;
128     aD = -aD;
129   }
130   aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
131   return aResult;
132 }