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