Salome HOME
6fbfd97a90ddce334d6147278c4c26107294f785
[modules/hydro.git] / src / HYDROData / HYDROData_Profile.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_Profile.h"
20
21 #include "HYDROData_Document.h"
22 #include "HYDROData_Iterator.h"
23 #include "HYDROData_Tool.h"
24 #include "HYDROData_PolylineXY.h"
25
26 #include <boost/math/special_functions/fpclassify.hpp>
27
28 #include <BRepBuilderAPI_MakeEdge.hxx>
29 #include <BRepBuilderAPI_MakeWire.hxx>
30 #include <BRepBuilderAPI_MakePolygon.hxx>
31
32 #include <gp_XY.hxx>
33 #include <gp_XYZ.hxx>
34 #include <gp_Pnt2d.hxx>
35
36 #include <TDataStd_AsciiString.hxx>
37 #include <TDataStd_RealArray.hxx>
38
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Wire.hxx>
41
42 #include <OSD_File.hxx>
43 #include <OSD_Protection.hxx>
44
45 #include <QColor>
46 #include <QStringList>
47
48 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
49 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
50
51 HYDROData_Profile::HYDROData_Profile()
52 : HYDROData_Object()
53 {
54 }
55
56 HYDROData_Profile::~HYDROData_Profile()
57 {
58 }
59
60 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
61 {
62   QStringList aResList = dumpObjectCreation( theTreatedObjects );
63   QString aProfileName = GetObjPyName();
64
65   //TCollection_AsciiString aFilePath = GetFilePath();
66   //if ( !aFilePath.IsEmpty() ) 
67   //{
68   //  aResList << QString( "%1.ImportFromFile( \"%2\" );" )
69   //            .arg( aName ).arg( aFilePath.ToCString() );
70   //}
71
72   bool anIsValidProfile = IsValid();
73   
74   QStringList aPntsDefinition;
75   QString aPntsListName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "profile_points" );
76
77   QString aGap = QString().fill( ' ', aPntsListName.length() + 5 );
78   if ( anIsValidProfile )
79   {
80     HYDROData_Profile::ProfilePoints aPointsList = GetProfilePoints( true );
81     for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
82     {
83       const ProfilePoint& aPoint = aPointsList.Value( k );
84       aPntsDefinition << QString( aGap + "gp_XYZ( %1, %2, %3 )%4" )
85                          .arg( aPoint.X() ).arg( aPoint.Y() ).arg( aPoint.Z() )
86                          .arg( ( k < aNbPoints ? "," : "" ) );
87     }
88   }
89   else
90   {
91     HYDROData_IPolyline::PointsList aPointsList = GetParametricPoints();
92     for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
93     {
94       const HYDROData_IPolyline::Point& aPoint = aPointsList.Value( k );
95       aPntsDefinition << QString( aGap + "gp_XY( %1, %2 )%3" )
96                          .arg( aPoint.X() ).arg( aPoint.Y() )
97                          .arg( ( k < aNbPoints ? "," : "" ) );
98     }
99   }
100
101   if ( !aPntsDefinition.isEmpty() )
102   {
103     QString& aFirstStr = aPntsDefinition.first();
104     aFirstStr = aFirstStr.trimmed();
105     aFirstStr.prepend( QString( "%1 = [ " ).arg( aPntsListName ) );
106     
107     aPntsDefinition.last().append( " ];" );
108
109     aResList << aPntsDefinition;
110     
111     aResList << QString( "%1.%3( %2 );" )
112                 .arg( aProfileName ).arg( aPntsListName )
113                 .arg( anIsValidProfile ? "SetProfilePoints" : "SetParametricPoints" );
114   
115     aResList << QString( "" );
116   }
117
118   // Set a polyline type if it is not default
119   Handle(HYDROData_ProfileUZ) aPrf = GetProfileUZ( false );
120   if ( !aPrf.IsNull() )
121   {
122     HYDROData_IPolyline::SectionType aSecType = aPrf->GetSectionType( 0 );
123     if ( aSecType != HYDROData_IPolyline::SECTION_POLYLINE )
124     {
125       aResList << QString( "%1.GetProfileUZ().SetSectionType( 0, %2 );" )
126                   .arg( aProfileName ).arg( "HYDROData_IPolyline.SECTION_SPLINE" );
127       aResList << QString( "" );
128     }
129   }
130
131   aResList << QString( "%1.Update();" ).arg( aProfileName );
132   aResList << QString( "" );
133
134   return aResList;
135 }
136
137 TopoDS_Shape HYDROData_Profile::GetTopShape() const
138 {
139   TopoDS_Wire aWire;
140
141   gp_XY aFirstPoint, aLastPoint;
142   if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
143     return aWire;
144
145   gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
146   gp_Pnt aPnt2( aLastPoint.X(),  aLastPoint.Y(),  0 );
147
148   BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
149   TopoDS_Edge anEdge = aMakeEdge;
150
151   BRepBuilderAPI_MakeWire aMakeWire( anEdge );
152   aWire = aMakeWire;
153
154   return aWire;
155 }
156
157 TopoDS_Shape HYDROData_Profile::GetShape3D() const
158 {
159   TopoDS_Shape aShape = getShape3D();
160   if( aShape.IsNull() )
161     aShape = CreateProfileWire( true );
162   return aShape;
163 }
164
165 TopoDS_Shape HYDROData_Profile::CreateProfileWire( bool canUseDefaultPoints ) const
166 {
167   TopoDS_Wire aWire;
168   Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
169   if ( !aProfile.IsNull() )
170   {
171     ProfilePoints aProfilePoints = GetProfilePoints( false, canUseDefaultPoints );
172     HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
173
174     aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
175   }
176   return aWire;
177 }
178
179 void HYDROData_Profile::Update()
180 {
181   HYDROData_Object::Update();
182
183   TopoDS_Shape aShape = CreateProfileWire( false );
184   SetShape3D( aShape );
185 }
186
187 QColor HYDROData_Profile::DefaultFillingColor()
188 {
189   return QColor( Qt::transparent );
190 }
191
192 QColor HYDROData_Profile::DefaultBorderColor()
193 {
194   return QColor( Qt::black );
195 }
196
197 QColor HYDROData_Profile::getDefaultFillingColor() const
198 {
199   return DefaultFillingColor();
200 }
201
202 QColor HYDROData_Profile::getDefaultBorderColor() const
203 {
204   return DefaultBorderColor();
205 }
206
207 bool HYDROData_Profile::IsValid() const
208 {
209   gp_XY aFirstPoint, aLastPoint;
210   if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
211     return false;
212
213   int aNbPoints = NbPoints();
214   return aNbPoints > 1;
215 }
216
217 void HYDROData_Profile::SetLeftPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
218 {
219   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
220   if ( aLabel.IsNull() )
221     return;
222
223   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
224   gp_XY aLPoint = theGPoint;
225   if( IsConvertFromGlobal )
226     aDoc->Transform( aLPoint, true );
227
228   Handle(TDataStd_RealArray) anArray;
229   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
230     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
231
232   anArray->SetValue( 0, aLPoint.X() );
233   anArray->SetValue( 1, aLPoint.Y() );
234
235   SetToUpdate( true );
236 }
237
238 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint, bool IsConvertToGlobal,
239                                       bool CanUseDefault ) const
240 {
241   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
242   if ( aParametricPoints.Length() < 2 )
243     return false;
244
245   thePoint = GetParametricPoints().First();
246
247   thePoint.SetX( 0 );
248   thePoint.SetY( 0 ); //default left point of not-georeferenced profile
249   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
250   if ( aLabel.IsNull() )
251   {
252     return CanUseDefault;
253   }
254
255   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
256   Handle(TDataStd_RealArray) anArray;
257   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
258   {
259     return CanUseDefault;
260   }
261
262   thePoint.SetX( anArray->Value( 0 ) );
263   thePoint.SetY( anArray->Value( 1 ) );
264
265   if( IsConvertToGlobal )
266     aDoc->Transform( thePoint, false );
267
268   return true;
269 }
270
271 void HYDROData_Profile::SetRightPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
272 {
273   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
274
275   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
276   gp_XY aLPoint = theGPoint;
277   if( IsConvertFromGlobal )
278     aDoc->Transform( aLPoint, true );
279
280   Handle(TDataStd_RealArray) anArray;
281   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
282     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
283
284   anArray->SetValue( 0, aLPoint.X() );
285   anArray->SetValue( 1, aLPoint.Y() );
286
287   SetToUpdate( true );
288 }
289
290 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint, bool IsConvertToGlobal,
291                                        bool CanUseDefault ) const
292 {
293   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
294   if ( aParametricPoints.Length() < 2 )
295     return false;
296
297   thePoint = GetParametricPoints().Last();
298
299   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
300   if ( aLabel.IsNull() )
301   {
302     return CanUseDefault;
303   }
304
305   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
306   Handle(TDataStd_RealArray) anArray;
307   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
308   {
309     return CanUseDefault;
310   }
311
312   thePoint.SetX( anArray->Value( 0 ) );
313   thePoint.SetY( anArray->Value( 1 ) );
314
315   if( IsConvertToGlobal )
316     aDoc->Transform( thePoint, false );
317
318   return true;
319 }
320
321 void HYDROData_Profile::Invalidate()
322 {
323   TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
324   if ( !aFirstLabel.IsNull() )
325     aFirstLabel.ForgetAllAttributes();
326
327   TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
328   if ( !aLastLabel.IsNull() )
329     aLastLabel.ForgetAllAttributes();
330
331   SetToUpdate( true );
332 }
333
334 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
335 {
336   Handle(HYDROData_ProfileUZ) aProfileUZ;
337
338   TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
339   if ( aLabel.IsNull() )
340     return aProfileUZ;
341
342   aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
343   if ( aProfileUZ.IsNull() && theIsCreate )
344   {
345     aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
346       HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
347   }
348
349   return aProfileUZ;
350 }
351
352 int HYDROData_Profile::NbPoints() const
353 {
354   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
355   return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
356 }
357
358 void HYDROData_Profile::RemovePoints()
359 {
360   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
361   if ( !aProfileUZ.IsNull() )
362   {
363     aProfileUZ->RemoveSections();
364     SetToUpdate( true );
365   }
366 }
367
368 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
369 {
370   RemovePoints();
371
372   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
373   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
374   {
375     const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
376     aProfileUZ->AddPoint( 0, aPoint );
377   }
378
379   SetToUpdate( true );
380 }
381
382 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
383 {
384   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
385   return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
386 }
387
388 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints, bool IsConvertFromGlobal )
389 {
390   RemovePoints();
391   if ( thePoints.Length() < 2 )
392     return;
393
394   gp_XY aFirstPoint, aLastPoint;
395
396   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
397   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
398   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
399   {
400     ProfilePoint aPoint = thePoints.Value( i );
401     if( IsConvertFromGlobal )
402       aDoc->Transform( aPoint, true );
403
404     gp_XY aPointXY( aPoint.X(), aPoint.Y() );
405
406     if ( i == 1 )
407       aFirstPoint = aPointXY;
408     else if ( i == n )
409       aLastPoint = aPointXY;
410
411     double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
412     
413     HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
414     aProfileUZ->AddPoint( 0, aParPoint );
415   }
416
417   SetLeftPoint( aFirstPoint, false );//already converted to local CS
418   SetRightPoint( aLastPoint, false );
419 }
420
421 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints
422   ( bool IsConvertToGlobal, bool CanUseDefaultLeftRight ) const
423 {
424   ProfilePoints aResPoints;
425
426   gp_XY aFirstPoint, aLastPoint;
427   if ( !GetLeftPoint( aFirstPoint, IsConvertToGlobal, CanUseDefaultLeftRight ) ||
428        !GetRightPoint( aLastPoint, IsConvertToGlobal, CanUseDefaultLeftRight ) )
429     return aResPoints;
430
431   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
432   if ( aParametricPoints.Length() < 2 )
433     return aResPoints;
434
435   const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
436   const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
437
438   double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
439   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
440
441   // Add first point as is
442   aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
443
444   // Compute all other points
445   for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
446   {
447     const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
448
449     double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
450     
451     double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
452
453     double aRatio = aParLen / ( aGeoDistance - aParLen );
454
455     double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
456     double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
457
458     ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
459     aResPoints.Append( aCompPoint );
460   }
461
462   // Add last point as is
463   aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
464
465   return aResPoints;
466 }
467
468 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
469 {
470   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
471 }
472
473 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
474 {
475   TCollection_AsciiString aRes;
476
477   Handle(TDataStd_AsciiString) anAsciiStr;
478   if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
479     aRes = anAsciiStr->Get();
480
481   return aRes;
482 }
483
484 int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
485                                        const TCollection_AsciiString&    theFileName,
486                                        NCollection_Sequence<int>&        theBadProfilesIds )
487 {
488   if ( theDoc.IsNull() || theFileName.IsEmpty() )
489     return 0;
490
491   OSD_File aFile( theFileName );
492   if ( !aFile.IsReadable() )
493     return 0;
494
495   aFile.Open( OSD_ReadOnly, OSD_Protection() );
496   if ( !aFile.IsOpen() )
497     return 0;
498
499   NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
500
501   int aProfileId = 1;
502   Handle(HYDROData_Profile) aNewProfile;
503   for ( ; !aFile.IsAtEnd(); ++aProfileId )
504   {
505     if ( aNewProfile.IsNull() )
506       aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
507     
508     bool anIsRead = false;
509     if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) )
510     {
511       aCreatedProfiles.Append( aNewProfile );
512       aNewProfile.Nullify();
513     }
514     else if ( anIsRead )
515     {
516       theBadProfilesIds.Append( aProfileId );
517     }
518   }
519
520   if ( !aNewProfile.IsNull() )
521     aNewProfile->Remove();
522
523   // Close the file
524   aFile.Close();
525
526   for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
527   {
528     Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
529
530     QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
531     aProfile->SetName( aProfileName );
532
533     aProfile->SetFilePath( theFileName );
534
535     aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
536   }
537
538   return aCreatedProfiles.Length();
539 }
540
541 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
542                                         bool*                          theIsRead )
543 {
544   if ( theIsRead )
545     *theIsRead = false;
546
547   // Try to open the file
548   OSD_File aFile( theFileName );
549   if ( !aFile.IsReadable() )
550     return false;
551
552   aFile.Open( OSD_ReadOnly, OSD_Protection() );
553   if ( !aFile.IsOpen() )
554     return false;
555
556   bool aRes = ImportFromFile( aFile, theIsRead );
557
558   // Close the file
559   aFile.Close();
560
561   if ( aRes )
562   {
563     // Update file path
564     SetFilePath( theFileName );
565   }
566
567   return aRes;
568 }
569
570 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
571                                         bool*     theIsRead )
572 {
573   if ( theIsRead )
574     *theIsRead = false;
575
576   if ( !theFile.IsOpen() )
577     return false;
578
579   bool aRes = true;
580
581   bool anIsParametric = false;
582   bool anIsGeoref     = false;
583
584   HYDROData_ProfileUZ::PointsList aPointsUZ;
585   ProfilePoints                   aPointsXYZ;
586
587   double aPrevVal = -DBL_MAX;
588   while ( !theFile.IsAtEnd() )
589   {
590     Standard_Integer aNbRead = 0;
591     TCollection_AsciiString aLine;
592     theFile.ReadLine( aLine, 1024, aNbRead );
593
594     aLine.LeftAdjust(); aLine.RightAdjust();
595     if ( aLine.IsEmpty() )
596     {
597       if ( !anIsParametric && !anIsGeoref )
598         continue; // Definition is not started yet
599
600       break; // Next profile started
601     }
602
603     // Set flag of read status to true
604     if ( theIsRead )
605       *theIsRead = true;
606
607     TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
608     TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
609     TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
610
611     if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
612          aValY.IsEmpty() || !aValY.IsRealValue() )
613     {
614       aRes = false;
615       break;
616     }
617
618     if ( !anIsParametric && !anIsGeoref )
619     {
620       anIsParametric = aValZ.IsEmpty();
621       anIsGeoref = !aValZ.IsEmpty();
622     }
623
624     double aCoordX = aValX.RealValue();
625     double aCoordY = aValY.RealValue();
626
627     if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) ||
628          boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) )
629       aRes = false;
630
631     if ( anIsParametric )
632     {
633       if ( aCoordX < aPrevVal )
634       {
635         // Move back readed line
636         theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
637         break;
638       }
639
640       HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
641       aPointsUZ.Append( aPoint );
642
643       aPrevVal = aCoordX;
644     }
645     else
646     {
647       if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
648       {
649         aRes = false;
650         break;
651       }
652
653       double aCoordZ = aValZ.RealValue();
654       if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) )
655         aRes = false;
656
657       ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
658       aPointsXYZ.Append( aPoint );
659     }
660   }
661   
662   aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() || 
663                    anIsGeoref && !aPointsXYZ.IsEmpty() );
664   if ( aRes )
665   {
666     // Update profile points
667     if ( anIsParametric )
668     {
669       SetParametricPoints( aPointsUZ );
670     }
671     else if ( anIsGeoref )
672     {
673       SetProfilePoints( aPointsXYZ, true );
674     }
675
676     Update();
677   }
678
679   return aRes;
680 }
681
682 void HYDROData_Profile::UpdateLocalCS( double theDx, double theDy )
683 {
684   gp_XY aDelta( theDx, theDy );
685   gp_XY aPnt;
686
687   GetLeftPoint( aPnt, false );
688   aPnt += aDelta;
689   SetLeftPoint( aPnt, false );
690
691   GetRightPoint( aPnt, false );
692   aPnt += aDelta;
693   SetRightPoint( aPnt, false );
694 }
695
696 HYDROData_Profile::ProfilePoint HYDROData_Profile::GetBottomPoint() const
697 {
698   ProfilePoint aBottom;
699
700   // Get parametric points
701   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
702   if ( aParametricPoints.Length() < 1 ) {
703     return aBottom;
704   }
705
706   // Calculate midvalue for U parameter
707   Standard_Real anUMidValue = aParametricPoints.First().X();
708   Standard_Real anUMinValue = anUMidValue;
709   Standard_Real anUMaxValue = anUMidValue;
710
711   for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
712     const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
713     Standard_Real anU = aParPoint.X();
714
715     if ( anU < anUMinValue ) {
716       anUMinValue = anU;
717     } else if ( anU > anUMaxValue ) {
718       anUMaxValue = anU;
719     }
720   }
721
722   anUMidValue = ( anUMinValue + anUMaxValue ) / 2;
723
724   // Find index of the parametric point with minimal Z value
725   int aBottomIndex = 1;
726   HYDROData_IPolyline::Point aParBottom = aParametricPoints.First();
727
728   for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
729     const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
730     if ( aParPoint.Y() < aParBottom.Y() ) {
731       aBottomIndex = i;
732       aParBottom = aParPoint;
733     } else if ( aParPoint.Y() == aParBottom.Y() ) {
734       // Check which point is neares to the U = 0.5
735       if ( fabs( aParPoint.X() - anUMidValue ) < fabs( aParBottom.X() - anUMidValue ) ) {
736         aBottomIndex = i;
737         aParBottom = aParPoint;
738       }
739     }
740   }
741
742   // Find the corresponding profile point
743   ProfilePoints aProfilePoints = GetProfilePoints( false );
744   if ( aBottomIndex >= 1 && aBottomIndex <= aProfilePoints.Length() ) {
745     aBottom = aProfilePoints.Value( aBottomIndex );
746   }
747
748   return aBottom;
749 }
750