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) {
88 // Map subshapes and shapes.
89 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
90 BOPCol_IndexedDataMapOfShapeListOfShape aMapEF;
91 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapEF);
92 if(aMapEF.IsEmpty()) {
96 // Get all shapes with common subshapes and free shapes.
97 NCollection_Map<TopoDS_Shape> aFreeShapes;
98 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
99 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapEF); anIter.More(); anIter.Next()) {
100 const TopoDS_Shape& aShape = anIter.Key();
101 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
102 if(aListOfShape.IsEmpty()) {
105 else if(aListOfShape.Size() == 1) {
106 const TopoDS_Shape& aF = aListOfShape.First();
108 aListOfShape.Clear();
110 NCollection_List<TopoDS_Shape> aTempList;
111 NCollection_Map<TopoDS_Shape> aTempMap;
112 const TopoDS_Shape& aF = aListOfShape.First();
113 const TopoDS_Shape& aL = aListOfShape.Last();
114 aTempList.Append(aF);
115 aTempList.Append(aL);
118 aFreeShapes.Remove(aF);
119 aFreeShapes.Remove(aL);
120 aListOfShape.Clear();
121 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
122 const TopoDS_Shape& aTempShape = aTempIter.Value();
123 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapEF); anIter.More(); anIter.Next()) {
124 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
125 if(aTempListOfShape.IsEmpty()) {
127 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
128 aTempListOfShape.Clear();
129 } else if(aTempListOfShape.Size() > 1) {
130 if(aTempListOfShape.First() == aTempShape) {
131 const TopoDS_Shape& aTL = aTempListOfShape.Last();
132 if(aTempMap.Add(aTL)) {
133 aTempList.Append(aTL);
134 aFreeShapes.Remove(aTL);
136 aTempListOfShape.Clear();
137 } else if(aTempListOfShape.Last() == aTempShape) {
138 const TopoDS_Shape& aTF = aTempListOfShape.First();
139 if(aTempMap.Add(aTF)) {
140 aTempList.Append(aTF);
141 aFreeShapes.Remove(aTF);
143 aTempListOfShape.Clear();
148 aShapesWithCommonSubshapes.Append(aTempMap);
152 // Combine shapes with common subshapes.
153 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
155 TopoDS_CompSolid aCSolid;
156 TopoDS_Builder aBuilder;
157 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
158 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
159 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
160 const TopoDS_Shape& aShape = anExp.Current();
161 if(aShapesMap.Contains(aShape)) {
162 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
163 aShapesMap.Remove(aShape);
166 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
167 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
168 aGeomShape->setImpl<TopoDS_Shape>(aSh);
169 theCombinedShapes.push_back(aGeomShape);
172 // Adding free shapes.
173 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
174 const TopoDS_Shape& aShape = anExp.Current();
175 if(aFreeShapes.Contains(aShape)) {
176 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
177 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
178 theFreeShapes.push_back(aGeomShape);
183 //=================================================================================================
184 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
186 // Bounding box of all objects.
190 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
191 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
192 BRepBndLib::Add(aShape, aBndBox);
195 if(theEnlarge != 0.0) {
196 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
197 aBndBox.Enlarge(theEnlarge);
200 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
201 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
202 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
203 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
205 for(int i = 0; i < 2; i++) {
206 for(int j = 0; j < 2; j++) {
207 for(int k = 0; k < 2; k++) {
208 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
209 aResultPoints.push_back(aPnt);
214 return aResultPoints;
217 //=================================================================================================
218 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
221 return std::shared_ptr<GeomAPI_Shape>();
223 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
224 if (aPlaneFace.IsNull())
225 return std::shared_ptr<GeomAPI_Shape>();
227 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
229 return std::shared_ptr<GeomAPI_Shape>();
231 // make an infinity face on the plane
232 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
234 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
235 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
239 //=================================================================================================
240 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
241 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
243 std::shared_ptr<GeomAPI_Shape> aResultShape;
245 if(!thePlane.get()) {
249 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
250 if(aShape.ShapeType() != TopAbs_FACE) {
254 TopoDS_Face aFace = TopoDS::Face(aShape);
255 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
260 GeomLib_IsPlanarSurface isPlanar(aSurf);
261 if(!isPlanar.IsPlanar()) {
265 if(thePoints.size() != 8) {
269 const gp_Pln& aFacePln = isPlanar.Plan();
270 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
271 IntAna_Quadric aQuadric(aFacePln);
272 Standard_Real UMin, UMax, VMin, VMax;
273 UMin = UMax = VMin = VMax = 0;
274 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
275 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
276 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
277 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
278 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
279 Standard_Real aPntU(0), aPntV(0);
280 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
281 if(aPntU < UMin) UMin = aPntU;
282 if(aPntU > UMax) UMax = aPntU;
283 if(aPntV < VMin) VMin = aPntV;
284 if(aPntV > VMax) VMax = aPntV;
286 aResultShape.reset(new GeomAPI_Shape);
287 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));