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