Salome HOME
Implement filter "F5: On geometry"
[modules/shaper.git] / src / GeomAPI / GeomAPI_Face.cpp
1 // Copyright (C) 2014-2019  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
18 //
19
20 #include "GeomAPI_Face.h"
21
22 #include "GeomAPI_Dir.h"
23 #include "GeomAPI_Pln.h"
24 #include "GeomAPI_Pnt.h"
25 #include "GeomAPI_Sphere.h"
26 #include "GeomAPI_Cylinder.h"
27 #include "GeomAPI_Cone.h"
28 #include "GeomAPI_Torus.h"
29
30 #include <BOPTools_AlgoTools.hxx>
31 #include <BRep_Tool.hxx>
32 #include <BRepAdaptor_Surface.hxx>
33 #include <BRepGProp_Face.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 because BOPTools_AlgoTools::AreFacesSameDomain not very smart.
101   Standard_Boolean aRes = BOPTools_AlgoTools::AreFacesSameDomain(aMyFace, aInFace, aContext)
102     && BOPTools_AlgoTools::AreFacesSameDomain(aInFace, aMyFace, aContext);
103
104   return aRes == Standard_True;
105 }
106
107 bool GeomAPI_Face::isSameGeometry(const std::shared_ptr<GeomAPI_Shape> theShape) const
108 {
109   if (!theShape->isFace())
110     return false;
111   TopoDS_Face anOwnFace = TopoDS::Face(impl<TopoDS_Shape>());
112   TopoDS_Face anOtherFace = TopoDS::Face(theShape->impl<TopoDS_Shape>());
113
114   Handle(Geom_Surface) anOwnSurf = BRep_Tool::Surface(anOwnFace);
115   Handle(Geom_Surface) anOtherSurf = BRep_Tool::Surface(anOtherFace);
116   return anOwnSurf == anOtherSurf;
117 }
118
119 bool GeomAPI_Face::isCylindrical() const
120 {
121   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
122   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
123   Handle(Geom_RectangularTrimmedSurface) aTrimmed =
124     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
125   if (!aTrimmed.IsNull())
126     aSurf = aTrimmed->BasisSurface();
127   return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
128 }
129
130 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
131 {
132   std::shared_ptr<GeomAPI_Pln> aResult;
133   TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
134   if (aShape.IsNull())
135     return aResult;  // null shape
136   if (aShape.ShapeType() != TopAbs_FACE)
137     return aResult;  // not face
138   TopoDS_Face aFace = TopoDS::Face(aShape);
139   if (aFace.IsNull())
140     return aResult;  // not face
141   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
142   if (aSurf.IsNull())
143     return aResult;  // no surface
144   GeomLib_IsPlanarSurface isPlanar(aSurf);
145   if(!isPlanar.IsPlanar()) {
146     return aResult;
147   }
148   gp_Pln aPln = isPlanar.Plan();
149   double aA, aB, aC, aD;
150   aPln.Coefficients(aA, aB, aC, aD);
151   if (aFace.Orientation() == TopAbs_REVERSED) {
152     aA = -aA;
153     aB = -aB;
154     aC = -aC;
155     aD = -aD;
156   }
157   aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
158   return aResult;
159 }
160
161 std::shared_ptr<GeomAPI_Sphere> GeomAPI_Face::getSphere() const
162 {
163   GeomSpherePtr aSphere;
164
165   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
166   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
167   if (aSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
168     gp_Sphere aSph = Handle(Geom_SphericalSurface)::DownCast(aSurf)->Sphere();
169     const gp_Pnt& aCenter = aSph.Location();
170     double aRadius = aSph.Radius();
171
172     GeomPointPtr aCenterPnt(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
173     aSphere = GeomSpherePtr(new GeomAPI_Sphere(aCenterPnt, aRadius));
174   }
175   return aSphere;
176 }
177
178 std::shared_ptr<GeomAPI_Cylinder> GeomAPI_Face::getCylinder() const
179 {
180   GeomCylinderPtr aCylinder;
181
182   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
183   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
184   if (aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
185     gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf)->Cylinder();
186     gp_Pnt aLoc = aCyl.Location();
187     const gp_Dir& aDir = aCyl.Position().Direction();
188     double aRadius = aCyl.Radius();
189
190     double aUMin, aUMax, aVMin, aVMax;
191     BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
192     double aHeight = aVMax - aVMin;
193
194     aLoc.ChangeCoord() += aDir.XYZ() * aVMin;
195     GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
196     GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
197     aCylinder = GeomCylinderPtr(new GeomAPI_Cylinder(aLocation, aDirection, aRadius, aHeight));
198   }
199   return aCylinder;
200 }
201
202 std::shared_ptr<GeomAPI_Cone> GeomAPI_Face::getCone() const
203 {
204   GeomConePtr aCone;
205
206   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
207   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
208   if (aSurf->IsKind(STANDARD_TYPE(Geom_ConicalSurface))) {
209     gp_Cone aCon = Handle(Geom_ConicalSurface)::DownCast(aSurf)->Cone();
210     gp_Pnt aLoc = aCon.Location();
211     gp_Dir aDir = aCon.Position().Direction();
212
213     double aUMin, aUMax, aVMin, aVMax;
214     BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
215
216     double aSemiAngle = Abs(aCon.SemiAngle());
217     double aRadius1 = Abs(aCon.RefRadius() + aVMin * Sin(aCon.SemiAngle()));
218     double aRadius2 = Abs(aCon.RefRadius() + aVMax * Sin(aCon.SemiAngle()));
219
220     aLoc.ChangeCoord() += aDir.XYZ() * aVMin * Cos(aCon.SemiAngle());
221
222     GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
223     GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
224     aCone = GeomConePtr(new GeomAPI_Cone(aLocation, aDirection, aSemiAngle, aRadius1, aRadius2));
225   }
226   return aCone;
227 }
228
229 std::shared_ptr<GeomAPI_Torus> GeomAPI_Face::getTorus() const
230 {
231   GeomTorusPtr aTorus;
232
233   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
234   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
235   if (aSurf->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))) {
236     gp_Torus aTor = Handle(Geom_ToroidalSurface)::DownCast(aSurf)->Torus();
237     const gp_Pnt& aLoc = aTor.Location();
238     const gp_Dir& aDir = aTor.Position().Direction();
239     double aMajorRadius = aTor.MajorRadius();
240     double aMinorRadius = aTor.MinorRadius();
241
242     GeomPointPtr aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
243     GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
244     aTorus = GeomTorusPtr(new GeomAPI_Torus(aCenter, aDirection, aMajorRadius, aMinorRadius));
245   }
246   return aTorus;
247 }
248
249 GeomPointPtr GeomAPI_Face::middlePoint() const
250 {
251   GeomPointPtr anInnerPoint;
252
253   const TopoDS_Face& aFace = impl<TopoDS_Face>();
254   if (aFace.IsNull())
255     return anInnerPoint;
256
257   BRepGProp_Face aProp(aFace);
258   double aUMin, aUMax, aVMin, aVMax;
259   aProp.Bounds(aUMin, aUMax, aVMin, aVMax);
260
261   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
262   if (aSurf.IsNull())
263     return anInnerPoint;
264
265   gp_Pnt aPnt = aSurf->Value((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5);
266   anInnerPoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
267   return anInnerPoint;
268 }