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_SketchBuilder.h"
11 #include <GeomAPI_Dir.h>
12 #include <GeomAPI_PlanarEdges.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_Pnt.h>
16 #include <Bnd_Box.hxx>
17 #include <BOPTools.hxx>
18 #include <BRep_Builder.hxx>
19 #include <BRepAlgo_FaceRestrictor.hxx>
20 #include <BRepBndLib.hxx>
21 #include <BRepBuilderAPI_FindPlane.hxx>
22 #include <BRepBuilderAPI_MakeFace.hxx>
23 #include <BRepGProp.hxx>
24 #include <BRep_Tool.hxx>
25 #include <Geom_Plane.hxx>
26 #include <GeomLib_IsPlanarSurface.hxx>
27 #include <GeomLib_Tool.hxx>
29 #include <GProp_GProps.hxx>
30 #include <IntAna_IntConicQuad.hxx>
31 #include <IntAna_Quadric.hxx>
32 #include <NCollection_Vector.hxx>
33 #include <ShapeAnalysis.hxx>
34 #include <TopoDS_Builder.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Shell.hxx>
39 #include <TopoDS_Vertex.hxx>
41 #include <TopExp_Explorer.hxx>
43 //=================================================================================================
44 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
50 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
54 const Standard_Real anEps = 1.e-6;
55 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
56 return aGProps.Mass();
59 //=================================================================================================
60 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
64 return std::shared_ptr<GeomAPI_Pnt>();
66 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
68 return std::shared_ptr<GeomAPI_Pnt>();
71 if(aShape.ShapeType() == TopAbs_VERTEX) {
72 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
73 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
74 BRepGProp::LinearProperties(aShape, aGProps);
75 aCentre = aGProps.CentreOfMass();
77 BRepGProp::SurfaceProperties(aShape, aGProps);
78 aCentre = aGProps.CentreOfMass();
80 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
83 //=================================================================================================
84 void GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
85 const GeomAPI_Shape::ShapeType theType,
86 ListOfShape& theCombinedShapes,
87 ListOfShape& theFreeShapes)
89 if(!theCompound.get()) {
93 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
97 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
98 TopAbs_ShapeEnum aTA = TopAbs_FACE;
99 if(theType == GeomAPI_Shape::COMPSOLID) {
105 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
106 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
107 const TopoDS_Shape& aShape = anIter.Value();
108 if(aShape.ShapeType() > aTA) {
109 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
110 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
111 theFreeShapes.push_back(aGeomShape);
115 // Map subshapes and shapes.
116 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
117 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
118 if(aMapSA.IsEmpty()) {
122 // Get all shapes with common subshapes and free shapes.
123 NCollection_Map<TopoDS_Shape> aFreeShapes;
124 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
125 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
126 const TopoDS_Shape& aShape = anIter.Key();
127 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
128 if(aListOfShape.IsEmpty()) {
131 else if(aListOfShape.Size() == 1) {
132 const TopoDS_Shape& aF = aListOfShape.First();
134 aListOfShape.Clear();
136 NCollection_List<TopoDS_Shape> aTempList;
137 NCollection_Map<TopoDS_Shape> aTempMap;
138 const TopoDS_Shape& aF = aListOfShape.First();
139 const TopoDS_Shape& aL = aListOfShape.Last();
140 aTempList.Append(aF);
141 aTempList.Append(aL);
144 aFreeShapes.Remove(aF);
145 aFreeShapes.Remove(aL);
146 aListOfShape.Clear();
147 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
148 const TopoDS_Shape& aTempShape = aTempIter.Value();
149 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
150 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
151 if(aTempListOfShape.IsEmpty()) {
153 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
154 aTempListOfShape.Clear();
155 } else if(aTempListOfShape.Size() > 1) {
156 if(aTempListOfShape.First() == aTempShape) {
157 const TopoDS_Shape& aTL = aTempListOfShape.Last();
158 if(aTempMap.Add(aTL)) {
159 aTempList.Append(aTL);
160 aFreeShapes.Remove(aTL);
162 aTempListOfShape.Clear();
163 } else if(aTempListOfShape.Last() == aTempShape) {
164 const TopoDS_Shape& aTF = aTempListOfShape.First();
165 if(aTempMap.Add(aTF)) {
166 aTempList.Append(aTF);
167 aFreeShapes.Remove(aTF);
169 aTempListOfShape.Clear();
174 aShapesWithCommonSubshapes.Append(aTempMap);
178 // Combine shapes with common subshapes.
179 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
181 TopoDS_CompSolid aCSolid;
182 TopoDS_Builder aBuilder;
183 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
184 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
185 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
186 const TopoDS_Shape& aShape = anExp.Current();
187 if(aShapesMap.Contains(aShape)) {
188 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
189 aShapesMap.Remove(aShape);
192 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
193 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
194 aGeomShape->setImpl<TopoDS_Shape>(aSh);
195 theCombinedShapes.push_back(aGeomShape);
198 // Adding free shapes.
199 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
200 const TopoDS_Shape& aShape = anExp.Current();
201 if(aFreeShapes.Contains(aShape)) {
202 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
203 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
204 theFreeShapes.push_back(aGeomShape);
209 //=================================================================================================
210 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
212 // Bounding box of all objects.
216 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
217 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
218 BRepBndLib::Add(aShape, aBndBox);
221 if(theEnlarge != 0.0) {
222 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
223 aBndBox.Enlarge(theEnlarge);
226 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
227 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
228 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
229 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
231 for(int i = 0; i < 2; i++) {
232 for(int j = 0; j < 2; j++) {
233 for(int k = 0; k < 2; k++) {
234 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
235 aResultPoints.push_back(aPnt);
240 return aResultPoints;
243 //=================================================================================================
244 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
247 return std::shared_ptr<GeomAPI_Shape>();
249 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
250 if (aPlaneFace.IsNull())
251 return std::shared_ptr<GeomAPI_Shape>();
253 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
255 return std::shared_ptr<GeomAPI_Shape>();
257 // make an infinity face on the plane
258 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
260 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
261 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
265 //=================================================================================================
266 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
267 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
269 std::shared_ptr<GeomAPI_Shape> aResultShape;
271 if(!thePlane.get()) {
275 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
276 if(aShape.ShapeType() != TopAbs_FACE) {
280 TopoDS_Face aFace = TopoDS::Face(aShape);
281 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
286 GeomLib_IsPlanarSurface isPlanar(aSurf);
287 if(!isPlanar.IsPlanar()) {
291 if(thePoints.size() != 8) {
295 const gp_Pln& aFacePln = isPlanar.Plan();
296 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
297 IntAna_Quadric aQuadric(aFacePln);
298 Standard_Real UMin, UMax, VMin, VMax;
299 UMin = UMax = VMin = VMax = 0;
300 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
301 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
302 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
303 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
304 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
305 Standard_Real aPntU(0), aPntV(0);
306 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
307 if(aPntU < UMin) UMin = aPntU;
308 if(aPntU > UMax) UMax = aPntU;
309 if(aPntV < VMin) VMin = aPntV;
310 if(aPntV > VMax) VMax = aPntV;
312 aResultShape.reset(new GeomAPI_Shape);
313 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
318 //=================================================================================================
319 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
320 std::shared_ptr<GeomAPI_Vertex>& theV1,
321 std::shared_ptr<GeomAPI_Vertex>& theV2)
323 if(!theShape.get()) {
324 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
325 aVertex->setImpl(new TopoDS_Vertex());
331 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
332 TopoDS_Vertex aV1, aV2;
333 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
335 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
336 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
337 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
342 //=================================================================================================
343 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
344 const std::shared_ptr<GeomAPI_Dir> theDirection,
345 const ListOfShape& theWires,
346 ListOfShape& theFaces)
348 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
349 theDirection->impl<gp_Dir>()));
350 TopoDS_Face aFace = aMKFace.Face();
352 BRepAlgo_FaceRestrictor aFRestrictor;
353 aFRestrictor.Init(aFace, Standard_False, Standard_True);
354 for(ListOfShape::const_iterator anIt = theWires.cbegin();
355 anIt != theWires.cend();
357 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
358 aFRestrictor.Add(aWire);
361 aFRestrictor.Perform();
363 if(!aFRestrictor.IsDone()) {
367 for(; aFRestrictor.More(); aFRestrictor.Next()) {
368 GeomShapePtr aShape(new GeomAPI_Shape());
369 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
370 theFaces.push_back(aShape);
374 //=================================================================================================
375 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
377 TopoDS_Compound aCompound;
378 BRep_Builder aBuilder;
379 aBuilder.MakeCompound(aCompound);
381 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
382 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
384 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
386 if(aFindPlane.Found() != Standard_True) {
387 return std::shared_ptr<GeomAPI_Pln>();
390 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
391 gp_Pnt aLoc = aPlane->Location();
392 gp_Dir aDir = aPlane->Axis().Direction();
394 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
395 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
397 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));