Salome HOME
7f407d6778f19bb900b07e0cd1222a32484f48bf
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeTools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_ShapeTools.h
4 // Created:     3 August 2015
5 // Author:      Dmitry Bobylev
6
7 #include "GeomAlgoAPI_ShapeTools.h"
8
9 #include "GeomAlgoAPI_SketchBuilder.h"
10
11 #include <GeomAPI_Dir.h>
12 #include <GeomAPI_PlanarEdges.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_Pnt.h>
15
16 #include <Bnd_Box.hxx>
17 #include <BOPTools.hxx>
18 #include <BRep_Builder.hxx>
19 #include <BRepAdaptor_Curve.hxx>
20 #include <BRepAlgo_FaceRestrictor.hxx>
21 #include <BRepBndLib.hxx>
22 #include <BRepBuilderAPI_FindPlane.hxx>
23 #include <BRepBuilderAPI_MakeEdge.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <BRepExtrema_DistShapeShape.hxx>
26 #include <BRepGProp.hxx>
27 #include <BRepTopAdaptor_FClass2d.hxx>
28 #include <Geom_Curve.hxx>
29 #include <Geom2d_Curve.hxx>
30 #include <BRepLib_CheckCurveOnSurface.hxx>
31 #include <BRep_Tool.hxx>
32 #include <Geom_Plane.hxx>
33 #include <GeomLib_IsPlanarSurface.hxx>
34 #include <GeomLib_Tool.hxx>
35 #include <GeomProjLib.hxx>
36 #include <gp_Pln.hxx>
37 #include <GProp_GProps.hxx>
38 #include <IntAna_IntConicQuad.hxx>
39 #include <IntAna_Quadric.hxx>
40 #include <NCollection_Vector.hxx>
41 #include <ShapeAnalysis.hxx>
42 #include <ShapeAnalysis_Surface.hxx>
43 #include <TopoDS_Builder.hxx>
44 #include <TopoDS_Edge.hxx>
45 #include <TopoDS_Face.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS_Shell.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <TopoDS.hxx>
50 #include <TopExp_Explorer.hxx>
51
52 //==================================================================================================
53 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
54 {
55   GProp_GProps aGProps;
56   if(!theShape.get()) {
57     return 0.0;
58   }
59   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
60   if(aShape.IsNull()) {
61     return 0.0;
62   }
63   const Standard_Real anEps = 1.e-6;
64   BRepGProp::VolumeProperties(aShape, aGProps, anEps);
65   return aGProps.Mass();
66 }
67
68 //==================================================================================================
69 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
70 {
71   GProp_GProps aGProps;
72   if(!theShape) {
73     return std::shared_ptr<GeomAPI_Pnt>();
74   }
75   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
76   if(aShape.IsNull()) {
77     return std::shared_ptr<GeomAPI_Pnt>();
78   }
79   gp_Pnt aCentre;
80   if(aShape.ShapeType() == TopAbs_VERTEX) {
81     aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
82   } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
83     BRepGProp::LinearProperties(aShape, aGProps);
84     aCentre = aGProps.CentreOfMass();
85   } else {
86     BRepGProp::SurfaceProperties(aShape, aGProps);
87     aCentre = aGProps.CentreOfMass();
88   }
89   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
90 }
91
92 //==================================================================================================
93 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
94                                                                      const GeomAPI_Shape::ShapeType theType,
95                                                                      ListOfShape& theCombinedShapes,
96                                                                      ListOfShape& theFreeShapes)
97 {
98   GeomShapePtr aResult = theCompound;
99
100   if(!theCompound.get()) {
101     return aResult;
102   }
103
104   if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
105     return aResult;
106   }
107
108   TopAbs_ShapeEnum aTS = TopAbs_EDGE;
109   TopAbs_ShapeEnum aTA = TopAbs_FACE;
110   if(theType == GeomAPI_Shape::COMPSOLID) {
111     aTS = TopAbs_FACE;
112     aTA = TopAbs_SOLID;
113   }
114
115   theCombinedShapes.clear();
116   theFreeShapes.clear();
117
118   // Get free shapes.
119   const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
120   for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
121     const TopoDS_Shape& aShape = anIter.Value();
122     if(aShape.ShapeType() > aTA) {
123       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
124       aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
125       theFreeShapes.push_back(aGeomShape);
126     }
127   }
128
129   // Map subshapes and shapes.
130   BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
131   BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
132   if(aMapSA.IsEmpty()) {
133     return aResult;
134   }
135
136   // Get all shapes with common subshapes and free shapes.
137   NCollection_Map<TopoDS_Shape> aFreeShapes;
138   NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
139   for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
140     const TopoDS_Shape& aShape = anIter.Key();
141     BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
142     if(aListOfShape.IsEmpty()) {
143       continue;
144     }
145     else if(aListOfShape.Size() == 1) {
146       const TopoDS_Shape& aF = aListOfShape.First();
147       aFreeShapes.Add(aF);
148       aListOfShape.Clear();
149     } else {
150       NCollection_List<TopoDS_Shape> aTempList;
151       NCollection_Map<TopoDS_Shape> aTempMap;
152       const TopoDS_Shape& aF = aListOfShape.First();
153       const TopoDS_Shape& aL = aListOfShape.Last();
154       aTempList.Append(aF);
155       aTempList.Append(aL);
156       aTempMap.Add(aF);
157       aTempMap.Add(aL);
158       aFreeShapes.Remove(aF);
159       aFreeShapes.Remove(aL);
160       aListOfShape.Clear();
161       for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
162         const TopoDS_Shape& aTempShape = aTempIter.Value();
163         for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
164           BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
165           if(aTempListOfShape.IsEmpty()) {
166             continue;
167           } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
168             aTempListOfShape.Clear();
169           } else if(aTempListOfShape.Size() > 1) {
170             if(aTempListOfShape.First() == aTempShape) {
171               const TopoDS_Shape& aTL = aTempListOfShape.Last();
172               if(aTempMap.Add(aTL)) {
173                 aTempList.Append(aTL);
174                 aFreeShapes.Remove(aTL);
175               }
176               aTempListOfShape.Clear();
177             } else if(aTempListOfShape.Last() == aTempShape) {
178               const TopoDS_Shape& aTF = aTempListOfShape.First();
179               if(aTempMap.Add(aTF)) {
180                 aTempList.Append(aTF);
181                 aFreeShapes.Remove(aTF);
182               }
183               aTempListOfShape.Clear();
184             }
185           }
186         }
187       }
188       aShapesWithCommonSubshapes.Append(aTempMap);
189     }
190   }
191
192   // Combine shapes with common subshapes.
193   for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
194     TopoDS_Shell aShell;
195     TopoDS_CompSolid aCSolid;
196     TopoDS_Builder aBuilder;
197     theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
198     NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
199     for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
200       const TopoDS_Shape& aShape = anExp.Current();
201       if(aShapesMap.Contains(aShape)) {
202         theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
203         aShapesMap.Remove(aShape);
204       }
205     }
206     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
207     TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
208     aGeomShape->setImpl<TopoDS_Shape>(aSh);
209     theCombinedShapes.push_back(aGeomShape);
210   }
211
212   // Adding free shapes.
213   for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
214     const TopoDS_Shape& aShape = anExp.Current();
215     if(aFreeShapes.Contains(aShape)) {
216       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
217       aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
218       theFreeShapes.push_back(aGeomShape);
219     }
220   }
221
222   if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
223     aResult = theCombinedShapes.front();
224   } else if (theCombinedShapes.size() > 1 || (theCombinedShapes.size() >= 1 && theFreeShapes.size() >= 1)) {
225     TopoDS_Compound aResultComp;
226     TopoDS_Builder aBuilder;
227     aBuilder.MakeCompound(aResultComp);
228     for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin(); anIter != theCombinedShapes.cend(); anIter++) {
229       aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
230     }
231     for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin(); anIter != theFreeShapes.cend(); anIter++) {
232       aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
233     }
234     aResult->setImpl(new TopoDS_Shape(aResultComp));
235   }
236
237   return aResult;
238 }
239
240 //==================================================================================================
241 static void addSimpleShapeToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
242 {
243   if(theShape.IsNull()) {
244     return;
245   }
246
247   if(theShape.ShapeType() == TopAbs_COMPOUND) {
248     for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
249       addSimpleShapeToList(anIt.Value(), theList);
250     }
251   } else {
252     theList.Append(theShape);
253   }
254 }
255
256 //==================================================================================================
257 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
258 {
259   TopoDS_Compound aCompound;
260
261   BRep_Builder aBuilder;
262   aBuilder.MakeCompound(aCompound);
263
264   for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
265     aBuilder.Add(aCompound, anIt.Value());
266   }
267
268   return aCompound;
269 }
270
271 //==================================================================================================
272 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(const std::shared_ptr<GeomAPI_Shape> theCompound)
273 {
274   GeomShapePtr aResult = theCompound;
275
276   if(!theCompound.get()) {
277     return aResult;
278   }
279
280   TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
281   NCollection_List<TopoDS_Shape> anUngroupedShapes;
282   addSimpleShapeToList(anInShape, anUngroupedShapes);
283
284   NCollection_Vector<NCollection_List<TopoDS_Shape>> aGroups;
285   while(!anUngroupedShapes.IsEmpty()) {
286     NCollection_List<TopoDS_Shape> aGroupedShapes;
287     aGroupedShapes.Append(anUngroupedShapes.First());
288     anUngroupedShapes.RemoveFirst();
289     for(NCollection_List<TopoDS_Shape>::Iterator aGroupIt(aGroupedShapes); aGroupIt.More(); aGroupIt.Next()) {
290       const TopoDS_Shape& aGroupShape = aGroupIt.Value();
291       for(NCollection_List<TopoDS_Shape>::Iterator anUngroupedIt(anUngroupedShapes); anUngroupedIt.More(); anUngroupedIt.Next()) {
292         const TopoDS_Shape& anUngroupedShape = anUngroupedIt.Value();
293         bool isFound = false;
294         for(TopExp_Explorer aGroupShapeExp(aGroupShape, TopAbs_VERTEX); aGroupShapeExp.More(); aGroupShapeExp.Next()) {
295           const TopoDS_Shape& aVertex1 = aGroupShapeExp.Current();
296           for(TopExp_Explorer anUngroupedShapeExp(anUngroupedShape, TopAbs_VERTEX); anUngroupedShapeExp.More(); anUngroupedShapeExp.Next()) {
297             const TopoDS_Shape& aVertex2 = anUngroupedShapeExp.Current();
298             if(aVertex1.IsSame(aVertex2)) {
299               aGroupedShapes.Append(anUngroupedShape);
300               anUngroupedShapes.Remove(anUngroupedIt);
301               isFound = true;
302               break;
303             }
304           }
305           if(isFound) {
306             break;
307           }
308         }
309         if(!anUngroupedIt.More()) {
310           break;
311         }
312       }
313     }
314     aGroups.Append(aGroupedShapes);
315   }
316
317   TopoDS_Compound aCompound;
318   BRep_Builder aBuilder;
319   aBuilder.MakeCompound(aCompound);
320   ListOfShape aCompSolids, aFreeSolids;
321   for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator anIt(aGroups); anIt.More(); anIt.Next()) {
322     TopoDS_Compound aGroupCompound = makeCompound(anIt.Value());
323     GeomShapePtr aGeomShape(new GeomAPI_Shape());
324     aGeomShape->setImpl(new TopoDS_Shape(aGroupCompound));
325     aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
326                                                        GeomAPI_Shape::COMPSOLID,
327                                                        aCompSolids,
328                                                        aFreeSolids);
329     aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
330   }
331
332   if(!aCompound.IsNull()) {
333     aResult->setImpl(new TopoDS_Shape(aCompound));
334   }
335
336   return aResult;
337 }
338
339 //==================================================================================================
340 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
341 {
342   // Bounding box of all objects.
343   Bnd_Box aBndBox;
344
345   // Getting box.
346   for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
347     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
348     BRepBndLib::Add(aShape, aBndBox);
349   }
350
351   if(theEnlarge != 0.0) {
352     // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
353     aBndBox.Enlarge(theEnlarge);
354   }
355
356   Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
357   Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
358   Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
359   std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
360   int aNum = 0;
361   for(int i = 0; i < 2; i++) {
362     for(int j = 0; j < 2; j++) {
363       for(int k = 0; k < 2; k++) {
364         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
365         aResultPoints.push_back(aPnt);
366       }
367     }
368   }
369
370   return aResultPoints;
371 }
372
373 //==================================================================================================
374 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
375 {
376   if (!theFace.get())
377     return std::shared_ptr<GeomAPI_Shape>();
378
379   TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
380   if (aPlaneFace.IsNull())
381     return std::shared_ptr<GeomAPI_Shape>();
382
383   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
384   if (aPlane.IsNull())
385     return std::shared_ptr<GeomAPI_Shape>();
386
387   // make an infinity face on the plane
388   TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
389
390   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
391   aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
392   return aResult;
393 }
394
395 //==================================================================================================
396 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
397                                                                      const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
398 {
399   std::shared_ptr<GeomAPI_Shape> aResultShape;
400
401   if(!thePlane.get()) {
402     return aResultShape;
403   }
404
405   const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
406   if(aShape.ShapeType() != TopAbs_FACE) {
407     return aResultShape;
408   }
409
410   TopoDS_Face aFace = TopoDS::Face(aShape);
411   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
412   if(aSurf.IsNull()) {
413     return aResultShape;
414   }
415
416   GeomLib_IsPlanarSurface isPlanar(aSurf);
417   if(!isPlanar.IsPlanar()) {
418     return aResultShape;
419   }
420
421   if(thePoints.size() != 8) {
422     return aResultShape;
423   }
424
425   const gp_Pln& aFacePln = isPlanar.Plan();
426   Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
427   IntAna_Quadric aQuadric(aFacePln);
428   Standard_Real UMin, UMax, VMin, VMax;
429   UMin = UMax = VMin = VMax = 0;
430   for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
431     const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
432     gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
433     IntAna_IntConicQuad anIntAna(aLin, aQuadric);
434     const gp_Pnt& aPntOnFace = anIntAna.Point(1);
435     Standard_Real aPntU(0), aPntV(0);
436     GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
437     if(aPntU < UMin) UMin = aPntU;
438     if(aPntU > UMax) UMax = aPntU;
439     if(aPntV < VMin) VMin = aPntV;
440     if(aPntV > VMax) VMax = aPntV;
441   }
442   aResultShape.reset(new GeomAPI_Shape);
443   aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
444
445   return aResultShape;
446 }
447
448 //==================================================================================================
449 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
450                                         std::shared_ptr<GeomAPI_Vertex>& theV1,
451                                         std::shared_ptr<GeomAPI_Vertex>& theV2)
452 {
453   if(!theShape.get()) {
454     std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
455     aVertex->setImpl(new TopoDS_Vertex());
456     theV1 = aVertex;
457     theV2 = aVertex;
458     return;
459   }
460
461   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
462   TopoDS_Vertex aV1, aV2;
463   ShapeAnalysis::FindBounds(aShape, aV1, aV2);
464
465   std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
466   aGeomV1->setImpl(new TopoDS_Vertex(aV1));
467   aGeomV2->setImpl(new TopoDS_Vertex(aV2));
468   theV1 = aGeomV1;
469   theV2 = aGeomV2;
470 }
471
472 //==================================================================================================
473 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
474                                                 const std::shared_ptr<GeomAPI_Dir> theDirection,
475                                                 const ListOfShape& theWires,
476                                                 ListOfShape& theFaces)
477 {
478   BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
479                                           theDirection->impl<gp_Dir>()));
480   TopoDS_Face aFace = aMKFace.Face();
481
482   BRepAlgo_FaceRestrictor aFRestrictor;
483   aFRestrictor.Init(aFace, Standard_False, Standard_True);
484   for(ListOfShape::const_iterator anIt = theWires.cbegin();
485       anIt != theWires.cend();
486       ++anIt) {
487     TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
488     aFRestrictor.Add(aWire);
489   }
490
491   aFRestrictor.Perform();
492
493   if(!aFRestrictor.IsDone()) {
494     return;
495   }
496
497   for(; aFRestrictor.More(); aFRestrictor.Next()) {
498     GeomShapePtr aShape(new GeomAPI_Shape());
499     aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
500     theFaces.push_back(aShape);
501   }
502 }
503
504 //==================================================================================================
505 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
506 {
507   TopoDS_Compound aCompound;
508   BRep_Builder aBuilder;
509   aBuilder.MakeCompound(aCompound);
510
511   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
512     aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
513   }
514   BRepBuilderAPI_FindPlane aFindPlane(aCompound);
515
516   if(aFindPlane.Found() != Standard_True) {
517     return std::shared_ptr<GeomAPI_Pln>();
518   }
519
520   Handle(Geom_Plane) aPlane = aFindPlane.Plane();
521   gp_Pnt aLoc = aPlane->Location();
522   gp_Dir aDir = aPlane->Axis().Direction();
523
524   std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
525   std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
526
527   std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
528
529   return aPln;
530 }
531
532 //==================================================================================================
533 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(const std::shared_ptr<GeomAPI_Shape> theSubShape,
534                                                    const std::shared_ptr<GeomAPI_Shape> theBaseShape)
535 {
536   if(!theSubShape.get() || !theBaseShape.get()) {
537     return false;
538   }
539
540   const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
541   const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
542
543   if(aSubShape.ShapeType() == TopAbs_VERTEX) {
544     // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
545     BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
546     aDist.Perform();
547     if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
548       return false;
549     }
550   } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
551     if(aBaseShape.ShapeType() == TopAbs_FACE) {
552       // Check that edge is on face surface.
553       TopoDS_Face aFace = TopoDS::Face(aBaseShape);
554       TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
555       BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
556       aCheck.Perform();
557       if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
558         return false;
559       }
560
561       // Check intersections.
562       TopoDS_Vertex aV1, aV2;
563       ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
564       gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
565       gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
566       for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
567         const TopoDS_Shape& anEdgeOnFace = anExp.Current();
568         BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
569         aDist.Perform();
570         if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
571           // Edge intersect face bound. Check that it is not on edge begin or end.
572           for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
573             gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
574             if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
575                 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
576               return false;
577             }
578           }
579         }
580       }
581
582       // No intersections found. Edge is inside or outside face. Check it.
583       BRepAdaptor_Curve aCurveAdaptor(anEdge);
584       gp_Pnt aPointToCheck = aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() + aCurveAdaptor.LastParameter()) / 2.0);
585       Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
586       ShapeAnalysis_Surface aSAS(aSurface);
587       gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
588       BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
589       if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
590         return false;
591       }
592
593     } else {
594       return false;
595     }
596   } else {
597     return false;
598   }
599
600   return true;
601 }