Salome HOME
e589ae9cbec11205a3d233c17013a183b29f6820
[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_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_SELF_SPLITTED("997995aa-5c19-40bf-9a60-ab4b70ad04d8");
23 static const Standard_GUID GUID_HAS_LOCAL_POINTS("FD8841AA-FC44-42fa-B6A7-0F682CCC6F27");
24 static const Standard_GUID GUID_HAS_GLOBAL_POINTS("330D0E81-742D-4ea3-92D4-484877CFA7C1");
25
26 #define PYTHON_IMAGE_ID "KIND_IMAGE"
27
28 IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Entity)
29 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Entity)
30
31 HYDROData_Image::HYDROData_Image()
32 : HYDROData_Entity()
33 {
34 }
35
36 HYDROData_Image::~HYDROData_Image()
37 {
38 }
39
40 QStringList HYDROData_Image::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
41 {
42   QStringList aResList;
43
44   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
45   if ( aDocument.IsNull() )
46     return aResList;
47                              
48   QString aDocName = aDocument->GetDocPyName();
49   QString anImageName = GetName();
50
51   aResList << QString( "%1 = %2.CreateObject( %3 );" )
52               .arg( anImageName ).arg( aDocName ).arg( PYTHON_IMAGE_ID );
53   aResList << QString( "%1.SetName( \"%2\" );" )
54               .arg( anImageName ).arg( anImageName );
55
56   QString aFilePath = GetFilePath();
57   if ( !aFilePath.isEmpty() )
58   {
59     aResList << QString( "" );
60     aResList << QString( "%1.LoadImage( \"%2\" );" )
61                 .arg( anImageName ).arg( aFilePath );
62     aResList << QString( "" );
63
64     // Dump transformation points for image
65
66     QString aGap = QString().fill( ' ', anImageName.size() + 16 );
67
68     bool anIsByTwoPoints = IsByTwoPoints();
69
70     QPoint aLocalPointA, aLocalPointB, aLocalPointC;
71     if ( GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC ) )
72     {
73       aResList << QString( "%1.SetLocalPoints( QPoint( %2, %3 )," )
74                   .arg( anImageName ).arg( aLocalPointA.x() ).arg( aLocalPointA.y() );
75       aResList << QString( aGap             + "QPoint( %1, %2 )," )
76                   .arg( aLocalPointB.x() ).arg( aLocalPointB.y() );
77       aResList << QString( aGap             + "QPoint( %1, %2 ) );" )
78                   .arg( aLocalPointC.x() ).arg( aLocalPointC.y() );
79       aResList << QString( "" );
80     }
81
82     HYDROData_Image::TransformationMode aTransformationMode;
83     QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
84     if ( GetGlobalPoints( aTransformationMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
85     {
86       aResList << QString( "%1.SetGlobalPoints( %2," )
87                   .arg( anImageName ).arg( aTransformationMode );
88       aResList << QString( aGap             +  "QPointF( %1, %2 )," )
89                   .arg( aTrsfPointA.x() ).arg( aTrsfPointA.y() );
90       aResList << QString( aGap             +  "QPointF( %1, %2 )," )
91                   .arg( aTrsfPointB.x() ).arg( aTrsfPointB.y() );
92       aResList << QString( aGap             +  "QPointF( %1, %2 ) );" )
93                   .arg( aTrsfPointC.x() ).arg( aTrsfPointC.y() );
94
95       if ( aTransformationMode == ReferenceImage )
96       {
97         Handle(HYDROData_Image) aRefImg = GetTrsfReferenceImage();
98         setPythonReferenceObject( theTreatedObjects, aResList, aRefImg, "SetTrsfReferenceImage" );
99       }
100     }
101   }
102   else
103   {
104     // Image is composed from other image(s)
105
106     QString anOperatorName = OperatorName();
107     if ( !anOperatorName.isEmpty() )
108     {
109       aResList << QString( "" );
110
111       aResList << QString( "%1.SetOperatorName( \"%2\" );" )
112                   .arg( anImageName ).arg( anOperatorName );
113
114       ImageComposer_Operator* anImageOp = 
115         HYDROData_OperationsFactory::Factory()->Operator( OperatorName() );
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();" ).arg( anImageName );
148   }
149
150   return aResList;
151 }
152
153 void HYDROData_Image::Update()
154 {
155   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
156
157   ImageComposer_Operator* anOp = aFactory->Operator( OperatorName() );
158   if ( anOp ) // Update image if there is an operation
159   {
160     // Fill by arguments and process the operation
161     QVariant anObj1, anObj2;
162     int aNbReferences = NbReferences();
163
164     if ( aNbReferences > 0 )
165     {
166       // First referenced object
167       Handle(HYDROData_Entity) aRefObj = Reference( 0 );
168       if ( !aRefObj.IsNull() )
169       {
170         anObj1 = aRefObj->GetDataVariant();
171         if ( !anObj1.isNull() && anObj1.canConvert<ImageComposer_Image>() )
172         {
173           ImageComposer_Image anImage = anObj1.value<ImageComposer_Image>();
174           QTransform aTransform = anImage.transform();
175           SetTrsf( aTransform );
176         }
177       }
178     }
179
180     if ( aNbReferences > 1 )
181     {
182       // Second referenced object
183       Handle(HYDROData_Entity) aRefObj = Reference( 1 );
184       if ( !aRefObj.IsNull() )
185         anObj2 = aRefObj->GetDataVariant();
186     }
187
188     ImageComposer_Image aResImg = anOp->process( anObj1, anObj2 );
189     SetImage( aResImg );
190     SetTrsf( aResImg.transform() );
191   }
192   else // Update image if it positioned relatively to other image
193   {
194     UpdateTrsf();
195   }
196
197   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
198   if ( !aDocument.IsNull() )
199   {
200     // Change the states of this and all depended images
201     SetToUpdate( true );
202     HYDROData_Tool::SetMustBeUpdatedImages( aDocument );
203     SetToUpdate( false );
204   }
205 }
206
207 QVariant HYDROData_Image::GetDataVariant()
208 {
209   QTransform aTransform = Trsf();
210
211   ImageComposer_Image anImage = Image();
212   anImage.setTransform( aTransform );
213
214   QVariant aVarData;
215   aVarData.setValue<ImageComposer_Image>( anImage );
216   
217   return aVarData;
218 }
219
220 HYDROData_SequenceOfObjects HYDROData_Image::GetAllReferenceObjects() const
221 {
222   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
223
224   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
225   if ( !aRefImage.IsNull() )
226     aResSeq.Append( aRefImage );
227
228   HYDROData_SequenceOfObjects aSeqRefObjects = GetReferenceObjects( 0 );
229   aResSeq.Append( aSeqRefObjects );
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   TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
288   if ( !aLabel.IsNull() )
289   {
290     Handle(TDataStd_AsciiString) anAsciiStr;
291     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
292       aRes = QString( anAsciiStr->Get().ToCString() );
293   }
294
295   return aRes;
296 }
297
298 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
299 {
300   // locate 9 coeffs of matrix into the real array
301   Handle(TDataStd_RealArray) anArray;
302   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
303     if (theTrsf.isIdentity()) return; // no need to store identity transformation
304     anArray = TDataStd_RealArray::Set(myLab, 1, 9);
305   }
306   anArray->SetValue(1, theTrsf.m11());
307   anArray->SetValue(2, theTrsf.m12());
308   anArray->SetValue(3, theTrsf.m13());
309   anArray->SetValue(4, theTrsf.m21());
310   anArray->SetValue(5, theTrsf.m22());
311   anArray->SetValue(6, theTrsf.m23());
312   anArray->SetValue(7, theTrsf.m31());
313   anArray->SetValue(8, theTrsf.m32());
314   anArray->SetValue(9, theTrsf.m33());
315 }
316
317 QTransform HYDROData_Image::Trsf() const
318 {
319   // get 9 coeffs of matrix from the real array
320   Handle(TDataStd_RealArray) anArray;
321   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
322     return QTransform(); // return identity if there is no array
323   QTransform aTrsf(
324     anArray->Value(1), anArray->Value(2), anArray->Value(3), 
325     anArray->Value(4), anArray->Value(5), anArray->Value(6), 
326     anArray->Value(7), anArray->Value(8), anArray->Value(9));
327   return aTrsf;
328 }
329
330 void HYDROData_Image::UpdateTrsf()
331 {
332   QPoint aPointA, aPointB, aPointC;
333   if ( !GetLocalPoints( aPointA, aPointB, aPointC ) )
334     return;
335
336   TransformationMode aTrsfMode;
337   QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
338   if ( !GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
339     return;
340
341   QTransform aRefTransform;
342   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
343
344   bool anIsRefImage = aTrsfMode == ReferenceImage;
345   if ( anIsRefImage )
346   {
347     if ( aRefImage.IsNull() )
348       return;
349
350     aRefTransform = aRefImage->Trsf();
351   }
352
353   bool anIsByTwoPoints = IsByTwoPoints();
354
355   // Convert lambert coordinates to cartesian
356   if ( aTrsfMode == ManualGeodesic )
357   {
358     double aXCart = 0, aYCart = 0;
359
360     HYDROData_Lambert93::toXY( aTrsfPointA.y(), aTrsfPointA.x(), aXCart, aYCart );
361     aTrsfPointA = QPointF( aXCart, aYCart );
362
363     HYDROData_Lambert93::toXY( aTrsfPointB.y(), aTrsfPointB.x(), aXCart, aYCart );
364     aTrsfPointB = QPointF( aXCart, aYCart );
365
366     if ( !anIsByTwoPoints )
367     {
368       HYDROData_Lambert93::toXY( aTrsfPointC.y(), aTrsfPointC.x(), aXCart, aYCart );
369       aTrsfPointC = QPointF( aXCart, aYCart );
370     }
371   }
372
373   // generate third points if needed
374   if ( anIsByTwoPoints )
375   {
376     aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
377     aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
378   }
379
380   int xa = aPointA.x();
381   int ya = aPointA.y();
382   int xb = aPointB.x();
383   int yb = aPointB.y();
384   int xc = aPointC.x();
385   int yc = aPointC.y();
386
387   double xta = aTrsfPointA.x();
388   double yta = aTrsfPointA.y();
389   double xtb = aTrsfPointB.x();
390   double ytb = aTrsfPointB.y();
391   double xtc = aTrsfPointC.x();
392   double ytc = aTrsfPointC.y();
393
394   // first, check that three input points don't belong to a single line
395   if( ( yb - ya ) * ( xc - xa ) == ( yc - ya ) * ( xb - xa ) )
396     return;
397
398   // the same check for the reference points
399   if( anIsRefImage && ( ( ytb - yta ) * ( xtc - xta ) == ( ytc - yta ) * ( xtb - xta ) ) )
400     return;
401
402   QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
403   QTransform aTransform2( xta, yta, 1, xtb, ytb, 1, xtc, ytc, 1 );
404
405   bool anIsInvertible = false;
406   QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
407   if( !anIsInvertible )
408     return;
409
410   QTransform aResTransform = aTransform1Inverted * aTransform2;
411   if( anIsRefImage )
412     aResTransform *= aRefTransform;
413
414   SetTrsf( aResTransform );
415 }
416
417 bool HYDROData_Image::IsByTwoPoints() const
418 {
419   if ( !HasLocalPoints() || !HasGlobalPoints() )
420     return false;
421
422   QPoint aPointA, aPointB, aPointC;
423   GetLocalPoints( aPointA, aPointB, aPointC );
424
425   return aPointC.x() < 0 && aPointC.y() < 0; 
426 }
427
428 bool HYDROData_Image::HasReferences() const
429 {
430   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
431   int aNbReferences = NbReferences();
432
433   return !aRefImage.IsNull() || aNbReferences > 0;
434 }
435
436 void HYDROData_Image::RemoveAllReferences()
437 {
438   if ( !HasReferences() )
439     return;
440
441   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
442   if ( !aRefImage.IsNull() )
443   {
444     RemoveTrsfReferenceImage();
445   }
446   else
447   {
448     ClearReferences();
449     SetOperatorName( "" );
450     SetArgs( "" );
451     SetIsSelfSplitted( false );
452   }
453
454   SetToUpdate( false );
455
456   bool anIsByTwoPoints = IsByTwoPoints();
457
458   QImage anImage = Image();
459   if ( anImage.isNull() )
460     return;
461
462   // Set local points to default position
463   QPoint aLocalPointA = QPoint( 0, 0 );
464   QPoint aLocalPointB = QPoint( anImage.width(), 0 );
465   QPoint aLocalPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) : QPoint( 0, anImage.height() );
466
467   SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
468
469   // Calculate global points
470   QTransform aTransform = Trsf();
471
472   QPointF aTrsfPointA = QPointF( aTransform.map( aLocalPointA ) );
473   QPointF aTrsfPointB = QPointF( aTransform.map( aLocalPointB ) );
474   QPointF aTrsfPointC = anIsByTwoPoints ? QPointF( INT_MIN, INT_MIN ) : 
475                                           QPointF( aTransform.map( aLocalPointC ) );
476
477   SetGlobalPoints( ManualCartesian, aTrsfPointA, aTrsfPointB, aTrsfPointC );
478 }
479
480 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
481                                       const QPoint& thePointB,
482                                       const QPoint& thePointC,
483                                       const bool    theIsUpdate )
484 {
485   Handle(TDataStd_RealArray) anArray;
486   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
487     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
488
489   anArray->SetValue( 1, thePointA.x() );
490   anArray->SetValue( 2, thePointA.y() );
491   anArray->SetValue( 3, thePointB.x() );
492   anArray->SetValue( 4, thePointB.y() );
493   anArray->SetValue( 5, thePointC.x() );
494   anArray->SetValue( 6, thePointC.y() );
495
496   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
497
498   if ( theIsUpdate )
499     UpdateTrsf();
500 }
501
502 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
503                                       QPoint& thePointB,
504                                       QPoint& thePointC ) const
505 {
506   if ( !HasLocalPoints() )
507     return false;
508
509   Handle(TDataStd_RealArray) anArray;
510   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
511
512   thePointA = QPointF( anArray->Value( 1 ), anArray->Value( 2 ) ).toPoint();
513   thePointB = QPointF( anArray->Value( 3 ), anArray->Value( 4 ) ).toPoint();
514   thePointC = QPointF( anArray->Value( 5 ), anArray->Value( 6 ) ).toPoint();
515
516   return true;
517 }
518
519 bool HYDROData_Image::HasLocalPoints() const
520 {
521   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
522   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_LOCAL_POINTS ) )
523     return false;
524
525   Handle(TDataStd_RealArray) anArray;
526   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
527 }
528
529
530 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
531                                        const QPointF&            thePointA,
532                                        const QPointF&            thePointB,
533                                        const QPointF&            thePointC,
534                                        const bool                theIsUpdate )
535 {
536   Handle(TDataStd_RealArray) anArray;
537   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
538     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
539
540   anArray->SetValue( 7,  thePointA.x() );
541   anArray->SetValue( 8,  thePointA.y() );
542   anArray->SetValue( 9,  thePointB.x() );
543   anArray->SetValue( 10, thePointB.y() );
544   anArray->SetValue( 11, thePointC.x() );
545   anArray->SetValue( 12, thePointC.y() );
546   
547   SetTrsfMode( theMode );
548
549   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
550
551   if ( theIsUpdate )
552     UpdateTrsf();
553
554   /*
555   if( anIsRefImage )
556   {
557     aCPointA = QPointF( aTransform.map( aPointA ) );
558     aCPointB = QPointF( aTransform.map( aPointB ) );
559     aCPointC = QPointF( aTransform.map( aPointC ) );
560
561     // compute Lambert93 points
562     xca = aCPointA.x();
563     yca = aCPointA.y();
564     xcb = aCPointB.x();
565     ycb = aCPointB.y();
566     xcc = aCPointC.x();
567     ycc = aCPointC.y();
568
569     double xla = 0, yla = 0, xlb = 0, ylb = 0, xlc = 0, ylc = 0;
570     HYDROData_Lambert93::toGeo( xca, yca, yla, xla );
571     HYDROData_Lambert93::toGeo( xcb, ycb, ylb, xlb );
572     HYDROData_Lambert93::toGeo( xcc, ycc, ylc, xlc );
573
574     aLPointA = QPointF( xla * 3600.0, yla * 3600.0 ); // convert degrees to seconds
575     aLPointB = QPointF( xlb * 3600.0, ylb * 3600.0 ); // convert degrees to seconds
576     aLPointC = QPointF( xlc * 3600.0, ylc * 3600.0 ); // convert degrees to seconds
577   }
578   */
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 }
657
658 HYDROData_Image::TransformationMode HYDROData_Image::GetTrsfMode() const
659 {
660   TransformationMode aResMode = ManualGeodesic;
661
662   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
663   if ( !aLabel.IsNull() )
664   {
665     Handle(TDataStd_Integer) aMode;
666     if ( myLab.FindChild( DataTag_TrsfMode ).FindAttribute( TDataStd_Integer::GetID(), aMode ) )
667       aResMode = (TransformationMode)aMode->Get();
668   }
669
670   return aResMode;
671 }
672
673 void HYDROData_Image::SetTrsfReferenceImage( const Handle(HYDROData_Image)& theRefImage )
674 {
675   SetReferenceObject( theRefImage, DataTag_TrsfImage );
676 }
677
678 Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
679 {
680   return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
681 }
682
683 void HYDROData_Image::RemoveTrsfReferenceImage()
684 {
685   return RemoveReferenceObject( DataTag_TrsfImage );
686 }
687
688 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
689 {
690   AddReferenceObject( theReferenced, 0 );
691 }
692
693 int HYDROData_Image::NbReferences() const
694 {
695   return NbReferenceObjects( 0 );
696 }
697
698 Handle(HYDROData_Entity) HYDROData_Image::Reference( const int theIndex ) const
699 {
700   return GetReferenceObject( 0, theIndex );
701 }
702
703 void HYDROData_Image::ChangeReference(
704     const int theIndex, Handle(HYDROData_Entity) theReferenced)
705 {
706   SetReferenceObject( theReferenced, 0, theIndex );
707 }
708
709 void HYDROData_Image::RemoveReference(const int theIndex)
710 {
711   RemoveReferenceObject( 0, theIndex );
712 }
713
714 void HYDROData_Image::ClearReferences()
715 {
716   ClearReferenceObjects( 0 );
717 }
718
719 void HYDROData_Image::SetOperatorName( const QString theOpName )
720 {
721   TCollection_AsciiString anAsciiStr( theOpName.toStdString().c_str() );
722   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_Operator ), anAsciiStr );
723 }
724
725 QString HYDROData_Image::OperatorName() const
726 {
727   QString aRes;
728
729   TDF_Label aLabel = myLab.FindChild( DataTag_Operator, false );
730   if ( !aLabel.IsNull() )
731   {
732     Handle(TDataStd_AsciiString) anAsciiStr;
733     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
734       aRes = QString( anAsciiStr->Get().ToCString() );
735   }
736
737   return aRes;
738 }
739
740 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
741 {
742   SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
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 }
762
763 bool HYDROData_Image::IsSelfSplitted() const
764 {
765   return myLab.IsAttribute(GUID_SELF_SPLITTED);
766 }
767
768 QPointF HYDROData_Image::generateThirdPoint( const QPointF& thePointA,
769                                              const QPointF& thePointB,
770                                              const bool&    theIsLocal ) const
771 {
772   // Rotate vector to 90 degrees : clockwise - for local
773   //                               anticlockwise - for global
774   const double aTheta = theIsLocal ? -M_PI_2 : M_PI_2;
775
776   QPointF aResPoint;
777
778   // Move to (0,0) for correct rotation
779   double x = thePointB.x() - thePointA.x();
780   double y = thePointB.y() - thePointA.y();
781
782   aResPoint.setX( x * cos( aTheta ) - y * sin( aTheta ) );
783   aResPoint.setY( x * sin( aTheta ) + y * cos( aTheta ) );
784
785   // Move back to origin position
786   aResPoint.setX( aResPoint.x() + thePointA.x() );
787   aResPoint.setY( aResPoint.y() + thePointA.y() );
788
789   return aResPoint;
790 }
791