Salome HOME
Refs #289 - Spline profile is represented in OCC view as polyline profile
[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   SetToUpdate( false );
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 HYDROData_SequenceOfObjects HYDROData_Image::GetAllReferenceObjects() const
222 {
223   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
224
225   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
226   if ( !aRefImage.IsNull() )
227     aResSeq.Append( aRefImage );
228
229   HYDROData_SequenceOfObjects aSeqRefObjects = GetReferenceObjects( 0 );
230   aResSeq.Append( aSeqRefObjects );
231
232   return aResSeq;
233 }
234
235 void HYDROData_Image::SetImage(const QImage& theImage)
236 {
237   if ( theImage.isNull() )
238   {
239     // for empty image remove all previously stored attributes
240     myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
241     myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
242   }
243   else
244   {
245     // store width, height, bytes per line and format in integer array 
246     Handle(TDataStd_IntegerArray) aParams;
247     if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
248       aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
249     }
250     aParams->SetValue(1, theImage.width());
251     aParams->SetValue(2, theImage.height());
252     aParams->SetValue(3, theImage.bytesPerLine());
253     aParams->SetValue(4, (int)(theImage.format()));
254     // store data of image in byte array
255     const char* aData = (const char*)(theImage.bits());
256     SaveByteArray(0, aData, theImage.byteCount());
257   }
258
259   SetToUpdate( true );
260 }
261
262 bool HYDROData_Image::LoadImage( const QString& theFilePath )
263 {
264   QImage anImage( theFilePath );
265   SetImage( anImage );
266   return !anImage.isNull();
267 }
268
269 QImage HYDROData_Image::Image()
270 {
271   Handle(TDataStd_IntegerArray) aParams;
272   if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
273     return QImage(); // return empty image if there is no array
274   int aLen = 0;
275   uchar* anArray = (uchar*)ByteArray(0, aLen);
276   if (!aLen)
277     return QImage(); // return empty image if there is no array
278   QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
279                  aParams->Value(3), QImage::Format(aParams->Value(4)));
280   return aResult;
281 }
282
283 void HYDROData_Image::SetFilePath( const QString& theFilePath )
284 {
285   TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
286   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
287
288   SetToUpdate( true );
289 }
290
291 QString HYDROData_Image::GetFilePath() const
292 {
293   QString aRes;
294
295   TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
296   if ( !aLabel.IsNull() )
297   {
298     Handle(TDataStd_AsciiString) anAsciiStr;
299     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
300       aRes = QString( anAsciiStr->Get().ToCString() );
301   }
302
303   return aRes;
304 }
305
306 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
307 {
308   // locate 9 coeffs of matrix into the real array
309   Handle(TDataStd_RealArray) anArray;
310   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
311     if (theTrsf.isIdentity()) return; // no need to store identity transformation
312     anArray = TDataStd_RealArray::Set(myLab, 1, 9);
313   }
314   anArray->SetValue(1, theTrsf.m11());
315   anArray->SetValue(2, theTrsf.m12());
316   anArray->SetValue(3, theTrsf.m13());
317   anArray->SetValue(4, theTrsf.m21());
318   anArray->SetValue(5, theTrsf.m22());
319   anArray->SetValue(6, theTrsf.m23());
320   anArray->SetValue(7, theTrsf.m31());
321   anArray->SetValue(8, theTrsf.m32());
322   anArray->SetValue(9, theTrsf.m33());
323
324   SetToUpdate( true );
325 }
326
327 QTransform HYDROData_Image::Trsf() const
328 {
329   // get 9 coeffs of matrix from the real array
330   Handle(TDataStd_RealArray) anArray;
331   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
332     return QTransform(); // return identity if there is no array
333   QTransform aTrsf(
334     anArray->Value(1), anArray->Value(2), anArray->Value(3), 
335     anArray->Value(4), anArray->Value(5), anArray->Value(6), 
336     anArray->Value(7), anArray->Value(8), anArray->Value(9));
337   return aTrsf;
338 }
339
340 void HYDROData_Image::UpdateTrsf()
341 {
342   QPoint aPointA, aPointB, aPointC;
343   if ( !GetLocalPoints( aPointA, aPointB, aPointC ) )
344     return;
345
346   TransformationMode aTrsfMode;
347   QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
348   if ( !GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
349     return;
350
351   QTransform aRefTransform;
352   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
353
354   bool anIsRefImage = aTrsfMode == ReferenceImage;
355   if ( anIsRefImage )
356   {
357     if ( aRefImage.IsNull() )
358       return;
359
360     aRefTransform = aRefImage->Trsf();
361   }
362
363   bool anIsByTwoPoints = IsByTwoPoints();
364
365   // Convert lambert coordinates to cartesian
366   if ( aTrsfMode == ManualGeodesic )
367   {
368     double aXCart = 0, aYCart = 0;
369
370     HYDROData_Lambert93::toXY( aTrsfPointA.y(), aTrsfPointA.x(), aXCart, aYCart );
371     aTrsfPointA = QPointF( aXCart, aYCart );
372
373     HYDROData_Lambert93::toXY( aTrsfPointB.y(), aTrsfPointB.x(), aXCart, aYCart );
374     aTrsfPointB = QPointF( aXCart, aYCart );
375
376     if ( !anIsByTwoPoints )
377     {
378       HYDROData_Lambert93::toXY( aTrsfPointC.y(), aTrsfPointC.x(), aXCart, aYCart );
379       aTrsfPointC = QPointF( aXCart, aYCart );
380     }
381   }
382
383   // generate third points if needed
384   if ( anIsByTwoPoints )
385   {
386     aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
387     aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
388   }
389
390   int xa = aPointA.x();
391   int ya = aPointA.y();
392   int xb = aPointB.x();
393   int yb = aPointB.y();
394   int xc = aPointC.x();
395   int yc = aPointC.y();
396
397   double xta = aTrsfPointA.x();
398   double yta = aTrsfPointA.y();
399   double xtb = aTrsfPointB.x();
400   double ytb = aTrsfPointB.y();
401   double xtc = aTrsfPointC.x();
402   double ytc = aTrsfPointC.y();
403
404   // first, check that three input points don't belong to a single line
405   if( ( yb - ya ) * ( xc - xa ) == ( yc - ya ) * ( xb - xa ) )
406     return;
407
408   // the same check for the reference points
409   if( anIsRefImage && ( ( ytb - yta ) * ( xtc - xta ) == ( ytc - yta ) * ( xtb - xta ) ) )
410     return;
411
412   QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
413   QTransform aTransform2( xta, yta, 1, xtb, ytb, 1, xtc, ytc, 1 );
414
415   bool anIsInvertible = false;
416   QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
417   if( !anIsInvertible )
418     return;
419
420   QTransform aResTransform = aTransform1Inverted * aTransform2;
421   if( anIsRefImage )
422     aResTransform *= aRefTransform;
423
424   SetTrsf( aResTransform );
425 }
426
427 bool HYDROData_Image::IsByTwoPoints() const
428 {
429   if ( !HasLocalPoints() || !HasGlobalPoints() )
430     return false;
431
432   QPoint aPointA, aPointB, aPointC;
433   GetLocalPoints( aPointA, aPointB, aPointC );
434
435   return aPointC.x() < 0 && aPointC.y() < 0; 
436 }
437
438 bool HYDROData_Image::HasReferences() const
439 {
440   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
441   int aNbReferences = NbReferences();
442
443   return !aRefImage.IsNull() || aNbReferences > 0;
444 }
445
446 void HYDROData_Image::RemoveAllReferences()
447 {
448   if ( !HasReferences() )
449     return;
450
451   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
452   if ( !aRefImage.IsNull() )
453   {
454     RemoveTrsfReferenceImage();
455   }
456   else
457   {
458     ClearReferences();
459     SetOperatorName( "" );
460     SetArgs( "" );
461     SetIsSelfSplitted( false );
462   }
463
464   bool anIsByTwoPoints = IsByTwoPoints();
465
466   QImage anImage = Image();
467   if ( anImage.isNull() )
468   {
469     SetToUpdate( false );
470     return;
471   }
472
473   // Set local points to default position
474   QPoint aLocalPointA = QPoint( 0, 0 );
475   QPoint aLocalPointB = QPoint( anImage.width(), 0 );
476   QPoint aLocalPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) : QPoint( 0, anImage.height() );
477
478   SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
479
480   // Calculate global points
481   QTransform aTransform = Trsf();
482
483   QPointF aTrsfPointA = QPointF( aTransform.map( aLocalPointA ) );
484   QPointF aTrsfPointB = QPointF( aTransform.map( aLocalPointB ) );
485   QPointF aTrsfPointC = anIsByTwoPoints ? QPointF( INT_MIN, INT_MIN ) : 
486                                           QPointF( aTransform.map( aLocalPointC ) );
487
488   SetGlobalPoints( ManualCartesian, aTrsfPointA, aTrsfPointB, aTrsfPointC );
489
490   SetToUpdate( false );
491 }
492
493 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
494                                       const QPoint& thePointB,
495                                       const QPoint& thePointC,
496                                       const bool    theIsUpdate )
497 {
498   Handle(TDataStd_RealArray) anArray;
499   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
500     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
501
502   anArray->SetValue( 1, thePointA.x() );
503   anArray->SetValue( 2, thePointA.y() );
504   anArray->SetValue( 3, thePointB.x() );
505   anArray->SetValue( 4, thePointB.y() );
506   anArray->SetValue( 5, thePointC.x() );
507   anArray->SetValue( 6, thePointC.y() );
508
509   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
510
511   if ( theIsUpdate )
512     UpdateTrsf();
513
514   SetToUpdate( true );
515 }
516
517 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
518                                       QPoint& thePointB,
519                                       QPoint& thePointC ) const
520 {
521   if ( !HasLocalPoints() )
522     return false;
523
524   Handle(TDataStd_RealArray) anArray;
525   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
526
527   thePointA = QPointF( anArray->Value( 1 ), anArray->Value( 2 ) ).toPoint();
528   thePointB = QPointF( anArray->Value( 3 ), anArray->Value( 4 ) ).toPoint();
529   thePointC = QPointF( anArray->Value( 5 ), anArray->Value( 6 ) ).toPoint();
530
531   return true;
532 }
533
534 bool HYDROData_Image::HasLocalPoints() const
535 {
536   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
537   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_LOCAL_POINTS ) )
538     return false;
539
540   Handle(TDataStd_RealArray) anArray;
541   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
542 }
543
544
545 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
546                                        const QPointF&            thePointA,
547                                        const QPointF&            thePointB,
548                                        const QPointF&            thePointC,
549                                        const bool                theIsUpdate )
550 {
551   Handle(TDataStd_RealArray) anArray;
552   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
553     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
554
555   anArray->SetValue( 7,  thePointA.x() );
556   anArray->SetValue( 8,  thePointA.y() );
557   anArray->SetValue( 9,  thePointB.x() );
558   anArray->SetValue( 10, thePointB.y() );
559   anArray->SetValue( 11, thePointC.x() );
560   anArray->SetValue( 12, thePointC.y() );
561   
562   SetTrsfMode( theMode );
563
564   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
565
566   if ( theIsUpdate )
567     UpdateTrsf();
568
569   SetToUpdate( true );
570 }
571
572 bool HYDROData_Image::GetGlobalPoints( TransformationMode& theMode,
573                                        QPointF&            thePointA,
574                                        QPointF&            thePointB,
575                                        QPointF&            thePointC ) const
576 {
577   if ( !HasGlobalPoints() )
578     return false;
579
580   theMode = GetTrsfMode();
581
582   Handle(TDataStd_RealArray) anArray;
583   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
584
585   thePointA = QPointF( anArray->Value( 7  ), anArray->Value( 8  ) );
586   thePointB = QPointF( anArray->Value( 9  ), anArray->Value( 10 ) );
587   thePointC = QPointF( anArray->Value( 11 ), anArray->Value( 12 ) );
588
589   return true;
590 }
591
592 bool HYDROData_Image::HasGlobalPoints() const
593 {
594   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
595   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_GLOBAL_POINTS ) )
596     return false;
597
598   Handle(TDataStd_RealArray) anArray;
599   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
600 }
601
602 void HYDROData_Image::SetReferencePoints( const Handle(HYDROData_Image)& theRefImage,
603                                           const QPointF&                 thePointA,
604                                           const QPointF&                 thePointB,
605                                           const QPointF&                 thePointC,
606                                           const bool                     theIsUpdate )
607 {
608   SetTrsfReferenceImage( theRefImage );
609   SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC, theIsUpdate );
610 }
611
612 bool HYDROData_Image::GetReferencePoints( Handle(HYDROData_Image)& theRefImage,
613                                           QPointF&                 thePointA,
614                                           QPointF&                 thePointB,
615                                           QPointF&                 thePointC ) const
616 {
617   if ( !HasReferencePoints() )
618     return false;
619
620   theRefImage = GetTrsfReferenceImage();
621
622   TransformationMode aMode;
623   GetGlobalPoints( aMode, thePointA, thePointB, thePointC );
624
625   return true;
626 }
627
628 bool HYDROData_Image::HasReferencePoints() const
629 {
630   if ( !HasGlobalPoints() )
631     return false;
632
633   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
634   if ( aRefImage.IsNull() )
635     return false;
636
637   TransformationMode aTrsfMode = GetTrsfMode();
638   if ( aTrsfMode != ReferenceImage )
639     return false;
640
641   return true;
642 }
643
644 void HYDROData_Image::SetTrsfMode( const TransformationMode& theMode )
645 {
646   TDataStd_Integer::Set( myLab.FindChild( DataTag_TrsfMode ), (int)theMode );
647   SetToUpdate( true );
648 }
649
650 HYDROData_Image::TransformationMode HYDROData_Image::GetTrsfMode() const
651 {
652   TransformationMode aResMode = ManualGeodesic;
653
654   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
655   if ( !aLabel.IsNull() )
656   {
657     Handle(TDataStd_Integer) aMode;
658     if ( myLab.FindChild( DataTag_TrsfMode ).FindAttribute( TDataStd_Integer::GetID(), aMode ) )
659       aResMode = (TransformationMode)aMode->Get();
660   }
661
662   return aResMode;
663 }
664
665 void HYDROData_Image::SetTrsfReferenceImage( const Handle(HYDROData_Image)& theRefImage )
666 {
667   SetReferenceObject( theRefImage, DataTag_TrsfImage );
668   SetToUpdate( true );
669 }
670
671 Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
672 {
673   return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
674 }
675
676 void HYDROData_Image::RemoveTrsfReferenceImage()
677 {
678   RemoveReferenceObject( DataTag_TrsfImage );
679   SetToUpdate( true );
680 }
681
682 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
683 {
684   AddReferenceObject( theReferenced, 0 );
685   SetToUpdate( true );
686 }
687
688 int HYDROData_Image::NbReferences() const
689 {
690   return NbReferenceObjects( 0 );
691 }
692
693 Handle(HYDROData_Entity) HYDROData_Image::Reference( const int theIndex ) const
694 {
695   return GetReferenceObject( 0, theIndex );
696 }
697
698 void HYDROData_Image::ChangeReference(
699     const int theIndex, Handle(HYDROData_Entity) theReferenced)
700 {
701   SetReferenceObject( theReferenced, 0, theIndex );
702   SetToUpdate( true );
703 }
704
705 void HYDROData_Image::RemoveReference(const int theIndex)
706 {
707   RemoveReferenceObject( 0, theIndex );
708   SetToUpdate( true );
709 }
710
711 void HYDROData_Image::ClearReferences()
712 {
713   ClearReferenceObjects( 0 );
714   SetToUpdate( true );
715 }
716
717 void HYDROData_Image::SetOperatorName( const QString theOpName )
718 {
719   TCollection_AsciiString anAsciiStr( theOpName.toStdString().c_str() );
720   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_Operator ), anAsciiStr );
721   SetToUpdate( true );
722 }
723
724 QString HYDROData_Image::OperatorName() const
725 {
726   QString aRes;
727
728   TDF_Label aLabel = myLab.FindChild( DataTag_Operator, false );
729   if ( !aLabel.IsNull() )
730   {
731     Handle(TDataStd_AsciiString) anAsciiStr;
732     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
733       aRes = QString( anAsciiStr->Get().ToCString() );
734   }
735
736   return aRes;
737 }
738
739 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
740 {
741   SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
742   SetToUpdate( true );
743 }
744
745 QByteArray HYDROData_Image::Args() const
746 {
747   int aLen = 0;
748   const char* aData = ByteArray(DataTag_Operator, aLen);
749   if (!aLen)
750     return QByteArray();
751   return QByteArray(aData, aLen);
752 }
753
754 void HYDROData_Image::SetIsSelfSplitted(bool theFlag)
755 {
756   if (theFlag) {
757     TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLITTED);
758   } else {
759     myLab.ForgetAttribute(GUID_SELF_SPLITTED);
760   }
761   SetToUpdate( true );
762 }
763
764 bool HYDROData_Image::IsSelfSplitted() const
765 {
766   return myLab.IsAttribute(GUID_SELF_SPLITTED);
767 }
768
769 QPointF HYDROData_Image::generateThirdPoint( const QPointF& thePointA,
770                                              const QPointF& thePointB,
771                                              const bool&    theIsLocal ) const
772 {
773   // Rotate vector to 90 degrees : clockwise - for local
774   //                               counterclockwise - for global
775   const double aTheta = theIsLocal ? -M_PI_2 : M_PI_2;
776
777   QPointF aResPoint;
778
779   // Move to (0,0) for correct rotation
780   double x = thePointB.x() - thePointA.x();
781   double y = thePointB.y() - thePointA.y();
782
783   aResPoint.setX( x * cos( aTheta ) - y * sin( aTheta ) );
784   aResPoint.setY( x * sin( aTheta ) + y * cos( aTheta ) );
785
786   // Move back to origin position
787   aResPoint.setX( aResPoint.x() + thePointA.x() );
788   aResPoint.setY( aResPoint.y() + thePointA.y() );
789
790   return aResPoint;
791 }
792