Salome HOME
9cc693cc8b1a2ea0e88970444f5303a1e62d2baf
[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   return getShape3D();
160 }
161
162 void HYDROData_Profile::Update()
163 {
164   HYDROData_Object::Update();
165
166   TopoDS_Wire aWire;
167   Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
168   if ( !aProfile.IsNull() )
169   {
170     ProfilePoints aProfilePoints = GetProfilePoints( false );
171     HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
172
173     aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
174   }
175   SetShape3D( aWire );
176 }
177
178 QColor HYDROData_Profile::DefaultFillingColor()
179 {
180   return QColor( Qt::transparent );
181 }
182
183 QColor HYDROData_Profile::DefaultBorderColor()
184 {
185   return QColor( Qt::black );
186 }
187
188 QColor HYDROData_Profile::getDefaultFillingColor() const
189 {
190   return DefaultFillingColor();
191 }
192
193 QColor HYDROData_Profile::getDefaultBorderColor() const
194 {
195   return DefaultBorderColor();
196 }
197
198 bool HYDROData_Profile::IsValid() const
199 {
200   gp_XY aFirstPoint, aLastPoint;
201   if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
202     return false;
203
204   int aNbPoints = NbPoints();
205   return aNbPoints > 1;
206 }
207
208 void HYDROData_Profile::SetLeftPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
209 {
210   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
211   if ( aLabel.IsNull() )
212     return;
213
214   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
215   gp_XY aLPoint = theGPoint;
216   if( IsConvertFromGlobal )
217     aDoc->Transform( aLPoint, true );
218
219   Handle(TDataStd_RealArray) anArray;
220   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
221     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
222
223   anArray->SetValue( 0, aLPoint.X() );
224   anArray->SetValue( 1, aLPoint.Y() );
225
226   SetToUpdate( true );
227 }
228
229 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint, bool IsConvertToGlobal ) const
230 {
231   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
232   if ( aLabel.IsNull() )
233     return false;
234
235   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
236   Handle(TDataStd_RealArray) anArray;
237   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
238     return false;
239
240   thePoint.SetX( anArray->Value( 0 ) );
241   thePoint.SetY( anArray->Value( 1 ) );
242
243   if( IsConvertToGlobal )
244     aDoc->Transform( thePoint, false );
245
246   return true;
247 }
248
249 void HYDROData_Profile::SetRightPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
250 {
251   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
252
253   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
254   gp_XY aLPoint = theGPoint;
255   if( IsConvertFromGlobal )
256     aDoc->Transform( aLPoint, true );
257
258   Handle(TDataStd_RealArray) anArray;
259   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
260     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
261
262   anArray->SetValue( 0, aLPoint.X() );
263   anArray->SetValue( 1, aLPoint.Y() );
264
265   SetToUpdate( true );
266 }
267
268 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint, bool IsConvertToGlobal ) const
269 {
270   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
271   if ( aLabel.IsNull() )
272     return false;
273
274   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
275   Handle(TDataStd_RealArray) anArray;
276   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
277     return false;
278
279   thePoint.SetX( anArray->Value( 0 ) );
280   thePoint.SetY( anArray->Value( 1 ) );
281
282   if( IsConvertToGlobal )
283     aDoc->Transform( thePoint, false );
284
285   return true;
286 }
287
288 void HYDROData_Profile::Invalidate()
289 {
290   TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
291   if ( !aFirstLabel.IsNull() )
292     aFirstLabel.ForgetAllAttributes();
293
294   TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
295   if ( !aLastLabel.IsNull() )
296     aLastLabel.ForgetAllAttributes();
297
298   SetToUpdate( true );
299 }
300
301 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
302 {
303   Handle(HYDROData_ProfileUZ) aProfileUZ;
304
305   TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
306   if ( aLabel.IsNull() )
307     return aProfileUZ;
308
309   aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
310   if ( aProfileUZ.IsNull() && theIsCreate )
311   {
312     aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
313       HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
314   }
315
316   return aProfileUZ;
317 }
318
319 int HYDROData_Profile::NbPoints() const
320 {
321   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
322   return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
323 }
324
325 void HYDROData_Profile::RemovePoints()
326 {
327   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
328   if ( !aProfileUZ.IsNull() )
329   {
330     aProfileUZ->RemoveSections();
331     SetToUpdate( true );
332   }
333 }
334
335 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
336 {
337   RemovePoints();
338
339   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
340   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
341   {
342     const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
343     aProfileUZ->AddPoint( 0, aPoint );
344   }
345
346   SetToUpdate( true );
347 }
348
349 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
350 {
351   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
352   return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
353 }
354
355 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints, bool IsConvertFromGlobal )
356 {
357   RemovePoints();
358   if ( thePoints.Length() < 2 )
359     return;
360
361   gp_XY aFirstPoint, aLastPoint;
362
363   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
364   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
365   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
366   {
367     ProfilePoint aPoint = thePoints.Value( i );
368     if( IsConvertFromGlobal )
369       aDoc->Transform( aPoint, true );
370
371     gp_XY aPointXY( aPoint.X(), aPoint.Y() );
372
373     if ( i == 1 )
374       aFirstPoint = aPointXY;
375     else if ( i == n )
376       aLastPoint = aPointXY;
377
378     double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
379     
380     HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
381     aProfileUZ->AddPoint( 0, aParPoint );
382   }
383
384   SetLeftPoint( aFirstPoint, false );//already converted to local CS
385   SetRightPoint( aLastPoint, false );
386 }
387
388 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints( bool IsConvertToGlobal ) const
389 {
390   ProfilePoints aResPoints;
391
392   gp_XY aFirstPoint, aLastPoint;
393   if ( !GetLeftPoint( aFirstPoint, IsConvertToGlobal ) ||
394        !GetRightPoint( aLastPoint, IsConvertToGlobal ) )
395     return aResPoints;
396
397   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
398   if ( aParametricPoints.Length() < 2 )
399     return aResPoints;
400
401   const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
402   const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
403
404   double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
405   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
406
407   // Add first point as is
408   aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
409
410   // Compute all other points
411   for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
412   {
413     const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
414
415     double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
416     
417     double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
418
419     double aRatio = aParLen / ( aGeoDistance - aParLen );
420
421     double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
422     double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
423
424     ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
425     aResPoints.Append( aCompPoint );
426   }
427
428   // Add last point as is
429   aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
430
431   return aResPoints;
432 }
433
434 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
435 {
436   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
437 }
438
439 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
440 {
441   TCollection_AsciiString aRes;
442
443   Handle(TDataStd_AsciiString) anAsciiStr;
444   if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
445     aRes = anAsciiStr->Get();
446
447   return aRes;
448 }
449
450 int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
451                                        const TCollection_AsciiString&    theFileName,
452                                        NCollection_Sequence<int>&        theBadProfilesIds )
453 {
454   if ( theDoc.IsNull() || theFileName.IsEmpty() )
455     return 0;
456
457   OSD_File aFile( theFileName );
458   if ( !aFile.IsReadable() )
459     return 0;
460
461   aFile.Open( OSD_ReadOnly, OSD_Protection() );
462   if ( !aFile.IsOpen() )
463     return 0;
464
465   NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
466
467   int aProfileId = 1;
468   Handle(HYDROData_Profile) aNewProfile;
469   for ( ; !aFile.IsAtEnd(); ++aProfileId )
470   {
471     if ( aNewProfile.IsNull() )
472       aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
473     
474     bool anIsRead = false;
475     if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) )
476     {
477       aCreatedProfiles.Append( aNewProfile );
478       aNewProfile.Nullify();
479     }
480     else if ( anIsRead )
481     {
482       theBadProfilesIds.Append( aProfileId );
483     }
484   }
485
486   if ( !aNewProfile.IsNull() )
487     aNewProfile->Remove();
488
489   // Close the file
490   aFile.Close();
491
492   for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
493   {
494     Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
495
496     QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
497     aProfile->SetName( aProfileName );
498
499     aProfile->SetFilePath( theFileName );
500
501     aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
502   }
503
504   return aCreatedProfiles.Length();
505 }
506
507 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
508                                         bool*                          theIsRead )
509 {
510   if ( theIsRead )
511     *theIsRead = false;
512
513   // Try to open the file
514   OSD_File aFile( theFileName );
515   if ( !aFile.IsReadable() )
516     return false;
517
518   aFile.Open( OSD_ReadOnly, OSD_Protection() );
519   if ( !aFile.IsOpen() )
520     return false;
521
522   bool aRes = ImportFromFile( aFile, theIsRead );
523
524   // Close the file
525   aFile.Close();
526
527   if ( aRes )
528   {
529     // Update file path
530     SetFilePath( theFileName );
531   }
532
533   return aRes;
534 }
535
536 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
537                                         bool*     theIsRead )
538 {
539   if ( theIsRead )
540     *theIsRead = false;
541
542   if ( !theFile.IsOpen() )
543     return false;
544
545   bool aRes = true;
546
547   bool anIsParametric = false;
548   bool anIsGeoref     = false;
549
550   HYDROData_ProfileUZ::PointsList aPointsUZ;
551   ProfilePoints                   aPointsXYZ;
552
553   double aPrevVal = -DBL_MAX;
554   while ( !theFile.IsAtEnd() )
555   {
556     Standard_Integer aNbRead = 0;
557     TCollection_AsciiString aLine;
558     theFile.ReadLine( aLine, 1024, aNbRead );
559
560     aLine.LeftAdjust(); aLine.RightAdjust();
561     if ( aLine.IsEmpty() )
562     {
563       if ( !anIsParametric && !anIsGeoref )
564         continue; // Definition is not started yet
565
566       break; // Next profile started
567     }
568
569     // Set flag of read status to true
570     if ( theIsRead )
571       *theIsRead = true;
572
573     TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
574     TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
575     TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
576
577     if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
578          aValY.IsEmpty() || !aValY.IsRealValue() )
579     {
580       aRes = false;
581       break;
582     }
583
584     if ( !anIsParametric && !anIsGeoref )
585     {
586       anIsParametric = aValZ.IsEmpty();
587       anIsGeoref = !aValZ.IsEmpty();
588     }
589
590     double aCoordX = aValX.RealValue();
591     double aCoordY = aValY.RealValue();
592
593     if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) ||
594          boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) )
595       aRes = false;
596
597     if ( anIsParametric )
598     {
599       if ( aCoordX < aPrevVal )
600       {
601         // Move back readed line
602         theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
603         break;
604       }
605
606       HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
607       aPointsUZ.Append( aPoint );
608
609       aPrevVal = aCoordX;
610     }
611     else
612     {
613       if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
614       {
615         aRes = false;
616         break;
617       }
618
619       double aCoordZ = aValZ.RealValue();
620       if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) )
621         aRes = false;
622
623       ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
624       aPointsXYZ.Append( aPoint );
625     }
626   }
627   
628   aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() || 
629                    anIsGeoref && !aPointsXYZ.IsEmpty() );
630   if ( aRes )
631   {
632     // Update profile points
633     if ( anIsParametric )
634     {
635       SetParametricPoints( aPointsUZ );
636     }
637     else if ( anIsGeoref )
638     {
639       SetProfilePoints( aPointsXYZ, true );
640     }
641
642     Update();
643   }
644
645   return aRes;
646 }
647
648 void HYDROData_Profile::UpdateLocalCS( double theDx, double theDy )
649 {
650   gp_XY aDelta( theDx, theDy );
651   gp_XY aPnt;
652
653   GetLeftPoint( aPnt, false );
654   aPnt += aDelta;
655   SetLeftPoint( aPnt, false );
656
657   GetRightPoint( aPnt, false );
658   aPnt += aDelta;
659   SetRightPoint( aPnt, false );
660 }
661
662 HYDROData_Profile::ProfilePoint HYDROData_Profile::GetBottomPoint() const
663 {
664   ProfilePoint aBottom;
665
666   // Get parametric points
667   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
668   if ( aParametricPoints.Length() < 1 ) {
669     return aBottom;
670   }
671
672   // Calculate midvalue for U parameter
673   Standard_Real anUMidValue = aParametricPoints.First().X();
674   Standard_Real anUMinValue = anUMidValue;
675   Standard_Real anUMaxValue = anUMidValue;
676
677   for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
678     const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
679     Standard_Real anU = aParPoint.X();
680
681     if ( anU < anUMinValue ) {
682       anUMinValue = anU;
683     } else if ( anU > anUMaxValue ) {
684       anUMaxValue = anU;
685     }
686   }
687
688   anUMidValue = ( anUMinValue + anUMaxValue ) / 2;
689
690   // Find index of the parametric point with minimal Z value
691   int aBottomIndex = 1;
692   HYDROData_IPolyline::Point aParBottom = aParametricPoints.First();
693
694   for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
695     const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
696     if ( aParPoint.Y() < aParBottom.Y() ) {
697       aBottomIndex = i;
698       aParBottom = aParPoint;
699     } else if ( aParPoint.Y() == aParBottom.Y() ) {
700       // Check which point is neares to the U = 0.5
701       if ( fabs( aParPoint.X() - anUMidValue ) < fabs( aParBottom.X() - anUMidValue ) ) {
702         aBottomIndex = i;
703         aParBottom = aParPoint;
704       }
705     }
706   }
707
708   // Find the corresponding profile point
709   ProfilePoints aProfilePoints = GetProfilePoints( false );
710   if ( aBottomIndex >= 1 && aBottomIndex <= aProfilePoints.Length() ) {
711     aBottom = aProfilePoints.Value( aBottomIndex );
712   }
713
714   return aBottom;
715 }
716