Salome HOME
ae6b08042693fc987ca8885b86ef58a9771f056b
[modules/hydro.git] / src / HYDROData / HYDROData_Image.cxx
1
2 #include "HYDROData_Image.h"
3
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Tool.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_Name.hxx>
14 #include <TDataStd_UAttribute.hxx>
15 #include <TDataStd_AsciiString.hxx>
16
17 #include <ImageComposer_Operator.h>
18 #include <ImageComposer_MetaTypes.h>
19
20 #include <QStringList>
21
22 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
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( this );
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 = HYDROData_OperationsFactory::Factory()->Operator( this );
116       if ( anImageOp )
117       {
118         // Dump operation arguments
119         QString anOpArgsArrayName;
120         QStringList anOpArgs = anImageOp->dumpArgsToPython( anOpArgsArrayName );
121         if ( !anOpArgs.isEmpty() )
122         {
123           aResList << QString( "" );
124           aResList << anOpArgs;
125
126           aResList << QString( "" );
127           aResList << QString( "%1.SetArgs( %2 );" )
128                       .arg( anImageName ).arg( anOpArgsArrayName );
129         }
130       }
131     }
132     
133     int aNbReferences = NbReferences();
134     if ( aNbReferences > 0 )
135     {
136       aResList << QString( "" );
137
138       for ( int i = 0; i < aNbReferences; ++i )
139       {
140         Handle(HYDROData_Image) aRefImg = Handle(HYDROData_Image)::DownCast( Reference( i ) );
141         setPythonReferenceObject( theTreatedObjects, aResList, aRefImg, "AppendReference" );
142       }
143     }
144
145     // Necessary to update image in case of composed operator
146     aResList << QString( "" );
147     aResList << QString( "%1.Update( False );" ).arg( anImageName );
148   }
149
150   return aResList;
151 }
152
153 void HYDROData_Image::Update( const bool theIsForce )
154 {
155   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
156
157   // Update image only if there is an operation
158   ImageComposer_Operator* anOp = aFactory->Operator( OperatorName() );
159   if ( anOp ) 
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   }
192
193   if ( theIsForce )
194   {
195     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
196     if ( !aDocument.IsNull() )
197     {
198       // Change the states of this and all depended images
199       MustBeUpdated( true );
200       HYDROData_Tool::SetMustBeUpdatedImages( aDocument );
201       MustBeUpdated( false );
202     }
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 void HYDROData_Image::SetImage(const QImage& theImage)
220 {
221   if (theImage.isNull()) {
222     // for empty image remove all previously stored attributes
223     myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
224     myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
225     return;
226   }
227   // store width, height, bytes per line and format in integer array 
228   Handle(TDataStd_IntegerArray) aParams;
229   if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
230     aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
231   }
232   aParams->SetValue(1, theImage.width());
233   aParams->SetValue(2, theImage.height());
234   aParams->SetValue(3, theImage.bytesPerLine());
235   aParams->SetValue(4, (int)(theImage.format()));
236   // store data of image in byte array
237   const char* aData = (const char*)(theImage.bits());
238   SaveByteArray(0, aData, theImage.byteCount());
239 }
240
241 bool HYDROData_Image::LoadImage(const QString& theFilePath)
242 {
243   QImage anImage( theFilePath );
244   SetImage( anImage );
245   return !anImage.isNull();
246 }
247
248 QImage HYDROData_Image::Image()
249 {
250   Handle(TDataStd_IntegerArray) aParams;
251   if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
252     return QImage(); // return empty image if there is no array
253   int aLen = 0;
254   uchar* anArray = (uchar*)ByteArray(0, aLen);
255   if (!aLen)
256     return QImage(); // return empty image if there is no array
257   QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
258                  aParams->Value(3), QImage::Format(aParams->Value(4)));
259   return aResult;
260 }
261
262 void HYDROData_Image::SetFilePath(const QString& theFilePath)
263 {
264   TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
265   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
266 }
267
268 QString HYDROData_Image::GetFilePath() const
269 {
270   QString aRes;
271
272   Handle(TDataStd_AsciiString) anAsciiStr;
273   if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
274     aRes = QString( anAsciiStr->Get().ToCString() );
275
276   return aRes;
277 }
278
279 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
280 {
281   // locate 9 coeffs of matrix into the real array
282   Handle(TDataStd_RealArray) anArray;
283   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
284     if (theTrsf.isIdentity()) return; // no need to store identity transformation
285     anArray = TDataStd_RealArray::Set(myLab, 1, 9);
286   }
287   anArray->SetValue(1, theTrsf.m11());
288   anArray->SetValue(2, theTrsf.m12());
289   anArray->SetValue(3, theTrsf.m13());
290   anArray->SetValue(4, theTrsf.m21());
291   anArray->SetValue(5, theTrsf.m22());
292   anArray->SetValue(6, theTrsf.m23());
293   anArray->SetValue(7, theTrsf.m31());
294   anArray->SetValue(8, theTrsf.m32());
295   anArray->SetValue(9, theTrsf.m33());
296 }
297
298 QTransform HYDROData_Image::Trsf() const
299 {
300   // get 9 coeffs of matrix from the real array
301   Handle(TDataStd_RealArray) anArray;
302   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
303     return QTransform(); // return identity if there is no array
304   QTransform aTrsf(
305     anArray->Value(1), anArray->Value(2), anArray->Value(3), 
306     anArray->Value(4), anArray->Value(5), anArray->Value(6), 
307     anArray->Value(7), anArray->Value(8), anArray->Value(9));
308   return aTrsf;
309 }
310
311 void HYDROData_Image::UpdateTrsf()
312 {
313   QPoint aPointA, aPointB, aPointC;
314   if ( !GetLocalPoints( aPointA, aPointB, aPointC ) )
315     return;
316
317   TransformationMode aTrsfMode;
318   QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
319   if ( !GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
320     return;
321
322   QTransform aRefTransform;
323   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
324
325   bool anIsRefImage = aTrsfMode == ReferenceImage;
326   if ( anIsRefImage )
327   {
328     if ( aRefImage.IsNull() )
329       return;
330
331     aRefTransform = aRefImage->Trsf();
332   }
333
334   // generate third points if needed
335   if ( IsByTwoPoints() )
336   {
337     aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
338     aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
339   }
340
341   int xa = aPointA.x();
342   int ya = aPointA.y();
343   int xb = aPointB.x();
344   int yb = aPointB.y();
345   int xc = aPointC.x();
346   int yc = aPointC.y();
347
348   double xta = aTrsfPointA.x();
349   double yta = aTrsfPointA.y();
350   double xtb = aTrsfPointB.x();
351   double ytb = aTrsfPointB.y();
352   double xtc = aTrsfPointC.x();
353   double ytc = aTrsfPointC.y();
354
355   // first, check that three input points don't belong to a single line
356   if( ( yb - ya ) * ( xc - xa ) == ( yc - ya ) * ( xb - xa ) )
357     return;
358
359   // the same check for the reference points
360   if( anIsRefImage && ( ( ytb - yta ) * ( xtc - xta ) == ( ytc - yta ) * ( xtb - xta ) ) )
361     return;
362
363   QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
364   QTransform aTransform2( xta, yta, 1, xtb, ytb, 1, xtc, ytc, 1 );
365
366   bool anIsInvertible = false;
367   QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
368   if( !anIsInvertible )
369     return;
370
371   QTransform aResTransform;
372   if( anIsRefImage )
373     aResTransform = aTransform1Inverted * aTransform2 * aRefTransform;
374   else
375     aResTransform = aTransform1Inverted * aTransform2;
376
377   SetTrsf( aResTransform );
378 }
379
380 bool HYDROData_Image::IsByTwoPoints() const
381 {
382   if ( !HasLocalPoints() || !HasGlobalPoints() )
383     return false;
384
385   QPoint aPointA, aPointB, aPointC;
386   GetLocalPoints( aPointA, aPointB, aPointC );
387
388   return aPointC.x() < 0 && aPointC.y() < 0; 
389 }
390
391 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
392                                       const QPoint& thePointB,
393                                       const QPoint& thePointC )
394 {
395   Handle(TDataStd_RealArray) anArray;
396   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
397     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
398
399   anArray->SetValue( 1, thePointA.x() );
400   anArray->SetValue( 2, thePointA.y() );
401   anArray->SetValue( 3, thePointB.x() );
402   anArray->SetValue( 4, thePointB.y() );
403   anArray->SetValue( 5, thePointC.x() );
404   anArray->SetValue( 6, thePointC.y() );
405
406   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
407
408   UpdateTrsf();
409 }
410
411 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
412                                       QPoint& thePointB,
413                                       QPoint& thePointC ) const
414 {
415   if ( !HasLocalPoints() )
416     return false;
417
418   Handle(TDataStd_RealArray) anArray;
419   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
420
421   thePointA = QPointF( anArray->Value( 1 ), anArray->Value( 2 ) ).toPoint();
422   thePointB = QPointF( anArray->Value( 3 ), anArray->Value( 4 ) ).toPoint();
423   thePointC = QPointF( anArray->Value( 5 ), anArray->Value( 6 ) ).toPoint();
424
425   return true;
426 }
427
428 bool HYDROData_Image::HasLocalPoints() const
429 {
430   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
431   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_LOCAL_POINTS ) )
432     return false;
433
434   Handle(TDataStd_RealArray) anArray;
435   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
436 }
437
438
439 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
440                                        const QPointF&            thePointA,
441                                        const QPointF&            thePointB,
442                                        const QPointF&            thePointC )
443 {
444   Handle(TDataStd_RealArray) anArray;
445   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
446     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
447
448   anArray->SetValue( 7,  thePointA.x() );
449   anArray->SetValue( 8,  thePointA.y() );
450   anArray->SetValue( 9,  thePointB.x() );
451   anArray->SetValue( 10, thePointB.y() );
452   anArray->SetValue( 11, thePointC.x() );
453   anArray->SetValue( 12, thePointC.y() );
454   
455   SetTrsfMode( theMode );
456
457   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
458
459   UpdateTrsf();
460
461   /*
462   if( anIsRefImage )
463   {
464     aCPointA = QPointF( aTransform.map( aPointA ) );
465     aCPointB = QPointF( aTransform.map( aPointB ) );
466     aCPointC = QPointF( aTransform.map( aPointC ) );
467
468     // compute Lambert93 points
469     xca = aCPointA.x();
470     yca = aCPointA.y();
471     xcb = aCPointB.x();
472     ycb = aCPointB.y();
473     xcc = aCPointC.x();
474     ycc = aCPointC.y();
475
476     double xla = 0, yla = 0, xlb = 0, ylb = 0, xlc = 0, ylc = 0;
477     HYDROData_Lambert93::toGeo( xca, yca, yla, xla );
478     HYDROData_Lambert93::toGeo( xcb, ycb, ylb, xlb );
479     HYDROData_Lambert93::toGeo( xcc, ycc, ylc, xlc );
480
481     aLPointA = QPointF( xla * 3600.0, yla * 3600.0 ); // convert degrees to seconds
482     aLPointB = QPointF( xlb * 3600.0, ylb * 3600.0 ); // convert degrees to seconds
483     aLPointC = QPointF( xlc * 3600.0, ylc * 3600.0 ); // convert degrees to seconds
484   }
485   */
486 }
487
488 bool HYDROData_Image::GetGlobalPoints( TransformationMode& theMode,
489                                        QPointF&            thePointA,
490                                        QPointF&            thePointB,
491                                        QPointF&            thePointC ) const
492 {
493   if ( !HasGlobalPoints() )
494     return false;
495
496   theMode = GetTrsfMode();
497
498   Handle(TDataStd_RealArray) anArray;
499   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
500
501   thePointA = QPointF( anArray->Value( 7  ), anArray->Value( 8  ) );
502   thePointB = QPointF( anArray->Value( 9  ), anArray->Value( 10 ) );
503   thePointC = QPointF( anArray->Value( 11 ), anArray->Value( 12 ) );
504
505   return true;
506 }
507
508 bool HYDROData_Image::HasGlobalPoints() const
509 {
510   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
511   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_GLOBAL_POINTS ) )
512     return false;
513
514   Handle(TDataStd_RealArray) anArray;
515   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
516 }
517
518 void HYDROData_Image::SetReferencePoints( const Handle(HYDROData_Image)& theRefImage,
519                                           const QPointF&                 thePointA,
520                                           const QPointF&                 thePointB,
521                                           const QPointF&                 thePointC )
522 {
523   SetTrsfReferenceImage( theRefImage );
524   SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC );
525
526   UpdateTrsf();
527 }
528
529 bool HYDROData_Image::GetReferencePoints( Handle(HYDROData_Image)& theRefImage,
530                                           QPointF&                 thePointA,
531                                           QPointF&                 thePointB,
532                                           QPointF&                 thePointC ) const
533 {
534   if ( !HasReferencePoints() )
535     return false;
536
537   theRefImage = GetTrsfReferenceImage();
538
539   TransformationMode aMode;
540   GetGlobalPoints( aMode, thePointA, thePointB, thePointC );
541
542   return true;
543 }
544
545 bool HYDROData_Image::HasReferencePoints() const
546 {
547   if ( !HasGlobalPoints() )
548     return false;
549
550   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
551   if ( aRefImage.IsNull() )
552     return false;
553
554   TransformationMode aTrsfMode = GetTrsfMode();
555   if ( aTrsfMode != ReferenceImage )
556     return false;
557
558   return true;
559 }
560
561 void HYDROData_Image::SetTrsfMode( const TransformationMode& theMode )
562 {
563   TDataStd_Integer::Set( myLab.FindChild( DataTag_TrsfMode ), (int)theMode );
564 }
565
566 HYDROData_Image::TransformationMode HYDROData_Image::GetTrsfMode() const
567 {
568   TransformationMode aResMode = ManualLambert;
569
570   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
571   if ( !aLabel.IsNull() )
572   {
573     Handle(TDataStd_Integer) aMode;
574     if ( myLab.FindChild( DataTag_TrsfMode ).FindAttribute( TDataStd_Integer::GetID(), aMode ) )
575       aResMode = (TransformationMode)aMode->Get();
576   }
577
578   return aResMode;
579 }
580
581 void HYDROData_Image::SetTrsfReferenceImage( const Handle(HYDROData_Image)& theRefImage )
582 {
583   SetReferenceObject( theRefImage, DataTag_TrsfImage );
584 }
585
586 Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
587 {
588   return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
589 }
590
591 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
592 {
593   AddReferenceObject( theReferenced, 0 );
594 }
595
596 int HYDROData_Image::NbReferences() const
597 {
598   return NbReferenceObjects( 0 );
599 }
600
601 Handle(HYDROData_Entity) HYDROData_Image::Reference( const int theIndex ) const
602 {
603   return GetReferenceObject( 0, theIndex );
604 }
605
606 void HYDROData_Image::ChangeReference(
607     const int theIndex, Handle(HYDROData_Entity) theReferenced)
608 {
609   SetReferenceObject( theReferenced, 0, theIndex );
610 }
611
612 void HYDROData_Image::RemoveReference(const int theIndex)
613 {
614   RemoveReferenceObject( 0, theIndex );
615 }
616
617 void HYDROData_Image::ClearReferences()
618 {
619   ClearReferenceObjects( 0 );
620 }
621
622 void HYDROData_Image::SetOperatorName(const QString theOpName)
623 {
624   TDataStd_Name::Set(myLab.FindChild(DataTag_Operator),
625     TCollection_ExtendedString(theOpName.toLatin1().constData()));
626 }
627
628 QString HYDROData_Image::OperatorName() const
629 {
630   Handle(TDataStd_Name) aName;
631   if (myLab.FindChild(DataTag_Operator).
632         FindAttribute(TDataStd_Name::GetID(), aName)) {
633     TCollection_AsciiString aStr(aName->Get());
634     return QString(aStr.ToCString());
635   }
636   return QString();
637 }
638
639 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
640 {
641   SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
642 }
643
644 QByteArray HYDROData_Image::Args() const
645 {
646   int aLen = 0;
647   const char* aData = ByteArray(DataTag_Operator, aLen);
648   if (!aLen)
649     return QByteArray();
650   return QByteArray(aData, aLen);
651 }
652
653 void HYDROData_Image::MustBeUpdated(bool theFlag)
654 {
655   if (theFlag) {
656     TDataStd_UAttribute::Set(myLab, GUID_MUST_BE_UPDATED);
657   } else {
658     myLab.ForgetAttribute(GUID_MUST_BE_UPDATED);
659   }
660 }
661
662 bool HYDROData_Image::MustBeUpdated() const
663 {
664   return myLab.IsAttribute(GUID_MUST_BE_UPDATED);
665 }
666
667 void HYDROData_Image::SetIsSelfSplitted(bool theFlag)
668 {
669   if (theFlag) {
670     TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLITTED);
671   } else {
672     myLab.ForgetAttribute(GUID_SELF_SPLITTED);
673   }
674 }
675
676 bool HYDROData_Image::IsSelfSplitted() const
677 {
678   return myLab.IsAttribute(GUID_SELF_SPLITTED);
679 }
680
681 QPointF HYDROData_Image::generateThirdPoint( const QPointF& thePointA,
682                                              const QPointF& thePointB,
683                                              const bool&    theIsLocal ) const
684 {
685   // Rotate vector to 90 degrees : clockwise - for local
686   //                               anticlockwise - for global
687   const double aTheta = theIsLocal ? -M_PI_2 : M_PI_2;
688
689   QPointF aResPoint;
690
691   // Move to (0,0) for correct rotation
692   double x = thePointB.x() - thePointA.x();
693   double y = thePointB.y() - thePointA.y();
694
695   aResPoint.setX( x * cos( aTheta ) - y * sin( aTheta ) );
696   aResPoint.setY( x * sin( aTheta ) + y * cos( aTheta ) );
697
698   // Move back to origin position
699   aResPoint.setX( aResPoint.x() + thePointA.x() );
700   aResPoint.setY( aResPoint.y() + thePointA.y() );
701
702   return aResPoint;
703 }
704