Salome HOME
d11b369168ddfac2bb90a472f71886eef06a5c13
[modules/hydro.git] / src / HYDROData / HYDROData_ShapesTool.cxx
1
2 #include "HYDROData_ShapesTool.h"
3
4 #include <BRep_Tool.hxx>
5
6 #include <gp_Pnt.hxx>
7
8 #include <Precision.hxx>
9
10 #include <TopExp.hxx>
11
12 #include <TopoDS.hxx>
13 #include <TopoDS_Edge.hxx>
14 #include <TopoDS_Shape.hxx>
15 #include <TopoDS_Vertex.hxx>
16 #include <TopoDS_Wire.hxx>
17
18 #include <TopTools_SequenceOfShape.hxx>
19 #include <TopTools_ListOfShape.hxx>
20 #include <TopTools_ListIteratorOfListOfShape.hxx>
21
22 #include <TopExp_Explorer.hxx>
23
24 void HYDROData_ShapesTool::ExploreShapeToShapes( const TopoDS_Shape&       theInShape,
25                                                  const TopAbs_ShapeEnum&   theExpType,
26                                                  TopTools_SequenceOfShape& theOutShapes )
27 {
28   theOutShapes.Clear();
29
30   if ( theInShape.IsNull() )
31     return;
32
33   TopExp_Explorer anExp( theInShape, theExpType );
34   for ( ; anExp.More(); anExp.Next() )
35   {
36     TopoDS_Shape anExpShape = anExp.Current();
37     theOutShapes.Append( anExpShape );
38   }
39 }
40
41 bool HYDROData_ShapesTool::Vertices( const TopoDS_Shape& theInShape,
42                                      TopoDS_Vertex&      theFirstVertex,
43                                      TopoDS_Vertex&      theLastVertex,
44                                      const bool          theIsCumOri  )
45 {
46   if ( theInShape.IsNull() )
47     return false;
48
49   if ( theInShape.ShapeType() == TopAbs_EDGE )
50   {
51     TopoDS_Edge anEdge = TopoDS::Edge( theInShape );
52     TopExp::Vertices( anEdge, theFirstVertex, theLastVertex, theIsCumOri );
53   }
54   else if ( theInShape.ShapeType() == TopAbs_WIRE )
55   {
56     TopoDS_Wire aWire = TopoDS::Wire( theInShape );
57     TopExp::Vertices( aWire, theFirstVertex, theLastVertex );
58   }
59   else
60   {
61     return false;
62   }
63
64   return true;
65 }
66
67 bool HYDROData_ShapesTool::IsVerticesEquals( const TopoDS_Vertex& theFirstVert,
68                                              const TopoDS_Vertex& theSecondVert,
69                                              const bool           theIs2D )
70 {
71   gp_Pnt aFirstPoint = BRep_Tool::Pnt( theFirstVert );
72   gp_Pnt aSecondPoint = BRep_Tool::Pnt( theSecondVert );
73
74   // Set the Z coord to zero for 2D equations
75   if ( theIs2D )
76   {
77     aFirstPoint.SetZ( 0.0 );
78     aSecondPoint.SetZ( 0.0 );
79   }
80
81   return aFirstPoint.IsEqual( aSecondPoint, Precision::Confusion() );
82 }
83
84 TopoDS_Shape HYDROData_ShapesTool::Translated( const TopoDS_Shape& theShape,
85                                                const double        theDx,
86                                                const double        theDy,
87                                                const double        theDz )
88 {
89   TopoDS_Shape aTranslatedShape;
90   if ( theShape.IsNull() )
91     return aTranslatedShape;
92
93   gp_Vec aVec( theDx, theDy, theDz );
94
95   gp_Trsf aTrsf;
96   aTrsf.SetTranslation( aVec );
97
98   TopLoc_Location aLocOrig = theShape.Location();
99   gp_Trsf aTrsfOrig = aLocOrig.Transformation();
100
101   TopLoc_Location aLocRes( aTrsf * aTrsfOrig );
102   aTranslatedShape = theShape.Located( aLocRes );
103
104   return aTranslatedShape;
105 }
106
107 bool HYDROData_ShapesTool::IsEdgesEquals( const TopoDS_Edge& theFirstEdge,
108                                           const TopoDS_Edge& theSecondEdge,
109                                           const bool         theIs2D )
110 {
111   TopoDS_Vertex aFFirstVert, aFLastVert, aSFirstVert, aSLastVert;
112   TopExp::Vertices( theFirstEdge, aFFirstVert, aFLastVert );
113   TopExp::Vertices( theSecondEdge, aSFirstVert, aSLastVert );
114   return IsVerticesEquals( aFFirstVert, aSFirstVert, theIs2D ) &&
115          IsVerticesEquals( aFLastVert, aSLastVert, theIs2D );
116 }
117
118 void HYDROData_ShapesTool::AddShapes( TopTools_SequenceOfShape&       theShapes,
119                                       const TopTools_SequenceOfShape& theShapesToAdd )
120 {
121   for ( int i = 1, n = theShapesToAdd.Length(); i <= n; ++i )
122   {
123     const TopoDS_Shape& aShape = theShapesToAdd.Value( i );
124     theShapes.Append( aShape );
125   }
126 }
127
128 void HYDROData_ShapesTool::AddShapes( TopTools_SequenceOfShape&   theShapes,
129                                       const TopTools_ListOfShape& theShapesToAdd )
130 {
131   TopTools_ListIteratorOfListOfShape anIter( theShapesToAdd );
132   for ( ; anIter.More(); anIter.Next() )
133   {
134     const TopoDS_Shape& aShape = anIter.Value();
135     theShapes.Append( aShape );
136   }
137 }
138
139 void HYDROData_ShapesTool::AddShapes( TopTools_ListOfShape&           theShapes,
140                                       const TopTools_SequenceOfShape& theShapesToAdd )
141 {
142   for ( int i = 1, n = theShapesToAdd.Length(); i <= n; ++i )
143   {
144     const TopoDS_Shape& aShape = theShapesToAdd.Value( i );
145     theShapes.Append( aShape );
146   }
147 }
148
149 void HYDROData_ShapesTool::AddShapes( TopTools_ListOfShape&       theShapes,
150                                       const TopTools_ListOfShape& theShapesToAdd )
151 {
152   TopTools_ListIteratorOfListOfShape anIter( theShapesToAdd );
153   for ( ; anIter.More(); anIter.Next() )
154   {
155     const TopoDS_Shape& aShape = anIter.Value();
156     theShapes.Append( aShape );
157   }
158 }
159
160 void HYDROData_ShapesTool::DumpShapeSubShapes( std::ostream&           theStream,
161                                                const char*             theTitle,
162                                                const TopoDS_Shape&     theShape,
163                                                const TopAbs_ShapeEnum& theExpType )
164 {
165   TopTools_SequenceOfShape aShapeSubShapes;
166   ExploreShapeToShapes( theShape, theExpType, aShapeSubShapes );
167
168   theStream << theTitle << "\n";
169   DumpSequenceOfShapes( theStream, aShapeSubShapes );
170 }
171
172 void HYDROData_ShapesTool::DumpSequenceOfShapes( std::ostream&                   theStream,
173                                                  const TopTools_SequenceOfShape& theShapes )
174 {
175   for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
176   {
177     const TopoDS_Shape& aShape = theShapes.Value( i );
178     theStream << "Shape " << i << " : " << aShape.TShape() << "\n";
179   }
180 }