]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp
Salome HOME
47d05e566058953cd77ab69d04fc1f59493010e6
[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 <BRepAlgo_FaceRestrictor.hxx>
20 #include <BRepBndLib.hxx>
21 #include <BRepBuilderAPI_FindPlane.hxx>
22 #include <BRepBuilderAPI_MakeFace.hxx>
23 #include <BRepGProp.hxx>
24 #include <BRep_Tool.hxx>
25 #include <Geom_Plane.hxx>
26 #include <GeomLib_IsPlanarSurface.hxx>
27 #include <GeomLib_Tool.hxx>
28 #include <gp_Pln.hxx>
29 #include <GProp_GProps.hxx>
30 #include <IntAna_IntConicQuad.hxx>
31 #include <IntAna_Quadric.hxx>
32 #include <NCollection_Vector.hxx>
33 #include <ShapeAnalysis.hxx>
34 #include <TopoDS_Builder.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Shell.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <TopoDS.hxx>
41 #include <TopExp_Explorer.hxx>
42
43 //=================================================================================================
44 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
45 {
46   GProp_GProps aGProps;
47   if(!theShape.get()) {
48     return 0.0;
49   }
50   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
51   if(aShape.IsNull()) {
52     return 0.0;
53   }
54   const Standard_Real anEps = 1.e-6;
55   BRepGProp::VolumeProperties(aShape, aGProps, anEps);
56   return aGProps.Mass();
57 }
58
59 //=================================================================================================
60 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
61 {
62   GProp_GProps aGProps;
63   if(!theShape) {
64     return std::shared_ptr<GeomAPI_Pnt>();
65   }
66   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
67   if(aShape.IsNull()) {
68     return std::shared_ptr<GeomAPI_Pnt>();
69   }
70   gp_Pnt aCentre;
71   if(aShape.ShapeType() == TopAbs_VERTEX) {
72     aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
73   } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
74     BRepGProp::LinearProperties(aShape, aGProps);
75     aCentre = aGProps.CentreOfMass();
76   } else {
77     BRepGProp::SurfaceProperties(aShape, aGProps);
78     aCentre = aGProps.CentreOfMass();
79   }
80   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
81 }
82
83 //=================================================================================================
84 void GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
85                                            const GeomAPI_Shape::ShapeType theType,
86                                            ListOfShape& theCombinedShapes,
87                                            ListOfShape& theFreeShapes)
88 {
89   if(!theCompound.get()) {
90     return;
91   }
92
93   if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
94     return;
95   }
96
97   TopAbs_ShapeEnum aTS = TopAbs_EDGE;
98   TopAbs_ShapeEnum aTA = TopAbs_FACE;
99   if(theType == GeomAPI_Shape::COMPSOLID) {
100     aTS = TopAbs_FACE;
101     aTA = TopAbs_SOLID;
102   }
103
104   // Get free shapes.
105   const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
106   for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
107     const TopoDS_Shape& aShape = anIter.Value();
108     if(aShape.ShapeType() > aTA) {
109       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
110       aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
111       theFreeShapes.push_back(aGeomShape);
112     }
113   }
114
115   // Map subshapes and shapes.
116   BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
117   BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
118   if(aMapSA.IsEmpty()) {
119     return;
120   }
121
122   // Get all shapes with common subshapes and free shapes.
123   NCollection_Map<TopoDS_Shape> aFreeShapes;
124   NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
125   for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
126     const TopoDS_Shape& aShape = anIter.Key();
127     BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
128     if(aListOfShape.IsEmpty()) {
129       continue;
130     }
131     else if(aListOfShape.Size() == 1) {
132       const TopoDS_Shape& aF = aListOfShape.First();
133       aFreeShapes.Add(aF);
134       aListOfShape.Clear();
135     } else {
136       NCollection_List<TopoDS_Shape> aTempList;
137       NCollection_Map<TopoDS_Shape> aTempMap;
138       const TopoDS_Shape& aF = aListOfShape.First();
139       const TopoDS_Shape& aL = aListOfShape.Last();
140       aTempList.Append(aF);
141       aTempList.Append(aL);
142       aTempMap.Add(aF);
143       aTempMap.Add(aL);
144       aFreeShapes.Remove(aF);
145       aFreeShapes.Remove(aL);
146       aListOfShape.Clear();
147       for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
148         const TopoDS_Shape& aTempShape = aTempIter.Value();
149         for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
150           BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
151           if(aTempListOfShape.IsEmpty()) {
152             continue;
153           } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
154             aTempListOfShape.Clear();
155           } else if(aTempListOfShape.Size() > 1) {
156             if(aTempListOfShape.First() == aTempShape) {
157               const TopoDS_Shape& aTL = aTempListOfShape.Last();
158               if(aTempMap.Add(aTL)) {
159                 aTempList.Append(aTL);
160                 aFreeShapes.Remove(aTL);
161               }
162               aTempListOfShape.Clear();
163             } else if(aTempListOfShape.Last() == aTempShape) {
164               const TopoDS_Shape& aTF = aTempListOfShape.First();
165               if(aTempMap.Add(aTF)) {
166                 aTempList.Append(aTF);
167                 aFreeShapes.Remove(aTF);
168               }
169               aTempListOfShape.Clear();
170             }
171           }
172         }
173       }
174       aShapesWithCommonSubshapes.Append(aTempMap);
175     }
176   }
177
178   // Combine shapes with common subshapes.
179   for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
180     TopoDS_Shell aShell;
181     TopoDS_CompSolid aCSolid;
182     TopoDS_Builder aBuilder;
183     theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
184     NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
185     for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
186       const TopoDS_Shape& aShape = anExp.Current();
187       if(aShapesMap.Contains(aShape)) {
188         theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
189         aShapesMap.Remove(aShape);
190       }
191     }
192     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
193     TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
194     aGeomShape->setImpl<TopoDS_Shape>(aSh);
195     theCombinedShapes.push_back(aGeomShape);
196   }
197
198   // Adding free shapes.
199   for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
200     const TopoDS_Shape& aShape = anExp.Current();
201     if(aFreeShapes.Contains(aShape)) {
202       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
203       aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
204       theFreeShapes.push_back(aGeomShape);
205     }
206   }
207 }
208
209 //=================================================================================================
210 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
211 {
212   // Bounding box of all objects.
213   Bnd_Box aBndBox;
214
215   // Getting box.
216   for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
217     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
218     BRepBndLib::Add(aShape, aBndBox);
219   }
220
221   if(theEnlarge != 0.0) {
222     // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
223     aBndBox.Enlarge(theEnlarge);
224   }
225
226   Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
227   Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
228   Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
229   std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
230   int aNum = 0;
231   for(int i = 0; i < 2; i++) {
232     for(int j = 0; j < 2; j++) {
233       for(int k = 0; k < 2; k++) {
234         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
235         aResultPoints.push_back(aPnt);
236       }
237     }
238   }
239
240   return aResultPoints;
241 }
242
243 //=================================================================================================
244 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
245 {
246   if (!theFace.get())
247     return std::shared_ptr<GeomAPI_Shape>();
248
249   TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
250   if (aPlaneFace.IsNull())
251     return std::shared_ptr<GeomAPI_Shape>();
252
253   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
254   if (aPlane.IsNull())
255     return std::shared_ptr<GeomAPI_Shape>();
256
257   // make an infinity face on the plane
258   TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
259
260   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
261   aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
262   return aResult;
263 }
264
265 //=================================================================================================
266 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
267                                                                      const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
268 {
269   std::shared_ptr<GeomAPI_Shape> aResultShape;
270
271   if(!thePlane.get()) {
272     return aResultShape;
273   }
274
275   const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
276   if(aShape.ShapeType() != TopAbs_FACE) {
277     return aResultShape;
278   }
279
280   TopoDS_Face aFace = TopoDS::Face(aShape);
281   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
282   if(aSurf.IsNull()) {
283     return aResultShape;
284   }
285
286   GeomLib_IsPlanarSurface isPlanar(aSurf);
287   if(!isPlanar.IsPlanar()) {
288     return aResultShape;
289   }
290
291   if(thePoints.size() != 8) {
292     return aResultShape;
293   }
294
295   const gp_Pln& aFacePln = isPlanar.Plan();
296   Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
297   IntAna_Quadric aQuadric(aFacePln);
298   Standard_Real UMin, UMax, VMin, VMax;
299   UMin = UMax = VMin = VMax = 0;
300   for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
301     const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
302     gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
303     IntAna_IntConicQuad anIntAna(aLin, aQuadric);
304     const gp_Pnt& aPntOnFace = anIntAna.Point(1);
305     Standard_Real aPntU(0), aPntV(0);
306     GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
307     if(aPntU < UMin) UMin = aPntU;
308     if(aPntU > UMax) UMax = aPntU;
309     if(aPntV < VMin) VMin = aPntV;
310     if(aPntV > VMax) VMax = aPntV;
311   }
312   aResultShape.reset(new GeomAPI_Shape);
313   aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
314
315   return aResultShape;
316 }
317
318 //=================================================================================================
319 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
320                                         std::shared_ptr<GeomAPI_Vertex>& theV1,
321                                         std::shared_ptr<GeomAPI_Vertex>& theV2)
322 {
323   if(!theShape.get()) {
324     std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
325     aVertex->setImpl(new TopoDS_Vertex());
326     theV1 = aVertex;
327     theV2 = aVertex;
328     return;
329   }
330
331   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
332   TopoDS_Vertex aV1, aV2;
333   ShapeAnalysis::FindBounds(aShape, aV1, aV2);
334
335   std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
336   aGeomV1->setImpl(new TopoDS_Vertex(aV1));
337   aGeomV2->setImpl(new TopoDS_Vertex(aV2));
338   theV1 = aGeomV1;
339   theV2 = aGeomV2;
340 }
341
342 //=================================================================================================
343 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
344                                                 const std::shared_ptr<GeomAPI_Dir> theDirection,
345                                                 const ListOfShape& theWires,
346                                                 ListOfShape& theFaces)
347 {
348   BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
349                                          theDirection->impl<gp_Dir>()));
350   TopoDS_Face aFace = aMKFace.Face();
351
352   BRepAlgo_FaceRestrictor aFRestrictor;
353   aFRestrictor.Init(aFace, Standard_False, Standard_True);
354   for(ListOfShape::const_iterator anIt = theWires.cbegin();
355       anIt != theWires.cend();
356       ++anIt) {
357     TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
358     aFRestrictor.Add(aWire);
359   }
360
361   aFRestrictor.Perform();
362
363   if(!aFRestrictor.IsDone()) {
364     return;
365   }
366
367   for(; aFRestrictor.More(); aFRestrictor.Next()) {
368     GeomShapePtr aShape(new GeomAPI_Shape());
369     aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
370     theFaces.push_back(aShape);
371   }
372 }
373
374 //=================================================================================================
375 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
376 {
377   TopoDS_Compound aCompound;
378   BRep_Builder aBuilder;
379   aBuilder.MakeCompound(aCompound);
380
381   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
382     aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
383   }
384   BRepBuilderAPI_FindPlane aFindPlane(aCompound);
385
386   if(aFindPlane.Found() != Standard_True) {
387     return std::shared_ptr<GeomAPI_Pln>();
388   }
389
390   Handle(Geom_Plane) aPlane = aFindPlane.Plane();
391   gp_Pnt aLoc = aPlane->Location();
392   gp_Dir aDir = aPlane->Axis().Direction();
393
394   std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
395   std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
396
397   std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
398
399   return aPln;
400 }