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