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