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