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