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