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