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