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