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