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