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