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