Salome HOME
Issue #17924: filter F5 on geometry for faces
[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_Curve.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 <BRepTopAdaptor_TopolTool.hxx>
37 #include <Geom_Surface.hxx>
38 #include <Geom_SphericalSurface.hxx>
39 #include <Geom_ConicalSurface.hxx>
40 #include <Geom_CylindricalSurface.hxx>
41 #include <Geom_OffsetSurface.hxx>
42 #include <Geom_Plane.hxx>
43 #include <Geom_RectangularTrimmedSurface.hxx>
44 #include <Geom_SurfaceOfLinearExtrusion.hxx>
45 #include <Geom_SurfaceOfRevolution.hxx>
46 #include <Geom_SweptSurface.hxx>
47 #include <Geom_ToroidalSurface.hxx>
48 #include <GeomAdaptor_HSurface.hxx>
49 #include <GeomAPI_ExtremaCurveCurve.hxx>
50 #include <GeomLib_IsPlanarSurface.hxx>
51 #include <IntPatch_ImpImpIntersection.hxx>
52 #include <IntTools_Context.hxx>
53 #include <Standard_Type.hxx>
54 #include <TopoDS.hxx>
55 #include <TopoDS_Face.hxx>
56
57 #include <gp_Sphere.hxx>
58 #include <gp_Cylinder.hxx>
59 #include <gp_Cone.hxx>
60 #include <gp_Torus.hxx>
61
62
63 GeomAPI_Face::GeomAPI_Face()
64   : GeomAPI_Shape()
65 {
66 }
67
68 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
69 {
70   if (!theShape->isNull() && theShape->isFace()) {
71     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
72   }
73 }
74
75 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
76 {
77   if (!theFace.get())
78     return false;
79
80   if (!theFace->isFace())
81     return false;
82
83   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
84   const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
85
86   TopoDS_Face aMyFace = TopoDS::Face(aMyShape);
87   TopoDS_Face aInFace = TopoDS::Face(aInShape);
88
89   Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(aMyFace);
90   Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(aInFace);
91
92   // Check that surfaces a the same type
93   if (aMySurf->DynamicType() != aInSurf->DynamicType())
94     return false;
95
96   // Get parameters of surfaces
97   double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
98   aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
99   double aInUMin, aInUMax, aInVMin, aInVMax;
100   aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
101
102   // Check that parameters are the same
103   if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
104       fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
105       fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
106       fabs(aMyVMax - aInVMax) > Precision::PConfusion())
107     return false;
108
109   Handle(IntTools_Context) aContext = new IntTools_Context();
110   // Double check needed because BOPTools_AlgoTools::AreFacesSameDomain not very smart.
111   Standard_Boolean aRes = BOPTools_AlgoTools::AreFacesSameDomain(aMyFace, aInFace, aContext)
112     && BOPTools_AlgoTools::AreFacesSameDomain(aInFace, aMyFace, aContext);
113
114   return aRes == Standard_True;
115 }
116
117 static Handle(Geom_Surface) baseSurface(const TopoDS_Face& theFace)
118 {
119   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(theFace);
120   while (aSurf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
121     Handle(Geom_RectangularTrimmedSurface) rts =
122         Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
123     aSurf = rts->BasisSurface();
124   }
125   return aSurf;
126 }
127
128 bool GeomAPI_Face::isSameGeometry(const std::shared_ptr<GeomAPI_Shape> theShape) const
129 {
130   if (!theShape->isFace())
131     return false;
132   if (isSame(theShape))
133     return true;
134
135   GeomFacePtr anOther = theShape->face();
136   if (isPlanar() && anOther->isPlanar()) {
137     GeomPlanePtr anOwnPlane = getPlane();
138     GeomPlanePtr anOtherPlane = anOther->getPlane();
139     return anOwnPlane->isCoincident(anOtherPlane);
140   }
141
142   TopoDS_Face anOwnFace = TopoDS::Face(impl<TopoDS_Shape>());
143   TopoDS_Face anOtherFace = TopoDS::Face(theShape->impl<TopoDS_Shape>());
144
145   Handle(Geom_Surface) anOwnSurf = baseSurface(anOwnFace);
146   Handle(Geom_Surface) anOtherSurf = baseSurface(anOtherFace);
147
148   // case of two elementary surfaces
149   if (anOwnSurf->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
150       anOtherSurf->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
151   {
152     Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(anOwnSurf);
153     Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(anOtherSurf);
154
155     Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
156     Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
157
158     try {
159       IntPatch_ImpImpIntersection anIIInt(aGA1, aTT1, aGA2, aTT2,
160                                           Precision::Confusion(),
161                                           Precision::Confusion());
162       if (!anIIInt.IsDone() || anIIInt.IsEmpty())
163         return false;
164
165       return anIIInt.TangentFaces();
166     }
167     catch (Standard_Failure const&) {
168       return false;
169     }
170   }
171
172   // case of two cylindrical surfaces, at least one of which is a swept surface
173   // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
174   if ((anOwnSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
175        anOwnSurf->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
176       (anOtherSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
177        anOtherSurf->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
178   {
179     GeomCylinderPtr anOwnCyl = getCylinder();
180     GeomCylinderPtr anOtherCyl = anOther->getCylinder();
181     if (anOwnCyl && anOtherCyl)
182       return anOwnCyl->isCoincident(anOtherCyl);
183
184     // compare two swept surfaces of the same type
185     if ((anOwnSurf->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion)) &&
186          anOtherSurf->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) ||
187         (anOwnSurf->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution)) &&
188          anOtherSurf->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution)))) {
189       Handle(Geom_SweptSurface) anOwnSwept = Handle(Geom_SweptSurface)::DownCast(anOwnSurf);
190       Handle(Geom_SweptSurface) anOtherSwept = Handle(Geom_SweptSurface)::DownCast(anOtherSurf);
191
192       const gp_Dir& anOwnDir = anOwnSwept->Direction();
193       const gp_Dir& anOtherDir = anOtherSwept->Direction();
194
195       if (anOwnDir.IsParallel(anOtherDir, Precision::Angular())) {
196         Handle(Geom_Curve) anOwnCurve = anOwnSwept->BasisCurve();
197         Handle(Geom_Curve) anOtherCurve = anOtherSwept->BasisCurve();
198         GeomAPI_ExtremaCurveCurve anExtrema(anOwnCurve, anOtherCurve);
199         return anExtrema.Extrema().IsParallel() &&
200                anExtrema.TotalLowerDistance() < Precision::Confusion();
201       }
202     }
203   }
204
205   return false;
206 }
207
208 bool GeomAPI_Face::isCylindrical() const
209 {
210   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
211   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
212   Handle(Geom_RectangularTrimmedSurface) aTrimmed =
213     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
214   if (!aTrimmed.IsNull())
215     aSurf = aTrimmed->BasisSurface();
216   return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
217 }
218
219 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
220 {
221   std::shared_ptr<GeomAPI_Pln> aResult;
222   TopoDS_Shape aShape = this->impl<TopoDS_Shape>();
223   if (aShape.IsNull())
224     return aResult;  // null shape
225   if (aShape.ShapeType() != TopAbs_FACE)
226     return aResult;  // not face
227   TopoDS_Face aFace = TopoDS::Face(aShape);
228   if (aFace.IsNull())
229     return aResult;  // not face
230   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
231   if (aSurf.IsNull())
232     return aResult;  // no surface
233   GeomLib_IsPlanarSurface isPlanarSurf(aSurf);
234   gp_Pln aPln;
235   bool isPlanar = false;
236   if (isPlanarSurf.IsPlanar()) {
237     aPln = isPlanarSurf.Plan();
238     isPlanar = true;
239   }
240   else if (aSurf->IsKind(STANDARD_TYPE(Geom_OffsetSurface))) {
241     Handle(Geom_OffsetSurface) anOffsetSurf = Handle(Geom_OffsetSurface)::DownCast(aSurf);
242     Handle(Geom_Surface) aBasisSurf = anOffsetSurf->BasisSurface();
243     if (aBasisSurf->IsKind(STANDARD_TYPE(Geom_Plane))) {
244       aPln = Handle(Geom_Plane)::DownCast(aBasisSurf)->Pln();
245       gp_Vec aTranslation(aPln.Axis().Direction().XYZ() * anOffsetSurf->Offset());
246       aPln.Translate(aTranslation);
247       isPlanar = true;
248     }
249   }
250
251   if (isPlanar) {
252     double aA, aB, aC, aD;
253     aPln.Coefficients(aA, aB, aC, aD);
254     if (aFace.Orientation() == TopAbs_REVERSED) {
255       aA = -aA;
256       aB = -aB;
257       aC = -aC;
258       aD = -aD;
259     }
260     aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
261   }
262   return aResult;
263 }
264
265 std::shared_ptr<GeomAPI_Sphere> GeomAPI_Face::getSphere() const
266 {
267   GeomSpherePtr aSphere;
268
269   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
270   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
271   if (aSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
272     gp_Sphere aSph = Handle(Geom_SphericalSurface)::DownCast(aSurf)->Sphere();
273     const gp_Pnt& aCenter = aSph.Location();
274     double aRadius = aSph.Radius();
275
276     GeomPointPtr aCenterPnt(new GeomAPI_Pnt(aCenter.X(), aCenter.Y(), aCenter.Z()));
277     aSphere = GeomSpherePtr(new GeomAPI_Sphere(aCenterPnt, aRadius));
278   }
279   return aSphere;
280 }
281
282 std::shared_ptr<GeomAPI_Cylinder> GeomAPI_Face::getCylinder() const
283 {
284   GeomCylinderPtr aCylinder;
285
286   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
287   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
288   if (aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
289     gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf)->Cylinder();
290     gp_Pnt aLoc = aCyl.Location();
291     const gp_Dir& aDir = aCyl.Position().Direction();
292     double aRadius = aCyl.Radius();
293
294     double aUMin, aUMax, aVMin, aVMax;
295     BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
296     double aHeight = aVMax - aVMin;
297
298     aLoc.ChangeCoord() += aDir.XYZ() * aVMin;
299     GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
300     GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
301     aCylinder = GeomCylinderPtr(new GeomAPI_Cylinder(aLocation, aDirection, aRadius, aHeight));
302   }
303   return aCylinder;
304 }
305
306 std::shared_ptr<GeomAPI_Cone> GeomAPI_Face::getCone() const
307 {
308   GeomConePtr aCone;
309
310   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
311   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
312   if (aSurf->IsKind(STANDARD_TYPE(Geom_ConicalSurface))) {
313     gp_Cone aCon = Handle(Geom_ConicalSurface)::DownCast(aSurf)->Cone();
314     gp_Pnt aLoc = aCon.Location();
315     gp_Dir aDir = aCon.Position().Direction();
316
317     double aUMin, aUMax, aVMin, aVMax;
318     BRepTools::UVBounds(aFace, aUMin, aUMax, aVMin, aVMax);
319
320     double aSemiAngle = Abs(aCon.SemiAngle());
321     double aRadius1 = Abs(aCon.RefRadius() + aVMin * Sin(aCon.SemiAngle()));
322     double aRadius2 = Abs(aCon.RefRadius() + aVMax * Sin(aCon.SemiAngle()));
323
324     aLoc.ChangeCoord() += aDir.XYZ() * aVMin * Cos(aCon.SemiAngle());
325
326     GeomPointPtr aLocation(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
327     GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
328     aCone = GeomConePtr(new GeomAPI_Cone(aLocation, aDirection, aSemiAngle, aRadius1, aRadius2));
329   }
330   return aCone;
331 }
332
333 std::shared_ptr<GeomAPI_Torus> GeomAPI_Face::getTorus() const
334 {
335   GeomTorusPtr aTorus;
336
337   const TopoDS_Face& aFace = TopoDS::Face(impl<TopoDS_Shape>());
338   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
339   if (aSurf->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))) {
340     gp_Torus aTor = Handle(Geom_ToroidalSurface)::DownCast(aSurf)->Torus();
341     const gp_Pnt& aLoc = aTor.Location();
342     const gp_Dir& aDir = aTor.Position().Direction();
343     double aMajorRadius = aTor.MajorRadius();
344     double aMinorRadius = aTor.MinorRadius();
345
346     GeomPointPtr aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
347     GeomDirPtr aDirection(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
348     aTorus = GeomTorusPtr(new GeomAPI_Torus(aCenter, aDirection, aMajorRadius, aMinorRadius));
349   }
350   return aTorus;
351 }
352
353 GeomPointPtr GeomAPI_Face::middlePoint() const
354 {
355   GeomPointPtr anInnerPoint;
356
357   const TopoDS_Face& aFace = impl<TopoDS_Face>();
358   if (aFace.IsNull())
359     return anInnerPoint;
360
361   BRepGProp_Face aProp(aFace);
362   double aUMin, aUMax, aVMin, aVMax;
363   aProp.Bounds(aUMin, aUMax, aVMin, aVMax);
364
365   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
366   if (aSurf.IsNull())
367     return anInnerPoint;
368
369   gp_Pnt aPnt = aSurf->Value((aUMin + aUMax) * 0.5, (aVMin + aVMax) * 0.5);
370   anInnerPoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
371   return anInnerPoint;
372 }