Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / GeomAPI / GeomAPI_Shape.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Shape.cpp
4 // Created:     23 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "GeomAPI_Shape.h"
8
9 #include <BRep_Tool.hxx>
10 #include <BRepAlgoAPI_Section.hxx>
11 #include <BRepBndLib.hxx>
12 #include <BRepBuilderAPI_FindPlane.hxx>
13 #include <BRepExtrema_DistShapeShape.hxx>
14 #include <BRepTools.hxx>
15 #include <Bnd_Box.hxx>
16 #include <Geom_Circle.hxx>
17 #include <Geom_Conic.hxx>
18 #include <Geom_Curve.hxx>
19 #include <Geom_Ellipse.hxx>
20 #include <Geom_Hyperbola.hxx>
21 #include <Geom_Line.hxx>
22 #include <Geom_Parabola.hxx>
23 #include <Geom_Plane.hxx>
24 #include <Geom_RectangularTrimmedSurface.hxx>
25 #include <Geom_TrimmedCurve.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopoDS.hxx>
28 #include <TopoDS_Iterator.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <NCollection_List.hxx>
31
32 #include <sstream>
33 #include <algorithm> // for std::transform
34
35 #include <BRepTools.hxx>
36
37 #define MY_SHAPE implPtr<TopoDS_Shape>()
38
39 GeomAPI_Shape::GeomAPI_Shape()
40     : GeomAPI_Interface(new TopoDS_Shape())
41 {
42 }
43
44 std::shared_ptr<GeomAPI_Shape> GeomAPI_Shape::emptyCopied() const
45 {
46   GeomShapePtr aShape(new GeomAPI_Shape());
47   aShape->setImpl(new TopoDS_Shape(MY_SHAPE->EmptyCopied()));
48   return aShape;
49 }
50
51 bool GeomAPI_Shape::isNull() const
52 {
53   return MY_SHAPE->IsNull() == Standard_True;
54 }
55
56 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
57 {
58   if (!theShape.get())
59     return false;
60   if (isNull())
61     return theShape->isNull();
62   if (theShape->isNull())
63     return false;
64
65   return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
66 }
67
68 bool GeomAPI_Shape::isSame(const std::shared_ptr<GeomAPI_Shape> theShape) const
69 {
70   if (!theShape.get())
71     return false;
72   if (isNull())
73     return theShape->isNull();
74   if (theShape->isNull())
75     return false;
76
77   return MY_SHAPE->IsSame(theShape->impl<TopoDS_Shape>()) == Standard_True;
78 }
79
80 bool GeomAPI_Shape::isVertex() const
81 {
82   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
83   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
84 }
85
86 bool GeomAPI_Shape::isEdge() const
87 {
88   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
89   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
90 }
91
92 bool GeomAPI_Shape::isFace() const
93 {
94   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
95   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
96 }
97
98 bool GeomAPI_Shape::isCompound() const
99 {
100   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
101   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
102 }
103
104 bool GeomAPI_Shape::isCompoundOfSolids() const
105 {
106   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
107   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
108     return false;
109   bool isAtLeastOne = false;
110   for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
111     if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
112       return false;
113     isAtLeastOne = true;
114   }
115   return isAtLeastOne;
116 }
117
118 // adds the nopt-compound elements recursively to the list
119 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
120 {
121   if (!theShape.IsNull()) {
122     if (theShape.ShapeType() == TopAbs_COMPOUND) {
123       for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
124         addSimpleToList(aSubs.Value(), theList);
125       }
126     } else {
127       theList.Append(theShape);
128     }
129   }
130 }
131
132 bool GeomAPI_Shape::isConnectedTopology() const
133 {
134   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
135   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
136     return false;
137   // list of simple elements that are not detected in connection to others
138   NCollection_List<TopoDS_Shape> aNotConnected;
139   addSimpleToList(aShape, aNotConnected);
140   if (aNotConnected.IsEmpty()) // an empty compound
141     return false;
142
143   // collect here the group of connected subs, starting with one first element
144   NCollection_List<TopoDS_Shape> aNewConnected;
145   aNewConnected.Append(aNotConnected.First());
146   aNotConnected.RemoveFirst();
147   // iterate until some new element become connected
148   while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
149     NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
150     NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
151     while(aNotIter.More()) {
152       // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
153       NCollection_List<TopoDS_Shape> aNotVertices;
154       for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
155         aNotVertices.Append(anExp1.Current());
156       }
157
158       bool aConnected =  false;
159       NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
160       for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
161         // checking topological connecion of aNotIter and aNewIter
162         // (if shapes are connected, vertices are connected for sure)
163         TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
164         for(; !aConnected && anExp2.More(); anExp2.Next()) {
165           NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
166           for(; aNotIter.More(); aNotIter.Next()) {
167             if (aNotIter.Value().IsSame(anExp2.Current())) {
168               aConnected = true;
169               break;
170             }
171           }
172         }
173       }
174       if (aConnected) {
175         aNew.Append(aNotIter.Value());
176         aNotConnected.Remove(aNotIter);
177       } else {
178         aNotIter.Next();
179       }
180     }
181     // remove all new connected and put to this list very new connected
182     aNewConnected.Clear();
183     aNewConnected.Append(aNew);
184   }
185   return aNotConnected.IsEmpty() == Standard_True;
186 }
187
188 bool GeomAPI_Shape::isSolid() const
189 {
190   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
191   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
192 }
193
194 bool GeomAPI_Shape::isCompSolid() const
195 {
196   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
197   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
198 }
199
200 bool GeomAPI_Shape::isPlanar() const
201 {
202   TopoDS_Shape aShape = impl<TopoDS_Shape>();
203
204   if(aShape.IsNull()) {
205     return false;
206   }
207
208   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
209   if(aShapeType == TopAbs_COMPOUND) {
210     TopoDS_Iterator anIt(aShape);
211     int aShNum = 0;
212     for(; anIt.More(); anIt.Next()) {
213       ++aShNum;
214     }
215     if(aShNum == 1) {
216       anIt.Initialize(aShape);
217       aShape = anIt.Value();
218     }
219   }
220
221   aShapeType = aShape.ShapeType();
222   if(aShapeType == TopAbs_VERTEX) {
223     return true;
224   } else if(aShapeType == TopAbs_FACE) {
225     const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
226     Handle(Standard_Type) aType = aSurface->DynamicType();
227
228     if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
229       Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
230         Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
231       aType = aTrimSurface->BasisSurface()->DynamicType();
232     }
233     return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
234   } else {
235     BRepBuilderAPI_FindPlane aFindPlane(aShape);
236     bool isFound = aFindPlane.Found() == Standard_True;
237
238     if(!isFound && aShapeType == TopAbs_EDGE) {
239       Standard_Real aFirst, aLast;
240       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
241       Handle(Standard_Type) aType = aCurve->DynamicType();
242
243       if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
244         Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
245         aType = aTrimCurve->BasisCurve()->DynamicType();
246       }
247
248       if(aType == STANDARD_TYPE(Geom_Line)
249           || aType == STANDARD_TYPE(Geom_Conic)
250           || aType == STANDARD_TYPE(Geom_Circle)
251           || aType == STANDARD_TYPE(Geom_Ellipse)
252           || aType == STANDARD_TYPE(Geom_Hyperbola)
253           || aType == STANDARD_TYPE(Geom_Parabola)) {
254         isFound = true;
255       }
256     }
257
258     return isFound;
259   }
260
261   return false;
262 }
263
264 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
265 {
266   const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
267
268   ShapeType aST = GeomAPI_Shape::SHAPE;
269
270   switch(aShape.ShapeType()) {
271   case TopAbs_COMPOUND:
272     aST = GeomAPI_Shape::COMPOUND;
273     break;
274   case TopAbs_COMPSOLID:
275     aST = GeomAPI_Shape::COMPSOLID;
276     break;
277   case TopAbs_SOLID:
278     aST = GeomAPI_Shape::SOLID;
279     break;
280   case TopAbs_SHELL:
281     aST = GeomAPI_Shape::SHELL;
282     break;
283   case TopAbs_FACE:
284     aST = GeomAPI_Shape::FACE;
285     break;
286   case TopAbs_WIRE:
287     aST = GeomAPI_Shape::WIRE;
288     break;
289   case TopAbs_EDGE:
290     aST = GeomAPI_Shape::EDGE;
291     break;
292   case TopAbs_VERTEX:
293     aST = GeomAPI_Shape::VERTEX;
294     break;
295   case TopAbs_SHAPE:
296     aST = GeomAPI_Shape::SHAPE;
297     break;
298   }
299
300   return aST;
301 }
302
303 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeTypeByStr(std::string theType)
304 {
305   std::transform(theType.begin(), theType.end(), theType.begin(), ::toupper);
306   if (theType == "COMPOUND")
307     return COMPOUND;
308   if (theType == "COMPSOLID")
309     return COMPSOLID;
310   if (theType == "SOLID")
311     return SOLID;
312   if (theType == "SHELL")
313     return SHELL;
314   if (theType == "FACE")
315     return FACE;
316   if (theType == "WIRE")
317     return WIRE;
318   if (theType == "EDGE")
319     return EDGE;
320   if (theType == "VERTEX")
321     return VERTEX;
322   return SHAPE; // default
323 }
324
325 std::string GeomAPI_Shape::shapeTypeStr() const
326 {
327   ShapeType aShapeType = shapeType();
328   std::string aShapeTypeStr;
329
330   switch(aShapeType) {
331     case COMPOUND: {
332       aShapeTypeStr = "COMPOUND";
333       break;
334     }
335     case COMPSOLID: {
336       aShapeTypeStr = "COMPSOLID";
337       break;
338     }
339     case SOLID: {
340       aShapeTypeStr = "SOLID";
341       break;
342     }
343     case SHELL: {
344       aShapeTypeStr = "SHELL";
345       break;
346     }
347     case FACE: {
348       aShapeTypeStr = "FACE";
349       break;
350     }
351     case WIRE: {
352       aShapeTypeStr = "WIRE";
353       break;
354     }
355     case EDGE: {
356       aShapeTypeStr = "EDGE";
357       break;
358     }
359     case VERTEX: {
360       aShapeTypeStr = "VERTEX";
361       break;
362     }
363     case SHAPE: {
364       aShapeTypeStr = "SHAPE";
365       break;
366     }
367   }
368
369   return aShapeTypeStr;
370 }
371
372 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
373 {
374   TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
375
376   switch(anOrientation) {
377     case TopAbs_FORWARD:  return FORWARD;
378     case TopAbs_REVERSED: return REVERSED;
379     case TopAbs_INTERNAL: return INTERNAL;
380     case TopAbs_EXTERNAL: return EXTERNAL;
381     default:              return FORWARD;
382   }
383 }
384
385 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
386 {
387   TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
388
389   switch(theOrientation) {
390     case FORWARD:  MY_SHAPE->Orientation(TopAbs_FORWARD);  break;
391     case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
392     case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
393     case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
394   }
395 }
396
397 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape) const
398 {
399   if(!theShape.get()) {
400     return false;
401   }
402
403   const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
404   if(aShapeToSearch.IsNull()) {
405     return false;
406   }
407
408   for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
409     if(aShapeToSearch.IsEqual(anExp.Current())) {
410       return true;
411     }
412   }
413
414   return false;
415 }
416
417 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
418                                 double& theXmax, double& theYmax, double& theZmax) const
419 {
420   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
421   if (aShape.IsNull())
422     return false;
423   Bnd_Box aBndBox;
424   BRepBndLib::Add(aShape, aBndBox);
425   aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
426   return true;
427 }
428
429 std::string GeomAPI_Shape::getShapeStream() const
430 {
431   std::ostringstream aStream;
432   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
433   BRepTools::Write(aShape, aStream);
434   return aStream.str();
435 }
436
437 GeomShapePtr GeomAPI_Shape::intersect(const GeomShapePtr theShape) const
438 {
439   const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
440   const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
441
442   BRepAlgoAPI_Section aCommon(aShape1, aShape2);
443   if (!aCommon.IsDone())
444     return GeomShapePtr();
445
446   TopoDS_Shape aResult = aCommon.Shape();
447   if (aResult.ShapeType() == TopAbs_COMPOUND) {
448     NCollection_List<TopoDS_Shape> aSubs;
449     addSimpleToList(aResult, aSubs);
450     if(aSubs.Size() == 1) {
451       aResult = aSubs.First();
452     } else if(aSubs.Size() == 0) {
453       return GeomShapePtr();
454     }
455   }
456
457   GeomShapePtr aResShape(new GeomAPI_Shape);
458   aResShape->setImpl(new TopoDS_Shape(aResult));
459   return aResShape;
460 }
461
462 bool GeomAPI_Shape::isIntersect(const GeomShapePtr theShape) const
463 {
464   if(!theShape.get()) {
465     return false;
466   }
467
468   const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
469   const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
470
471   BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
472   aDist.Perform();
473   if(aDist.IsDone() && aDist.Value() < Precision::Confusion()) {
474     return true;
475   }
476
477   return false;
478 }