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