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