Salome HOME
ea59ae09b468ba0bf9a403cc5c97dc9f43624401
[modules/shaper.git] / src / GeomAPI / GeomAPI_Shape.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAPI_Shape.h"
21
22 #include <GeomAPI_Pnt.h>
23 #include <GeomAPI_Vertex.h>
24 #include <GeomAPI_Edge.h>
25 #include <GeomAPI_Wire.h>
26 #include <GeomAPI_Face.h>
27 #include <GeomAPI_Shell.h>
28 #include <GeomAPI_Solid.h>
29 #include <GeomAPI_Trsf.h>
30
31 #include <BRep_Tool.hxx>
32 #include <BRepAlgoAPI_Section.hxx>
33 #include <BRepBndLib.hxx>
34 #include <BRepBuilderAPI_FindPlane.hxx>
35 #include <BRepBuilderAPI_Copy.hxx>
36 #include <BRepExtrema_DistShapeShape.hxx>
37 #include <BRepTools.hxx>
38 #include <Bnd_Box.hxx>
39 #include <Geom_Circle.hxx>
40 #include <Geom_Conic.hxx>
41 #include <Geom_Curve.hxx>
42 #include <Geom_Ellipse.hxx>
43 #include <Geom_Hyperbola.hxx>
44 #include <Geom_Line.hxx>
45 #include <Geom_Parabola.hxx>
46 #include <Geom_Plane.hxx>
47 #include <Geom_RectangularTrimmedSurface.hxx>
48 #include <Geom_TrimmedCurve.hxx>
49 #include <GeomLib_IsPlanarSurface.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Iterator.hxx>
53 #include <TopoDS_Shape.hxx>
54 #include <NCollection_List.hxx>
55
56 #include <BOPAlgo_CheckerSI.hxx>
57 #include <BOPDS_DS.hxx>
58
59 #include <sstream>
60 #include <algorithm> // for std::transform
61
62 #include <BRepTools.hxx>
63
64 #define MY_SHAPE implPtr<TopoDS_Shape>()
65
66 GeomAPI_Shape::GeomAPI_Shape()
67     : GeomAPI_Interface(new TopoDS_Shape())
68 {
69 }
70
71 std::shared_ptr<GeomAPI_Shape> GeomAPI_Shape::emptyCopied() const
72 {
73   GeomShapePtr aShape(new GeomAPI_Shape());
74   aShape->setImpl(new TopoDS_Shape(MY_SHAPE->EmptyCopied()));
75   return aShape;
76 }
77
78 bool GeomAPI_Shape::isNull() const
79 {
80   return MY_SHAPE->IsNull() == Standard_True;
81 }
82
83 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
84 {
85   if (!theShape.get())
86     return false;
87   if (isNull())
88     return theShape->isNull();
89   if (theShape->isNull())
90     return false;
91
92   return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
93 }
94
95 bool GeomAPI_Shape::isSame(const std::shared_ptr<GeomAPI_Shape> theShape) const
96 {
97   bool isNullShape = !theShape.get() || theShape->isNull();;
98   if (isNull())
99     return isNullShape;
100   if (isNullShape)
101     return false;
102
103   return MY_SHAPE->IsSame(theShape->impl<TopoDS_Shape>()) == Standard_True;
104 }
105
106 bool GeomAPI_Shape::isSameGeometry(const std::shared_ptr<GeomAPI_Shape> theShape) const
107 {
108   if (isFace())
109     return face()->isSameGeometry(theShape);
110   else if (isEdge())
111     return edge()->isSameGeometry(theShape);
112   return false;
113 }
114
115 bool GeomAPI_Shape::isVertex() const
116 {
117   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
118   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
119 }
120
121 bool GeomAPI_Shape::isEdge() const
122 {
123   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
124   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
125 }
126
127 bool GeomAPI_Shape::isWire() const
128 {
129   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
130   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_WIRE;
131 }
132
133 bool GeomAPI_Shape::isFace() const
134 {
135   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
136   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
137 }
138
139 bool GeomAPI_Shape::isShell() const
140 {
141   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
142   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SHELL;
143 }
144
145 bool GeomAPI_Shape::isCompound() const
146 {
147   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
148   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
149 }
150
151 bool GeomAPI_Shape::isCompoundOfSolids() const
152 {
153   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
154   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
155     return false;
156   bool isAtLeastOne = false;
157   for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
158     if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
159       return false;
160     isAtLeastOne = true;
161   }
162   return isAtLeastOne;
163 }
164
165 // LCOV_EXCL_START
166 GeomAPI_Shape::ShapeType GeomAPI_Shape::typeOfCompoundShapes() const
167 {
168   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
169   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
170     return SHAPE;
171   int aType = -1;
172   for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
173     if (!aSubs.Value().IsNull()) {
174       if (aType == -1)
175         aType = aSubs.Value().ShapeType();
176       else if (aSubs.Value().ShapeType() != aType)
177         return SHAPE;
178     }
179   }
180   return (GeomAPI_Shape::ShapeType) aType;
181 }
182 // LCOV_EXCL_STOP
183
184 // adds the nopt-compound elements recursively to the list
185 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
186 {
187   if (!theShape.IsNull()) {
188     if (theShape.ShapeType() == TopAbs_COMPOUND) {
189       for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
190         addSimpleToList(aSubs.Value(), theList);
191       }
192     } else {
193       theList.Append(theShape);
194     }
195   }
196 }
197
198 bool GeomAPI_Shape::isConnectedTopology() const
199 {
200   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
201   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
202     return false;
203   // list of simple elements that are not detected in connection to others
204   NCollection_List<TopoDS_Shape> aNotConnected;
205   addSimpleToList(aShape, aNotConnected);
206   if (aNotConnected.IsEmpty()) // an empty compound
207     return false;
208
209   // collect here the group of connected subs, starting with one first element
210   NCollection_List<TopoDS_Shape> aNewConnected;
211   aNewConnected.Append(aNotConnected.First());
212   aNotConnected.RemoveFirst();
213   // iterate until some new element become connected
214   while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
215     NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
216     NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
217     while(aNotIter.More()) {
218       // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
219       NCollection_List<TopoDS_Shape> aNotVertices;
220       for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
221         aNotVertices.Append(anExp1.Current());
222       }
223
224       bool aConnected =  false;
225       NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
226       for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
227         // checking topological connecion of aNotIter and aNewIter
228         // (if shapes are connected, vertices are connected for sure)
229         TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
230         for(; !aConnected && anExp2.More(); anExp2.Next()) {
231           NCollection_List<TopoDS_Shape>::Iterator aNotVIter(aNotVertices);
232           for(; aNotVIter.More(); aNotVIter.Next()) {
233             if (aNotVIter.Value().IsSame(anExp2.Current())) {
234               aConnected = true;
235               break;
236             }
237           }
238         }
239       }
240       if (aConnected) {
241         aNew.Append(aNotIter.Value());
242         aNotConnected.Remove(aNotIter);
243       } else {
244         aNotIter.Next();
245       }
246     }
247     // remove all new connected and put to this list very new connected
248     aNewConnected.Clear();
249     aNewConnected.Append(aNew);
250   }
251   return aNotConnected.IsEmpty() == Standard_True;
252 }
253
254 bool GeomAPI_Shape::isSolid() const
255 {
256   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
257   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
258 }
259
260 bool GeomAPI_Shape::isCompSolid() const
261 {
262   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
263   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
264 }
265
266 bool GeomAPI_Shape::isPlanar() const
267 {
268   TopoDS_Shape aShape = impl<TopoDS_Shape>();
269
270   if(aShape.IsNull()) {
271     return false;
272   }
273
274   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
275   if(aShapeType == TopAbs_COMPOUND) {
276     TopoDS_Iterator anIt(aShape);
277     int aShNum = 0;
278     for(; anIt.More(); anIt.Next()) {
279       ++aShNum;
280     }
281     if(aShNum == 1) {
282       anIt.Initialize(aShape);
283       aShape = anIt.Value();
284     }
285   }
286
287   aShapeType = aShape.ShapeType();
288   if(aShapeType == TopAbs_VERTEX) {
289     return true;
290   } else if(aShapeType == TopAbs_FACE) {
291     Handle(Geom_Surface) aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
292     if(aSurface->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
293       Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
294           Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
295       aSurface = aTrimSurface->BasisSurface();
296     }
297     return GeomLib_IsPlanarSurface(aSurface).IsPlanar();
298   } else {
299     BRepBuilderAPI_FindPlane aFindPlane(aShape);
300     bool isFound = aFindPlane.Found() == Standard_True;
301
302     if(!isFound && aShapeType == TopAbs_EDGE) {
303       Standard_Real aFirst, aLast;
304       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
305       Handle(Standard_Type) aType = aCurve->DynamicType();
306
307       if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
308         Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
309         aType = aTrimCurve->BasisCurve()->DynamicType();
310       }
311
312       if(aType == STANDARD_TYPE(Geom_Line)
313           || aType == STANDARD_TYPE(Geom_Conic)
314           || aType == STANDARD_TYPE(Geom_Circle)
315           || aType == STANDARD_TYPE(Geom_Ellipse)
316           || aType == STANDARD_TYPE(Geom_Hyperbola)
317           || aType == STANDARD_TYPE(Geom_Parabola)) {
318         isFound = true;
319       }
320     }
321
322     return isFound;
323   }
324
325   return false;
326 }
327
328 std::shared_ptr<GeomAPI_Vertex> GeomAPI_Shape::vertex() const
329 {
330   GeomVertexPtr aVertex;
331   if (isVertex()) {
332     const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
333     aVertex = GeomVertexPtr(new GeomAPI_Vertex);
334     aVertex->setImpl(new TopoDS_Shape(aShape));
335   }
336   return aVertex;
337 }
338
339 std::shared_ptr<GeomAPI_Edge> GeomAPI_Shape::edge() const
340 {
341   GeomEdgePtr anEdge;
342   if (isEdge()) {
343     const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
344     anEdge = GeomEdgePtr(new GeomAPI_Edge);
345     anEdge->setImpl(new TopoDS_Shape(aShape));
346   }
347   return anEdge;
348 }
349
350 std::shared_ptr<GeomAPI_Wire> GeomAPI_Shape::wire() const
351 {
352   GeomWirePtr aWire;
353   if (isWire()) {
354     const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
355     aWire = GeomWirePtr(new GeomAPI_Wire);
356     aWire->setImpl(new TopoDS_Shape(aShape));
357   }
358   return aWire;
359 }
360
361 std::shared_ptr<GeomAPI_Face> GeomAPI_Shape::face() const
362 {
363   GeomFacePtr aFace;
364   if (isFace()) {
365     const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
366     aFace = GeomFacePtr(new GeomAPI_Face);
367     aFace->setImpl(new TopoDS_Shape(aShape));
368   }
369   return aFace;
370 }
371
372 std::shared_ptr<GeomAPI_Shell> GeomAPI_Shape::shell() const
373 {
374   GeomShellPtr aShell;
375   if (isShell()) {
376     const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
377     aShell = GeomShellPtr(new GeomAPI_Shell);
378     aShell->setImpl(new TopoDS_Shape(aShape));
379   }
380   return aShell;
381 }
382
383 std::shared_ptr<GeomAPI_Solid> GeomAPI_Shape::solid() const
384 {
385   GeomSolidPtr aSolid;
386   if (isSolid()) {
387     const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
388     aSolid = GeomSolidPtr(new GeomAPI_Solid);
389     aSolid->setImpl(new TopoDS_Shape(aShape));
390   }
391   return aSolid;
392 }
393
394 std::list<std::shared_ptr<GeomAPI_Shape> >
395 GeomAPI_Shape::subShapes(ShapeType theSubShapeType) const
396 {
397   ListOfShape aSubs;
398   const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
399   if (aShape.IsNull())
400     return aSubs;
401
402   // process multi-level compounds
403   if (shapeType() == COMPOUND && theSubShapeType == COMPOUND) {
404     for (TopoDS_Iterator anIt(aShape); anIt.More(); anIt.Next()) {
405       const TopoDS_Shape& aCurrent = anIt.Value();
406       if (aCurrent.ShapeType() == TopAbs_COMPOUND) {
407         GeomShapePtr aSub(new GeomAPI_Shape);
408         aSub->setImpl(new TopoDS_Shape(aCurrent));
409         aSubs.push_back(aSub);
410       }
411     }
412     // add self
413     GeomShapePtr aSub(new GeomAPI_Shape);
414     aSub->setImpl(new TopoDS_Shape(aShape));
415     aSubs.push_back(aSub);
416   }
417   else {
418     for (TopExp_Explorer anExp(aShape, (TopAbs_ShapeEnum)theSubShapeType);
419          anExp.More(); anExp.Next()) {
420       GeomShapePtr aSub(new GeomAPI_Shape);
421       aSub->setImpl(new TopoDS_Shape(anExp.Current()));
422       aSubs.push_back(aSub);
423     }
424   }
425   return aSubs;
426 }
427
428 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
429 {
430   const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
431   if (aShape.IsNull())
432     return GeomAPI_Shape::SHAPE;
433
434   ShapeType aST = GeomAPI_Shape::SHAPE;
435
436   switch(aShape.ShapeType()) {
437   case TopAbs_COMPOUND:
438     aST = GeomAPI_Shape::COMPOUND;
439     break;
440   case TopAbs_COMPSOLID:
441     aST = GeomAPI_Shape::COMPSOLID;
442     break;
443   case TopAbs_SOLID:
444     aST = GeomAPI_Shape::SOLID;
445     break;
446   case TopAbs_SHELL:
447     aST = GeomAPI_Shape::SHELL;
448     break;
449   case TopAbs_FACE:
450     aST = GeomAPI_Shape::FACE;
451     break;
452   case TopAbs_WIRE:
453     aST = GeomAPI_Shape::WIRE;
454     break;
455   case TopAbs_EDGE:
456     aST = GeomAPI_Shape::EDGE;
457     break;
458   case TopAbs_VERTEX:
459     aST = GeomAPI_Shape::VERTEX;
460     break;
461   case TopAbs_SHAPE:
462     aST = GeomAPI_Shape::SHAPE;
463     break;
464   }
465
466   return aST;
467 }
468
469 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeTypeByStr(std::string theType)
470 {
471   std::transform(theType.begin(), theType.end(), theType.begin(),
472                  [](char c) { return static_cast<char>(::toupper(c)); });
473   if (theType == "COMPOUND" || theType == "COMPOUNDS")
474     return COMPOUND;
475   if (theType == "COMPSOLID" || theType == "COMPSOLIDS")
476     return COMPSOLID;
477   if (theType == "SOLID" || theType == "SOLIDS")
478     return SOLID;
479   if (theType == "SHELL" || theType == "SHELLS")
480     return SHELL;
481   if (theType == "FACE" || theType == "FACES")
482     return FACE;
483   if (theType == "WIRE" || theType == "WIRES")
484     return WIRE;
485   if (theType == "EDGE" || theType == "EDGES")
486     return EDGE;
487   if (theType == "VERTEX" || theType == "VERTICES")
488     return VERTEX;
489   return SHAPE; // default
490 }
491
492 std::string GeomAPI_Shape::shapeTypeStr() const
493 {
494   ShapeType aShapeType = shapeType();
495   std::string aShapeTypeStr;
496
497   switch(aShapeType) {
498     case COMPOUND: {
499       aShapeTypeStr = "COMPOUND";
500       break;
501     }
502     case COMPSOLID: {
503       aShapeTypeStr = "COMPSOLID";
504       break;
505     }
506     case SOLID: {
507       aShapeTypeStr = "SOLID";
508       break;
509     }
510     case SHELL: {
511       aShapeTypeStr = "SHELL";
512       break;
513     }
514     case FACE: {
515       aShapeTypeStr = "FACE";
516       break;
517     }
518     case WIRE: {
519       aShapeTypeStr = "WIRE";
520       break;
521     }
522     case EDGE: {
523       aShapeTypeStr = "EDGE";
524       break;
525     }
526     case VERTEX: {
527       aShapeTypeStr = "VERTEX";
528       break;
529     }
530     case SHAPE: {
531       aShapeTypeStr = "SHAPE";
532       break;
533     }
534   }
535
536   return aShapeTypeStr;
537 }
538
539 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
540 {
541   TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
542
543   switch(anOrientation) {
544     case TopAbs_FORWARD:  return FORWARD;
545     case TopAbs_REVERSED: return REVERSED;
546     case TopAbs_INTERNAL: return INTERNAL;
547     case TopAbs_EXTERNAL: return EXTERNAL;
548     default:              return FORWARD;
549   }
550 }
551
552 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
553 {
554   switch(theOrientation) {
555     case FORWARD:  MY_SHAPE->Orientation(TopAbs_FORWARD);  break;
556     case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
557     case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
558     case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
559   }
560 }
561
562 void GeomAPI_Shape::reverse()
563 {
564   MY_SHAPE->Reverse();
565 }
566
567 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape,
568                                const bool theCheckOrientation) const
569 {
570   if(!theShape.get()) {
571     return false;
572   }
573
574   const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
575   if(aShapeToSearch.IsNull()) {
576     return false;
577   }
578
579   for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
580     if(theCheckOrientation ?
581        aShapeToSearch.IsEqual(anExp.Current()) : aShapeToSearch.IsSame(anExp.Current())) {
582       return true;
583     }
584   }
585
586   return false;
587 }
588
589 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
590                                 double& theXmax, double& theYmax, double& theZmax) const
591 {
592   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
593   if (aShape.IsNull())
594     return false;
595   Bnd_Box aBndBox;
596   BRepBndLib::AddOptimal(aShape, aBndBox, false, true);
597   if (aBndBox.IsVoid())
598     return false;
599   aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
600   return true;
601 }
602
603 GeomPointPtr GeomAPI_Shape::middlePoint() const
604 {
605   GeomPointPtr aMiddlePoint;
606
607   switch (shapeType()) {
608   case VERTEX:
609     aMiddlePoint = vertex()->point();
610     break;
611   case EDGE:
612     aMiddlePoint = edge()->middlePoint();
613     break;
614   case WIRE:
615     aMiddlePoint = wire()->middlePoint();
616     break;
617   case FACE:
618     aMiddlePoint = face()->middlePoint();
619     break;
620   case SHELL:
621     aMiddlePoint = shell()->middlePoint();
622     break;
623   case SOLID:
624     aMiddlePoint = solid()->middlePoint();
625     break;
626   default: {
627       // get middle point as center of the bounding box
628       double aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ;
629       computeSize(aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ);
630       aMiddlePoint = GeomPointPtr(new GeomAPI_Pnt(
631           (aMinX + aMaxX) * 0.5, (aMinY + aMaxY) * 0.5, (aMinZ + aMaxZ) * 0.5));
632     }
633   }
634
635   return aMiddlePoint;
636 }
637
638 // LCOV_EXCL_START
639 std::string GeomAPI_Shape::getShapeStream(const bool theWithTriangulation) const
640 {
641   std::ostringstream aStream;
642   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
643   if (!theWithTriangulation) { // make a copy of shape without triangulation
644     BRepBuilderAPI_Copy aCopy(aShape, Standard_False, Standard_False);
645     const TopoDS_Shape& aCopyShape = aCopy.Shape();
646     // make all faces unchecked to make the stream of shapes the same
647     TopExp_Explorer aFaceExp(aCopyShape, TopAbs_FACE);
648     for(; aFaceExp.More(); aFaceExp.Next()) {
649       aFaceExp.Current().TShape()->Checked(Standard_False);
650     }
651     BRepTools::Write(aCopyShape, aStream);
652   } else {
653     BRepTools::Write(aShape, aStream);
654   }
655   return aStream.str();
656 }
657 // LCOV_EXCL_STOP
658
659 GeomShapePtr GeomAPI_Shape::intersect(const GeomShapePtr theShape) const
660 {
661   const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
662   const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
663
664   BRepAlgoAPI_Section aCommon(aShape1, aShape2);
665   if (!aCommon.IsDone())
666     return GeomShapePtr();
667
668   TopoDS_Shape aResult = aCommon.Shape();
669   if (aResult.ShapeType() == TopAbs_COMPOUND) {
670     NCollection_List<TopoDS_Shape> aSubs;
671     addSimpleToList(aResult, aSubs);
672     if(aSubs.Size() == 1) {
673       aResult = aSubs.First();
674     } else if(aSubs.Size() == 0) {
675       return GeomShapePtr();
676     }
677   }
678
679   GeomShapePtr aResShape(new GeomAPI_Shape);
680   aResShape->setImpl(new TopoDS_Shape(aResult));
681   return aResShape;
682 }
683
684 bool GeomAPI_Shape::isIntersect(const GeomShapePtr theShape) const
685 {
686   if(!theShape.get()) {
687     return false;
688   }
689
690   const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
691   const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
692
693   BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
694   aDist.Perform();
695   if(aDist.IsDone() && aDist.Value() < Precision::Confusion()) {
696     return true;
697   }
698
699   return false;
700 }
701
702 void GeomAPI_Shape::translate(const std::shared_ptr<GeomAPI_Dir> theDir, const double theOffset)
703 {
704   gp_Dir aDir = theDir->impl<gp_Dir>();
705   gp_Vec aTrsfVec(aDir.XYZ() * theOffset);
706   gp_Trsf aTranslation;
707   aTranslation.SetTranslation(aTrsfVec);
708   TopoDS_Shape aResult = MY_SHAPE->Moved(aTranslation);
709   setImpl(new TopoDS_Shape(aResult));
710 }
711
712 void GeomAPI_Shape::move(const std::shared_ptr<GeomAPI_Trsf> theTransformation)
713 {
714   TopoDS_Shape aResult = MY_SHAPE->Moved(theTransformation->impl<gp_Trsf>());
715   setImpl(new TopoDS_Shape(aResult));
716 }
717
718 bool GeomAPI_Shape::isSelfIntersected(const int theLevelOfCheck) const
719 {
720   BOPAlgo_CheckerSI aCSI;  // checker of self-interferences
721   aCSI.SetLevelOfCheck(theLevelOfCheck);
722   TopTools_ListOfShape aList;
723   const TopoDS_Shape& aThisShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
724   aList.Append(aThisShape);
725   aCSI.SetArguments(aList);
726   aCSI.Perform();
727   if (aCSI.HasErrors() || aCSI.DS().Interferences().Extent() > 0) {
728     return true;
729   }
730
731   return false;
732 }
733
734 bool GeomAPI_Shape::Comparator::operator()(const std::shared_ptr<GeomAPI_Shape>& theShape1,
735                                            const std::shared_ptr<GeomAPI_Shape>& theShape2) const
736 {
737   const TopoDS_Shape& aShape1 = theShape1->impl<TopoDS_Shape>();
738   const TopoDS_Shape& aShape2 = theShape2->impl<TopoDS_Shape>();
739   bool isLess = aShape1.TShape() < aShape2.TShape();
740   if (aShape1.TShape() == aShape2.TShape()) {
741     Standard_Integer aHash1 = aShape1.Location().HashCode(IntegerLast());
742     Standard_Integer aHash2 = aShape2.Location().HashCode(IntegerLast());
743     isLess = aHash1 < aHash2;
744   }
745   return isLess;
746 }
747
748 bool GeomAPI_Shape::ComparatorWithOri::operator()(
749     const std::shared_ptr<GeomAPI_Shape>& theShape1,
750     const std::shared_ptr<GeomAPI_Shape>& theShape2) const
751 {
752   const TopoDS_Shape& aShape1 = theShape1->impl<TopoDS_Shape>();
753   const TopoDS_Shape& aShape2 = theShape2->impl<TopoDS_Shape>();
754   bool isLess = aShape1.TShape() < aShape2.TShape();
755   if (aShape1.TShape() == aShape2.TShape()) {
756     Standard_Integer aHash1 = aShape1.Location().HashCode(IntegerLast());
757     Standard_Integer aHash2 = aShape2.Location().HashCode(IntegerLast());
758     isLess = (aHash1 < aHash2) ||
759              (aHash1 == aHash2 && aShape1.Orientation() < aShape2.Orientation());
760   }
761   return isLess;
762 }
763
764 int GeomAPI_Shape::Hash::operator()(const std::shared_ptr<GeomAPI_Shape>& theShape) const
765 {
766   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
767   return aShape.HashCode(IntegerLast());
768 }
769
770 bool GeomAPI_Shape::Equal::operator()(const std::shared_ptr<GeomAPI_Shape>& theShape1,
771                                       const std::shared_ptr<GeomAPI_Shape>& theShape2) const
772 {
773   const TopoDS_Shape& aShape1 = theShape1->impl<TopoDS_Shape>();
774   const TopoDS_Shape& aShape2 = theShape2->impl<TopoDS_Shape>();
775
776   Standard_Integer aHash1 = aShape1.Location().HashCode(IntegerLast());
777   Standard_Integer aHash2 = aShape2.Location().HashCode(IntegerLast());
778
779   return aShape1.TShape() == aShape2.TShape() && aHash1 == aHash2;
780 }