Salome HOME
refs #560: Strickler table: import/export
[modules/hydro.git] / src / HYDROData / HYDROData_ProfileUZ.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_ProfileUZ.h"
20
21 #include "HYDROData_Tool.h"
22
23 #include <gp_XY.hxx>
24 #include <gp_Pnt2d.hxx>
25
26 #include <TColStd_ListIteratorOfListOfReal.hxx>
27
28 #include <TDataStd_BooleanList.hxx>
29 #include <TDataStd_ExtStringList.hxx>
30 #include <TDataStd_IntegerList.hxx>
31 #include <TDataStd_RealList.hxx>
32
33 #include <TopoDS_Shape.hxx>
34
35
36 IMPLEMENT_STANDARD_HANDLE(HYDROData_ProfileUZ, HYDROData_IPolyline)
37 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_ProfileUZ, HYDROData_IPolyline)
38
39 HYDROData_ProfileUZ::HYDROData_ProfileUZ()
40 : HYDROData_IPolyline()
41 {
42 }
43
44 HYDROData_ProfileUZ::~HYDROData_ProfileUZ()
45 {
46 }
47
48 TopoDS_Shape HYDROData_ProfileUZ::GetShape() const
49 {
50   return TopoDS_Shape();
51 }
52
53 double HYDROData_ProfileUZ::GetDepthFromDistance( const PointsList& thePoints,
54                                                   const double&     theDistance )
55 {
56   double aResDepth = 0.0;
57
58   int aNbPoints = thePoints.Size();
59   if ( aNbPoints < 2 )
60     return aResDepth;
61
62   double aCompDist = 0.0;
63   HYDROData_IPolyline::Point aPrevPoint = thePoints.First();
64   for ( int i = 2; i <= aNbPoints; ++i )
65   {
66     const Point& aCurPoint = thePoints.Value( i );
67
68     double aPntDist = gp_Pnt2d( aPrevPoint.X(), 0 ).Distance( gp_Pnt2d( aCurPoint.X(), 0 ) );
69
70     aCompDist += aPntDist;
71
72     if ( theDistance < aCompDist )
73     {
74       double aComPntDist = gp_Pnt2d( thePoints.First().X(), 0 ).Distance( gp_Pnt2d( aPrevPoint.X(), 0 ) );
75
76       double aFindDist = theDistance - aComPntDist;
77       double aRatio = aFindDist / ( aPntDist - aFindDist );
78
79       aResDepth = ( aPrevPoint.Y() + aRatio * aCurPoint.Y() ) / ( 1 + aRatio );
80       break;
81     }
82
83     aPrevPoint = aCurPoint;
84   }
85
86   return aResDepth;
87 }
88
89 int HYDROData_ProfileUZ::NbSections() const
90 {
91   return 1;
92 }
93
94 void HYDROData_ProfileUZ::AddSection( const TCollection_AsciiString& /*theSectName*/,
95                                       const SectionType              /*theSectionType*/,
96                                       const bool                     /*theIsClosed*/ )
97 {
98 }
99
100 TCollection_AsciiString HYDROData_ProfileUZ::GetSectionName( const int /*theSectionIndex*/ ) const
101 {
102   return "Section_1";
103 }
104
105 void HYDROData_ProfileUZ::SetSectionName( const int                      /*theSectionIndex*/, 
106                                           const TCollection_AsciiString& /*theSectionName*/ )
107 {
108 }
109
110 HYDROData_ProfileUZ::SectionType HYDROData_ProfileUZ::GetSectionType( const int /*theSectionIndex*/ ) const
111 {
112   Handle(TDataStd_ExtStringList) aNamesList;
113   Handle(TDataStd_IntegerList) aTypesList;
114   Handle(TDataStd_BooleanList) aClosuresList;
115   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
116   if ( aTypesList.IsNull() || aTypesList->IsEmpty() )
117     return SECTION_POLYLINE;
118
119   return (SectionType)aTypesList->First();
120 }
121
122 void HYDROData_ProfileUZ::SetSectionType( const int         /*theSectionIndex*/, 
123                                           const SectionType theSectionType )
124 {
125   Handle(TDataStd_ExtStringList) aNamesList;
126   Handle(TDataStd_IntegerList) aTypesList;
127   Handle(TDataStd_BooleanList) aClosuresList;
128   getSectionsLists( aNamesList, aTypesList, aClosuresList );
129   if ( aTypesList.IsNull()  )
130     return;
131
132   // Refill the existing list
133   aTypesList->Clear();
134   aTypesList->Append( theSectionType );
135 }
136
137 bool HYDROData_ProfileUZ::IsClosedSection( const int /*theSectionIndex*/ ) const
138 {
139   return false;
140 }
141
142 void HYDROData_ProfileUZ::SetSectionClosed( const int  /*theSectionIndex*/, 
143                                             const bool /*theIsClosed*/ )
144 {
145 }
146
147 void HYDROData_ProfileUZ::RemoveSection( const int /*theSectionIndex*/ )
148 {
149   RemoveSections();
150 }
151
152 void HYDROData_ProfileUZ::RemoveSections()
153 {
154   removePointsLists( 0 );
155 }
156
157 void HYDROData_ProfileUZ::AddPoint( const int    /*theSectionIndex*/,
158                                     const Point& thePoint,
159                                     const int    thePointIndex )
160 {
161   double aNewCoordU = thePoint.X();
162   double aNewCoordZ = thePoint.Y();
163
164   Handle(TDataStd_RealList) aListU, aListZ;
165   getPointsLists( 0, aListU, aListZ );
166
167   if ( aListU->IsEmpty() || aNewCoordU > aListU->Last() )
168   {
169     aListU->Append( aNewCoordU );
170     aListZ->Append( aNewCoordZ );
171     return;
172   }
173   else if ( aNewCoordU < aListU->First() )
174   {
175     aListU->Prepend( aNewCoordU );
176     aListZ->Prepend( aNewCoordZ );
177     return;
178   }
179
180   TColStd_ListOfReal anOldListU;
181   anOldListU = aListU->List();
182
183   TColStd_ListOfReal anOldListZ;
184   anOldListZ = aListZ->List();
185
186   // Crsat new lists
187   removePointsLists( 0 );
188   getPointsLists( 0, aListU, aListZ );
189
190   bool anIsInserted = false;
191   TColStd_ListIteratorOfListOfReal anIterU( anOldListU );
192   TColStd_ListIteratorOfListOfReal anIterZ( anOldListZ );
193   for ( ; anIterU.More() && anIterZ.More(); anIterU.Next(), anIterZ.Next() )
194   {
195     double aCoordU = anIterU.Value();
196     double aCoordZ = anIterZ.Value();
197
198     if ( !anIsInserted )
199     {
200       if ( ValuesEquals( aNewCoordU, aCoordU ) )
201       {
202         // Just update Z value
203         aCoordZ = aNewCoordZ;
204         anIsInserted = true;
205       }
206       else if ( aNewCoordU < aCoordU )
207       {
208         // Insert new point
209         aListU->Append( aNewCoordU );
210         aListZ->Append( aNewCoordZ );
211         anIsInserted = true;
212       }
213     }
214
215     aListU->Append( aCoordU );
216     aListZ->Append( aCoordZ );
217   }
218 }
219
220 void HYDROData_ProfileUZ::SetPoint( const int    theSectionIndex,
221                                     const Point& thePoint,
222                                     const int    /*thePointIndex*/ )
223 {
224   AddPoint( theSectionIndex, thePoint );
225 }
226
227 void HYDROData_ProfileUZ::RemovePoint( const int /*theSectionIndex*/,
228                                        const int thePointIndex )
229 {
230   Handle(TDataStd_RealList) aListU, aListZ;
231   getPointsLists( 0, aListU, aListZ, false );
232   if ( aListU.IsNull() || aListZ.IsNull() || aListU->IsEmpty() )
233     return;
234
235   TColStd_ListOfReal anOldListU;
236   anOldListU = aListU->List();
237
238   TColStd_ListOfReal anOldListZ;
239   anOldListZ = aListZ->List();
240
241   // Creat new lists
242   removePointsLists( 0 );
243   getPointsLists( 0, aListU, aListZ );
244
245   bool anIsInserted = false;
246   TColStd_ListIteratorOfListOfReal anIterU( anOldListU );
247   TColStd_ListIteratorOfListOfReal anIterZ( anOldListZ );
248   for ( int i = 0; anIterU.More() && anIterZ.More(); anIterU.Next(), anIterZ.Next(), ++i )
249   {
250     if ( i == thePointIndex )
251       continue; // skip index to remove
252
253     aListU->Append( anIterU.Value() );
254     aListZ->Append( anIterZ.Value() );
255   }
256 }
257
258 HYDROData_ProfileUZ::PointsList HYDROData_ProfileUZ::GetPoints( const int /*theSectionIndex*/ ) const
259 {
260   PointsList aResList;
261
262   Handle(TDataStd_RealList) aListU, aListZ;
263   getPointsLists( 0, aListU, aListZ, false );
264   if ( aListU.IsNull() || aListZ.IsNull() )
265     return aResList;
266
267   TColStd_ListIteratorOfListOfReal anIterU( aListU->List() );
268   TColStd_ListIteratorOfListOfReal anIterZ( aListZ->List() );
269   for ( ; anIterU.More() && anIterZ.More(); anIterU.Next(), anIterZ.Next() )
270   {
271     Point aPoint( anIterU.Value(), anIterZ.Value() );
272     aResList.Append( aPoint );
273   }
274
275   return aResList;
276 }
277
278