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