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 <ShapeAnalysis.hxx>
28 #include <TCollection_AsciiString.hxx>
29 #include <TopoDS_Builder.hxx>
30 #include <TopoDS_Face.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Shell.hxx>
33 #include <TopoDS_Vertex.hxx>
35 #include <TopExp_Explorer.hxx>
38 //=================================================================================================
39 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
45 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
49 const Standard_Real anEps = 1.e-6;
50 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
51 return aGProps.Mass();
54 //=================================================================================================
55 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
59 return std::shared_ptr<GeomAPI_Pnt>();
61 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
63 return std::shared_ptr<GeomAPI_Pnt>();
66 if(aShape.ShapeType() == TopAbs_VERTEX) {
67 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
68 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
69 BRepGProp::LinearProperties(aShape, aGProps);
70 aCentre = aGProps.CentreOfMass();
72 BRepGProp::SurfaceProperties(aShape, aGProps);
73 aCentre = aGProps.CentreOfMass();
75 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
78 //=================================================================================================
79 void GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
80 const GeomAPI_Shape::ShapeType theType,
81 ListOfShape& theCombinedShapes,
82 ListOfShape& theFreeShapes)
84 if(!theCompound.get()) {
88 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
92 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
93 TopAbs_ShapeEnum aTA = TopAbs_FACE;
94 if(theType == GeomAPI_Shape::COMPSOLID) {
100 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
101 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
102 const TopoDS_Shape& aShape = anIter.Value();
103 if(aShape.ShapeType() > aTA) {
104 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
105 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
106 theFreeShapes.push_back(aGeomShape);
110 // Map subshapes and shapes.
111 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
112 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
113 if(aMapSA.IsEmpty()) {
117 // Get all shapes with common subshapes and free shapes.
118 NCollection_Map<TopoDS_Shape> aFreeShapes;
119 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
120 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
121 const TopoDS_Shape& aShape = anIter.Key();
122 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
123 if(aListOfShape.IsEmpty()) {
126 else if(aListOfShape.Size() == 1) {
127 const TopoDS_Shape& aF = aListOfShape.First();
129 aListOfShape.Clear();
131 NCollection_List<TopoDS_Shape> aTempList;
132 NCollection_Map<TopoDS_Shape> aTempMap;
133 const TopoDS_Shape& aF = aListOfShape.First();
134 const TopoDS_Shape& aL = aListOfShape.Last();
135 aTempList.Append(aF);
136 aTempList.Append(aL);
139 aFreeShapes.Remove(aF);
140 aFreeShapes.Remove(aL);
141 aListOfShape.Clear();
142 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
143 const TopoDS_Shape& aTempShape = aTempIter.Value();
144 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
145 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
146 if(aTempListOfShape.IsEmpty()) {
148 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
149 aTempListOfShape.Clear();
150 } else if(aTempListOfShape.Size() > 1) {
151 if(aTempListOfShape.First() == aTempShape) {
152 const TopoDS_Shape& aTL = aTempListOfShape.Last();
153 if(aTempMap.Add(aTL)) {
154 aTempList.Append(aTL);
155 aFreeShapes.Remove(aTL);
157 aTempListOfShape.Clear();
158 } else if(aTempListOfShape.Last() == aTempShape) {
159 const TopoDS_Shape& aTF = aTempListOfShape.First();
160 if(aTempMap.Add(aTF)) {
161 aTempList.Append(aTF);
162 aFreeShapes.Remove(aTF);
164 aTempListOfShape.Clear();
169 aShapesWithCommonSubshapes.Append(aTempMap);
173 // Combine shapes with common subshapes.
174 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
176 TopoDS_CompSolid aCSolid;
177 TopoDS_Builder aBuilder;
178 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
179 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
180 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
181 const TopoDS_Shape& aShape = anExp.Current();
182 if(aShapesMap.Contains(aShape)) {
183 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
184 aShapesMap.Remove(aShape);
187 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
188 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
189 aGeomShape->setImpl<TopoDS_Shape>(aSh);
190 theCombinedShapes.push_back(aGeomShape);
193 // Adding free shapes.
194 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
195 const TopoDS_Shape& aShape = anExp.Current();
196 if(aFreeShapes.Contains(aShape)) {
197 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
198 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
199 theFreeShapes.push_back(aGeomShape);
204 //=================================================================================================
205 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
207 // Bounding box of all objects.
211 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
212 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
213 BRepBndLib::Add(aShape, aBndBox);
216 if(theEnlarge != 0.0) {
217 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
218 aBndBox.Enlarge(theEnlarge);
221 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
222 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
223 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
224 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
226 for(int i = 0; i < 2; i++) {
227 for(int j = 0; j < 2; j++) {
228 for(int k = 0; k < 2; k++) {
229 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
230 aResultPoints.push_back(aPnt);
235 return aResultPoints;
238 //=================================================================================================
239 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
242 return std::shared_ptr<GeomAPI_Shape>();
244 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
245 if (aPlaneFace.IsNull())
246 return std::shared_ptr<GeomAPI_Shape>();
248 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
250 return std::shared_ptr<GeomAPI_Shape>();
252 // make an infinity face on the plane
253 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
255 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
256 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
260 //=================================================================================================
261 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
262 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
264 std::shared_ptr<GeomAPI_Shape> aResultShape;
266 if(!thePlane.get()) {
270 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
271 if(aShape.ShapeType() != TopAbs_FACE) {
275 TopoDS_Face aFace = TopoDS::Face(aShape);
276 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
281 GeomLib_IsPlanarSurface isPlanar(aSurf);
282 if(!isPlanar.IsPlanar()) {
286 if(thePoints.size() != 8) {
290 const gp_Pln& aFacePln = isPlanar.Plan();
291 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
292 IntAna_Quadric aQuadric(aFacePln);
293 Standard_Real UMin, UMax, VMin, VMax;
294 UMin = UMax = VMin = VMax = 0;
295 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
296 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
297 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
298 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
299 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
300 Standard_Real aPntU(0), aPntV(0);
301 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
302 if(aPntU < UMin) UMin = aPntU;
303 if(aPntU > UMax) UMax = aPntU;
304 if(aPntV < VMin) VMin = aPntV;
305 if(aPntV > VMax) VMax = aPntV;
307 aResultShape.reset(new GeomAPI_Shape);
308 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
313 //=================================================================================================
314 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
315 std::shared_ptr<GeomAPI_Vertex>& theV1,
316 std::shared_ptr<GeomAPI_Vertex>& theV2)
318 if(!theShape.get()) {
319 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
320 aVertex->setImpl(new TopoDS_Vertex());
326 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
327 TopoDS_Vertex aV1, aV2;
328 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
330 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
331 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
332 aGeomV2->setImpl(new TopoDS_Vertex(aV2));