1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_ShapeTools.h
4 // Created: 3 August 2015
5 // Author: Dmitry Bobylev
7 #include <GeomAlgoAPI_ShapeTools.h>
9 #include <GeomAlgoAPI_CompoundBuilder.h>
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>
35 #include <TopExp_Explorer.hxx>
38 //=================================================================================================
39 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
45 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
49 BRepGProp::VolumeProperties(aShape, aGProps);
50 return aGProps.Mass();
53 //=================================================================================================
54 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
58 return std::shared_ptr<GeomAPI_Pnt>();
60 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
62 return std::shared_ptr<GeomAPI_Pnt>();
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()));
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)
75 if(!theCompound.get()) {
79 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
83 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
84 TopAbs_ShapeEnum aTA = TopAbs_FACE;
85 if(theType == GeomAPI_Shape::COMPSOLID) {
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);
101 // Map subshapes and shapes.
102 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
103 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
104 if(aMapSA.IsEmpty()) {
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()) {
117 else if(aListOfShape.Size() == 1) {
118 const TopoDS_Shape& aF = aListOfShape.First();
120 aListOfShape.Clear();
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);
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()) {
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);
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);
155 aTempListOfShape.Clear();
160 aShapesWithCommonSubshapes.Append(aTempMap);
164 // Combine shapes with common subshapes.
165 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
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);
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);
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);
195 //=================================================================================================
196 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
198 // Bounding box of all objects.
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);
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);
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;
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);
226 return aResultPoints;
229 //=================================================================================================
230 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
233 return std::shared_ptr<GeomAPI_Shape>();
235 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
236 if (aPlaneFace.IsNull())
237 return std::shared_ptr<GeomAPI_Shape>();
239 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
241 return std::shared_ptr<GeomAPI_Shape>();
243 // make an infinity face on the plane
244 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
246 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
247 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
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)
255 std::shared_ptr<GeomAPI_Shape> aResultShape;
257 if(!thePlane.get()) {
261 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
262 if(aShape.ShapeType() != TopAbs_FACE) {
266 TopoDS_Face aFace = TopoDS::Face(aShape);
267 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
272 GeomLib_IsPlanarSurface isPlanar(aSurf);
273 if(!isPlanar.IsPlanar()) {
277 if(thePoints.size() != 8) {
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;
298 aResultShape.reset(new GeomAPI_Shape);
299 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
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)
309 if(!theShape.get()) {
310 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
311 aVertex->setImpl(new TopoDS_Vertex());
317 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
318 TopoDS_Vertex aV1, aV2;
319 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
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));