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