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_Pnt.h>
15 #include <Bnd_Box.hxx>
16 #include <BOPTools.hxx>
17 #include <BRep_Builder.hxx>
18 #include <BRepAlgo_FaceRestrictor.hxx>
19 #include <BRepBndLib.hxx>
20 #include <BRepBuilderAPI_MakeFace.hxx>
21 #include <BRepGProp.hxx>
22 #include <BRep_Tool.hxx>
23 #include <Geom_Plane.hxx>
24 #include <GeomLib_IsPlanarSurface.hxx>
25 #include <GeomLib_Tool.hxx>
27 #include <GProp_GProps.hxx>
28 #include <IntAna_IntConicQuad.hxx>
29 #include <IntAna_Quadric.hxx>
30 #include <NCollection_Vector.hxx>
31 #include <ShapeAnalysis.hxx>
32 #include <TopoDS_Builder.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Shell.hxx>
37 #include <TopoDS_Vertex.hxx>
39 #include <TopExp_Explorer.hxx>
42 void mapWireFaces(const TopoDS_Shape& theShape,
43 BOPCol_IndexedDataMapOfShapeListOfShape& theMapWireFaces);
45 //=================================================================================================
46 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
52 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
56 const Standard_Real anEps = 1.e-6;
57 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
58 return aGProps.Mass();
61 //=================================================================================================
62 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
66 return std::shared_ptr<GeomAPI_Pnt>();
68 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
70 return std::shared_ptr<GeomAPI_Pnt>();
73 if(aShape.ShapeType() == TopAbs_VERTEX) {
74 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
75 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
76 BRepGProp::LinearProperties(aShape, aGProps);
77 aCentre = aGProps.CentreOfMass();
79 BRepGProp::SurfaceProperties(aShape, aGProps);
80 aCentre = aGProps.CentreOfMass();
82 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
85 //=================================================================================================
86 void GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
87 const GeomAPI_Shape::ShapeType theType,
88 ListOfShape& theCombinedShapes,
89 ListOfShape& theFreeShapes)
91 if(!theCompound.get()) {
95 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
99 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
100 TopAbs_ShapeEnum aTA = TopAbs_FACE;
101 if(theType == GeomAPI_Shape::COMPSOLID) {
107 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
108 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
109 const TopoDS_Shape& aShape = anIter.Value();
110 if(aShape.ShapeType() > aTA) {
111 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
112 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
113 theFreeShapes.push_back(aGeomShape);
117 // Map subshapes and shapes.
118 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
119 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
120 if(aMapSA.IsEmpty()) {
124 // Get all shapes with common subshapes and free shapes.
125 NCollection_Map<TopoDS_Shape> aFreeShapes;
126 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
127 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
128 const TopoDS_Shape& aShape = anIter.Key();
129 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
130 if(aListOfShape.IsEmpty()) {
133 else if(aListOfShape.Size() == 1) {
134 const TopoDS_Shape& aF = aListOfShape.First();
136 aListOfShape.Clear();
138 NCollection_List<TopoDS_Shape> aTempList;
139 NCollection_Map<TopoDS_Shape> aTempMap;
140 const TopoDS_Shape& aF = aListOfShape.First();
141 const TopoDS_Shape& aL = aListOfShape.Last();
142 aTempList.Append(aF);
143 aTempList.Append(aL);
146 aFreeShapes.Remove(aF);
147 aFreeShapes.Remove(aL);
148 aListOfShape.Clear();
149 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
150 const TopoDS_Shape& aTempShape = aTempIter.Value();
151 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
152 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
153 if(aTempListOfShape.IsEmpty()) {
155 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
156 aTempListOfShape.Clear();
157 } else if(aTempListOfShape.Size() > 1) {
158 if(aTempListOfShape.First() == aTempShape) {
159 const TopoDS_Shape& aTL = aTempListOfShape.Last();
160 if(aTempMap.Add(aTL)) {
161 aTempList.Append(aTL);
162 aFreeShapes.Remove(aTL);
164 aTempListOfShape.Clear();
165 } else if(aTempListOfShape.Last() == aTempShape) {
166 const TopoDS_Shape& aTF = aTempListOfShape.First();
167 if(aTempMap.Add(aTF)) {
168 aTempList.Append(aTF);
169 aFreeShapes.Remove(aTF);
171 aTempListOfShape.Clear();
176 aShapesWithCommonSubshapes.Append(aTempMap);
180 // Combine shapes with common subshapes.
181 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
183 TopoDS_CompSolid aCSolid;
184 TopoDS_Builder aBuilder;
185 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
186 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
187 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
188 const TopoDS_Shape& aShape = anExp.Current();
189 if(aShapesMap.Contains(aShape)) {
190 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
191 aShapesMap.Remove(aShape);
194 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
195 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
196 aGeomShape->setImpl<TopoDS_Shape>(aSh);
197 theCombinedShapes.push_back(aGeomShape);
200 // Adding free shapes.
201 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
202 const TopoDS_Shape& aShape = anExp.Current();
203 if(aFreeShapes.Contains(aShape)) {
204 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
205 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
206 theFreeShapes.push_back(aGeomShape);
211 //=================================================================================================
212 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
214 // Bounding box of all objects.
218 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
219 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
220 BRepBndLib::Add(aShape, aBndBox);
223 if(theEnlarge != 0.0) {
224 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
225 aBndBox.Enlarge(theEnlarge);
228 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
229 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
230 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
231 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
233 for(int i = 0; i < 2; i++) {
234 for(int j = 0; j < 2; j++) {
235 for(int k = 0; k < 2; k++) {
236 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
237 aResultPoints.push_back(aPnt);
242 return aResultPoints;
245 //=================================================================================================
246 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
249 return std::shared_ptr<GeomAPI_Shape>();
251 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
252 if (aPlaneFace.IsNull())
253 return std::shared_ptr<GeomAPI_Shape>();
255 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
257 return std::shared_ptr<GeomAPI_Shape>();
259 // make an infinity face on the plane
260 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
262 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
263 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
267 //=================================================================================================
268 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
269 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
271 std::shared_ptr<GeomAPI_Shape> aResultShape;
273 if(!thePlane.get()) {
277 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
278 if(aShape.ShapeType() != TopAbs_FACE) {
282 TopoDS_Face aFace = TopoDS::Face(aShape);
283 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
288 GeomLib_IsPlanarSurface isPlanar(aSurf);
289 if(!isPlanar.IsPlanar()) {
293 if(thePoints.size() != 8) {
297 const gp_Pln& aFacePln = isPlanar.Plan();
298 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
299 IntAna_Quadric aQuadric(aFacePln);
300 Standard_Real UMin, UMax, VMin, VMax;
301 UMin = UMax = VMin = VMax = 0;
302 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
303 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
304 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
305 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
306 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
307 Standard_Real aPntU(0), aPntV(0);
308 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
309 if(aPntU < UMin) UMin = aPntU;
310 if(aPntU > UMax) UMax = aPntU;
311 if(aPntV < VMin) VMin = aPntV;
312 if(aPntV > VMax) VMax = aPntV;
314 aResultShape.reset(new GeomAPI_Shape);
315 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
320 //=================================================================================================
321 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
322 std::shared_ptr<GeomAPI_Vertex>& theV1,
323 std::shared_ptr<GeomAPI_Vertex>& theV2)
325 if(!theShape.get()) {
326 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
327 aVertex->setImpl(new TopoDS_Vertex());
333 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
334 TopoDS_Vertex aV1, aV2;
335 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
337 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
338 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
339 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
344 //=================================================================================================
345 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
346 const std::shared_ptr<GeomAPI_Dir> theDirection,
347 const ListOfShape& theWires,
348 ListOfShape& theFaces)
350 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
351 theDirection->impl<gp_Dir>()));
352 TopoDS_Face aFace = aMKFace.Face();
354 BRepAlgo_FaceRestrictor aFRestrictor;
355 aFRestrictor.Init(aFace, Standard_False, Standard_True);
356 for(ListOfShape::const_iterator anIt = theWires.cbegin();
357 anIt != theWires.cend();
359 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
360 aFRestrictor.Add(aWire);
363 aFRestrictor.Perform();
365 if(!aFRestrictor.IsDone()) {
369 for(; aFRestrictor.More(); aFRestrictor.Next()) {
370 GeomShapePtr aShape(new GeomAPI_Shape());
371 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
372 theFaces.push_back(aShape);