Salome HOME
refs #482: new function for export calculation case
[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 bool HYDROData_Image::IsHas2dPrs() const
216 {
217   return true;
218 }
219
220 QVariant HYDROData_Image::GetDataVariant()
221 {
222   QTransform aTransform = Trsf();
223
224   ImageComposer_Image anImage = Image();
225   anImage.setTransform( aTransform );
226
227   QVariant aVarData;
228   aVarData.setValue<ImageComposer_Image>( anImage );
229   
230   return aVarData;
231 }
232
233 HYDROData_SequenceOfObjects HYDROData_Image::GetAllReferenceObjects() const
234 {
235   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
236
237   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
238   if ( !aRefImage.IsNull() )
239     aResSeq.Append( aRefImage );
240
241   HYDROData_SequenceOfObjects aSeqRefObjects = GetReferenceObjects( 0 );
242   aResSeq.Append( aSeqRefObjects );
243
244   return aResSeq;
245 }
246
247 void HYDROData_Image::SetImage(const QImage& theImage)
248 {
249   if ( theImage.isNull() )
250   {
251     // for empty image remove all previously stored attributes
252     myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
253     myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
254   }
255   else
256   {
257     QImage anImage;
258
259     // convert 8-bits images
260     if ( theImage.format() == QImage::Format_Indexed8 ) {
261       anImage = theImage.convertToFormat( QImage::Format_RGB32 );
262     } else {
263       anImage = theImage;
264     }
265
266     // store width, height, bytes per line and format in integer array 
267     Handle(TDataStd_IntegerArray) aParams;
268     if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
269       aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
270     }
271     aParams->SetValue(1, anImage.width());
272     aParams->SetValue(2, anImage.height());
273     aParams->SetValue(3, anImage.bytesPerLine());
274     aParams->SetValue(4, (int)(anImage.format()));
275     // store data of image in byte array
276     const char* aData = (const char*)(anImage.bits());
277     SaveByteArray(0, aData, anImage.byteCount());
278   }
279
280   SetToUpdate( true );
281 }
282
283 bool HYDROData_Image::LoadImage( const QString& theFilePath )
284 {
285   QImage anImage( theFilePath );
286   SetImage( anImage );
287
288   SetFilePath( theFilePath );
289
290   return !anImage.isNull();
291 }
292
293 QImage HYDROData_Image::Image()
294 {
295   Handle(TDataStd_IntegerArray) aParams;
296   if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
297     return QImage(); // return empty image if there is no array
298   int aLen = 0;
299   uchar* anArray = (uchar*)ByteArray(0, aLen);
300   if (!aLen)
301     return QImage(); // return empty image if there is no array
302   QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
303                  aParams->Value(3), QImage::Format(aParams->Value(4)));
304   return aResult;
305 }
306
307 void HYDROData_Image::SetFilePath( const QString& theFilePath )
308 {
309   TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
310   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
311
312   SetToUpdate( true );
313 }
314
315 QString HYDROData_Image::GetFilePath() const
316 {
317   QString aRes;
318
319   TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
320   if ( !aLabel.IsNull() )
321   {
322     Handle(TDataStd_AsciiString) anAsciiStr;
323     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
324       aRes = QString( anAsciiStr->Get().ToCString() );
325   }
326
327   return aRes;
328 }
329
330 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
331 {
332   // locate 9 coeffs of matrix into the real array
333   Handle(TDataStd_RealArray) anArray;
334   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
335     if (theTrsf.isIdentity()) return; // no need to store identity transformation
336     anArray = TDataStd_RealArray::Set(myLab, 1, 9);
337   }
338   anArray->SetValue(1, theTrsf.m11());
339   anArray->SetValue(2, theTrsf.m12());
340   anArray->SetValue(3, theTrsf.m13());
341   anArray->SetValue(4, theTrsf.m21());
342   anArray->SetValue(5, theTrsf.m22());
343   anArray->SetValue(6, theTrsf.m23());
344   anArray->SetValue(7, theTrsf.m31());
345   anArray->SetValue(8, theTrsf.m32());
346   anArray->SetValue(9, theTrsf.m33());
347
348   SetToUpdate( true );
349 }
350
351 QTransform HYDROData_Image::Trsf() const
352 {
353   // get 9 coeffs of matrix from the real array
354   Handle(TDataStd_RealArray) anArray;
355   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
356     return QTransform(); // return identity if there is no array
357   QTransform aTrsf(
358     anArray->Value(1), anArray->Value(2), anArray->Value(3), 
359     anArray->Value(4), anArray->Value(5), anArray->Value(6), 
360     anArray->Value(7), anArray->Value(8), anArray->Value(9));
361   return aTrsf;
362 }
363
364 void HYDROData_Image::UpdateTrsf()
365 {
366   QPoint aPointA, aPointB, aPointC;
367   if ( !GetLocalPoints( aPointA, aPointB, aPointC ) )
368     return;
369
370   TransformationMode aTrsfMode;
371   QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
372   if ( !GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
373     return;
374
375   QTransform aRefTransform;
376   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
377
378   bool anIsRefImage = aTrsfMode == ReferenceImage;
379   if ( anIsRefImage )
380   {
381     if ( aRefImage.IsNull() )
382       return;
383
384     aRefTransform = aRefImage->Trsf();
385   }
386
387   bool anIsByTwoPoints = IsByTwoPoints();
388
389   // Convert lambert coordinates to cartesian
390   if ( aTrsfMode == ManualGeodesic )
391   {
392     double aXCart = 0, aYCart = 0;
393
394     HYDROData_Lambert93::toXY( aTrsfPointA.y(), aTrsfPointA.x(), aXCart, aYCart );
395     aTrsfPointA = QPointF( aXCart, aYCart );
396
397     HYDROData_Lambert93::toXY( aTrsfPointB.y(), aTrsfPointB.x(), aXCart, aYCart );
398     aTrsfPointB = QPointF( aXCart, aYCart );
399
400     if ( !anIsByTwoPoints )
401     {
402       HYDROData_Lambert93::toXY( aTrsfPointC.y(), aTrsfPointC.x(), aXCart, aYCart );
403       aTrsfPointC = QPointF( aXCart, aYCart );
404     }
405   }
406
407   // generate third points if needed
408   if ( anIsByTwoPoints )
409   {
410     aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
411     aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
412   }
413
414   int xa = aPointA.x();
415   int ya = aPointA.y();
416   int xb = aPointB.x();
417   int yb = aPointB.y();
418   int xc = aPointC.x();
419   int yc = aPointC.y();
420
421   double xta = aTrsfPointA.x();
422   double yta = aTrsfPointA.y();
423   double xtb = aTrsfPointB.x();
424   double ytb = aTrsfPointB.y();
425   double xtc = aTrsfPointC.x();
426   double ytc = aTrsfPointC.y();
427
428   // first, check that three input points don't belong to a single line
429   if( ( yb - ya ) * ( xc - xa ) == ( yc - ya ) * ( xb - xa ) )
430     return;
431
432   // the same check for the reference points
433   if( anIsRefImage && ( ( ytb - yta ) * ( xtc - xta ) == ( ytc - yta ) * ( xtb - xta ) ) )
434     return;
435
436   QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
437   QTransform aTransform2( xta, yta, 1, xtb, ytb, 1, xtc, ytc, 1 );
438
439   bool anIsInvertible = false;
440   QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
441   if( !anIsInvertible )
442     return;
443
444   QTransform aResTransform = aTransform1Inverted * aTransform2;
445   if( anIsRefImage )
446     aResTransform *= aRefTransform;
447
448   SetTrsf( aResTransform );
449 }
450
451 bool HYDROData_Image::IsByTwoPoints() const
452 {
453   if ( !HasLocalPoints() || !HasGlobalPoints() )
454     return false;
455
456   QPoint aPointA, aPointB, aPointC;
457   GetLocalPoints( aPointA, aPointB, aPointC );
458
459   return aPointC.x() < 0 && aPointC.y() < 0; 
460 }
461
462 bool HYDROData_Image::HasReferences() const
463 {
464   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
465   int aNbReferences = NbReferences();
466
467   return !aRefImage.IsNull() || aNbReferences > 0;
468 }
469
470 void HYDROData_Image::RemoveAllReferences()
471 {
472   if ( !HasReferences() )
473     return;
474
475   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
476   if ( !aRefImage.IsNull() )
477   {
478     RemoveTrsfReferenceImage();
479   }
480   else
481   {
482     ClearReferences();
483     SetOperatorName( "" );
484     SetArgs( "" );
485     SetIsSelfSplitted( false );
486   }
487
488   bool anIsByTwoPoints = IsByTwoPoints();
489
490   QImage anImage = Image();
491   if ( anImage.isNull() )
492   {
493     SetToUpdate( false );
494     return;
495   }
496
497   // Set local points to default position
498   QPoint aLocalPointA = QPoint( 0, 0 );
499   QPoint aLocalPointB = QPoint( anImage.width(), 0 );
500   QPoint aLocalPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) : QPoint( 0, anImage.height() );
501
502   SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
503
504   // Calculate global points
505   QTransform aTransform = Trsf();
506
507   QPointF aTrsfPointA = QPointF( aTransform.map( aLocalPointA ) );
508   QPointF aTrsfPointB = QPointF( aTransform.map( aLocalPointB ) );
509   QPointF aTrsfPointC = anIsByTwoPoints ? QPointF( INT_MIN, INT_MIN ) : 
510                                           QPointF( aTransform.map( aLocalPointC ) );
511
512   SetGlobalPoints( ManualCartesian, aTrsfPointA, aTrsfPointB, aTrsfPointC );
513
514   SetToUpdate( false );
515 }
516
517 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
518                                       const QPoint& thePointB,
519                                       const QPoint& thePointC,
520                                       const bool    theIsUpdate )
521 {
522   Handle(TDataStd_RealArray) anArray;
523   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
524     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
525
526   anArray->SetValue( 1, thePointA.x() );
527   anArray->SetValue( 2, thePointA.y() );
528   anArray->SetValue( 3, thePointB.x() );
529   anArray->SetValue( 4, thePointB.y() );
530   anArray->SetValue( 5, thePointC.x() );
531   anArray->SetValue( 6, thePointC.y() );
532
533   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
534
535   if ( theIsUpdate )
536     UpdateTrsf();
537
538   SetToUpdate( true );
539 }
540
541 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
542                                       QPoint& thePointB,
543                                       QPoint& thePointC ) const
544 {
545   if ( !HasLocalPoints() )
546     return false;
547
548   Handle(TDataStd_RealArray) anArray;
549   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
550
551   thePointA = QPointF( anArray->Value( 1 ), anArray->Value( 2 ) ).toPoint();
552   thePointB = QPointF( anArray->Value( 3 ), anArray->Value( 4 ) ).toPoint();
553   thePointC = QPointF( anArray->Value( 5 ), anArray->Value( 6 ) ).toPoint();
554
555   return true;
556 }
557
558 bool HYDROData_Image::HasLocalPoints() const
559 {
560   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
561   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_LOCAL_POINTS ) )
562     return false;
563
564   Handle(TDataStd_RealArray) anArray;
565   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
566 }
567
568
569 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
570                                        const QPointF&            thePointA,
571                                        const QPointF&            thePointB,
572                                        const QPointF&            thePointC,
573                                        const bool                theIsUpdate )
574 {
575   Handle(TDataStd_RealArray) anArray;
576   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
577     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
578
579   anArray->SetValue( 7,  thePointA.x() );
580   anArray->SetValue( 8,  thePointA.y() );
581   anArray->SetValue( 9,  thePointB.x() );
582   anArray->SetValue( 10, thePointB.y() );
583   anArray->SetValue( 11, thePointC.x() );
584   anArray->SetValue( 12, thePointC.y() );
585   
586   SetTrsfMode( theMode );
587
588   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
589
590   if ( theIsUpdate )
591     UpdateTrsf();
592
593   SetToUpdate( true );
594 }
595
596 bool HYDROData_Image::GetGlobalPoints( TransformationMode& theMode,
597                                        QPointF&            thePointA,
598                                        QPointF&            thePointB,
599                                        QPointF&            thePointC ) const
600 {
601   if ( !HasGlobalPoints() )
602     return false;
603
604   theMode = GetTrsfMode();
605
606   Handle(TDataStd_RealArray) anArray;
607   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
608
609   thePointA = QPointF( anArray->Value( 7  ), anArray->Value( 8  ) );
610   thePointB = QPointF( anArray->Value( 9  ), anArray->Value( 10 ) );
611   thePointC = QPointF( anArray->Value( 11 ), anArray->Value( 12 ) );
612
613   return true;
614 }
615
616 bool HYDROData_Image::SetGlobalPointsFromFile( const QString& theFileName )
617 {
618   bool aRes = false;
619
620   // Try to open the file
621   QFile aFile( theFileName );
622   if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) ) {
623     return aRes;
624   }
625
626   QPointF aPointA, aPointB;
627   double aXmin, anYmin, aXmax, anYmax;
628   aXmin = anYmin = aXmax = anYmax = -1;
629
630   while ( !aFile.atEnd() && 
631           ( aXmin < 0 || anYmin < 0 || aXmax < 0 || anYmax < 0 ) ) {
632     // Read line
633     QString aLine = aFile.readLine().simplified();
634     aLine.replace( " ", "" );
635     if ( aLine.isEmpty() ) {
636       continue;
637     }
638
639     // Try to read double value after ":"
640     bool isDoubleOk = false;
641     double aDoubleValue = -1;
642     QStringList aValues = aLine.split( ":", QString::SkipEmptyParts );
643     if ( aValues.count() == 2 ) {
644       aDoubleValue = aValues.last().toDouble( &isDoubleOk );
645     }
646
647     // Check the result
648     if ( !isDoubleOk ||
649          boost::math::isnan( aDoubleValue ) ||
650          boost::math::isinf( aDoubleValue ) ) {
651       continue;
652     }
653
654     // Set the value
655     if ( aLine.startsWith( "Xminimum" ) ) {
656       aXmin = aDoubleValue;    
657     } 
658     else if ( aLine.startsWith( "Yminimum" ) ) {
659       anYmin = aDoubleValue; 
660     }
661     else if ( aLine.startsWith( "Xmaximum" ) ) {
662       aXmax = aDoubleValue;
663     }
664     else if ( aLine.startsWith( "Ymaximum" ) ) {
665       anYmax = aDoubleValue;  
666     }
667   }
668
669   // Close the file
670   aFile.close();
671
672   if ( aXmin >= 0 && anYmin >= 0 ) {
673     aPointA.setX( aXmin );
674     aPointA.setY( anYmin );
675   }
676     
677   if ( aXmax >= 0 && anYmax >= 0 ) {
678     aPointB.setX( aXmax );
679     aPointB.setY( anYmax );
680   }
681
682   if ( !aPointA.isNull() && !aPointB.isNull() ) {
683     SetGlobalPoints( ManualCartesian, aPointA, aPointB );
684     aRes = true;
685   }
686
687   return aRes;
688 }
689
690 bool HYDROData_Image::HasGlobalPoints() const
691 {
692   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
693   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_GLOBAL_POINTS ) )
694     return false;
695
696   Handle(TDataStd_RealArray) anArray;
697   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
698 }
699
700 void HYDROData_Image::SetReferencePoints( const Handle(HYDROData_Image)& theRefImage,
701                                           const QPointF&                 thePointA,
702                                           const QPointF&                 thePointB,
703                                           const QPointF&                 thePointC,
704                                           const bool                     theIsUpdate )
705 {
706   SetTrsfReferenceImage( theRefImage );
707   SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC, theIsUpdate );
708 }
709
710 bool HYDROData_Image::GetReferencePoints( Handle(HYDROData_Image)& theRefImage,
711                                           QPointF&                 thePointA,
712                                           QPointF&                 thePointB,
713                                           QPointF&                 thePointC ) const
714 {
715   if ( !HasReferencePoints() )
716     return false;
717
718   theRefImage = GetTrsfReferenceImage();
719
720   TransformationMode aMode;
721   GetGlobalPoints( aMode, thePointA, thePointB, thePointC );
722
723   return true;
724 }
725
726 bool HYDROData_Image::HasReferencePoints() const
727 {
728   if ( !HasGlobalPoints() )
729     return false;
730
731   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
732   if ( aRefImage.IsNull() )
733     return false;
734
735   TransformationMode aTrsfMode = GetTrsfMode();
736   if ( aTrsfMode != ReferenceImage )
737     return false;
738
739   return true;
740 }
741
742 void HYDROData_Image::SetTrsfMode( const TransformationMode& theMode )
743 {
744   TDataStd_Integer::Set( myLab.FindChild( DataTag_TrsfMode ), (int)theMode );
745   SetToUpdate( true );
746 }
747
748 HYDROData_Image::TransformationMode HYDROData_Image::GetTrsfMode() const
749 {
750   TransformationMode aResMode = ManualGeodesic;
751
752   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
753   if ( !aLabel.IsNull() )
754   {
755     Handle(TDataStd_Integer) aMode;
756     if ( myLab.FindChild( DataTag_TrsfMode ).FindAttribute( TDataStd_Integer::GetID(), aMode ) )
757       aResMode = (TransformationMode)aMode->Get();
758   }
759
760   return aResMode;
761 }
762
763 void HYDROData_Image::SetTrsfReferenceImage( const Handle(HYDROData_Image)& theRefImage )
764 {
765   SetReferenceObject( theRefImage, DataTag_TrsfImage );
766   SetToUpdate( true );
767 }
768
769 Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
770 {
771   return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
772 }
773
774 void HYDROData_Image::RemoveTrsfReferenceImage()
775 {
776   RemoveReferenceObject( DataTag_TrsfImage );
777   SetToUpdate( true );
778 }
779
780 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
781 {
782   AddReferenceObject( theReferenced, 0 );
783   SetToUpdate( true );
784 }
785
786 int HYDROData_Image::NbReferences() const
787 {
788   return NbReferenceObjects( 0 );
789 }
790
791 Handle(HYDROData_Entity) HYDROData_Image::Reference( const int theIndex ) const
792 {
793   return GetReferenceObject( 0, theIndex );
794 }
795
796 void HYDROData_Image::ChangeReference(
797     const int theIndex, Handle(HYDROData_Entity) theReferenced)
798 {
799   SetReferenceObject( theReferenced, 0, theIndex );
800   SetToUpdate( true );
801 }
802
803 void HYDROData_Image::RemoveReference(const int theIndex)
804 {
805   RemoveReferenceObject( 0, theIndex );
806   SetToUpdate( true );
807 }
808
809 void HYDROData_Image::ClearReferences()
810 {
811   ClearReferenceObjects( 0 );
812   SetToUpdate( true );
813 }
814
815 void HYDROData_Image::SetOperatorName( const QString theOpName )
816 {
817   TCollection_AsciiString anAsciiStr( theOpName.toStdString().c_str() );
818   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_Operator ), anAsciiStr );
819   SetToUpdate( true );
820 }
821
822 QString HYDROData_Image::OperatorName() const
823 {
824   QString aRes;
825
826   TDF_Label aLabel = myLab.FindChild( DataTag_Operator, false );
827   if ( !aLabel.IsNull() )
828   {
829     Handle(TDataStd_AsciiString) anAsciiStr;
830     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
831       aRes = QString( anAsciiStr->Get().ToCString() );
832   }
833
834   return aRes;
835 }
836
837 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
838 {
839   SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
840   SetToUpdate( true );
841 }
842
843 QByteArray HYDROData_Image::Args() const
844 {
845   int aLen = 0;
846   const char* aData = ByteArray(DataTag_Operator, aLen);
847   if (!aLen)
848     return QByteArray();
849   return QByteArray(aData, aLen);
850 }
851
852 void HYDROData_Image::SetIsSelfSplitted(bool theFlag)
853 {
854   if (theFlag) {
855     TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLITTED);
856   } else {
857     myLab.ForgetAttribute(GUID_SELF_SPLITTED);
858   }
859   SetToUpdate( true );
860 }
861
862 bool HYDROData_Image::IsSelfSplitted() const
863 {
864   return myLab.IsAttribute(GUID_SELF_SPLITTED);
865 }
866
867 QPointF HYDROData_Image::generateThirdPoint( const QPointF& thePointA,
868                                              const QPointF& thePointB,
869                                              const bool&    theIsLocal ) const
870 {
871   // Rotate vector to 90 degrees : clockwise - for local
872   //                               counterclockwise - for global
873   const double aTheta = theIsLocal ? -M_PI_2 : M_PI_2;
874
875   QPointF aResPoint;
876
877   // Move to (0,0) for correct rotation
878   double x = thePointB.x() - thePointA.x();
879   double y = thePointB.y() - thePointA.y();
880
881   aResPoint.setX( x * cos( aTheta ) - y * sin( aTheta ) );
882   aResPoint.setY( x * sin( aTheta ) + y * cos( aTheta ) );
883
884   // Move back to origin position
885   aResPoint.setX( aResPoint.x() + thePointA.x() );
886   aResPoint.setY( aResPoint.y() + thePointA.y() );
887
888   return aResPoint;
889 }
890