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