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