Salome HOME
compilation on Linux
[modules/hydro.git] / src / HYDROData / HYDROData_Image.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROData_Image.h"
24
25 #include "HYDROData_Document.h"
26 #include "HYDROData_Lambert93.h"
27 #include "HYDROData_OperationsFactory.h"
28
29 #include <TDataStd_RealArray.hxx>
30 #include <TDataStd_ByteArray.hxx>
31 #include <TDataStd_Integer.hxx>
32 #include <TDataStd_IntegerArray.hxx>
33 #include <TDataStd_ReferenceList.hxx>
34 #include <TDataStd_UAttribute.hxx>
35 #include <TDataStd_AsciiString.hxx>
36
37 #include <ImageComposer_Operator.h>
38 #include <ImageComposer_MetaTypes.h>
39
40 #include <QStringList>
41 #include <QFile>
42
43 #include <boost/math/special_functions/fpclassify.hpp>
44
45 static const Standard_GUID GUID_SELF_SPLITTED("997995aa-5c19-40bf-9a60-ab4b70ad04d8");
46 static const Standard_GUID GUID_HAS_LOCAL_POINTS("FD8841AA-FC44-42fa-B6A7-0F682CCC6F27");
47 static const Standard_GUID GUID_HAS_GLOBAL_POINTS("330D0E81-742D-4ea3-92D4-484877CFA7C1");
48
49 IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Entity)
50 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Entity)
51
52 HYDROData_Image::HYDROData_Image()
53 : HYDROData_Entity()
54 {
55 }
56
57 HYDROData_Image::~HYDROData_Image()
58 {
59 }
60
61 QStringList HYDROData_Image::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
62 {
63   QStringList aResList = dumpObjectCreation( theTreatedObjects );
64   QString anImageName = GetObjPyName();
65
66   QString aFilePath = GetFilePath();
67   if ( !aFilePath.isEmpty() )
68   {
69     aResList << QString( "" );
70     aResList << QString( "%1.LoadImage( \"%2\" );" )
71                 .arg( anImageName ).arg( aFilePath );
72     aResList << QString( "" );
73
74     // Dump transformation points for image
75
76     bool anIsByTwoPoints = IsByTwoPoints();
77
78     QPoint aLocalPointA, aLocalPointB, aLocalPointC;
79     if ( GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC ) )
80     {
81       QString aGap = QString().fill( ' ', anImageName.size() + 17 );
82
83       aResList << QString( "%1.SetLocalPoints( QPoint( %2, %3 )," )
84                   .arg( anImageName ).arg( aLocalPointA.x() ).arg( aLocalPointA.y() );
85       aResList << QString( aGap             + "QPoint( %1, %2 )" )
86                   .arg( aLocalPointB.x() ).arg( aLocalPointB.y() );
87       if ( !anIsByTwoPoints )
88       {
89         aResList.last().append( "," );
90         aResList << QString( aGap             +  "QPoint( %1, %2 ) );" )
91                     .arg( aLocalPointC.x() ).arg( aLocalPointC.y() );
92       }
93       else
94       {
95         aResList.last().append( " );" );
96       }
97       aResList << QString( "" );
98     }
99
100     HYDROData_Image::TransformationMode aTransformationMode;
101     QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
102     if ( GetGlobalPoints( aTransformationMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
103     {
104       QString aGap = QString().fill( ' ', anImageName.size() + 18 );
105
106       aResList << QString( "%1.SetGlobalPoints( %2," )
107                   .arg( anImageName ).arg( aTransformationMode );
108       aResList << QString( aGap             +  "QPointF( %1, %2 )," )
109                   .arg( aTrsfPointA.x() ).arg( aTrsfPointA.y() );
110       aResList << QString( aGap             +  "QPointF( %1, %2 )" )
111                   .arg( aTrsfPointB.x() ).arg( aTrsfPointB.y() );
112       if ( !anIsByTwoPoints )
113       {
114         aResList.last().append( "," );
115         aResList << QString( aGap             +  "QPointF( %1, %2 ) );" )
116                     .arg( aTrsfPointC.x() ).arg( aTrsfPointC.y() );
117       }
118       else
119       {
120         aResList.last().append( " );" );
121       }
122
123       if ( aTransformationMode == ReferenceImage )
124       {
125         Handle(HYDROData_Image) aRefImg = GetTrsfReferenceImage();
126         setPythonReferenceObject( theTreatedObjects, aResList, aRefImg, "SetTrsfReferenceImage" );
127       }
128     }
129   }
130   else
131   {
132     // Image is composed from other image(s)
133
134     QString anOperatorName = OperatorName();
135     if ( !anOperatorName.isEmpty() )
136     {
137       aResList << QString( "" );
138
139       aResList << QString( "%1.SetOperatorName( \"%2\" );" )
140                   .arg( anImageName ).arg( anOperatorName );
141
142       ImageComposer_Operator* anImageOp = 
143         HYDROData_OperationsFactory::Factory()->Operator( OperatorName() );
144       if ( anImageOp )
145       {
146         // Dump operation arguments
147         QString anOpArgsArrayName;
148         QStringList anOpArgs = anImageOp->dumpArgsToPython( anOpArgsArrayName );
149         if ( !anOpArgs.isEmpty() )
150         {
151           aResList << QString( "" );
152           aResList << anOpArgs;
153
154           aResList << QString( "" );
155           aResList << QString( "%1.SetArgs( %2 );" )
156                       .arg( anImageName ).arg( anOpArgsArrayName );
157         }
158       }
159     }
160     
161     int aNbReferences = NbReferences();
162     if ( aNbReferences > 0 )
163     {
164       aResList << QString( "" );
165
166       for ( int i = 0; i < aNbReferences; ++i )
167       {
168         Handle(HYDROData_Image) aRefImg = Handle(HYDROData_Image)::DownCast( Reference( i ) );
169         setPythonReferenceObject( theTreatedObjects, aResList, aRefImg, "AppendReference" );
170       }
171     }
172   }
173
174   aResList << QString( "" );
175   aResList << QString( "%1.Update();" ).arg( anImageName );
176
177   return aResList;
178 }
179
180 void HYDROData_Image::Update()
181 {
182   bool anIsToUpdate = IsMustBeUpdated();
183
184   HYDROData_Entity::Update();
185
186   if ( !anIsToUpdate )
187     return;
188
189   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
190
191   ImageComposer_Operator* anOp = aFactory->Operator( OperatorName() );
192   if ( anOp ) // Update image if there is an operation
193   {
194     // Fill by arguments and process the operation
195     anOp->setBinArgs( Args() );
196
197     QVariant anObj1, anObj2;
198     int aNbReferences = NbReferences();
199
200     if ( aNbReferences > 0 )
201     {
202       // First referenced object
203       Handle(HYDROData_Entity) aRefObj = Reference( 0 );
204       if ( !aRefObj.IsNull() )
205       {
206         anObj1 = aRefObj->GetDataVariant();
207         if ( !anObj1.isNull() && anObj1.canConvert<ImageComposer_Image>() )
208         {
209           ImageComposer_Image anImage = anObj1.value<ImageComposer_Image>();
210           QTransform aTransform = anImage.transform();
211           SetTrsf( aTransform );
212         }
213       }
214     }
215
216     if ( aNbReferences > 1 )
217     {
218       // Second referenced object
219       Handle(HYDROData_Entity) aRefObj = Reference( 1 );
220       if ( !aRefObj.IsNull() )
221         anObj2 = aRefObj->GetDataVariant();
222     }
223
224     ImageComposer_Image aResImg = anOp->process( anObj1, anObj2 );
225     SetImage( aResImg );
226     SetTrsf( aResImg.transform() );
227   }
228   else // Update image if it positioned relatively to other image
229   {
230     UpdateTrsf();
231   }
232
233   SetToUpdate( false );
234 }
235
236 bool HYDROData_Image::IsHas2dPrs() const
237 {
238   return true;
239 }
240
241 QVariant HYDROData_Image::GetDataVariant()
242 {
243   QTransform aTransform = Trsf();
244
245   ImageComposer_Image anImage = Image();
246   anImage.setTransform( aTransform );
247
248   QVariant aVarData;
249   aVarData.setValue<ImageComposer_Image>( anImage );
250   
251   return aVarData;
252 }
253
254 HYDROData_SequenceOfObjects HYDROData_Image::GetAllReferenceObjects() const
255 {
256   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
257
258   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
259   if ( !aRefImage.IsNull() )
260     aResSeq.Append( aRefImage );
261
262   HYDROData_SequenceOfObjects aSeqRefObjects = GetReferenceObjects( 0 );
263   aResSeq.Append( aSeqRefObjects );
264
265   return aResSeq;
266 }
267
268 void HYDROData_Image::SetImage(const QImage& theImage)
269 {
270   if ( theImage.isNull() )
271   {
272     // for empty image remove all previously stored attributes
273     myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
274     myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
275   }
276   else
277   {
278     QImage anImage;
279
280     // convert 8-bits images
281     if ( theImage.format() == QImage::Format_Indexed8 ) {
282       anImage = theImage.convertToFormat( QImage::Format_RGB32 );
283     } else {
284       anImage = theImage;
285     }
286
287     // store width, height, bytes per line and format in integer array 
288     Handle(TDataStd_IntegerArray) aParams;
289     if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
290       aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
291     }
292     aParams->SetValue(1, anImage.width());
293     aParams->SetValue(2, anImage.height());
294     aParams->SetValue(3, anImage.bytesPerLine());
295     aParams->SetValue(4, (int)(anImage.format()));
296     // store data of image in byte array
297     const char* aData = (const char*)(anImage.bits());
298     SaveByteArray(0, aData, anImage.byteCount());
299   }
300
301   SetToUpdate( true );
302 }
303
304 bool HYDROData_Image::LoadImage( const QString& theFilePath )
305 {
306   QImage anImage( theFilePath );
307   SetImage( anImage );
308
309   SetFilePath( theFilePath );
310
311   return !anImage.isNull();
312 }
313
314 QImage HYDROData_Image::Image()
315 {
316   Handle(TDataStd_IntegerArray) aParams;
317   if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
318     return QImage(); // return empty image if there is no array
319   int aLen = 0;
320   uchar* anArray = (uchar*)ByteArray(0, aLen);
321   if (!aLen)
322     return QImage(); // return empty image if there is no array
323   QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
324                  aParams->Value(3), QImage::Format(aParams->Value(4)));
325   return aResult;
326 }
327
328 void HYDROData_Image::SetFilePath( const QString& theFilePath )
329 {
330   TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
331   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
332
333   SetToUpdate( true );
334 }
335
336 QString HYDROData_Image::GetFilePath() const
337 {
338   QString aRes;
339
340   TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
341   if ( !aLabel.IsNull() )
342   {
343     Handle(TDataStd_AsciiString) anAsciiStr;
344     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
345       aRes = QString( anAsciiStr->Get().ToCString() );
346   }
347
348   return aRes;
349 }
350
351 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
352 {
353   // locate 9 coeffs of matrix into the real array
354   Handle(TDataStd_RealArray) anArray;
355   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
356     if (theTrsf.isIdentity()) return; // no need to store identity transformation
357     anArray = TDataStd_RealArray::Set(myLab, 1, 9);
358   }
359   anArray->SetValue(1, theTrsf.m11());
360   anArray->SetValue(2, theTrsf.m12());
361   anArray->SetValue(3, theTrsf.m13());
362   anArray->SetValue(4, theTrsf.m21());
363   anArray->SetValue(5, theTrsf.m22());
364   anArray->SetValue(6, theTrsf.m23());
365   anArray->SetValue(7, theTrsf.m31());
366   anArray->SetValue(8, theTrsf.m32());
367   anArray->SetValue(9, theTrsf.m33());
368
369   SetToUpdate( true );
370 }
371
372 QTransform HYDROData_Image::Trsf() const
373 {
374   // get 9 coeffs of matrix from the real array
375   Handle(TDataStd_RealArray) anArray;
376   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
377     return QTransform(); // return identity if there is no array
378   QTransform aTrsf(
379     anArray->Value(1), anArray->Value(2), anArray->Value(3), 
380     anArray->Value(4), anArray->Value(5), anArray->Value(6), 
381     anArray->Value(7), anArray->Value(8), anArray->Value(9));
382   return aTrsf;
383 }
384
385 void HYDROData_Image::UpdateTrsf()
386 {
387   QPoint aPointA, aPointB, aPointC;
388   if ( !GetLocalPoints( aPointA, aPointB, aPointC ) )
389     return;
390
391   TransformationMode aTrsfMode;
392   QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
393   if ( !GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
394     return;
395
396   QTransform aRefTransform;
397   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
398
399   bool anIsRefImage = aTrsfMode == ReferenceImage;
400   if ( anIsRefImage )
401   {
402     if ( aRefImage.IsNull() )
403       return;
404
405     aRefTransform = aRefImage->Trsf();
406   }
407
408   bool anIsByTwoPoints = IsByTwoPoints();
409
410   // Convert lambert coordinates to cartesian
411   if ( aTrsfMode == ManualGeodesic )
412   {
413     double aXCart = 0, aYCart = 0;
414
415     HYDROData_Lambert93::toXY( aTrsfPointA.y(), aTrsfPointA.x(), aXCart, aYCart );
416     aTrsfPointA = QPointF( aXCart, aYCart );
417
418     HYDROData_Lambert93::toXY( aTrsfPointB.y(), aTrsfPointB.x(), aXCart, aYCart );
419     aTrsfPointB = QPointF( aXCart, aYCart );
420
421     if ( !anIsByTwoPoints )
422     {
423       HYDROData_Lambert93::toXY( aTrsfPointC.y(), aTrsfPointC.x(), aXCart, aYCart );
424       aTrsfPointC = QPointF( aXCart, aYCart );
425     }
426   }
427
428   // generate third points if needed
429   if ( anIsByTwoPoints )
430   {
431     aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
432     aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
433   }
434
435   int xa = aPointA.x();
436   int ya = aPointA.y();
437   int xb = aPointB.x();
438   int yb = aPointB.y();
439   int xc = aPointC.x();
440   int yc = aPointC.y();
441
442   double xta = aTrsfPointA.x();
443   double yta = aTrsfPointA.y();
444   double xtb = aTrsfPointB.x();
445   double ytb = aTrsfPointB.y();
446   double xtc = aTrsfPointC.x();
447   double ytc = aTrsfPointC.y();
448
449   // first, check that three input points don't belong to a single line
450   if( ( yb - ya ) * ( xc - xa ) == ( yc - ya ) * ( xb - xa ) )
451     return;
452
453   // the same check for the reference points
454   if( anIsRefImage && ( ( ytb - yta ) * ( xtc - xta ) == ( ytc - yta ) * ( xtb - xta ) ) )
455     return;
456
457   QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
458   QTransform aTransform2( xta, yta, 1, xtb, ytb, 1, xtc, ytc, 1 );
459
460   bool anIsInvertible = false;
461   QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
462   if( !anIsInvertible )
463     return;
464
465   QTransform aResTransform = aTransform1Inverted * aTransform2;
466   if( anIsRefImage )
467     aResTransform *= aRefTransform;
468
469   SetTrsf( aResTransform );
470 }
471
472 bool HYDROData_Image::IsByTwoPoints() const
473 {
474   if ( !HasLocalPoints() || !HasGlobalPoints() )
475     return false;
476
477   QPoint aPointA, aPointB, aPointC;
478   GetLocalPoints( aPointA, aPointB, aPointC );
479
480   return aPointC.x() < 0 && aPointC.y() < 0; 
481 }
482
483 bool HYDROData_Image::HasReferences() const
484 {
485   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
486   int aNbReferences = NbReferences();
487
488   return !aRefImage.IsNull() || aNbReferences > 0;
489 }
490
491 void HYDROData_Image::RemoveAllReferences()
492 {
493   if ( !HasReferences() )
494     return;
495
496   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
497   if ( !aRefImage.IsNull() )
498   {
499     RemoveTrsfReferenceImage();
500   }
501   else
502   {
503     ClearReferences();
504     SetOperatorName( "" );
505     SetArgs( "" );
506     SetIsSelfSplitted( false );
507   }
508
509   bool anIsByTwoPoints = IsByTwoPoints();
510
511   QImage anImage = Image();
512   if ( anImage.isNull() )
513   {
514     SetToUpdate( false );
515     return;
516   }
517
518   // Set local points to default position
519   QPoint aLocalPointA = QPoint( 0, 0 );
520   QPoint aLocalPointB = QPoint( anImage.width(), 0 );
521   QPoint aLocalPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) : QPoint( 0, anImage.height() );
522
523   SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
524
525   // Calculate global points
526   QTransform aTransform = Trsf();
527
528   QPointF aTrsfPointA = QPointF( aTransform.map( aLocalPointA ) );
529   QPointF aTrsfPointB = QPointF( aTransform.map( aLocalPointB ) );
530   QPointF aTrsfPointC = anIsByTwoPoints ? QPointF( INT_MIN, INT_MIN ) : 
531                                           QPointF( aTransform.map( aLocalPointC ) );
532
533   SetGlobalPoints( ManualCartesian, aTrsfPointA, aTrsfPointB, aTrsfPointC );
534
535   SetToUpdate( false );
536 }
537
538 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
539                                       const QPoint& thePointB,
540                                       const QPoint& thePointC,
541                                       const bool    theIsUpdate )
542 {
543   Handle(TDataStd_RealArray) anArray;
544   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
545     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
546
547   anArray->SetValue( 1, thePointA.x() );
548   anArray->SetValue( 2, thePointA.y() );
549   anArray->SetValue( 3, thePointB.x() );
550   anArray->SetValue( 4, thePointB.y() );
551   anArray->SetValue( 5, thePointC.x() );
552   anArray->SetValue( 6, thePointC.y() );
553
554   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
555
556   if ( theIsUpdate )
557     UpdateTrsf();
558
559   SetToUpdate( true );
560 }
561
562 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
563                                       QPoint& thePointB,
564                                       QPoint& thePointC ) const
565 {
566   if ( !HasLocalPoints() )
567     return false;
568
569   Handle(TDataStd_RealArray) anArray;
570   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
571
572   thePointA = QPointF( anArray->Value( 1 ), anArray->Value( 2 ) ).toPoint();
573   thePointB = QPointF( anArray->Value( 3 ), anArray->Value( 4 ) ).toPoint();
574   thePointC = QPointF( anArray->Value( 5 ), anArray->Value( 6 ) ).toPoint();
575
576   return true;
577 }
578
579 bool HYDROData_Image::HasLocalPoints() const
580 {
581   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
582   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_LOCAL_POINTS ) )
583     return false;
584
585   Handle(TDataStd_RealArray) anArray;
586   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
587 }
588
589
590 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
591                                        const QPointF&            thePointA,
592                                        const QPointF&            thePointB,
593                                        const QPointF&            thePointC,
594                                        const bool                theIsUpdate )
595 {
596   Handle(TDataStd_RealArray) anArray;
597   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
598     anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
599
600   anArray->SetValue( 7,  thePointA.x() );
601   anArray->SetValue( 8,  thePointA.y() );
602   anArray->SetValue( 9,  thePointB.x() );
603   anArray->SetValue( 10, thePointB.y() );
604   anArray->SetValue( 11, thePointC.x() );
605   anArray->SetValue( 12, thePointC.y() );
606   
607   SetTrsfMode( theMode );
608
609   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
610
611   if ( theIsUpdate )
612     UpdateTrsf();
613
614   SetToUpdate( true );
615 }
616
617 bool HYDROData_Image::GetGlobalPoints( TransformationMode& theMode,
618                                        QPointF&            thePointA,
619                                        QPointF&            thePointB,
620                                        QPointF&            thePointC ) const
621 {
622   if ( !HasGlobalPoints() )
623     return false;
624
625   theMode = GetTrsfMode();
626
627   Handle(TDataStd_RealArray) anArray;
628   myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
629
630   thePointA = QPointF( anArray->Value( 7  ), anArray->Value( 8  ) );
631   thePointB = QPointF( anArray->Value( 9  ), anArray->Value( 10 ) );
632   thePointC = QPointF( anArray->Value( 11 ), anArray->Value( 12 ) );
633
634   return true;
635 }
636
637 bool HYDROData_Image::SetGlobalPointsFromFile( const QString& theFileName )
638 {
639   bool aRes = false;
640
641   // Try to open the file
642   QFile aFile( theFileName );
643   if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) ) {
644     return aRes;
645   }
646
647   QPointF aPointA, aPointB;
648   double aXmin, anYmin, aXmax, anYmax;
649   aXmin = anYmin = aXmax = anYmax = -1;
650
651   while ( !aFile.atEnd() && 
652           ( aXmin < 0 || anYmin < 0 || aXmax < 0 || anYmax < 0 ) ) {
653     // Read line
654     QString aLine = aFile.readLine().simplified();
655     aLine.replace( " ", "" );
656     if ( aLine.isEmpty() ) {
657       continue;
658     }
659
660     // Try to read double value after ":"
661     bool isDoubleOk = false;
662     double aDoubleValue = -1;
663     QStringList aValues = aLine.split( ":", QString::SkipEmptyParts );
664     if ( aValues.count() == 2 ) {
665       aDoubleValue = aValues.last().toDouble( &isDoubleOk );
666     }
667
668     // Check the result
669     if ( !isDoubleOk ||
670          boost::math::isnan( aDoubleValue ) ||
671          boost::math::isinf( aDoubleValue ) ) {
672       continue;
673     }
674
675     // Set the value
676     if ( aLine.startsWith( "Xminimum" ) ) {
677       aXmin = aDoubleValue;    
678     } 
679     else if ( aLine.startsWith( "Yminimum" ) ) {
680       anYmin = aDoubleValue; 
681     }
682     else if ( aLine.startsWith( "Xmaximum" ) ) {
683       aXmax = aDoubleValue;
684     }
685     else if ( aLine.startsWith( "Ymaximum" ) ) {
686       anYmax = aDoubleValue;  
687     }
688   }
689
690   // Close the file
691   aFile.close();
692
693   if ( aXmin >= 0 && anYmin >= 0 ) {
694     aPointA.setX( aXmin );
695     aPointA.setY( anYmin );
696   }
697     
698   if ( aXmax >= 0 && anYmax >= 0 ) {
699     aPointB.setX( aXmax );
700     aPointB.setY( anYmax );
701   }
702
703   if ( !aPointA.isNull() && !aPointB.isNull() ) {
704     SetGlobalPoints( ManualCartesian, aPointA, aPointB );
705     aRes = true;
706   }
707
708   return aRes;
709 }
710
711 bool HYDROData_Image::HasGlobalPoints() const
712 {
713   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
714   if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_GLOBAL_POINTS ) )
715     return false;
716
717   Handle(TDataStd_RealArray) anArray;
718   return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
719 }
720
721 void HYDROData_Image::SetReferencePoints( const Handle(HYDROData_Image)& theRefImage,
722                                           const QPointF&                 thePointA,
723                                           const QPointF&                 thePointB,
724                                           const QPointF&                 thePointC,
725                                           const bool                     theIsUpdate )
726 {
727   SetTrsfReferenceImage( theRefImage );
728   SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC, theIsUpdate );
729 }
730
731 bool HYDROData_Image::GetReferencePoints( Handle(HYDROData_Image)& theRefImage,
732                                           QPointF&                 thePointA,
733                                           QPointF&                 thePointB,
734                                           QPointF&                 thePointC ) const
735 {
736   if ( !HasReferencePoints() )
737     return false;
738
739   theRefImage = GetTrsfReferenceImage();
740
741   TransformationMode aMode;
742   GetGlobalPoints( aMode, thePointA, thePointB, thePointC );
743
744   return true;
745 }
746
747 bool HYDROData_Image::HasReferencePoints() const
748 {
749   if ( !HasGlobalPoints() )
750     return false;
751
752   Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
753   if ( aRefImage.IsNull() )
754     return false;
755
756   TransformationMode aTrsfMode = GetTrsfMode();
757   if ( aTrsfMode != ReferenceImage )
758     return false;
759
760   return true;
761 }
762
763 void HYDROData_Image::SetTrsfMode( const TransformationMode& theMode )
764 {
765   TDataStd_Integer::Set( myLab.FindChild( DataTag_TrsfMode ), (int)theMode );
766   SetToUpdate( true );
767 }
768
769 HYDROData_Image::TransformationMode HYDROData_Image::GetTrsfMode() const
770 {
771   TransformationMode aResMode = ManualGeodesic;
772
773   TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
774   if ( !aLabel.IsNull() )
775   {
776     Handle(TDataStd_Integer) aMode;
777     if ( myLab.FindChild( DataTag_TrsfMode ).FindAttribute( TDataStd_Integer::GetID(), aMode ) )
778       aResMode = (TransformationMode)aMode->Get();
779   }
780
781   return aResMode;
782 }
783
784 void HYDROData_Image::SetTrsfReferenceImage( const Handle(HYDROData_Image)& theRefImage )
785 {
786   SetReferenceObject( theRefImage, DataTag_TrsfImage );
787   SetToUpdate( true );
788 }
789
790 Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
791 {
792   return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
793 }
794
795 void HYDROData_Image::RemoveTrsfReferenceImage()
796 {
797   RemoveReferenceObject( DataTag_TrsfImage );
798   SetToUpdate( true );
799 }
800
801 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
802 {
803   AddReferenceObject( theReferenced, 0 );
804   SetToUpdate( true );
805 }
806
807 int HYDROData_Image::NbReferences() const
808 {
809   return NbReferenceObjects( 0 );
810 }
811
812 Handle(HYDROData_Entity) HYDROData_Image::Reference( const int theIndex ) const
813 {
814   return GetReferenceObject( 0, theIndex );
815 }
816
817 void HYDROData_Image::ChangeReference(
818     const int theIndex, Handle(HYDROData_Entity) theReferenced)
819 {
820   SetReferenceObject( theReferenced, 0, theIndex );
821   SetToUpdate( true );
822 }
823
824 void HYDROData_Image::RemoveReference(const int theIndex)
825 {
826   RemoveReferenceObject( 0, theIndex );
827   SetToUpdate( true );
828 }
829
830 void HYDROData_Image::ClearReferences()
831 {
832   ClearReferenceObjects( 0 );
833   SetToUpdate( true );
834 }
835
836 void HYDROData_Image::SetOperatorName( const QString theOpName )
837 {
838   TCollection_AsciiString anAsciiStr( theOpName.toStdString().c_str() );
839   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_Operator ), anAsciiStr );
840   SetToUpdate( true );
841 }
842
843 QString HYDROData_Image::OperatorName() const
844 {
845   QString aRes;
846
847   TDF_Label aLabel = myLab.FindChild( DataTag_Operator, false );
848   if ( !aLabel.IsNull() )
849   {
850     Handle(TDataStd_AsciiString) anAsciiStr;
851     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
852       aRes = QString( anAsciiStr->Get().ToCString() );
853   }
854
855   return aRes;
856 }
857
858 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
859 {
860   SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
861   SetToUpdate( true );
862 }
863
864 QByteArray HYDROData_Image::Args() const
865 {
866   int aLen = 0;
867   const char* aData = ByteArray(DataTag_Operator, aLen);
868   if (!aLen)
869     return QByteArray();
870   return QByteArray(aData, aLen);
871 }
872
873 void HYDROData_Image::SetIsSelfSplitted(bool theFlag)
874 {
875   if (theFlag) {
876     TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLITTED);
877   } else {
878     myLab.ForgetAttribute(GUID_SELF_SPLITTED);
879   }
880   SetToUpdate( true );
881 }
882
883 bool HYDROData_Image::IsSelfSplitted() const
884 {
885   return myLab.IsAttribute(GUID_SELF_SPLITTED);
886 }
887
888 QPointF HYDROData_Image::generateThirdPoint( const QPointF& thePointA,
889                                              const QPointF& thePointB,
890                                              const bool&    theIsLocal ) const
891 {
892   // Rotate vector to 90 degrees : clockwise - for local
893   //                               counterclockwise - for global
894   const double aTheta = theIsLocal ? -M_PI_2 : M_PI_2;
895
896   QPointF aResPoint;
897
898   // Move to (0,0) for correct rotation
899   double x = thePointB.x() - thePointA.x();
900   double y = thePointB.y() - thePointA.y();
901
902   aResPoint.setX( x * cos( aTheta ) - y * sin( aTheta ) );
903   aResPoint.setY( x * sin( aTheta ) + y * cos( aTheta ) );
904
905   // Move back to origin position
906   aResPoint.setX( aResPoint.x() + thePointA.x() );
907   aResPoint.setY( aResPoint.y() + thePointA.y() );
908
909   return aResPoint;
910 }
911