Salome HOME
merge master
[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 #include <HYDROData_Profile.h>
36 #include <HYDROData_PolylineXY.h>
37
38
39 IMPLEMENT_STANDARD_HANDLE(HYDROData_ProfileUZ, HYDROData_IPolyline)
40 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_ProfileUZ, HYDROData_IPolyline)
41
42 HYDROData_ProfileUZ::HYDROData_ProfileUZ()
43 : HYDROData_IPolyline()
44 {
45 }
46
47 HYDROData_ProfileUZ::~HYDROData_ProfileUZ()
48 {
49 }
50
51 TopoDS_Shape HYDROData_ProfileUZ::GetShape() const
52 {
53   return TopoDS_Shape();
54 }
55
56 double HYDROData_ProfileUZ::GetDepthFromDistance( const PointsList& thePoints,
57                                                   const double&     theDistance )
58 {
59   double aResDepth = 0.0;
60
61   int aNbPoints = thePoints.Size();
62   if ( aNbPoints < 2 )
63     return aResDepth;
64
65   double aCompDist = 0.0;
66   HYDROData_IPolyline::Point aPrevPoint = thePoints.First();
67   for ( int i = 2; i <= aNbPoints; ++i )
68   {
69     const Point& aCurPoint = thePoints.Value( i );
70
71     double aPntDist = gp_Pnt2d( aPrevPoint.X(), 0 ).Distance( gp_Pnt2d( aCurPoint.X(), 0 ) );
72
73     aCompDist += aPntDist;
74
75     if ( theDistance < aCompDist )
76     {
77       double aComPntDist = gp_Pnt2d( thePoints.First().X(), 0 ).Distance( gp_Pnt2d( aPrevPoint.X(), 0 ) );
78
79       double aFindDist = theDistance - aComPntDist;
80       double aRatio = aFindDist / ( aPntDist - aFindDist );
81
82       aResDepth = ( aPrevPoint.Y() + aRatio * aCurPoint.Y() ) / ( 1 + aRatio );
83       break;
84     }
85     else aResDepth = aCurPoint.Y();  // TODO: workaround for normalized flat altitudes
86
87     aPrevPoint = aCurPoint;
88   }
89
90   return aResDepth;
91 }
92
93 int HYDROData_ProfileUZ::NbSections() const
94 {
95   return 1;
96 }
97
98 void HYDROData_ProfileUZ::AddSection( const TCollection_AsciiString& /*theSectName*/,
99                                       const SectionType              /*theSectionType*/,
100                                       const bool                     /*theIsClosed*/ )
101 {
102 }
103
104 TCollection_AsciiString HYDROData_ProfileUZ::GetSectionName( const int /*theSectionIndex*/ ) const
105 {
106   return "Section_1";
107 }
108
109 void HYDROData_ProfileUZ::SetSectionName( const int                      /*theSectionIndex*/, 
110                                           const TCollection_AsciiString& /*theSectionName*/ )
111 {
112 }
113
114 HYDROData_ProfileUZ::SectionType HYDROData_ProfileUZ::GetSectionType( const int /*theSectionIndex*/ ) const
115 {
116   Handle(TDataStd_ExtStringList) aNamesList;
117   Handle(TDataStd_IntegerList) aTypesList;
118   Handle(TDataStd_BooleanList) aClosuresList;
119   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
120   if ( aTypesList.IsNull() || aTypesList->IsEmpty() )
121     return SECTION_POLYLINE;
122
123   return (SectionType)aTypesList->First();
124 }
125
126 void HYDROData_ProfileUZ::SetSectionType( const int         /*theSectionIndex*/, 
127                                           const SectionType theSectionType )
128 {
129   Handle(TDataStd_ExtStringList) aNamesList;
130   Handle(TDataStd_IntegerList) aTypesList;
131   Handle(TDataStd_BooleanList) aClosuresList;
132   getSectionsLists( aNamesList, aTypesList, aClosuresList );
133   if ( aTypesList.IsNull()  )
134     return;
135
136   // Refill the existing list
137   aTypesList->Clear();
138   aTypesList->Append( theSectionType );
139 }
140
141 bool HYDROData_ProfileUZ::IsClosedSection( const int /*theSectionIndex*/ ) const
142 {
143   return false;
144 }
145
146 void HYDROData_ProfileUZ::SetSectionClosed( const int  /*theSectionIndex*/, 
147                                             const bool /*theIsClosed*/ )
148 {
149 }
150
151 void HYDROData_ProfileUZ::RemoveSection( const int /*theSectionIndex*/ )
152 {
153   RemoveSections();
154 }
155
156 void HYDROData_ProfileUZ::RemoveSections()
157 {
158   removePointsLists( 0 );
159 }
160
161 void HYDROData_ProfileUZ::AddPoint( const int    /*theSectionIndex*/,
162                                     const Point& thePoint,
163                                     const int    thePointIndex )
164 {
165   double aNewCoordU = thePoint.X();
166   double aNewCoordZ = thePoint.Y();
167
168   Handle(TDataStd_RealList) aListU, aListZ;
169   getPointsLists( 0, aListU, aListZ );
170
171   if ( aListU->IsEmpty() || aNewCoordU > aListU->Last() )
172   {
173     aListU->Append( aNewCoordU );
174     aListZ->Append( aNewCoordZ );
175     return;
176   }
177   else if ( aNewCoordU < aListU->First() )
178   {
179     aListU->Prepend( aNewCoordU );
180     aListZ->Prepend( aNewCoordZ );
181     return;
182   }
183
184   TColStd_ListOfReal anOldListU;
185   anOldListU = aListU->List();
186
187   TColStd_ListOfReal anOldListZ;
188   anOldListZ = aListZ->List();
189
190   // Crsat new lists
191   removePointsLists( 0 );
192   getPointsLists( 0, aListU, aListZ );
193
194   bool anIsInserted = false;
195   TColStd_ListIteratorOfListOfReal anIterU( anOldListU );
196   TColStd_ListIteratorOfListOfReal anIterZ( anOldListZ );
197   for ( ; anIterU.More() && anIterZ.More(); anIterU.Next(), anIterZ.Next() )
198   {
199     double aCoordU = anIterU.Value();
200     double aCoordZ = anIterZ.Value();
201
202     if ( !anIsInserted )
203     {
204       if ( ValuesEquals( aNewCoordU, aCoordU ) )
205       {
206         // Just update Z value
207         aCoordZ = aNewCoordZ;
208         anIsInserted = true;
209       }
210       else if ( aNewCoordU < aCoordU )
211       {
212         // Insert new point
213         aListU->Append( aNewCoordU );
214         aListZ->Append( aNewCoordZ );
215         anIsInserted = true;
216       }
217     }
218
219     aListU->Append( aCoordU );
220     aListZ->Append( aCoordZ );
221   }
222 }
223
224 void HYDROData_ProfileUZ::SetPoint( const int    theSectionIndex,
225                                     const Point& thePoint,
226                                     const int    /*thePointIndex*/ )
227 {
228   AddPoint( theSectionIndex, thePoint );
229 }
230
231 void HYDROData_ProfileUZ::RemovePoint( const int /*theSectionIndex*/,
232                                        const int thePointIndex )
233 {
234   Handle(TDataStd_RealList) aListU, aListZ;
235   getPointsLists( 0, aListU, aListZ, false );
236   if ( aListU.IsNull() || aListZ.IsNull() || aListU->IsEmpty() )
237     return;
238
239   TColStd_ListOfReal anOldListU;
240   anOldListU = aListU->List();
241
242   TColStd_ListOfReal anOldListZ;
243   anOldListZ = aListZ->List();
244
245   // Creat new lists
246   removePointsLists( 0 );
247   getPointsLists( 0, aListU, aListZ );
248
249   bool anIsInserted = false;
250   TColStd_ListIteratorOfListOfReal anIterU( anOldListU );
251   TColStd_ListIteratorOfListOfReal anIterZ( anOldListZ );
252   for ( int i = 0; anIterU.More() && anIterZ.More(); anIterU.Next(), anIterZ.Next(), ++i )
253   {
254     if ( i == thePointIndex )
255       continue; // skip index to remove
256
257     aListU->Append( anIterU.Value() );
258     aListZ->Append( anIterZ.Value() );
259   }
260 }
261
262 HYDROData_ProfileUZ::PointsList HYDROData_ProfileUZ::GetPoints( const int /*theSectionIndex*/, bool /*IsConvertToGlobal*/ ) const
263 {
264   PointsList aResList;
265
266   Handle(TDataStd_RealList) aListU, aListZ;
267   getPointsLists( 0, aListU, aListZ, false );
268   if ( aListU.IsNull() || aListZ.IsNull() )
269     return aResList;
270
271   TColStd_ListIteratorOfListOfReal anIterU( aListU->List() );
272   TColStd_ListIteratorOfListOfReal anIterZ( aListZ->List() );
273   for ( ; anIterU.More() && anIterZ.More(); anIterU.Next(), anIterZ.Next() )
274   {
275     Point aPoint( anIterU.Value(), anIterZ.Value() );
276     aResList.Append( aPoint );
277   }
278
279   return aResList;
280 }
281
282 void HYDROData_ProfileUZ::CalculateAndAddPoints(const NCollection_Sequence<gp_XYZ>& theXYZPoints, Handle_HYDROData_PolylineXY& thePolylineXY)
283 {
284    // Fill 2D polyline
285   for ( int i = 1; i <= theXYZPoints.Size(); i++ ) {
286     const HYDROData_Profile::ProfilePoint& aBottomPoint = theXYZPoints.Value( i );
287     thePolylineXY->AddPoint( 0, HYDROData_PolylineXY::Point( aBottomPoint.X(), aBottomPoint.Y() ) );
288   }
289   
290   // Calculate profile UZ points
291
292   // First point
293   const HYDROData_Profile::ProfilePoint& aFirstBottomPoint = theXYZPoints.First();
294   AddPoint( 0, HYDROData_ProfileUZ::Point( 0, aFirstBottomPoint.Z() ) );
295
296   // Intermediate points
297   double aPolylineCommonDist = thePolylineXY->GetDistance( 0, thePolylineXY->NbPoints( 0 ) - 1 );
298
299   for ( int i = 2, aNbPoints = theXYZPoints.Size(); i < aNbPoints; i++ ) {
300     const HYDROData_Profile::ProfilePoint& aBottomPoint = theXYZPoints.Value( i );
301     
302     double aDistance = thePolylineXY->GetDistance( 0, i - 1 );
303     
304     Standard_Real anU = aDistance; // = ( aDistance / aPolylineCommonDist ) * aPolylineCommonDist;
305     AddPoint( 0, HYDROData_ProfileUZ::Point( anU, aBottomPoint.Z() ) );
306   }
307   
308   // Last point
309   const HYDROData_Profile::ProfilePoint& aLastBottomPoint = theXYZPoints.Last();
310   AddPoint( 0, HYDROData_ProfileUZ::Point( aPolylineCommonDist, aLastBottomPoint.Z() ) );
311  
312 }
313
314