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_Face.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Shell.hxx>
36 #include <TopoDS_Vertex.hxx>
38 #include <TopExp_Explorer.hxx>
41 void mapWireFaces(const TopoDS_Shape& theShape,
42 BOPCol_IndexedDataMapOfShapeListOfShape& theMapWireFaces);
44 //=================================================================================================
45 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
51 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
55 const Standard_Real anEps = 1.e-6;
56 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
57 return aGProps.Mass();
60 //=================================================================================================
61 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
65 return std::shared_ptr<GeomAPI_Pnt>();
67 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
69 return std::shared_ptr<GeomAPI_Pnt>();
72 if(aShape.ShapeType() == TopAbs_VERTEX) {
73 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
74 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
75 BRepGProp::LinearProperties(aShape, aGProps);
76 aCentre = aGProps.CentreOfMass();
78 BRepGProp::SurfaceProperties(aShape, aGProps);
79 aCentre = aGProps.CentreOfMass();
81 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
84 //=================================================================================================
85 void GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
86 const GeomAPI_Shape::ShapeType theType,
87 ListOfShape& theCombinedShapes,
88 ListOfShape& theFreeShapes)
90 if(!theCompound.get()) {
94 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
98 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
99 TopAbs_ShapeEnum aTA = TopAbs_FACE;
100 if(theType == GeomAPI_Shape::COMPSOLID) {
106 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
107 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
108 const TopoDS_Shape& aShape = anIter.Value();
109 if(aShape.ShapeType() > aTA) {
110 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
111 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
112 theFreeShapes.push_back(aGeomShape);
116 // Map subshapes and shapes.
117 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
118 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
119 if(aMapSA.IsEmpty()) {
123 // Get all shapes with common subshapes and free shapes.
124 NCollection_Map<TopoDS_Shape> aFreeShapes;
125 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
126 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
127 const TopoDS_Shape& aShape = anIter.Key();
128 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
129 if(aListOfShape.IsEmpty()) {
132 else if(aListOfShape.Size() == 1) {
133 const TopoDS_Shape& aF = aListOfShape.First();
135 aListOfShape.Clear();
137 NCollection_List<TopoDS_Shape> aTempList;
138 NCollection_Map<TopoDS_Shape> aTempMap;
139 const TopoDS_Shape& aF = aListOfShape.First();
140 const TopoDS_Shape& aL = aListOfShape.Last();
141 aTempList.Append(aF);
142 aTempList.Append(aL);
145 aFreeShapes.Remove(aF);
146 aFreeShapes.Remove(aL);
147 aListOfShape.Clear();
148 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
149 const TopoDS_Shape& aTempShape = aTempIter.Value();
150 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
151 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
152 if(aTempListOfShape.IsEmpty()) {
154 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
155 aTempListOfShape.Clear();
156 } else if(aTempListOfShape.Size() > 1) {
157 if(aTempListOfShape.First() == aTempShape) {
158 const TopoDS_Shape& aTL = aTempListOfShape.Last();
159 if(aTempMap.Add(aTL)) {
160 aTempList.Append(aTL);
161 aFreeShapes.Remove(aTL);
163 aTempListOfShape.Clear();
164 } else if(aTempListOfShape.Last() == aTempShape) {
165 const TopoDS_Shape& aTF = aTempListOfShape.First();
166 if(aTempMap.Add(aTF)) {
167 aTempList.Append(aTF);
168 aFreeShapes.Remove(aTF);
170 aTempListOfShape.Clear();
175 aShapesWithCommonSubshapes.Append(aTempMap);
179 // Combine shapes with common subshapes.
180 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
182 TopoDS_CompSolid aCSolid;
183 TopoDS_Builder aBuilder;
184 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
185 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
186 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
187 const TopoDS_Shape& aShape = anExp.Current();
188 if(aShapesMap.Contains(aShape)) {
189 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
190 aShapesMap.Remove(aShape);
193 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
194 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
195 aGeomShape->setImpl<TopoDS_Shape>(aSh);
196 theCombinedShapes.push_back(aGeomShape);
199 // Adding free shapes.
200 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
201 const TopoDS_Shape& aShape = anExp.Current();
202 if(aFreeShapes.Contains(aShape)) {
203 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
204 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
205 theFreeShapes.push_back(aGeomShape);
210 //=================================================================================================
211 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
213 // Bounding box of all objects.
217 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
218 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
219 BRepBndLib::Add(aShape, aBndBox);
222 if(theEnlarge != 0.0) {
223 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
224 aBndBox.Enlarge(theEnlarge);
227 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
228 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
229 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
230 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
232 for(int i = 0; i < 2; i++) {
233 for(int j = 0; j < 2; j++) {
234 for(int k = 0; k < 2; k++) {
235 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
236 aResultPoints.push_back(aPnt);
241 return aResultPoints;
244 //=================================================================================================
245 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
248 return std::shared_ptr<GeomAPI_Shape>();
250 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
251 if (aPlaneFace.IsNull())
252 return std::shared_ptr<GeomAPI_Shape>();
254 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
256 return std::shared_ptr<GeomAPI_Shape>();
258 // make an infinity face on the plane
259 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
261 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
262 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
266 //=================================================================================================
267 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
268 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
270 std::shared_ptr<GeomAPI_Shape> aResultShape;
272 if(!thePlane.get()) {
276 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
277 if(aShape.ShapeType() != TopAbs_FACE) {
281 TopoDS_Face aFace = TopoDS::Face(aShape);
282 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
287 GeomLib_IsPlanarSurface isPlanar(aSurf);
288 if(!isPlanar.IsPlanar()) {
292 if(thePoints.size() != 8) {
296 const gp_Pln& aFacePln = isPlanar.Plan();
297 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
298 IntAna_Quadric aQuadric(aFacePln);
299 Standard_Real UMin, UMax, VMin, VMax;
300 UMin = UMax = VMin = VMax = 0;
301 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
302 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
303 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
304 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
305 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
306 Standard_Real aPntU(0), aPntV(0);
307 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
308 if(aPntU < UMin) UMin = aPntU;
309 if(aPntU > UMax) UMax = aPntU;
310 if(aPntV < VMin) VMin = aPntV;
311 if(aPntV > VMax) VMax = aPntV;
313 aResultShape.reset(new GeomAPI_Shape);
314 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
319 //=================================================================================================
320 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
321 std::shared_ptr<GeomAPI_Vertex>& theV1,
322 std::shared_ptr<GeomAPI_Vertex>& theV2)
324 if(!theShape.get()) {
325 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
326 aVertex->setImpl(new TopoDS_Vertex());
332 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
333 TopoDS_Vertex aV1, aV2;
334 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
336 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
337 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
338 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
343 //=================================================================================================
344 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
345 const std::shared_ptr<GeomAPI_Dir> theDirection,
346 const ListOfShape& theWires,
347 ListOfShape& theFaces)
349 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
350 theDirection->impl<gp_Dir>()));
351 TopoDS_Face aFace = aMKFace.Face();
353 BRepAlgo_FaceRestrictor aFRestrictor;
354 aFRestrictor.Init(aFace);
355 for(ListOfShape::const_iterator anIt = theWires.cbegin();
356 anIt != theWires.cend();
358 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
359 aFRestrictor.Add(aWire);
362 aFRestrictor.Perform();
364 if(!aFRestrictor.IsDone()) {
368 for(; aFRestrictor.More(); aFRestrictor.Next()) {
369 GeomShapePtr aShape(new GeomAPI_Shape());
370 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
371 theFaces.push_back(aShape);