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