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