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