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_CompoundBuilder.h>
13 #include <Bnd_Box.hxx>
14 #include <BOPTools.hxx>
15 #include <BRepBndLib.hxx>
16 #include <BRepBuilderAPI_MakeFace.hxx>
17 #include <BRepGProp.hxx>
18 #include <BRepTools.hxx>
19 #include <BRep_Tool.hxx>
20 #include <Geom_Plane.hxx>
21 #include <GeomLib_IsPlanarSurface.hxx>
22 #include <GeomLib_Tool.hxx>
23 #include <GProp_GProps.hxx>
24 #include <IntAna_IntConicQuad.hxx>
25 #include <IntAna_Quadric.hxx>
26 #include <NCollection_Vector.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TopoDS_Builder.hxx>
29 #include <TopoDS_Face.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Shell.hxx>
33 #include <TopExp_Explorer.hxx>
36 //=================================================================================================
37 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
43 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
47 BRepGProp::VolumeProperties(aShape, aGProps);
48 return aGProps.Mass();
51 //=================================================================================================
52 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
56 return std::shared_ptr<GeomAPI_Pnt>();
58 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
60 return std::shared_ptr<GeomAPI_Pnt>();
62 BRepGProp::SurfaceProperties(aShape, aGProps);
63 gp_Pnt aCentre = aGProps.CentreOfMass();
64 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
67 //=================================================================================================
68 void GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
69 const GeomAPI_Shape::ShapeType theType,
70 ListOfShape& theCombinedShapes,
71 ListOfShape& theFreeShapes)
73 if(!theCompound.get()) {
77 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
81 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
82 TopAbs_ShapeEnum aTA = TopAbs_FACE;
83 if(theType == GeomAPI_Shape::COMPSOLID) {
89 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
90 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
91 const TopoDS_Shape& aShape = anIter.Value();
92 if(aShape.ShapeType() > aTA) {
93 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
94 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
95 theFreeShapes.push_back(aGeomShape);
99 // Map subshapes and shapes.
100 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
101 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
102 if(aMapSA.IsEmpty()) {
106 // Get all shapes with common subshapes and free shapes.
107 NCollection_Map<TopoDS_Shape> aFreeShapes;
108 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
109 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
110 const TopoDS_Shape& aShape = anIter.Key();
111 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
112 if(aListOfShape.IsEmpty()) {
115 else if(aListOfShape.Size() == 1) {
116 const TopoDS_Shape& aF = aListOfShape.First();
118 aListOfShape.Clear();
120 NCollection_List<TopoDS_Shape> aTempList;
121 NCollection_Map<TopoDS_Shape> aTempMap;
122 const TopoDS_Shape& aF = aListOfShape.First();
123 const TopoDS_Shape& aL = aListOfShape.Last();
124 aTempList.Append(aF);
125 aTempList.Append(aL);
128 aFreeShapes.Remove(aF);
129 aFreeShapes.Remove(aL);
130 aListOfShape.Clear();
131 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
132 const TopoDS_Shape& aTempShape = aTempIter.Value();
133 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
134 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
135 if(aTempListOfShape.IsEmpty()) {
137 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
138 aTempListOfShape.Clear();
139 } else if(aTempListOfShape.Size() > 1) {
140 if(aTempListOfShape.First() == aTempShape) {
141 const TopoDS_Shape& aTL = aTempListOfShape.Last();
142 if(aTempMap.Add(aTL)) {
143 aTempList.Append(aTL);
144 aFreeShapes.Remove(aTL);
146 aTempListOfShape.Clear();
147 } else if(aTempListOfShape.Last() == aTempShape) {
148 const TopoDS_Shape& aTF = aTempListOfShape.First();
149 if(aTempMap.Add(aTF)) {
150 aTempList.Append(aTF);
151 aFreeShapes.Remove(aTF);
153 aTempListOfShape.Clear();
158 aShapesWithCommonSubshapes.Append(aTempMap);
162 // Combine shapes with common subshapes.
163 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
165 TopoDS_CompSolid aCSolid;
166 TopoDS_Builder aBuilder;
167 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
168 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
169 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
170 const TopoDS_Shape& aShape = anExp.Current();
171 if(aShapesMap.Contains(aShape)) {
172 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
173 aShapesMap.Remove(aShape);
176 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
177 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
178 aGeomShape->setImpl<TopoDS_Shape>(aSh);
179 theCombinedShapes.push_back(aGeomShape);
182 // Adding free shapes.
183 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
184 const TopoDS_Shape& aShape = anExp.Current();
185 if(aFreeShapes.Contains(aShape)) {
186 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
187 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
188 theFreeShapes.push_back(aGeomShape);
193 //=================================================================================================
194 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
196 // Bounding box of all objects.
200 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
201 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
202 BRepBndLib::Add(aShape, aBndBox);
205 if(theEnlarge != 0.0) {
206 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
207 aBndBox.Enlarge(theEnlarge);
210 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
211 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
212 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
213 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
215 for(int i = 0; i < 2; i++) {
216 for(int j = 0; j < 2; j++) {
217 for(int k = 0; k < 2; k++) {
218 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
219 aResultPoints.push_back(aPnt);
224 return aResultPoints;
227 //=================================================================================================
228 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
231 return std::shared_ptr<GeomAPI_Shape>();
233 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
234 if (aPlaneFace.IsNull())
235 return std::shared_ptr<GeomAPI_Shape>();
237 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
239 return std::shared_ptr<GeomAPI_Shape>();
241 // make an infinity face on the plane
242 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
244 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
245 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
249 //=================================================================================================
250 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
251 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
253 std::shared_ptr<GeomAPI_Shape> aResultShape;
255 if(!thePlane.get()) {
259 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
260 if(aShape.ShapeType() != TopAbs_FACE) {
264 TopoDS_Face aFace = TopoDS::Face(aShape);
265 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
270 GeomLib_IsPlanarSurface isPlanar(aSurf);
271 if(!isPlanar.IsPlanar()) {
275 if(thePoints.size() != 8) {
279 const gp_Pln& aFacePln = isPlanar.Plan();
280 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
281 IntAna_Quadric aQuadric(aFacePln);
282 Standard_Real UMin, UMax, VMin, VMax;
283 UMin = UMax = VMin = VMax = 0;
284 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
285 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
286 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
287 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
288 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
289 Standard_Real aPntU(0), aPntV(0);
290 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
291 if(aPntU < UMin) UMin = aPntU;
292 if(aPntU > UMax) UMax = aPntU;
293 if(aPntV < VMin) VMin = aPntV;
294 if(aPntV > VMax) VMax = aPntV;
296 aResultShape.reset(new GeomAPI_Shape);
297 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));