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