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