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