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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "GeomAPI_Face.h"
21
22 #include "GeomAPI_Dir.h"
23 #include "GeomAPI_Pln.h"
24 #include "GeomAPI_Pnt.h"
25
26 #include <BOPTools_AlgoTools.hxx>
27 #include <BRep_Tool.hxx>
28 #include <BRepAdaptor_Surface.hxx>
29 #include <Geom_Surface.hxx>
30 #include <Geom_CylindricalSurface.hxx>
31 #include <Geom_RectangularTrimmedSurface.hxx>
32 #include <GeomLib_IsPlanarSurface.hxx>
33 #include <IntTools_Context.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Face.hxx>
36
37 GeomAPI_Face::GeomAPI_Face()
38   : GeomAPI_Shape()
39 {
40 }
41
42 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
43 {
44   if (!theShape->isNull() && theShape->isFace()) {
45     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
46   }
47 }
48
49 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
50 {
51   if (!theFace.get())
52     return false;
53
54   if (!theFace->isFace())
55     return false;
56
57   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
58   const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
59
60   TopoDS_Face aMyFace = TopoDS::Face(aMyShape);
61   TopoDS_Face aInFace = TopoDS::Face(aInShape);
62
63   Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(aMyFace);
64   Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(aInFace);
65
66   // Check that surfaces a the same type
67   if (aMySurf->DynamicType() != aInSurf->DynamicType())
68     return false;
69
70   // Get parameters of surfaces
71   double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
72   aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
73   double aInUMin, aInUMax, aInVMin, aInVMax;
74   aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
75
76   // Check that parameters are the same
77   if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
78       fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
79       fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
80       fabs(aMyVMax - aInVMax) > Precision::PConfusion())
81     return false;
82
83   Handle(IntTools_Context) aContext = new IntTools_Context();
84   // Double check needed bacause BOPTools_AlgoTools::CheckSameGeom not very smart.
85   Standard_Boolean aRes = BOPTools_AlgoTools::CheckSameGeom(aMyFace, aInFace, aContext)
86     && BOPTools_AlgoTools::CheckSameGeom(aInFace, aMyFace, aContext);
87
88   return aRes == Standard_True;
89 }
90
91 bool GeomAPI_Face::isCylindrical() const
92 {
93   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
94   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
95   Handle(Geom_RectangularTrimmedSurface) aTrimmed =
96     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
97   if (!aTrimmed.IsNull())
98     aSurf = aTrimmed->BasisSurface();
99   return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
100 }
101
102 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
103 {
104   std::shared_ptr<GeomAPI_Pln> aResult;
105   TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
106   if (aShape.IsNull())
107     return aResult;  // null shape
108   if (aShape.ShapeType() != TopAbs_FACE)
109     return aResult;  // not face
110   TopoDS_Face aFace = TopoDS::Face(aShape);
111   if (aFace.IsNull())
112     return aResult;  // not face
113   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
114   if (aSurf.IsNull())
115     return aResult;  // no surface
116   GeomLib_IsPlanarSurface isPlanar(aSurf);
117   if(!isPlanar.IsPlanar()) {
118     return aResult;
119   }
120   gp_Pln aPln = isPlanar.Plan();
121   double aA, aB, aC, aD;
122   aPln.Coefficients(aA, aB, aC, aD);
123   if (aFace.Orientation() == TopAbs_REVERSED) {
124     aA = -aA;
125     aB = -aB;
126     aC = -aC;
127     aD = -aD;
128   }
129   aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
130   return aResult;
131 }