]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp
Salome HOME
Issue #1369: Added "SubShapes" feature.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeTools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_ShapeTools.h
4 // Created:     3 August 2015
5 // Author:      Dmitry Bobylev
6
7 #include "GeomAlgoAPI_ShapeTools.h"
8
9 #include "GeomAlgoAPI_SketchBuilder.h"
10
11 #include <GeomAPI_Dir.h>
12 #include <GeomAPI_PlanarEdges.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_Pnt.h>
15
16 #include <Bnd_Box.hxx>
17 #include <BOPTools.hxx>
18 #include <BRep_Builder.hxx>
19 #include <BRepAdaptor_Curve.hxx>
20 #include <BRepAlgo_FaceRestrictor.hxx>
21 #include <BRepBndLib.hxx>
22 #include <BRepBuilderAPI_FindPlane.hxx>
23 #include <BRepBuilderAPI_MakeEdge.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <BRepExtrema_DistShapeShape.hxx>
26 #include <BRepGProp.hxx>
27 #include <BRepTopAdaptor_FClass2d.hxx>
28 #include <Geom_Curve.hxx>
29 #include <Geom2d_Curve.hxx>
30 #include <BRepLib_CheckCurveOnSurface.hxx>
31 #include <BRep_Tool.hxx>
32 #include <Geom_Plane.hxx>
33 #include <GeomLib_IsPlanarSurface.hxx>
34 #include <GeomLib_Tool.hxx>
35 #include <GeomProjLib.hxx>
36 #include <gp_Pln.hxx>
37 #include <GProp_GProps.hxx>
38 #include <IntAna_IntConicQuad.hxx>
39 #include <IntAna_Quadric.hxx>
40 #include <NCollection_Vector.hxx>
41 #include <ShapeAnalysis.hxx>
42 #include <ShapeAnalysis_Surface.hxx>
43 #include <TopoDS_Builder.hxx>
44 #include <TopoDS_Edge.hxx>
45 #include <TopoDS_Face.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS_Shell.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <TopoDS.hxx>
50 #include <TopExp_Explorer.hxx>
51
52 //=================================================================================================
53 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
54 {
55   GProp_GProps aGProps;
56   if(!theShape.get()) {
57     return 0.0;
58   }
59   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
60   if(aShape.IsNull()) {
61     return 0.0;
62   }
63   const Standard_Real anEps = 1.e-6;
64   BRepGProp::VolumeProperties(aShape, aGProps, anEps);
65   return aGProps.Mass();
66 }
67
68 //=================================================================================================
69 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
70 {
71   GProp_GProps aGProps;
72   if(!theShape) {
73     return std::shared_ptr<GeomAPI_Pnt>();
74   }
75   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
76   if(aShape.IsNull()) {
77     return std::shared_ptr<GeomAPI_Pnt>();
78   }
79   gp_Pnt aCentre;
80   if(aShape.ShapeType() == TopAbs_VERTEX) {
81     aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
82   } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
83     BRepGProp::LinearProperties(aShape, aGProps);
84     aCentre = aGProps.CentreOfMass();
85   } else {
86     BRepGProp::SurfaceProperties(aShape, aGProps);
87     aCentre = aGProps.CentreOfMass();
88   }
89   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
90 }
91
92 //=================================================================================================
93 void GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
94                                            const GeomAPI_Shape::ShapeType theType,
95                                            ListOfShape& theCombinedShapes,
96                                            ListOfShape& theFreeShapes)
97 {
98   if(!theCompound.get()) {
99     return;
100   }
101
102   if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
103     return;
104   }
105
106   TopAbs_ShapeEnum aTS = TopAbs_EDGE;
107   TopAbs_ShapeEnum aTA = TopAbs_FACE;
108   if(theType == GeomAPI_Shape::COMPSOLID) {
109     aTS = TopAbs_FACE;
110     aTA = TopAbs_SOLID;
111   }
112
113   // Get free shapes.
114   const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
115   for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
116     const TopoDS_Shape& aShape = anIter.Value();
117     if(aShape.ShapeType() > aTA) {
118       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
119       aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
120       theFreeShapes.push_back(aGeomShape);
121     }
122   }
123
124   // Map subshapes and shapes.
125   BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
126   BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
127   if(aMapSA.IsEmpty()) {
128     return;
129   }
130
131   // Get all shapes with common subshapes and free shapes.
132   NCollection_Map<TopoDS_Shape> aFreeShapes;
133   NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
134   for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
135     const TopoDS_Shape& aShape = anIter.Key();
136     BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
137     if(aListOfShape.IsEmpty()) {
138       continue;
139     }
140     else if(aListOfShape.Size() == 1) {
141       const TopoDS_Shape& aF = aListOfShape.First();
142       aFreeShapes.Add(aF);
143       aListOfShape.Clear();
144     } else {
145       NCollection_List<TopoDS_Shape> aTempList;
146       NCollection_Map<TopoDS_Shape> aTempMap;
147       const TopoDS_Shape& aF = aListOfShape.First();
148       const TopoDS_Shape& aL = aListOfShape.Last();
149       aTempList.Append(aF);
150       aTempList.Append(aL);
151       aTempMap.Add(aF);
152       aTempMap.Add(aL);
153       aFreeShapes.Remove(aF);
154       aFreeShapes.Remove(aL);
155       aListOfShape.Clear();
156       for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
157         const TopoDS_Shape& aTempShape = aTempIter.Value();
158         for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
159           BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
160           if(aTempListOfShape.IsEmpty()) {
161             continue;
162           } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
163             aTempListOfShape.Clear();
164           } else if(aTempListOfShape.Size() > 1) {
165             if(aTempListOfShape.First() == aTempShape) {
166               const TopoDS_Shape& aTL = aTempListOfShape.Last();
167               if(aTempMap.Add(aTL)) {
168                 aTempList.Append(aTL);
169                 aFreeShapes.Remove(aTL);
170               }
171               aTempListOfShape.Clear();
172             } else if(aTempListOfShape.Last() == aTempShape) {
173               const TopoDS_Shape& aTF = aTempListOfShape.First();
174               if(aTempMap.Add(aTF)) {
175                 aTempList.Append(aTF);
176                 aFreeShapes.Remove(aTF);
177               }
178               aTempListOfShape.Clear();
179             }
180           }
181         }
182       }
183       aShapesWithCommonSubshapes.Append(aTempMap);
184     }
185   }
186
187   // Combine shapes with common subshapes.
188   for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
189     TopoDS_Shell aShell;
190     TopoDS_CompSolid aCSolid;
191     TopoDS_Builder aBuilder;
192     theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
193     NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
194     for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
195       const TopoDS_Shape& aShape = anExp.Current();
196       if(aShapesMap.Contains(aShape)) {
197         theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
198         aShapesMap.Remove(aShape);
199       }
200     }
201     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
202     TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
203     aGeomShape->setImpl<TopoDS_Shape>(aSh);
204     theCombinedShapes.push_back(aGeomShape);
205   }
206
207   // Adding free shapes.
208   for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
209     const TopoDS_Shape& aShape = anExp.Current();
210     if(aFreeShapes.Contains(aShape)) {
211       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
212       aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
213       theFreeShapes.push_back(aGeomShape);
214     }
215   }
216 }
217
218 //=================================================================================================
219 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
220 {
221   // Bounding box of all objects.
222   Bnd_Box aBndBox;
223
224   // Getting box.
225   for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
226     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
227     BRepBndLib::Add(aShape, aBndBox);
228   }
229
230   if(theEnlarge != 0.0) {
231     // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
232     aBndBox.Enlarge(theEnlarge);
233   }
234
235   Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
236   Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
237   Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
238   std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
239   int aNum = 0;
240   for(int i = 0; i < 2; i++) {
241     for(int j = 0; j < 2; j++) {
242       for(int k = 0; k < 2; k++) {
243         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
244         aResultPoints.push_back(aPnt);
245       }
246     }
247   }
248
249   return aResultPoints;
250 }
251
252 //=================================================================================================
253 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
254 {
255   if (!theFace.get())
256     return std::shared_ptr<GeomAPI_Shape>();
257
258   TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
259   if (aPlaneFace.IsNull())
260     return std::shared_ptr<GeomAPI_Shape>();
261
262   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
263   if (aPlane.IsNull())
264     return std::shared_ptr<GeomAPI_Shape>();
265
266   // make an infinity face on the plane
267   TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
268
269   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
270   aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
271   return aResult;
272 }
273
274 //=================================================================================================
275 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
276                                                                      const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
277 {
278   std::shared_ptr<GeomAPI_Shape> aResultShape;
279
280   if(!thePlane.get()) {
281     return aResultShape;
282   }
283
284   const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
285   if(aShape.ShapeType() != TopAbs_FACE) {
286     return aResultShape;
287   }
288
289   TopoDS_Face aFace = TopoDS::Face(aShape);
290   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
291   if(aSurf.IsNull()) {
292     return aResultShape;
293   }
294
295   GeomLib_IsPlanarSurface isPlanar(aSurf);
296   if(!isPlanar.IsPlanar()) {
297     return aResultShape;
298   }
299
300   if(thePoints.size() != 8) {
301     return aResultShape;
302   }
303
304   const gp_Pln& aFacePln = isPlanar.Plan();
305   Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
306   IntAna_Quadric aQuadric(aFacePln);
307   Standard_Real UMin, UMax, VMin, VMax;
308   UMin = UMax = VMin = VMax = 0;
309   for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
310     const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
311     gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
312     IntAna_IntConicQuad anIntAna(aLin, aQuadric);
313     const gp_Pnt& aPntOnFace = anIntAna.Point(1);
314     Standard_Real aPntU(0), aPntV(0);
315     GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
316     if(aPntU < UMin) UMin = aPntU;
317     if(aPntU > UMax) UMax = aPntU;
318     if(aPntV < VMin) VMin = aPntV;
319     if(aPntV > VMax) VMax = aPntV;
320   }
321   aResultShape.reset(new GeomAPI_Shape);
322   aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
323
324   return aResultShape;
325 }
326
327 //=================================================================================================
328 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
329                                         std::shared_ptr<GeomAPI_Vertex>& theV1,
330                                         std::shared_ptr<GeomAPI_Vertex>& theV2)
331 {
332   if(!theShape.get()) {
333     std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
334     aVertex->setImpl(new TopoDS_Vertex());
335     theV1 = aVertex;
336     theV2 = aVertex;
337     return;
338   }
339
340   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
341   TopoDS_Vertex aV1, aV2;
342   ShapeAnalysis::FindBounds(aShape, aV1, aV2);
343
344   std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
345   aGeomV1->setImpl(new TopoDS_Vertex(aV1));
346   aGeomV2->setImpl(new TopoDS_Vertex(aV2));
347   theV1 = aGeomV1;
348   theV2 = aGeomV2;
349 }
350
351 //=================================================================================================
352 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
353                                                 const std::shared_ptr<GeomAPI_Dir> theDirection,
354                                                 const ListOfShape& theWires,
355                                                 ListOfShape& theFaces)
356 {
357   BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
358                                          theDirection->impl<gp_Dir>()));
359   TopoDS_Face aFace = aMKFace.Face();
360
361   BRepAlgo_FaceRestrictor aFRestrictor;
362   aFRestrictor.Init(aFace, Standard_False, Standard_True);
363   for(ListOfShape::const_iterator anIt = theWires.cbegin();
364       anIt != theWires.cend();
365       ++anIt) {
366     TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
367     aFRestrictor.Add(aWire);
368   }
369
370   aFRestrictor.Perform();
371
372   if(!aFRestrictor.IsDone()) {
373     return;
374   }
375
376   for(; aFRestrictor.More(); aFRestrictor.Next()) {
377     GeomShapePtr aShape(new GeomAPI_Shape());
378     aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
379     theFaces.push_back(aShape);
380   }
381 }
382
383 //=================================================================================================
384 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
385 {
386   TopoDS_Compound aCompound;
387   BRep_Builder aBuilder;
388   aBuilder.MakeCompound(aCompound);
389
390   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
391     aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
392   }
393   BRepBuilderAPI_FindPlane aFindPlane(aCompound);
394
395   if(aFindPlane.Found() != Standard_True) {
396     return std::shared_ptr<GeomAPI_Pln>();
397   }
398
399   Handle(Geom_Plane) aPlane = aFindPlane.Plane();
400   gp_Pnt aLoc = aPlane->Location();
401   gp_Dir aDir = aPlane->Axis().Direction();
402
403   std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
404   std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
405
406   std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
407
408   return aPln;
409 }
410
411 //=================================================================================================
412 bool GeomAlgoAPI_ShapeTools::isSubShapeInShape(const std::shared_ptr<GeomAPI_Shape> theSubShape,
413                                                const std::shared_ptr<GeomAPI_Shape> theBaseShape)
414 {
415   if(!theSubShape.get() || !theBaseShape.get()) {
416     return false;
417   }
418
419   const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
420   const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
421
422   if(aSubShape.ShapeType() == TopAbs_VERTEX) {
423     // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
424     BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
425     aDist.Perform();
426     if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
427       return false;
428     }
429   } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
430     if(aBaseShape.ShapeType() == TopAbs_FACE) {
431       // Check that edge is on face surface.
432       TopoDS_Face aFace = TopoDS::Face(aBaseShape);
433       TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
434       BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
435       aCheck.Perform();
436       if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
437         return false;
438       }
439
440       // Check intersections.
441       TopoDS_Vertex aV1, aV2;
442       ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
443       gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
444       gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
445       for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
446         const TopoDS_Shape& anEdgeOnFace = anExp.Current();
447         BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
448         aDist.Perform();
449         if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
450           // Edge intersect face bound. Check that it is not on edge begin or end.
451           for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
452             gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
453             if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
454                 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
455               return false;
456             }
457           }
458         }
459       }
460
461       // No intersections found. Edge is inside or outside face. Check it.
462       BRepAdaptor_Curve aCurveAdaptor(anEdge);
463       gp_Pnt aPointToCheck = aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() + aCurveAdaptor.LastParameter()) / 2.0);
464       Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
465       ShapeAnalysis_Surface aSAS(aSurface);
466       gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
467       BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
468       if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
469         return false;
470       }
471
472     } else {
473       return false;
474     }
475   } else {
476     return false;
477   }
478
479   return true;
480 }