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 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
94 const GeomAPI_Shape::ShapeType theType,
95 ListOfShape& theCombinedShapes,
96 ListOfShape& theFreeShapes)
98 GeomShapePtr aResult = theCompound;
100 if(!theCompound.get()) {
104 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
108 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
109 TopAbs_ShapeEnum aTA = TopAbs_FACE;
110 if(theType == GeomAPI_Shape::COMPSOLID) {
116 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
117 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
118 const TopoDS_Shape& aShape = anIter.Value();
119 if(aShape.ShapeType() > aTA) {
120 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
121 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
122 theFreeShapes.push_back(aGeomShape);
126 // Map subshapes and shapes.
127 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
128 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
129 if(aMapSA.IsEmpty()) {
133 // Get all shapes with common subshapes and free shapes.
134 NCollection_Map<TopoDS_Shape> aFreeShapes;
135 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
136 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
137 const TopoDS_Shape& aShape = anIter.Key();
138 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
139 if(aListOfShape.IsEmpty()) {
142 else if(aListOfShape.Size() == 1) {
143 const TopoDS_Shape& aF = aListOfShape.First();
145 aListOfShape.Clear();
147 NCollection_List<TopoDS_Shape> aTempList;
148 NCollection_Map<TopoDS_Shape> aTempMap;
149 const TopoDS_Shape& aF = aListOfShape.First();
150 const TopoDS_Shape& aL = aListOfShape.Last();
151 aTempList.Append(aF);
152 aTempList.Append(aL);
155 aFreeShapes.Remove(aF);
156 aFreeShapes.Remove(aL);
157 aListOfShape.Clear();
158 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
159 const TopoDS_Shape& aTempShape = aTempIter.Value();
160 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
161 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
162 if(aTempListOfShape.IsEmpty()) {
164 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
165 aTempListOfShape.Clear();
166 } else if(aTempListOfShape.Size() > 1) {
167 if(aTempListOfShape.First() == aTempShape) {
168 const TopoDS_Shape& aTL = aTempListOfShape.Last();
169 if(aTempMap.Add(aTL)) {
170 aTempList.Append(aTL);
171 aFreeShapes.Remove(aTL);
173 aTempListOfShape.Clear();
174 } else if(aTempListOfShape.Last() == aTempShape) {
175 const TopoDS_Shape& aTF = aTempListOfShape.First();
176 if(aTempMap.Add(aTF)) {
177 aTempList.Append(aTF);
178 aFreeShapes.Remove(aTF);
180 aTempListOfShape.Clear();
185 aShapesWithCommonSubshapes.Append(aTempMap);
189 // Combine shapes with common subshapes.
190 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
192 TopoDS_CompSolid aCSolid;
193 TopoDS_Builder aBuilder;
194 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
195 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
196 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
197 const TopoDS_Shape& aShape = anExp.Current();
198 if(aShapesMap.Contains(aShape)) {
199 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
200 aShapesMap.Remove(aShape);
203 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
204 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
205 aGeomShape->setImpl<TopoDS_Shape>(aSh);
206 theCombinedShapes.push_back(aGeomShape);
209 // Adding free shapes.
210 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
211 const TopoDS_Shape& aShape = anExp.Current();
212 if(aFreeShapes.Contains(aShape)) {
213 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
214 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
215 theFreeShapes.push_back(aGeomShape);
219 if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
220 aResult = theCombinedShapes.front();
221 } else if (theCombinedShapes.size() > 1 || (theCombinedShapes.size() >= 1 && theFreeShapes.size() >= 1)) {
222 TopoDS_Compound aResultComp;
223 TopoDS_Builder aBuilder;
224 aBuilder.MakeCompound(aResultComp);
225 for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin(); anIter != theCombinedShapes.cend(); anIter++) {
226 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
228 for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin(); anIter != theFreeShapes.cend(); anIter++) {
229 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
231 aResult->setImpl(new TopoDS_Shape(aResultComp));
237 //=================================================================================================
238 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
240 // Bounding box of all objects.
244 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
245 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
246 BRepBndLib::Add(aShape, aBndBox);
249 if(theEnlarge != 0.0) {
250 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
251 aBndBox.Enlarge(theEnlarge);
254 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
255 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
256 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
257 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
259 for(int i = 0; i < 2; i++) {
260 for(int j = 0; j < 2; j++) {
261 for(int k = 0; k < 2; k++) {
262 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
263 aResultPoints.push_back(aPnt);
268 return aResultPoints;
271 //=================================================================================================
272 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
275 return std::shared_ptr<GeomAPI_Shape>();
277 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
278 if (aPlaneFace.IsNull())
279 return std::shared_ptr<GeomAPI_Shape>();
281 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
283 return std::shared_ptr<GeomAPI_Shape>();
285 // make an infinity face on the plane
286 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
288 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
289 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
293 //=================================================================================================
294 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
295 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
297 std::shared_ptr<GeomAPI_Shape> aResultShape;
299 if(!thePlane.get()) {
303 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
304 if(aShape.ShapeType() != TopAbs_FACE) {
308 TopoDS_Face aFace = TopoDS::Face(aShape);
309 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
314 GeomLib_IsPlanarSurface isPlanar(aSurf);
315 if(!isPlanar.IsPlanar()) {
319 if(thePoints.size() != 8) {
323 const gp_Pln& aFacePln = isPlanar.Plan();
324 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
325 IntAna_Quadric aQuadric(aFacePln);
326 Standard_Real UMin, UMax, VMin, VMax;
327 UMin = UMax = VMin = VMax = 0;
328 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
329 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
330 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
331 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
332 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
333 Standard_Real aPntU(0), aPntV(0);
334 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
335 if(aPntU < UMin) UMin = aPntU;
336 if(aPntU > UMax) UMax = aPntU;
337 if(aPntV < VMin) VMin = aPntV;
338 if(aPntV > VMax) VMax = aPntV;
340 aResultShape.reset(new GeomAPI_Shape);
341 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
346 //=================================================================================================
347 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
348 std::shared_ptr<GeomAPI_Vertex>& theV1,
349 std::shared_ptr<GeomAPI_Vertex>& theV2)
351 if(!theShape.get()) {
352 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
353 aVertex->setImpl(new TopoDS_Vertex());
359 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
360 TopoDS_Vertex aV1, aV2;
361 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
363 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
364 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
365 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
370 //=================================================================================================
371 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
372 const std::shared_ptr<GeomAPI_Dir> theDirection,
373 const ListOfShape& theWires,
374 ListOfShape& theFaces)
376 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
377 theDirection->impl<gp_Dir>()));
378 TopoDS_Face aFace = aMKFace.Face();
380 BRepAlgo_FaceRestrictor aFRestrictor;
381 aFRestrictor.Init(aFace, Standard_False, Standard_True);
382 for(ListOfShape::const_iterator anIt = theWires.cbegin();
383 anIt != theWires.cend();
385 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
386 aFRestrictor.Add(aWire);
389 aFRestrictor.Perform();
391 if(!aFRestrictor.IsDone()) {
395 for(; aFRestrictor.More(); aFRestrictor.Next()) {
396 GeomShapePtr aShape(new GeomAPI_Shape());
397 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
398 theFaces.push_back(aShape);
402 //=================================================================================================
403 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
405 TopoDS_Compound aCompound;
406 BRep_Builder aBuilder;
407 aBuilder.MakeCompound(aCompound);
409 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
410 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
412 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
414 if(aFindPlane.Found() != Standard_True) {
415 return std::shared_ptr<GeomAPI_Pln>();
418 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
419 gp_Pnt aLoc = aPlane->Location();
420 gp_Dir aDir = aPlane->Axis().Direction();
422 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
423 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
425 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
430 //=================================================================================================
431 bool GeomAlgoAPI_ShapeTools::isSubShapeInShape(const std::shared_ptr<GeomAPI_Shape> theSubShape,
432 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
434 if(!theSubShape.get() || !theBaseShape.get()) {
438 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
439 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
441 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
442 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
443 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
445 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
448 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
449 if(aBaseShape.ShapeType() == TopAbs_FACE) {
450 // Check that edge is on face surface.
451 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
452 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
453 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
455 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
459 // Check intersections.
460 TopoDS_Vertex aV1, aV2;
461 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
462 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
463 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
464 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
465 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
466 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
468 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
469 // Edge intersect face bound. Check that it is not on edge begin or end.
470 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
471 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
472 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
473 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
480 // No intersections found. Edge is inside or outside face. Check it.
481 BRepAdaptor_Curve aCurveAdaptor(anEdge);
482 gp_Pnt aPointToCheck = aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() + aCurveAdaptor.LastParameter()) / 2.0);
483 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
484 ShapeAnalysis_Surface aSAS(aSurface);
485 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
486 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
487 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {