Salome HOME
Convert the altitude for bathymetry to negative number during it reading from the...
[modules/hydro.git] / src / HYDROData / HYDROData_Profile.cxx
1
2 #include "HYDROData_Profile.h"
3
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Iterator.h"
6 #include "HYDROData_Tool.h"
7
8 #include <BRepBuilderAPI_MakeEdge.hxx>
9 #include <BRepBuilderAPI_MakeWire.hxx>
10
11 #include <gp_XY.hxx>
12 #include <gp_XYZ.hxx>
13 #include <gp_Pnt2d.hxx>
14
15 #include <TDataStd_AsciiString.hxx>
16 #include <TDataStd_RealArray.hxx>
17
18 #include <TopoDS_Edge.hxx>
19 #include <TopoDS_Wire.hxx>
20
21 #include <OSD_File.hxx>
22 #include <OSD_Protection.hxx>
23
24 #include <QStringList>
25
26 #define PYTHON_PROFILE_ID "KIND_PROFILE"
27
28 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
29 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
30
31 HYDROData_Profile::HYDROData_Profile()
32 : HYDROData_Object()
33 {
34 }
35
36 HYDROData_Profile::~HYDROData_Profile()
37 {
38 }
39
40 TopoDS_Shape HYDROData_Profile::GetTopShape() const
41 {
42   TopoDS_Wire aWire;
43
44   gp_XY aFirstPoint, aLastPoint;
45   if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
46     return aWire;
47
48   gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
49   gp_Pnt aPnt2( aLastPoint.X(),  aLastPoint.Y(),  0 );
50
51   BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
52   TopoDS_Edge anEdge = aMakeEdge;
53
54   BRepBuilderAPI_MakeWire aMakeWire( anEdge );
55   aWire = aMakeWire;
56
57   return aWire;
58 }
59
60 TopoDS_Shape HYDROData_Profile::GetShape3D() const
61 {
62   return getShape3D();
63 }
64
65 void HYDROData_Profile::UpdateShape3D()
66 {
67   BRepBuilderAPI_MakeWire aMakeWire;
68
69   ProfilePoints aProfilePoints = GetProfilePoints();
70   for ( int i = 1, n = aProfilePoints.Length(); i < n ; ++i )
71   {
72     ProfilePoint aFirstPoint = aProfilePoints.Value( i );
73     ProfilePoint aSecPoint = aProfilePoints.Value( i + 1 );
74
75     gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
76     gp_Pnt aPnt2( aSecPoint.X(),   aSecPoint.Y(),   aSecPoint.Z()   );
77
78     BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
79     TopoDS_Edge anEdge = aMakeEdge;
80
81     aMakeWire.Add( anEdge );
82   }
83
84   TopoDS_Wire aWire;
85   if ( aMakeWire.IsDone() )
86     aWire = aMakeWire;
87
88   SetShape3D( aWire );
89 }
90
91 /**
92  * Dump object to Python script representation.
93  */
94 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
95 {
96   QStringList aResList;
97
98   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
99   if ( aDocument.IsNull() )
100     return aResList;
101                              
102   QString aDocName = aDocument->GetDocPyName();
103   QString aProfileName = GetName();
104
105   aResList << QString( "%1 = %2.CreateObject( %3 );" )
106               .arg( aProfileName ).arg( aDocName ).arg( PYTHON_PROFILE_ID );
107   aResList << QString( "%1.SetName( \"%1\" );" ).arg( aProfileName );
108
109   return aResList;
110 }
111
112 bool HYDROData_Profile::IsValid() const
113 {
114   gp_XY aFirstPoint, aLastPoint;
115   if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
116     return false;
117
118   int aNbPoints = NbPoints();
119   return aNbPoints > 1;
120 }
121
122 void HYDROData_Profile::SetFirstPoint( const gp_XY& thePoint )
123 {
124   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
125
126   Handle(TDataStd_RealArray) anArray;
127   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
128     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
129
130   anArray->SetValue( 0, thePoint.X() );
131   anArray->SetValue( 1, thePoint.Y() );
132 }
133
134 bool HYDROData_Profile::GetFirstPoint( gp_XY& thePoint ) const
135 {
136   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
137   if ( aLabel.IsNull() )
138     return false;
139
140   Handle(TDataStd_RealArray) anArray;
141   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
142     return false;
143
144   thePoint.SetX( anArray->Value( 0 ) );
145   thePoint.SetY( anArray->Value( 1 ) );
146
147   return true;
148 }
149
150 void HYDROData_Profile::SetLastPoint( const gp_XY& thePoint )
151 {
152   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
153
154   Handle(TDataStd_RealArray) anArray;
155   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
156     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
157
158   anArray->SetValue( 0, thePoint.X() );
159   anArray->SetValue( 1, thePoint.Y() );
160 }
161
162 bool HYDROData_Profile::GetLastPoint( gp_XY& thePoint ) const
163 {
164   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
165   if ( aLabel.IsNull() )
166     return false;
167
168   Handle(TDataStd_RealArray) anArray;
169   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
170     return false;
171
172   thePoint.SetX( anArray->Value( 0 ) );
173   thePoint.SetY( anArray->Value( 1 ) );
174
175   return true;
176 }
177
178 void HYDROData_Profile::Invalidate()
179 {
180   TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
181   if ( !aFirstLabel.IsNull() )
182     aFirstLabel.ForgetAllAttributes();
183
184   TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
185   if ( !aLastLabel.IsNull() )
186     aLastLabel.ForgetAllAttributes();
187 }
188
189 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
190 {
191   Handle(HYDROData_ProfileUZ) aProfileUZ;
192
193   TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
194   if ( aLabel.IsNull() )
195     return aProfileUZ;
196
197   aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
198   if ( aProfileUZ.IsNull() && theIsCreate )
199   {
200     aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
201       HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
202   }
203
204   return aProfileUZ;
205 }
206
207 int HYDROData_Profile::NbPoints() const
208 {
209   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
210   return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
211 }
212
213 void HYDROData_Profile::RemovePoints()
214 {
215   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
216   if ( !aProfileUZ.IsNull() )
217     aProfileUZ->RemoveSections();
218 }
219
220 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
221 {
222   RemovePoints();
223
224   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
225   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
226   {
227     const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
228     aProfileUZ->AddPoint( 0, aPoint );
229   }
230 }
231
232 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
233 {
234   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
235   return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
236 }
237
238 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
239 {
240   RemovePoints();
241   if ( thePoints.Length() < 2 )
242     return;
243
244   gp_XY aFirstPoint, aLastPoint;
245
246   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
247   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
248   {
249     const ProfilePoint& aPoint = thePoints.Value( i );
250     gp_XY aPointXY( aPoint.X(), aPoint.Y() );
251
252     if ( i == 1 )
253       aFirstPoint = aPointXY;
254     else if ( i == n )
255       aLastPoint = aPointXY;
256
257     double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
258     
259     HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
260     aProfileUZ->AddPoint( 0, aParPoint );
261   }
262
263   SetFirstPoint( aFirstPoint );
264   SetLastPoint( aLastPoint );
265 }
266
267 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
268 {
269   ProfilePoints aResPoints;
270
271   gp_XY aFirstPoint, aLastPoint;
272   if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
273     return aResPoints;
274
275   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
276   if ( aParametricPoints.Length() < 2 )
277     return aResPoints;
278
279   const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
280   const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
281
282   double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
283   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
284
285   // Add first point as is
286   aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
287
288   // Compute all other points
289   for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
290   {
291     const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
292
293     double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
294     
295     double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
296
297     double aRatio = aParLen / ( aGeoDistance - aParLen );
298
299     double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
300     double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
301
302     ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
303     aResPoints.Append( aCompPoint );
304   }
305
306   // Add last point as is
307   aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
308
309   return aResPoints;
310 }
311
312 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
313 {
314   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
315 }
316
317 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
318 {
319   TCollection_AsciiString aRes;
320
321   Handle(TDataStd_AsciiString) anAsciiStr;
322   if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
323     aRes = anAsciiStr->Get();
324
325   return aRes;
326 }
327
328 bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
329                                         const TCollection_AsciiString&    theFileName )
330 {
331   if ( theDoc.IsNull() || theFileName.IsEmpty() )
332     return false;
333
334   OSD_File aFile( theFileName );
335   if ( !aFile.IsReadable() )
336     return false;
337
338   aFile.Open( OSD_ReadOnly, OSD_Protection() );
339   if ( !aFile.IsOpen() )
340     return false;
341
342   bool aRes = true;
343
344   NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
345
346   while ( aRes && !aFile.IsAtEnd() )
347   {
348     Handle(HYDROData_Profile) aNewProfile = 
349       Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
350     
351     aRes = aNewProfile->ImportFromFile( aFile );
352
353     aCreatedProfiles.Append( aNewProfile );
354   }
355
356   // Close the file
357   aFile.Close();
358
359   for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
360   {
361     Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
362     if ( aRes )
363     {
364       QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
365       aProfile->SetName( aProfileName );
366
367       aProfile->SetFilePath( theFileName );
368     }
369     else
370     {
371       aProfile->Remove();
372     }
373   }
374
375   return aRes;
376 }
377
378 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
379 {
380   // Try to open the file
381   OSD_File aFile( theFileName );
382   if ( !aFile.IsReadable() )
383     return false;
384
385   aFile.Open( OSD_ReadOnly, OSD_Protection() );
386   if ( !aFile.IsOpen() )
387     return false;
388
389   bool aRes = ImportFromFile( aFile );
390
391   // Close the file
392   aFile.Close();
393
394   if ( aRes )
395   {
396     // Update file path
397     SetFilePath( theFileName );
398   }
399
400   return aRes;
401 }
402
403 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
404 {
405   if ( !theFile.IsOpen() )
406     return false;
407
408   bool aRes = true;
409
410   bool anIsParametric = false;
411   bool anIsGeoref     = false;
412
413   HYDROData_ProfileUZ::PointsList aPointsUZ;
414   ProfilePoints                   aPointsXYZ;
415
416   double aPrevVal = -DBL_MAX;
417   while ( !theFile.IsAtEnd() )
418   {
419     Standard_Integer aNbRead = 0;
420     TCollection_AsciiString aLine;
421     theFile.ReadLine( aLine, 1024, aNbRead );
422
423     aLine.LeftAdjust(); aLine.RightAdjust();
424     if ( aLine.IsEmpty() )
425       continue;
426
427     TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
428     TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
429     TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
430
431     if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
432          aValY.IsEmpty() || !aValY.IsRealValue() )
433     {
434       aRes = false;
435       break;
436     }
437
438     if ( !anIsParametric && !anIsGeoref )
439     {
440       anIsParametric = aValZ.IsEmpty();
441       anIsGeoref = !aValZ.IsEmpty();
442     }
443
444     double aCoordX = aValX.RealValue();
445     double aCoordY = aValY.RealValue();
446
447     if ( anIsParametric )
448     {
449       if ( aCoordX < aPrevVal )
450       {
451         // Move back readed line
452         theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
453         break;
454       }
455
456       HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
457       aPointsUZ.Append( aPoint );
458
459       aPrevVal = aCoordX;
460     }
461     else
462     {
463       if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
464       {
465         aRes = false;
466         break;
467       }
468
469       double aCoordZ = aValZ.RealValue();
470
471       ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
472       aPointsXYZ.Append( aPoint );
473     }
474   }
475   
476   aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() || 
477                    anIsGeoref && !aPointsXYZ.IsEmpty() );
478   if ( aRes )
479   {
480     // Update profile points
481     if ( anIsParametric )
482     {
483       SetParametricPoints( aPointsUZ );
484     }
485     else if ( anIsGeoref )
486     {
487       SetProfilePoints( aPointsXYZ );
488     }
489   }
490
491   return aRes;
492 }
493
494
495