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