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 <BRepAdaptor_Curve.hxx>
20 #include <BRepAlgo_FaceRestrictor.hxx>
21 #include <BRepBndLib.hxx>
22 #include <BRepBuilderAPI_FindPlane.hxx>
23 #include <BRepBuilderAPI_MakeEdge.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <BRepExtrema_DistShapeShape.hxx>
26 #include <BRepGProp.hxx>
27 #include <BRepTopAdaptor_FClass2d.hxx>
28 #include <Geom_Curve.hxx>
29 #include <Geom2d_Curve.hxx>
30 #include <BRepLib_CheckCurveOnSurface.hxx>
31 #include <BRep_Tool.hxx>
32 #include <Geom_Plane.hxx>
33 #include <GeomLib_IsPlanarSurface.hxx>
34 #include <GeomLib_Tool.hxx>
35 #include <GeomProjLib.hxx>
37 #include <GProp_GProps.hxx>
38 #include <IntAna_IntConicQuad.hxx>
39 #include <IntAna_Quadric.hxx>
40 #include <NCollection_Vector.hxx>
41 #include <ShapeAnalysis.hxx>
42 #include <ShapeAnalysis_Surface.hxx>
43 #include <TopoDS_Builder.hxx>
44 #include <TopoDS_Edge.hxx>
45 #include <TopoDS_Face.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS_Shell.hxx>
48 #include <TopoDS_Vertex.hxx>
50 #include <TopExp_Explorer.hxx>
52 //=================================================================================================
53 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
59 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
63 const Standard_Real anEps = 1.e-6;
64 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
65 return aGProps.Mass();
68 //=================================================================================================
69 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
73 return std::shared_ptr<GeomAPI_Pnt>();
75 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
77 return std::shared_ptr<GeomAPI_Pnt>();
80 if(aShape.ShapeType() == TopAbs_VERTEX) {
81 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
82 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
83 BRepGProp::LinearProperties(aShape, aGProps);
84 aCentre = aGProps.CentreOfMass();
86 BRepGProp::SurfaceProperties(aShape, aGProps);
87 aCentre = aGProps.CentreOfMass();
89 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
92 //=================================================================================================
93 void GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
94 const GeomAPI_Shape::ShapeType theType,
95 ListOfShape& theCombinedShapes,
96 ListOfShape& theFreeShapes)
98 if(!theCompound.get()) {
102 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
106 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
107 TopAbs_ShapeEnum aTA = TopAbs_FACE;
108 if(theType == GeomAPI_Shape::COMPSOLID) {
114 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
115 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
116 const TopoDS_Shape& aShape = anIter.Value();
117 if(aShape.ShapeType() > aTA) {
118 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
119 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
120 theFreeShapes.push_back(aGeomShape);
124 // Map subshapes and shapes.
125 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
126 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
127 if(aMapSA.IsEmpty()) {
131 // Get all shapes with common subshapes and free shapes.
132 NCollection_Map<TopoDS_Shape> aFreeShapes;
133 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
134 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
135 const TopoDS_Shape& aShape = anIter.Key();
136 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
137 if(aListOfShape.IsEmpty()) {
140 else if(aListOfShape.Size() == 1) {
141 const TopoDS_Shape& aF = aListOfShape.First();
143 aListOfShape.Clear();
145 NCollection_List<TopoDS_Shape> aTempList;
146 NCollection_Map<TopoDS_Shape> aTempMap;
147 const TopoDS_Shape& aF = aListOfShape.First();
148 const TopoDS_Shape& aL = aListOfShape.Last();
149 aTempList.Append(aF);
150 aTempList.Append(aL);
153 aFreeShapes.Remove(aF);
154 aFreeShapes.Remove(aL);
155 aListOfShape.Clear();
156 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
157 const TopoDS_Shape& aTempShape = aTempIter.Value();
158 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
159 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
160 if(aTempListOfShape.IsEmpty()) {
162 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
163 aTempListOfShape.Clear();
164 } else if(aTempListOfShape.Size() > 1) {
165 if(aTempListOfShape.First() == aTempShape) {
166 const TopoDS_Shape& aTL = aTempListOfShape.Last();
167 if(aTempMap.Add(aTL)) {
168 aTempList.Append(aTL);
169 aFreeShapes.Remove(aTL);
171 aTempListOfShape.Clear();
172 } else if(aTempListOfShape.Last() == aTempShape) {
173 const TopoDS_Shape& aTF = aTempListOfShape.First();
174 if(aTempMap.Add(aTF)) {
175 aTempList.Append(aTF);
176 aFreeShapes.Remove(aTF);
178 aTempListOfShape.Clear();
183 aShapesWithCommonSubshapes.Append(aTempMap);
187 // Combine shapes with common subshapes.
188 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
190 TopoDS_CompSolid aCSolid;
191 TopoDS_Builder aBuilder;
192 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
193 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
194 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
195 const TopoDS_Shape& aShape = anExp.Current();
196 if(aShapesMap.Contains(aShape)) {
197 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
198 aShapesMap.Remove(aShape);
201 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
202 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
203 aGeomShape->setImpl<TopoDS_Shape>(aSh);
204 theCombinedShapes.push_back(aGeomShape);
207 // Adding free shapes.
208 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
209 const TopoDS_Shape& aShape = anExp.Current();
210 if(aFreeShapes.Contains(aShape)) {
211 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
212 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
213 theFreeShapes.push_back(aGeomShape);
218 //=================================================================================================
219 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
221 // Bounding box of all objects.
225 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
226 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
227 BRepBndLib::Add(aShape, aBndBox);
230 if(theEnlarge != 0.0) {
231 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
232 aBndBox.Enlarge(theEnlarge);
235 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
236 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
237 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
238 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
240 for(int i = 0; i < 2; i++) {
241 for(int j = 0; j < 2; j++) {
242 for(int k = 0; k < 2; k++) {
243 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
244 aResultPoints.push_back(aPnt);
249 return aResultPoints;
252 //=================================================================================================
253 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
256 return std::shared_ptr<GeomAPI_Shape>();
258 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
259 if (aPlaneFace.IsNull())
260 return std::shared_ptr<GeomAPI_Shape>();
262 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
264 return std::shared_ptr<GeomAPI_Shape>();
266 // make an infinity face on the plane
267 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
269 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
270 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
274 //=================================================================================================
275 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
276 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
278 std::shared_ptr<GeomAPI_Shape> aResultShape;
280 if(!thePlane.get()) {
284 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
285 if(aShape.ShapeType() != TopAbs_FACE) {
289 TopoDS_Face aFace = TopoDS::Face(aShape);
290 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
295 GeomLib_IsPlanarSurface isPlanar(aSurf);
296 if(!isPlanar.IsPlanar()) {
300 if(thePoints.size() != 8) {
304 const gp_Pln& aFacePln = isPlanar.Plan();
305 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
306 IntAna_Quadric aQuadric(aFacePln);
307 Standard_Real UMin, UMax, VMin, VMax;
308 UMin = UMax = VMin = VMax = 0;
309 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
310 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
311 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
312 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
313 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
314 Standard_Real aPntU(0), aPntV(0);
315 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
316 if(aPntU < UMin) UMin = aPntU;
317 if(aPntU > UMax) UMax = aPntU;
318 if(aPntV < VMin) VMin = aPntV;
319 if(aPntV > VMax) VMax = aPntV;
321 aResultShape.reset(new GeomAPI_Shape);
322 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
327 //=================================================================================================
328 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
329 std::shared_ptr<GeomAPI_Vertex>& theV1,
330 std::shared_ptr<GeomAPI_Vertex>& theV2)
332 if(!theShape.get()) {
333 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
334 aVertex->setImpl(new TopoDS_Vertex());
340 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
341 TopoDS_Vertex aV1, aV2;
342 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
344 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
345 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
346 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
351 //=================================================================================================
352 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
353 const std::shared_ptr<GeomAPI_Dir> theDirection,
354 const ListOfShape& theWires,
355 ListOfShape& theFaces)
357 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
358 theDirection->impl<gp_Dir>()));
359 TopoDS_Face aFace = aMKFace.Face();
361 BRepAlgo_FaceRestrictor aFRestrictor;
362 aFRestrictor.Init(aFace, Standard_False, Standard_True);
363 for(ListOfShape::const_iterator anIt = theWires.cbegin();
364 anIt != theWires.cend();
366 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
367 aFRestrictor.Add(aWire);
370 aFRestrictor.Perform();
372 if(!aFRestrictor.IsDone()) {
376 for(; aFRestrictor.More(); aFRestrictor.Next()) {
377 GeomShapePtr aShape(new GeomAPI_Shape());
378 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
379 theFaces.push_back(aShape);
383 //=================================================================================================
384 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
386 TopoDS_Compound aCompound;
387 BRep_Builder aBuilder;
388 aBuilder.MakeCompound(aCompound);
390 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
391 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
393 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
395 if(aFindPlane.Found() != Standard_True) {
396 return std::shared_ptr<GeomAPI_Pln>();
399 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
400 gp_Pnt aLoc = aPlane->Location();
401 gp_Dir aDir = aPlane->Axis().Direction();
403 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
404 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
406 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
411 //=================================================================================================
412 bool GeomAlgoAPI_ShapeTools::isSubShapeInShape(const std::shared_ptr<GeomAPI_Shape> theSubShape,
413 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
415 if(!theSubShape.get() || !theBaseShape.get()) {
419 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
420 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
422 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
423 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
424 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
426 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
429 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
430 if(aBaseShape.ShapeType() == TopAbs_FACE) {
431 // Check that edge is on face surface.
432 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
433 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
434 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
436 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
440 // Check intersections.
441 TopoDS_Vertex aV1, aV2;
442 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
443 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
444 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
445 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
446 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
447 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
449 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
450 // Edge intersect face bound. Check that it is not on edge begin or end.
451 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
452 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
453 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
454 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
461 // No intersections found. Edge is inside or outside face. Check it.
462 BRepAdaptor_Curve aCurveAdaptor(anEdge);
463 gp_Pnt aPointToCheck = aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() + aCurveAdaptor.LastParameter()) / 2.0);
464 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
465 ShapeAnalysis_Surface aSAS(aSurface);
466 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
467 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
468 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {