Salome HOME
Merge branch 'master' of ssh://git.salome-platform.org/modules/hydro
[modules/hydro.git] / src / HYDROData / HYDROData_ShapesTool.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROData_ShapesTool.h"
24
25 #include <BRep_Tool.hxx>
26
27 #include <gp_Pnt.hxx>
28
29 #include <Precision.hxx>
30
31 #include <TopExp.hxx>
32
33 #include <TopoDS.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Wire.hxx>
38
39 #include <TopTools_SequenceOfShape.hxx>
40 #include <TopTools_ListOfShape.hxx>
41 #include <TopTools_ListIteratorOfListOfShape.hxx>
42
43 #include <TopExp_Explorer.hxx>
44
45 void HYDROData_ShapesTool::ExploreShapeToShapes( const TopoDS_Shape&       theInShape,
46                                                  const TopAbs_ShapeEnum&   theExpType,
47                                                  TopTools_SequenceOfShape& theOutShapes )
48 {
49   theOutShapes.Clear();
50
51   if ( theInShape.IsNull() )
52     return;
53
54   TopExp_Explorer anExp( theInShape, theExpType );
55   for ( ; anExp.More(); anExp.Next() )
56   {
57     TopoDS_Shape anExpShape = anExp.Current();
58     theOutShapes.Append( anExpShape );
59   }
60 }
61
62 bool HYDROData_ShapesTool::Vertices( const TopoDS_Shape& theInShape,
63                                      TopoDS_Vertex&      theFirstVertex,
64                                      TopoDS_Vertex&      theLastVertex,
65                                      const bool          theIsCumOri  )
66 {
67   if ( theInShape.IsNull() )
68     return false;
69
70   if ( theInShape.ShapeType() == TopAbs_EDGE )
71   {
72     TopoDS_Edge anEdge = TopoDS::Edge( theInShape );
73     TopExp::Vertices( anEdge, theFirstVertex, theLastVertex, theIsCumOri );
74   }
75   else if ( theInShape.ShapeType() == TopAbs_WIRE )
76   {
77     TopoDS_Wire aWire = TopoDS::Wire( theInShape );
78     TopExp::Vertices( aWire, theFirstVertex, theLastVertex );
79   }
80   else
81   {
82     return false;
83   }
84
85   return true;
86 }
87
88 bool HYDROData_ShapesTool::IsVerticesEquals( const TopoDS_Vertex& theFirstVert,
89                                              const TopoDS_Vertex& theSecondVert,
90                                              const bool           theIs2D )
91 {
92   gp_Pnt aFirstPoint = BRep_Tool::Pnt( theFirstVert );
93   gp_Pnt aSecondPoint = BRep_Tool::Pnt( theSecondVert );
94
95   // Set the Z coord to zero for 2D equations
96   if ( theIs2D )
97   {
98     aFirstPoint.SetZ( 0.0 );
99     aSecondPoint.SetZ( 0.0 );
100   }
101
102   return aFirstPoint.IsEqual( aSecondPoint, Precision::Confusion() );
103 }
104
105 TopoDS_Shape HYDROData_ShapesTool::Translated( const TopoDS_Shape& theShape,
106                                                const double        theDx,
107                                                const double        theDy,
108                                                const double        theDz )
109 {
110   TopoDS_Shape aTranslatedShape;
111   if ( theShape.IsNull() )
112     return aTranslatedShape;
113
114   gp_Vec aVec( theDx, theDy, theDz );
115
116   gp_Trsf aTrsf;
117   aTrsf.SetTranslation( aVec );
118
119   TopLoc_Location aLocOrig = theShape.Location();
120   gp_Trsf aTrsfOrig = aLocOrig.Transformation();
121
122   TopLoc_Location aLocRes( aTrsf * aTrsfOrig );
123   aTranslatedShape = theShape.Located( aLocRes );
124
125   return aTranslatedShape;
126 }
127
128 bool HYDROData_ShapesTool::IsEdgesEquals( const TopoDS_Edge& theFirstEdge,
129                                           const TopoDS_Edge& theSecondEdge,
130                                           const bool         theIs2D )
131 {
132   TopoDS_Vertex aFFirstVert, aFLastVert, aSFirstVert, aSLastVert;
133   TopExp::Vertices( theFirstEdge, aFFirstVert, aFLastVert );
134   TopExp::Vertices( theSecondEdge, aSFirstVert, aSLastVert );
135   return IsVerticesEquals( aFFirstVert, aSFirstVert, theIs2D ) &&
136          IsVerticesEquals( aFLastVert, aSLastVert, theIs2D );
137 }
138
139 void HYDROData_ShapesTool::AddShapes( TopTools_SequenceOfShape&       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_SequenceOfShape&   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::AddShapes( TopTools_ListOfShape&           theShapes,
161                                       const TopTools_SequenceOfShape& theShapesToAdd )
162 {
163   for ( int i = 1, n = theShapesToAdd.Length(); i <= n; ++i )
164   {
165     const TopoDS_Shape& aShape = theShapesToAdd.Value( i );
166     theShapes.Append( aShape );
167   }
168 }
169
170 void HYDROData_ShapesTool::AddShapes( TopTools_ListOfShape&       theShapes,
171                                       const TopTools_ListOfShape& theShapesToAdd )
172 {
173   TopTools_ListIteratorOfListOfShape anIter( theShapesToAdd );
174   for ( ; anIter.More(); anIter.Next() )
175   {
176     const TopoDS_Shape& aShape = anIter.Value();
177     theShapes.Append( aShape );
178   }
179 }
180
181 void HYDROData_ShapesTool::DumpShapeSubShapes( std::ostream&           theStream,
182                                                const char*             theTitle,
183                                                const TopoDS_Shape&     theShape,
184                                                const TopAbs_ShapeEnum& theExpType )
185 {
186   TopTools_SequenceOfShape aShapeSubShapes;
187   ExploreShapeToShapes( theShape, theExpType, aShapeSubShapes );
188
189   theStream << theTitle << "\n";
190   DumpSequenceOfShapes( theStream, aShapeSubShapes );
191 }
192
193 void HYDROData_ShapesTool::DumpSequenceOfShapes( std::ostream&                   theStream,
194                                                  const TopTools_SequenceOfShape& theShapes )
195 {
196   for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
197   {
198     const TopoDS_Shape& aShape = theShapes.Value( i );
199     theStream << "Shape " << i << " : " << aShape.TShape() << "\n";
200   }
201 }