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>();
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();
71 BRepGProp::SurfaceProperties(aShape, aGProps);
72 aCentre = aGProps.CentreOfMass();
74 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
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)
83 if(!theCompound.get()) {
87 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
91 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
92 TopAbs_ShapeEnum aTA = TopAbs_FACE;
93 if(theType == GeomAPI_Shape::COMPSOLID) {
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);
109 // Map subshapes and shapes.
110 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
111 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
112 if(aMapSA.IsEmpty()) {
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()) {
125 else if(aListOfShape.Size() == 1) {
126 const TopoDS_Shape& aF = aListOfShape.First();
128 aListOfShape.Clear();
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);
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()) {
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);
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);
163 aTempListOfShape.Clear();
168 aShapesWithCommonSubshapes.Append(aTempMap);
172 // Combine shapes with common subshapes.
173 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
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);
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);
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);
203 //=================================================================================================
204 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
206 // Bounding box of all objects.
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);
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);
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;
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);
234 return aResultPoints;
237 //=================================================================================================
238 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
241 return std::shared_ptr<GeomAPI_Shape>();
243 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
244 if (aPlaneFace.IsNull())
245 return std::shared_ptr<GeomAPI_Shape>();
247 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
249 return std::shared_ptr<GeomAPI_Shape>();
251 // make an infinity face on the plane
252 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
254 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
255 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
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)
263 std::shared_ptr<GeomAPI_Shape> aResultShape;
265 if(!thePlane.get()) {
269 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
270 if(aShape.ShapeType() != TopAbs_FACE) {
274 TopoDS_Face aFace = TopoDS::Face(aShape);
275 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
280 GeomLib_IsPlanarSurface isPlanar(aSurf);
281 if(!isPlanar.IsPlanar()) {
285 if(thePoints.size() != 8) {
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;
306 aResultShape.reset(new GeomAPI_Shape);
307 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
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)
317 if(!theShape.get()) {
318 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
319 aVertex->setImpl(new TopoDS_Vertex());
325 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
326 TopoDS_Vertex aV1, aV2;
327 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
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));