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