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