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