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