Salome HOME
Issue 2556: Functionality of inspection “WhatIs”
[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 #include "GeomAPI_Sphere.h"
27 #include "GeomAPI_Cylinder.h"
28 #include "GeomAPI_Cone.h"
29 #include "GeomAPI_Torus.h"
30
31 #include <BOPTools_AlgoTools.hxx>
32 #include <BRep_Tool.hxx>
33 #include <BRepAdaptor_Surface.hxx>
34 #include <BRepTools.hxx>
35 #include <Geom_Surface.hxx>
36 #include <Geom_SphericalSurface.hxx>
37 #include <Geom_ConicalSurface.hxx>
38 #include <Geom_CylindricalSurface.hxx>
39 #include <Geom_RectangularTrimmedSurface.hxx>
40 #include <Geom_ToroidalSurface.hxx>
41 #include <GeomLib_IsPlanarSurface.hxx>
42 #include <IntTools_Context.hxx>
43 #include <Standard_Type.hxx>
44 #include <TopoDS.hxx>
45 #include <TopoDS_Face.hxx>
46
47 #include <gp_Sphere.hxx>
48 #include <gp_Cylinder.hxx>
49 #include <gp_Cone.hxx>
50 #include <gp_Torus.hxx>
51
52
53 GeomAPI_Face::GeomAPI_Face()
54   : GeomAPI_Shape()
55 {
56 }
57
58 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
59 {
60   if (!theShape->isNull() && theShape->isFace()) {
61     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
62   }
63 }
64
65 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
66 {
67   if (!theFace.get())
68     return false;
69
70   if (!theFace->isFace())
71     return false;
72
73   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
74   const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
75
76   TopoDS_Face aMyFace = TopoDS::Face(aMyShape);
77   TopoDS_Face aInFace = TopoDS::Face(aInShape);
78
79   Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(aMyFace);
80   Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(aInFace);
81
82   // Check that surfaces a the same type
83   if (aMySurf->DynamicType() != aInSurf->DynamicType())
84     return false;
85
86   // Get parameters of surfaces
87   double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
88   aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
89   double aInUMin, aInUMax, aInVMin, aInVMax;
90   aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
91
92   // Check that parameters are the same
93   if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
94       fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
95       fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
96       fabs(aMyVMax - aInVMax) > Precision::PConfusion())
97     return false;
98
99   Handle(IntTools_Context) aContext = new IntTools_Context();
100   // Double check needed bacause BOPTools_AlgoTools::CheckSameGeom not very smart.
101   Standard_Boolean aRes = BOPTools_AlgoTools::CheckSameGeom(aMyFace, aInFace, aContext)
102     && BOPTools_AlgoTools::CheckSameGeom(aInFace, aMyFace, aContext);
103
104   return aRes == Standard_True;
105 }
106
107 bool GeomAPI_Face::isCylindrical() const
108 {
109   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
110   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
111   Handle(Geom_RectangularTrimmedSurface) aTrimmed =
112     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
113   if (!aTrimmed.IsNull())
114     aSurf = aTrimmed->BasisSurface();
115   return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
116 }
117
118 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
119 {
120   std::shared_ptr<GeomAPI_Pln> aResult;
121   TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
122   if (aShape.IsNull())
123     return aResult;  // null shape
124   if (aShape.ShapeType() != TopAbs_FACE)
125     return aResult;  // not face
126   TopoDS_Face aFace = TopoDS::Face(aShape);
127   if (aFace.IsNull())
128     return aResult;  // not face
129   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
130   if (aSurf.IsNull())
131     return aResult;  // no surface
132   GeomLib_IsPlanarSurface isPlanar(aSurf);
133   if(!isPlanar.IsPlanar()) {
134     return aResult;
135   }
136   gp_Pln aPln = isPlanar.Plan();
137   double aA, aB, aC, aD;
138   aPln.Coefficients(aA, aB, aC, aD);
139   if (aFace.Orientation() == TopAbs_REVERSED) {
140     aA = -aA;
141     aB = -aB;
142     aC = -aC;
143     aD = -aD;
144   }
145   aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
146   return aResult;
147 }
148
149 std::shared_ptr<GeomAPI_Sphere> GeomAPI_Face::getSphere() const
150 {
151   GeomSpherePtr aSphere;
152
153   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
154   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
155   if (aSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
156     gp_Sphere aSph = Handle(Geom_SphericalSurface)::DownCast(aSurf)->Sphere();
157     const gp_Pnt& aCenter = aSph.Location();
158     double aRadius = aSph.Radius();
159
160     GeomPointPtr aCenterPnt(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
161     aSphere = GeomSpherePtr(new GeomAPI_Sphere(aCenterPnt, aRadius));
162   }
163   return aSphere;
164 }
165
166 std::shared_ptr<GeomAPI_Cylinder> GeomAPI_Face::getCylinder() const
167 {
168   GeomCylinderPtr aCylinder;
169
170   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
171   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
172   if (aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
173     gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf)->Cylinder();
174     gp_Pnt aLoc = aCyl.Location();
175     const gp_Dir& aDir = aCyl.Position().Direction();
176     double aRadius = aCyl.Radius();
177
178     double aUMin, aUMax, aVMin, aVMax;
179     BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
180     double aHeight = aVMax - aVMin;
181
182     aLoc.ChangeCoord() += aDir.XYZ() * aVMin;
183     GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
184     GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
185     aCylinder = GeomCylinderPtr(new GeomAPI_Cylinder(aLocation, aDirection, aRadius, aHeight));
186   }
187   return aCylinder;
188 }
189
190 std::shared_ptr<GeomAPI_Cone> GeomAPI_Face::getCone() const
191 {
192   GeomConePtr aCone;
193
194   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
195   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
196   if (aSurf->IsKind(STANDARD_TYPE(Geom_ConicalSurface))) {
197     gp_Cone aCon = Handle(Geom_ConicalSurface)::DownCast(aSurf)->Cone();
198     const gp_Pnt& aLoc = aCon.Location();
199     const gp_Dir& aDir = aCon.Position().Direction();
200     double aRadius1 = aCon.RefRadius();
201
202     double aUMin, aUMax, aVMin, aVMax;
203     BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
204
205     double aSemiAngle = Abs(aCon.SemiAngle());
206     double aRadius2 = aRadius1 - (aVMax - aVMin) * Sin(aSemiAngle);
207
208     GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
209     GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
210     aCone = GeomConePtr(new GeomAPI_Cone(aLocation, aDirection, aSemiAngle, aRadius1, aRadius2));
211   }
212   return aCone;
213 }
214
215 std::shared_ptr<GeomAPI_Torus> GeomAPI_Face::getTorus() const
216 {
217   GeomTorusPtr aTorus;
218
219   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
220   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
221   if (aSurf->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))) {
222     gp_Torus aTor = Handle(Geom_ToroidalSurface)::DownCast(aSurf)->Torus();
223     const gp_Pnt& aLoc = aTor.Location();
224     const gp_Dir& aDir = aTor.Position().Direction();
225     double aMajorRadius = aTor.MajorRadius();
226     double aMinorRadius = aTor.MinorRadius();
227
228     GeomPointPtr aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
229     GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
230     aTorus = GeomTorusPtr(new GeomAPI_Torus(aCenter, aDirection, aMajorRadius, aMinorRadius));
231   }
232   return aTorus;
233 }