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