Salome HOME
09ce073a6d648bb49f9bace91d4637ff444f67de
[modules/hydro.git] / src / HYDROData / HYDROData_Image.cxx
1
2 #include "HYDROData_Image.h"
3
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Lambert93.h"
6 #include "HYDROData_OperationsFactory.h"
7
8 #include <TDataStd_RealArray.hxx>
9 #include <TDataStd_ByteArray.hxx>
10 #include <TDataStd_Integer.hxx>
11 #include <TDataStd_IntegerArray.hxx>
12 #include <TDataStd_ReferenceList.hxx>
13 #include <TDataStd_UAttribute.hxx>
14 #include <TDataStd_AsciiString.hxx>
15
16 #include <ImageComposer_Operator.h>
17 #include <ImageComposer_MetaTypes.h>
18
19 #include <QStringList>
20 #include <QFile>
21
22 #include <boost/math/special_functions/fpclassify.hpp>
23
24 static const Standard_GUID GUID_SELF_SPLITTED("997995aa-5c19-40bf-9a60-ab4b70ad04d8");
25 static const Standard_GUID GUID_HAS_LOCAL_POINTS("FD8841AA-FC44-42fa-B6A7-0F682CCC6F27");
26 static const Standard_GUID GUID_HAS_GLOBAL_POINTS("330D0E81-742D-4ea3-92D4-484877CFA7C1");
27
28 IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Entity)
29 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Entity)
30
31 HYDROData_Image::HYDROData_Image()
32 : HYDROData_Entity()
33 {
34 }
35
36 HYDROData_Image::~HYDROData_Image()
37 {
38 }
39
40 QStringList HYDROData_Image::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
41 {
42   QStringList aResList = dumpObjectCreation( theTreatedObjects );
43   QString anImageName = GetObjPyName();
44
45   QString aFilePath = GetFilePath();
46   if ( !aFilePath.isEmpty() )
47   {
48     aResList << QString( "" );
49     aResList << QString( "%1.LoadImage( \"%2\" );" )
50                 .arg( anImageName ).arg( aFilePath );
51     aResList << QString( "" );
52
53     // Dump transformation points for image
54
55     bool anIsByTwoPoints = IsByTwoPoints();
56
57     QPoint aLocalPointA, aLocalPointB, aLocalPointC;
58     if ( GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC ) )
59     {
60       QString aGap = QString().fill( ' ', anImageName.size() + 17 );
61
62       aResList << QString( "%1.SetLocalPoints( QPoint( %2, %3 )," )
63                   .arg( anImageName ).arg( aLocalPointA.x() ).arg( aLocalPointA.y() );
64       aResList << QString( aGap             + "QPoint( %1, %2 )" )
65                   .arg( aLocalPointB.x() ).arg( aLocalPointB.y() );
66       if ( !anIsByTwoPoints )
67       {
68         aResList.last().append( "," );
69         aResList << QString( aGap             +  "QPoint( %1, %2 ) );" )
70                     .arg( aLocalPointC.x() ).arg( aLocalPointC.y() );
71       }
72       else
73       {
74         aResList.last().append( " );" );
75       }
76       aResList << QString( "" );
77     }
78
79     HYDROData_Image::TransformationMode aTransformationMode;
80     QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
81     if ( GetGlobalPoints( aTransformationMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
82     {
83       QString aGap = QString().fill( ' ', anImageName.size() + 18 );
84
85       aResList << QString( "%1.SetGlobalPoints( %2," )
86                   .arg( anImageName ).arg( aTransformationMode );
87       aResList << QString( aGap             +  "QPointF( %1, %2 )," )
88                   .arg( aTrsfPointA.x() ).arg( aTrsfPointA.y() );
89       aResList << QString( aGap             +  "QPointF( %1, %2 )" )
90                   .arg( aTrsfPointB.x() ).arg( aTrsfPointB.y() );
91       if ( !anIsByTwoPoints )
92       {
93         aResList.last().append( "," );
94         aResList << QString( aGap             +  "QPointF( %1, %2 ) );" )
95                     .arg( aTrsfPointC.x() ).arg( aTrsfPointC.y() );
96       }
97       else
98       {
99         aResList.last().append( " );" );
100       }
101
102       if ( aTransformationMode == ReferenceImage )
103       {
104         Handle(HYDROData_Image) aRefImg = GetTrsfReferenceImage();
105         setPythonReferenceObject( theTreatedObjects, aResList, aRefImg, "SetTrsfReferenceImage" );
106       }
107     }
108   }
109   else
110   {
111     // Image is composed from other image(s)
112
113     QString anOperatorName = OperatorName();
114     if ( !anOperatorName.isEmpty() )
115     {
116       aResList << QString( "" );
117
118       aResList << QString( "%1.SetOperatorName( \"%2\" );" )
119                   .arg( anImageName ).arg( anOperatorName );
120
121       ImageComposer_Operator* anImageOp = 
122         HYDROData_OperationsFactory::Factory()->Operator( OperatorName() );
123       if ( anImageOp )
124       {
125         // Dump operation arguments
126         QString anOpArgsArrayName;
127         QStringList anOpArgs = anImageOp->dumpArgsToPython( anOpArgsArrayName );
128         if ( !anOpArgs.isEmpty() )
129         {
130           aResList << QString( "" );
131           aResList << anOpArgs;
132
133           aResList << QString( "" );
134           aResList << QString( "%1.SetArgs( %2 );" )
135                       .arg( anImageName ).arg( anOpArgsArrayName );
136         }
137       }
138     }
139     
140     int aNbReferences = NbReferences();
141     if ( aNbReferences > 0 )
142     {
143       aResList << QString( "" );
144
145       for ( int i = 0; i < aNbReferences; ++i )
146       {
147         Handle(HYDROData_Image) aRefImg = Handle(HYDROData_Image)::DownCast( Reference( i ) );
148         setPythonReferenceObject( theTreatedObjects, aResList, aRefImg, "AppendReference" );
149       }
150     }
151   }
152
153   aResList << QString( "" );
154   aResList << QString( "%1.Update();" ).arg( anImageName );
155
156   return aResList;
157 }
158
159 void HYDROData_Image::Update()
160 {
161   bool anIsToUpdate = IsMustBeUpdated();
162
163   HYDROData_Entity::Update();
164
165   if ( !anIsToUpdate )
166     return;
167
168   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
169
170   ImageComposer_Operator* anOp = aFactory->Operator( OperatorName() );
171   if ( anOp ) // Update image if there is an operation
172   {
173     // Fill by arguments and process the operation
174     anOp->setBinArgs( Args() );
175
176     QVariant anObj1, anObj2;
177     int aNbReferences = NbReferences();
178
179     if ( aNbReferences > 0 )
180     {
181       // First referenced object
182       Handle(HYDROData_Entity) aRefObj = Reference( 0 );
183       if ( !aRefObj.IsNull() )
184       {
185         anObj1 = aRefObj->GetDataVariant();
186         if ( !anObj1.isNull() && anObj1.canConvert<ImageComposer_Image>() )
187         {
188           ImageComposer_Image anImage = anObj1.value<ImageComposer_Image>();
189           QTransform aTransform = anImage.transform();
190           SetTrsf( aTransform );
191         }
192       }
193     }
194
195     if ( aNbReferences > 1 )
196     {
197       // Second referenced object
198       Handle(HYDROData_Entity) aRefObj = Reference( 1 );
199       if ( !aRefObj.IsNull() )
200         anObj2 = aRefObj->GetDataVariant();
201     }
202
203     ImageComposer_Image aResImg = anOp->process( anObj1, anObj2 );
204     SetImage( aResImg );
205     SetTrsf( aResImg.transform() );
206   }
207   else // Update image if it positioned relatively to other image
208   {
209     UpdateTrsf();
210   }
211
212   SetToUpdate( false );
213 }
214
215 QVariant HYDROData_Image::GetDataVariant()
216 {
217   QTransform aTransform = Trsf();
218
219   ImageComposer_Image anImage = Image();
220   anImage.setTransform( aTransform );
221
222   QVariant aVarData;
223   aVarData.setValue<ImageComposer_Image>( anImage );
224   
225   return aVarData;
226 }
227
228 HYDROData_SequenceOfObjects HYDROData_Image::GetAllReferenceObjects() const
229 {
230   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
231
232   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
233   if ( !aRefImage.IsNull() )
234     aResSeq.Append( aRefImage );
235
236   HYDROData_SequenceOfObjects aSeqRefObjects = GetReferenceObjects( 0 );
237   aResSeq.Append( aSeqRefObjects );
238
239   return aResSeq;
240 }
241
242 void HYDROData_Image::SetImage(const QImage& theImage)
243 {
244   if ( theImage.isNull() )
245   {
246     // for empty image remove all previously stored attributes
247     myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
248     myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
249   }
250   else
251   {
252     QImage anImage;
253
254     // convert 8-bits images
255     if ( theImage.format() == QImage::Format_Indexed8 ) {
256       anImage = theImage.convertToFormat( QImage::Format_RGB32 );
257     } else {
258       anImage = theImage;
259     }
260
261     // store width, height, bytes per line and format in integer array 
262     Handle(TDataStd_IntegerArray) aParams;
263     if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
264       aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
265     }
266     aParams->SetValue(1, anImage.width());
267     aParams->SetValue(2, anImage.height());
268     aParams->SetValue(3, anImage.bytesPerLine());
269     aParams->SetValue(4, (int)(anImage.format()));
270     // store data of image in byte array
271     const char* aData = (const char*)(anImage.bits());
272     SaveByteArray(0, aData, anImage.byteCount());
273   }
274
275   SetToUpdate( true );
276 }
277
278 bool HYDROData_Image::LoadImage( const QString& theFilePath )
279 {
280   QImage anImage( theFilePath );
281   SetImage( anImage );
282
283   SetFilePath( theFilePath );
284
285   return !anImage.isNull();
286 }
287
288 QImage HYDROData_Image::Image()
289 {
290   Handle(TDataStd_IntegerArray) aParams;
291   if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
292     return QImage(); // return empty image if there is no array
293   int aLen = 0;
294   uchar* anArray = (uchar*)ByteArray(0, aLen);
295   if (!aLen)
296     return QImage(); // return empty image if there is no array
297   QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
298                  aParams->Value(3), QImage::Format(aParams->Value(4)));
299   return aResult;
300 }
301
302 void HYDROData_Image::SetFilePath( const QString& theFilePath )
303 {
304   TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
305   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
306
307   SetToUpdate( true );
308 }
309
310 QString HYDROData_Image::GetFilePath() const
311 {
312   QString aRes;
313
314   TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
315   if ( !aLabel.IsNull() )
316   {
317     Handle(TDataStd_AsciiString) anAsciiStr;
318     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
319       aRes = QString( anAsciiStr->Get().ToCString() );
320   }
321
322   return aRes;
323 }
324
325 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
326 {
327   // locate 9 coeffs of matrix into the real array
328   Handle(TDataStd_RealArray) anArray;
329   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
330     if (theTrsf.isIdentity()) return; // no need to store identity transformation
331     anArray = TDataStd_RealArray::Set(myLab, 1, 9);
332   }
333   anArray->SetValue(1, theTrsf.m11());
334   anArray->SetValue(2, theTrsf.m12());
335   anArray->SetValue(3, theTrsf.m13());
336   anArray->SetValue(4, theTrsf.m21());
337   anArray->SetValue(5, theTrsf.m22());
338   anArray->SetValue(6, theTrsf.m23());
339   anArray->SetValue(7, theTrsf.m31());
340   anArray->SetValue(8, theTrsf.m32());
341   anArray->SetValue(9, theTrsf.m33());
342
343   SetToUpdate( true );
344 }
345
346 QTransform HYDROData_Image::Trsf() const
347 {
348   // get 9 coeffs of matrix from the real array
349   Handle(TDataStd_RealArray) anArray;
350   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
351     return QTransform(); // return identity if there is no array
352   QTransform aTrsf(
353     anArray->Value(1), anArray->Value(2), anArray->Value(3), 
354     anArray->Value(4), anArray->Value(5), anArray->Value(6), 
355     anArray->Value(7), anArray->Value(8), anArray->Value(9));
356   return aTrsf;
357 }
358
359 void HYDROData_Image::UpdateTrsf()
360 {
361   QPoint aPointA, aPointB, aPointC;
362   if ( !GetLocalPoints( aPointA, aPointB, aPointC ) )
363     return;
364
365   TransformationMode aTrsfMode;
366   QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
367   if ( !GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
368     return;
369
370   QTransform aRefTransform;
371   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
372
373   bool anIsRefImage = aTrsfMode == ReferenceImage;
374   if ( anIsRefImage )
375   {
376     if ( aRefImage.IsNull() )
377       return;
378
379     aRefTransform = aRefImage->Trsf();
380   }
381
382   bool anIsByTwoPoints = IsByTwoPoints();
383
384   // Convert lambert coordinates to cartesian
385   if ( aTrsfMode == ManualGeodesic )
386   {
387     double aXCart = 0, aYCart = 0;
388
389     HYDROData_Lambert93::toXY( aTrsfPointA.y(), aTrsfPointA.x(), aXCart, aYCart );
390     aTrsfPointA = QPointF( aXCart, aYCart );
391
392     HYDROData_Lambert93::toXY( aTrsfPointB.y(), aTrsfPointB.x(), aXCart, aYCart );
393     aTrsfPointB = QPointF( aXCart, aYCart );
394
395     if ( !anIsByTwoPoints )
396     {
397       HYDROData_Lambert93::toXY( aTrsfPointC.y(), aTrsfPointC.x(), aXCart, aYCart );
398       aTrsfPointC = QPointF( aXCart, aYCart );
399     }
400   }
401
402   // generate third points if needed
403   if ( anIsByTwoPoints )
404   {
405     aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
406     aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
407   }
408
409   int xa = aPointA.x();
410   int ya = aPointA.y();
411   int xb = aPointB.x();
412   int yb = aPointB.y();
413   int xc = aPointC.x();
414   int yc = aPointC.y();
415
416   double xta = aTrsfPointA.x();
417   double yta = aTrsfPointA.y();
418   double xtb = aTrsfPointB.x();
419   double ytb = aTrsfPointB.y();
420   double xtc = aTrsfPointC.x();
421   double ytc = aTrsfPointC.y();
422
423   // first, check that three input points don't belong to a single line
424   if( ( yb - ya ) * ( xc - xa ) == ( yc - ya ) * ( xb - xa ) )
425     return;
426
427   // the same check for the reference points
428   if( anIsRefImage && ( ( ytb - yta ) * ( xtc - xta ) == ( ytc - yta ) * ( xtb - xta ) ) )
429     return;
430
431   QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
432   QTransform aTransform2( xta, yta, 1, xtb, ytb, 1, xtc, ytc, 1 );
433
434   bool anIsInvertible = false;
435   QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
436   if( !anIsInvertible )
437     return;
438
439   QTransform aResTransform = aTransform1Inverted * aTransform2;
440   if( anIsRefImage )
441     aResTransform *= aRefTransform;
442
443   SetTrsf( aResTransform );
444 }
445
446 bool HYDROData_Image::IsByTwoPoints() const
447 {
448   if ( !HasLocalPoints() || !HasGlobalPoints() )
449     return false;
450
451   QPoint aPointA, aPointB, aPointC;
452   GetLocalPoints( aPointA, aPointB, aPointC );
453
454   return aPointC.x() < 0 && aPointC.y() < 0; 
455 }
456
457 bool HYDROData_Image::HasReferences() const
458 {
459   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
460   int aNbReferences = NbReferences();
461
462   return !aRefImage.IsNull() || aNbReferences > 0;
463 }
464
465 void HYDROData_Image::RemoveAllReferences()
466 {
467   if ( !HasReferences() )
468     return;
469
470   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
471   if ( !aRefImage.IsNull() )
472   {
473     RemoveTrsfReferenceImage();
474   }
475   else
476   {
477     ClearReferences();
478     SetOperatorName( "" );
479     SetArgs( "" );
480     SetIsSelfSplitted( false );
481   }
482
483   bool anIsByTwoPoints = IsByTwoPoints();
484
485   QImage anImage = Image();
486   if ( anImage.isNull() )
487   {
488     SetToUpdate( false );
489     return;
490   }
491
492   // Set local points to default position
493   QPoint aLocalPointA = QPoint( 0, 0 );
494   QPoint aLocalPointB = QPoint( anImage.width(), 0 );
495   QPoint aLocalPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) : QPoint( 0, anImage.height() );
496
497   SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
498
499   // Calculate global points
500   QTransform aTransform = Trsf();
501
502   QPointF aTrsfPointA = QPointF( aTransform.map( aLocalPointA ) );
503   QPointF aTrsfPointB = QPointF( aTransform.map( aLocalPointB ) );
504   QPointF aTrsfPointC = anIsByTwoPoints ? QPointF( INT_MIN, INT_MIN ) : 
505                                           QPointF( aTransform.map( aLocalPointC ) );
506
507   SetGlobalPoints( ManualCartesian, aTrsfPointA, aTrsfPointB, aTrsfPointC );
508
509   SetToUpdate( false );
510 }
511
512 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
513                                       const QPoint& thePointB,
514                                       const QPoint& thePointC,
515                                       const bool    theIsUpdate )
516 {
517   Handle(TDataStd_RealArray) anArray;
518   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
519     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
520
521   anArray->SetValue( 1, thePointA.x() );
522   anArray->SetValue( 2, thePointA.y() );
523   anArray->SetValue( 3, thePointB.x() );
524   anArray->SetValue( 4, thePointB.y() );
525   anArray->SetValue( 5, thePointC.x() );
526   anArray->SetValue( 6, thePointC.y() );
527
528   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
529
530   if ( theIsUpdate )
531     UpdateTrsf();
532
533   SetToUpdate( true );
534 }
535
536 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
537                                       QPoint& thePointB,
538                                       QPoint& thePointC ) const
539 {
540   if ( !HasLocalPoints() )
541     return false;
542
543   Handle(TDataStd_RealArray) anArray;
544   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
545
546   thePointA = QPointF( anArray->Value( 1 ), anArray->Value( 2 ) ).toPoint();
547   thePointB = QPointF( anArray->Value( 3 ), anArray->Value( 4 ) ).toPoint();
548   thePointC = QPointF( anArray->Value( 5 ), anArray->Value( 6 ) ).toPoint();
549
550   return true;
551 }
552
553 bool HYDROData_Image::HasLocalPoints() const
554 {
555   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
556   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_LOCAL_POINTS ) )
557     return false;
558
559   Handle(TDataStd_RealArray) anArray;
560   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
561 }
562
563
564 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
565                                        const QPointF&            thePointA,
566                                        const QPointF&            thePointB,
567                                        const QPointF&            thePointC,
568                                        const bool                theIsUpdate )
569 {
570   Handle(TDataStd_RealArray) anArray;
571   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
572     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
573
574   anArray->SetValue( 7,  thePointA.x() );
575   anArray->SetValue( 8,  thePointA.y() );
576   anArray->SetValue( 9,  thePointB.x() );
577   anArray->SetValue( 10, thePointB.y() );
578   anArray->SetValue( 11, thePointC.x() );
579   anArray->SetValue( 12, thePointC.y() );
580   
581   SetTrsfMode( theMode );
582
583   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
584
585   if ( theIsUpdate )
586     UpdateTrsf();
587
588   SetToUpdate( true );
589 }
590
591 bool HYDROData_Image::GetGlobalPoints( TransformationMode& theMode,
592                                        QPointF&            thePointA,
593                                        QPointF&            thePointB,
594                                        QPointF&            thePointC ) const
595 {
596   if ( !HasGlobalPoints() )
597     return false;
598
599   theMode = GetTrsfMode();
600
601   Handle(TDataStd_RealArray) anArray;
602   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
603
604   thePointA = QPointF( anArray->Value( 7  ), anArray->Value( 8  ) );
605   thePointB = QPointF( anArray->Value( 9  ), anArray->Value( 10 ) );
606   thePointC = QPointF( anArray->Value( 11 ), anArray->Value( 12 ) );
607
608   return true;
609 }
610
611 bool HYDROData_Image::SetGlobalPointsFromFile( const QString& theFileName )
612 {
613   bool aRes = false;
614
615   // Try to open the file
616   QFile aFile( theFileName );
617   if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) ) {
618     return aRes;
619   }
620
621   QPointF aPointA, aPointB;
622   double aXmin, anYmin, aXmax, anYmax;
623   aXmin = anYmin = aXmax = anYmax = -1;
624
625   while ( !aFile.atEnd() && 
626           ( aXmin < 0 || anYmin < 0 || aXmax < 0 || anYmax < 0 ) ) {
627     // Read line
628     QString aLine = aFile.readLine().simplified();
629     aLine.replace( " ", "" );
630     if ( aLine.isEmpty() ) {
631       continue;
632     }
633
634     // Try to read double value after ":"
635     bool isDoubleOk = false;
636     double aDoubleValue = -1;
637     QStringList aValues = aLine.split( ":", QString::SkipEmptyParts );
638     if ( aValues.count() == 2 ) {
639       aDoubleValue = aValues.last().toDouble( &isDoubleOk );
640     }
641
642     // Check the result
643     if ( !isDoubleOk ||
644          boost::math::isnan( aDoubleValue ) ||
645          boost::math::isinf( aDoubleValue ) ) {
646       continue;
647     }
648
649     // Set the value
650     if ( aLine.startsWith( "Xminimum" ) ) {
651       aXmin = aDoubleValue;    
652     } 
653     else if ( aLine.startsWith( "Yminimum" ) ) {
654       anYmin = aDoubleValue; 
655     }
656     else if ( aLine.startsWith( "Xmaximum" ) ) {
657       aXmax = aDoubleValue;
658     }
659     else if ( aLine.startsWith( "Ymaximum" ) ) {
660       anYmax = aDoubleValue;  
661     }
662   }
663
664   // Close the file
665   aFile.close();
666
667   if ( aXmin >= 0 && anYmin >= 0 ) {
668     aPointA.setX( aXmin );
669     aPointA.setY( anYmin );
670   }
671     
672   if ( aXmax >= 0 && anYmax >= 0 ) {
673     aPointB.setX( aXmax );
674     aPointB.setY( anYmax );
675   }
676
677   if ( !aPointA.isNull() && !aPointB.isNull() ) {
678     SetGlobalPoints( ManualCartesian, aPointA, aPointB );
679     aRes = true;
680   }
681
682   return aRes;
683 }
684
685 bool HYDROData_Image::HasGlobalPoints() const
686 {
687   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
688   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_GLOBAL_POINTS ) )
689     return false;
690
691   Handle(TDataStd_RealArray) anArray;
692   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
693 }
694
695 void HYDROData_Image::SetReferencePoints( const Handle(HYDROData_Image)& theRefImage,
696                                           const QPointF&                 thePointA,
697                                           const QPointF&                 thePointB,
698                                           const QPointF&                 thePointC,
699                                           const bool                     theIsUpdate )
700 {
701   SetTrsfReferenceImage( theRefImage );
702   SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC, theIsUpdate );
703 }
704
705 bool HYDROData_Image::GetReferencePoints( Handle(HYDROData_Image)& theRefImage,
706                                           QPointF&                 thePointA,
707                                           QPointF&                 thePointB,
708                                           QPointF&                 thePointC ) const
709 {
710   if ( !HasReferencePoints() )
711     return false;
712
713   theRefImage = GetTrsfReferenceImage();
714
715   TransformationMode aMode;
716   GetGlobalPoints( aMode, thePointA, thePointB, thePointC );
717
718   return true;
719 }
720
721 bool HYDROData_Image::HasReferencePoints() const
722 {
723   if ( !HasGlobalPoints() )
724     return false;
725
726   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
727   if ( aRefImage.IsNull() )
728     return false;
729
730   TransformationMode aTrsfMode = GetTrsfMode();
731   if ( aTrsfMode != ReferenceImage )
732     return false;
733
734   return true;
735 }
736
737 void HYDROData_Image::SetTrsfMode( const TransformationMode& theMode )
738 {
739   TDataStd_Integer::Set( myLab.FindChild( DataTag_TrsfMode ), (int)theMode );
740   SetToUpdate( true );
741 }
742
743 HYDROData_Image::TransformationMode HYDROData_Image::GetTrsfMode() const
744 {
745   TransformationMode aResMode = ManualGeodesic;
746
747   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
748   if ( !aLabel.IsNull() )
749   {
750     Handle(TDataStd_Integer) aMode;
751     if ( myLab.FindChild( DataTag_TrsfMode ).FindAttribute( TDataStd_Integer::GetID(), aMode ) )
752       aResMode = (TransformationMode)aMode->Get();
753   }
754
755   return aResMode;
756 }
757
758 void HYDROData_Image::SetTrsfReferenceImage( const Handle(HYDROData_Image)& theRefImage )
759 {
760   SetReferenceObject( theRefImage, DataTag_TrsfImage );
761   SetToUpdate( true );
762 }
763
764 Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
765 {
766   return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
767 }
768
769 void HYDROData_Image::RemoveTrsfReferenceImage()
770 {
771   RemoveReferenceObject( DataTag_TrsfImage );
772   SetToUpdate( true );
773 }
774
775 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
776 {
777   AddReferenceObject( theReferenced, 0 );
778   SetToUpdate( true );
779 }
780
781 int HYDROData_Image::NbReferences() const
782 {
783   return NbReferenceObjects( 0 );
784 }
785
786 Handle(HYDROData_Entity) HYDROData_Image::Reference( const int theIndex ) const
787 {
788   return GetReferenceObject( 0, theIndex );
789 }
790
791 void HYDROData_Image::ChangeReference(
792     const int theIndex, Handle(HYDROData_Entity) theReferenced)
793 {
794   SetReferenceObject( theReferenced, 0, theIndex );
795   SetToUpdate( true );
796 }
797
798 void HYDROData_Image::RemoveReference(const int theIndex)
799 {
800   RemoveReferenceObject( 0, theIndex );
801   SetToUpdate( true );
802 }
803
804 void HYDROData_Image::ClearReferences()
805 {
806   ClearReferenceObjects( 0 );
807   SetToUpdate( true );
808 }
809
810 void HYDROData_Image::SetOperatorName( const QString theOpName )
811 {
812   TCollection_AsciiString anAsciiStr( theOpName.toStdString().c_str() );
813   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_Operator ), anAsciiStr );
814   SetToUpdate( true );
815 }
816
817 QString HYDROData_Image::OperatorName() const
818 {
819   QString aRes;
820
821   TDF_Label aLabel = myLab.FindChild( DataTag_Operator, false );
822   if ( !aLabel.IsNull() )
823   {
824     Handle(TDataStd_AsciiString) anAsciiStr;
825     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
826       aRes = QString( anAsciiStr->Get().ToCString() );
827   }
828
829   return aRes;
830 }
831
832 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
833 {
834   SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
835   SetToUpdate( true );
836 }
837
838 QByteArray HYDROData_Image::Args() const
839 {
840   int aLen = 0;
841   const char* aData = ByteArray(DataTag_Operator, aLen);
842   if (!aLen)
843     return QByteArray();
844   return QByteArray(aData, aLen);
845 }
846
847 void HYDROData_Image::SetIsSelfSplitted(bool theFlag)
848 {
849   if (theFlag) {
850     TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLITTED);
851   } else {
852     myLab.ForgetAttribute(GUID_SELF_SPLITTED);
853   }
854   SetToUpdate( true );
855 }
856
857 bool HYDROData_Image::IsSelfSplitted() const
858 {
859   return myLab.IsAttribute(GUID_SELF_SPLITTED);
860 }
861
862 QPointF HYDROData_Image::generateThirdPoint( const QPointF& thePointA,
863                                              const QPointF& thePointB,
864                                              const bool&    theIsLocal ) const
865 {
866   // Rotate vector to 90 degrees : clockwise - for local
867   //                               counterclockwise - for global
868   const double aTheta = theIsLocal ? -M_PI_2 : M_PI_2;
869
870   QPointF aResPoint;
871
872   // Move to (0,0) for correct rotation
873   double x = thePointB.x() - thePointA.x();
874   double y = thePointB.y() - thePointA.y();
875
876   aResPoint.setX( x * cos( aTheta ) - y * sin( aTheta ) );
877   aResPoint.setY( x * sin( aTheta ) + y * cos( aTheta ) );
878
879   // Move back to origin position
880   aResPoint.setX( aResPoint.x() + thePointA.x() );
881   aResPoint.setY( aResPoint.y() + thePointA.y() );
882
883   return aResPoint;
884 }
885