Salome HOME
Move color tags to the base geometrical object class.
[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
8 #include <TColStd_ListIteratorOfListOfReal.hxx>
9
10 #include <TDataStd_RealList.hxx>
11
12 #include <TopoDS_Wire.hxx>
13
14
15 IMPLEMENT_STANDARD_HANDLE(HYDROData_ProfileUZ, HYDROData_IPolyline)
16 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_ProfileUZ, HYDROData_IPolyline)
17
18 HYDROData_ProfileUZ::HYDROData_ProfileUZ()
19 : HYDROData_IPolyline()
20 {
21 }
22
23 HYDROData_ProfileUZ::~HYDROData_ProfileUZ()
24 {
25 }
26
27 CurveCreator_ICurve::ListAISObjects HYDROData_ProfileUZ::constructWire() const
28 {
29   // TODO
30   ListAISObjects aProfileObjects;
31   return aProfileObjects;
32 }
33
34 bool HYDROData_ProfileUZ::clear()
35 {
36   removePointsLists();
37   return true;
38 }
39
40 bool HYDROData_ProfileUZ::join( const int theISectionTo, 
41                                 const int theISectionFrom )
42 {
43   return false;
44 }
45
46 int HYDROData_ProfileUZ::getNbSections() const
47 {
48   return 1;
49 }
50
51 int HYDROData_ProfileUZ::addSection( const std::string&              theName, 
52                                      const CurveCreator::SectionType theType,
53                                      const bool                      theIsClosed )
54 {
55   return 0;
56 }
57
58 bool HYDROData_ProfileUZ::removeSection( const int theISection )
59 {
60   return clear();
61 }
62
63 bool HYDROData_ProfileUZ::isClosed( const int theISection ) const
64 {
65   return false;
66 }
67
68 bool HYDROData_ProfileUZ::setClosed( const int  theISection, 
69                                      const bool theIsClosed )
70 {
71   return false;
72 }
73
74 std::string HYDROData_ProfileUZ::getSectionName( const int theISection ) const
75 {
76   return "Section_1";
77 }
78
79 bool HYDROData_ProfileUZ::setSectionName( const int          theISection, 
80                                           const std::string& theName )
81 {
82   return false;
83 }
84
85 CurveCreator::SectionType HYDROData_ProfileUZ::getSectionType( const int theISection ) const
86 {
87   return CurveCreator::Polyline;
88 }
89
90 bool HYDROData_ProfileUZ::setSectionType( const int                       theISection, 
91                                           const CurveCreator::SectionType theType )
92 {
93   return false;
94 }
95
96 bool HYDROData_ProfileUZ::addPoints( const CurveCreator::Coordinates& theCoords,
97                                      const int                        theISection,
98                                      const int                        theIPnt )
99 {
100   bool anIsOperation = myIsOperation;
101   if ( !anIsOperation )
102     startOperation();
103
104   bool aRes = true;
105
106   int anAfterPnt = theIPnt;
107
108   CurveCreator::Coordinates::const_iterator aBegIter = theCoords.begin();
109   CurveCreator::Coordinates::const_iterator anEndIter = theCoords.end();
110   while ( aBegIter != anEndIter )
111   {
112     const CurveCreator::TypeCoord& aCoordX = *aBegIter++;
113     if ( aBegIter == anEndIter )
114       break;
115
116     const CurveCreator::TypeCoord& aCoordY = *aBegIter++;
117
118     aRes = addPoint( aCoordX, aCoordY, theISection, anAfterPnt ) && aRes;
119     
120     if ( anAfterPnt != -1 )
121       ++anAfterPnt;
122   }
123
124   if ( !anIsOperation )
125     commitOperation();
126
127   return aRes;
128 }
129
130 bool HYDROData_ProfileUZ::addPoint( const CurveCreator::TypeCoord& theCoordX,
131                                     const CurveCreator::TypeCoord& theCoordY,
132                                     const int                      theISection,
133                                     const int                      theIPnt )
134 {
135   CurveCreator::TypeCoord aNewCoordU = theCoordX;
136   CurveCreator::TypeCoord aNewCoordZ = theCoordY;
137
138   Handle(TDataStd_RealList) aListU, aListZ;
139   getPointsLists( 0, aListU, aListZ );
140
141   if ( aListU->IsEmpty() || aNewCoordU > aListU->Last() )
142   {
143     aListU->Append( aNewCoordU );
144     aListZ->Append( aNewCoordZ );
145   }
146   else if ( aNewCoordU < aListU->First() )
147   {
148     aListU->Prepend( aNewCoordU );
149     aListZ->Prepend( aNewCoordZ );
150   }
151   else 
152   {
153     TColStd_ListOfReal anOldListU;
154     anOldListU = aListU->List();
155
156     TColStd_ListOfReal anOldListZ;
157     anOldListZ = aListZ->List();
158
159     // Refill the existing lists
160     aListU->Clear();
161     aListZ->Clear();
162
163     bool anIsInserted = false;
164     TColStd_ListIteratorOfListOfReal anIterU( anOldListU );
165     TColStd_ListIteratorOfListOfReal anIterZ( anOldListZ );
166     for ( ; anIterU.More() && anIterZ.More(); anIterU.Next(), anIterZ.Next() )
167     {
168       double aCoordU = anIterU.Value();
169       double aCoordZ = anIterZ.Value();
170
171       if ( !anIsInserted )
172       {
173         if ( ValuesEquals( aNewCoordU, aCoordU ) )
174         {
175           // Just update Z value
176           aCoordZ = aNewCoordZ;
177           anIsInserted = true;
178         }
179         else if ( aNewCoordU < aCoordU )
180         {
181           // Insert new point
182           aListU->Append( aNewCoordU );
183           aListZ->Append( aNewCoordZ );
184           anIsInserted = true;
185         }
186       }
187
188       aListU->Append( aCoordU );
189       aListZ->Append( aCoordZ );
190     }
191   }
192
193   return true;
194 }
195
196 bool HYDROData_ProfileUZ::removePoint( const int /*theISection*/,
197                                        const int theIPnt )
198 {
199   Handle(TDataStd_RealList) aListU, aListZ;
200   getPointsLists( 0, aListU, aListZ, false );
201   if ( aListU.IsNull() || aListZ.IsNull() || 
202        theIPnt < 0 || theIPnt >= aListU->Extent() )
203     return false;
204
205   bool anIsOperation = myIsOperation;
206   if ( !anIsOperation )
207     startOperation();
208
209   TColStd_ListOfReal anOldListU;
210   anOldListU = aListU->List();
211
212   TColStd_ListOfReal anOldListZ;
213   anOldListZ = aListZ->List();
214
215   // Refill the existing lists
216   aListU->Clear();
217   aListZ->Clear();
218
219   TColStd_ListIteratorOfListOfReal anIterU( anOldListU );
220   TColStd_ListIteratorOfListOfReal anIterZ( anOldListZ );
221   for ( int i = 0; anIterU.More() && anIterZ.More(); anIterU.Next(), anIterZ.Next(), ++i )
222   {
223     if ( i == theIPnt )
224       continue; // skip index to remove
225
226     aListU->Append( anIterU.Value() );
227     aListZ->Append( anIterZ.Value() );
228   }
229
230   if ( !anIsOperation )
231     commitOperation();
232
233   return true;
234 }
235
236 bool HYDROData_ProfileUZ::setPoint( const int                        /*theISection*/,
237                                     const int                        theIPoint,
238                                     const CurveCreator::Coordinates& theNewCoords )
239 {
240   bool anIsOperation = myIsOperation;
241   if ( !anIsOperation )
242     startOperation();
243
244   // At first we remove point
245   removePoint( 0, theIPoint );
246
247   // And then we insert it to correct place
248   bool aRes = addPoints( theNewCoords, 0 );
249
250   if ( !anIsOperation )
251     commitOperation();
252
253   return aRes;
254 }
255
256 CurveCreator::Coordinates HYDROData_ProfileUZ::getPoints( const int /*theISection*/ ) const
257 {
258   CurveCreator::Coordinates aResList;
259
260   Handle(TDataStd_RealList) aListU, aListZ;
261   getPointsLists( 0, aListU, aListZ, false );
262   if ( aListU.IsNull() || aListZ.IsNull() )
263     return aResList;
264
265   TColStd_ListIteratorOfListOfReal anIterU( aListU->List() );
266   TColStd_ListIteratorOfListOfReal anIterZ( aListZ->List() );
267   for ( ; anIterU.More() && anIterZ.More(); anIterU.Next(), anIterZ.Next() )
268   {
269     const double& aCoordU = anIterU.Value();
270     const double& aCoordZ = anIterZ.Value();
271
272     aResList.push_back( aCoordU );
273     aResList.push_back( aCoordZ );
274   }
275
276   return aResList;
277 }
278
279