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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_Image.h"
21 #include "HYDROData_Document.h"
22 #include "HYDROData_Lambert93.h"
23 #include "HYDROData_OperationsFactory.h"
24 #include "HYDROData_Tool.h"
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>
38 #pragma warning ( disable: 4251 )
41 #include <ImageComposer_Operator.h>
42 #include <ImageComposer_MetaTypes.h>
44 #include <QStringList>
49 #pragma warning ( default: 4251 )
52 static const Standard_GUID GUID_SELF_SPLIT("997995aa-5c19-40bf-9a60-ab4b70ad04d8");
53 static const Standard_GUID GUID_HAS_LOCAL_POINTS("FD8841AA-FC44-42fa-B6A7-0F682CCC6F27");
54 static const Standard_GUID GUID_HAS_GLOBAL_POINTS("330D0E81-742D-4ea3-92D4-484877CFA7C1");
56 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Entity)
58 HYDROData_Image::HYDROData_Image()
59 : HYDROData_Entity( Geom_2d )
63 HYDROData_Image::~HYDROData_Image()
67 QStringList HYDROData_Image::DumpToPython( const QString& thePyScriptPath,
68 MapOfTreatedObjects& theTreatedObjects ) const
70 QStringList aResList = dumpObjectCreation( theTreatedObjects );
71 QString anImageName = GetObjPyName();
73 QString aFilePath = GetFilePath();
74 if ( !aFilePath.isEmpty() )
76 aResList << QString( "" );
77 aResList << QString( "if not(%1.LoadImage( \"%2\" )):" )
78 .arg( anImageName ).arg( aFilePath );
79 aResList << QString( " raise ValueError('problem while loading image')" );
80 aResList << QString( "" );
82 // Dump transformation points for image
84 bool anIsByTwoPoints = IsByTwoPoints();
86 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
87 if ( GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC ) )
89 QString aGap = QString().fill( ' ', anImageName.size() + 17 );
91 aResList << QString( "%1.SetLocalPoints( QPoint( %2, %3 )," )
92 .arg( anImageName ).arg( aLocalPointA.x() ).arg( aLocalPointA.y() );
93 aResList << QString( aGap + "QPoint( %1, %2 )" )
94 .arg( aLocalPointB.x() ).arg( aLocalPointB.y() );
95 if ( !anIsByTwoPoints )
97 aResList.last().append( "," );
98 aResList << QString( aGap + "QPoint( %1, %2 ) )" )
99 .arg( aLocalPointC.x() ).arg( aLocalPointC.y() );
103 aResList.last().append( " )" );
105 aResList << QString( "" );
108 HYDROData_Image::TransformationMode aTransformationMode;
109 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
110 if ( GetGlobalPoints( aTransformationMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
112 QString aGap = QString().fill( ' ', anImageName.size() + 18 );
114 aResList << QString( "%1.SetGlobalPoints( %2," )
115 .arg( anImageName ).arg( aTransformationMode );
116 aResList << QString( aGap + "QPointF( %1, %2 )," )
117 .arg( aTrsfPointA.x(), 0, 'f', 3 ).arg( aTrsfPointA.y(), 0, 'f', 3 );
118 aResList << QString( aGap + "QPointF( %1, %2 )" )
119 .arg( aTrsfPointB.x(), 0, 'f', 3 ).arg( aTrsfPointB.y(), 0, 'f', 3 );
120 if ( !anIsByTwoPoints )
122 aResList.last().append( "," );
123 aResList << QString( aGap + "QPointF( %1, %2 ) )" )
124 .arg( aTrsfPointC.x(), 0, 'f', 3 ).arg( aTrsfPointC.y(), 0, 'f', 3 );
128 aResList.last().append( " )" );
131 if ( aTransformationMode == ReferenceImage )
133 Handle(HYDROData_Image) aRefImg = GetTrsfReferenceImage();
134 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefImg, "SetTrsfReferenceImage" );
140 // Image is composed from other image(s)
142 QString anOperatorName = OperatorName();
143 if ( !anOperatorName.isEmpty() )
145 aResList << QString( "" );
147 aResList << QString( "%1.SetOperatorName( \"%2\" )" )
148 .arg( anImageName ).arg( anOperatorName );
150 ImageComposer_Operator* anImageOp =
151 HYDROData_OperationsFactory::Factory()->Operator( OperatorName() );
154 // Dump operation arguments
155 QString anOpArgsArrayName;
156 QStringList anOpArgs = anImageOp->dumpArgsToPython( anOpArgsArrayName );
157 if ( !anOpArgs.isEmpty() )
159 aResList << QString( "" );
160 aResList << anOpArgs;
162 aResList << QString( "" );
163 aResList << QString( "%1.SetArgs( %2 )" )
164 .arg( anImageName ).arg( anOpArgsArrayName );
169 int aNbReferences = NbReferences();
170 if ( aNbReferences > 0 )
172 aResList << QString( "" );
174 for ( int i = 0; i < aNbReferences; ++i )
176 Handle(HYDROData_Image) aRefImg = Handle(HYDROData_Image)::DownCast( Reference( i ) );
177 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefImg, "AppendReference" );
182 aResList << QString( "" );
183 aResList << QString( "%1.Update()" ).arg( anImageName );
188 void HYDROData_Image::Update()
190 bool anIsToUpdate = IsMustBeUpdated( Geom_2d );
192 HYDROData_Entity::Update();
197 HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
199 ImageComposer_Operator* anOp = aFactory->Operator( OperatorName() );
200 if ( anOp ) // Update image if there is an operation
202 // Fill by arguments and process the operation
203 anOp->setBinArgs( Args() );
205 QVariant anObj1, anObj2;
206 int aNbReferences = NbReferences();
208 if ( aNbReferences > 0 )
210 // First referenced object
211 Handle(HYDROData_Entity) aRefObj = Reference( 0 );
212 if ( !aRefObj.IsNull() )
214 anObj1 = aRefObj->GetDataVariant();
215 if ( !anObj1.isNull() && anObj1.canConvert<ImageComposer_Image>() )
217 ImageComposer_Image anImage = anObj1.value<ImageComposer_Image>();
218 QTransform aTransform = anImage.transform();
219 SetTrsf( aTransform );
224 if ( aNbReferences > 1 )
226 // Second referenced object
227 Handle(HYDROData_Entity) aRefObj = Reference( 1 );
228 if ( !aRefObj.IsNull() )
229 anObj2 = aRefObj->GetDataVariant();
232 ImageComposer_Image aResImg = anOp->process( anObj1, anObj2 );
234 SetTrsf( aResImg.transform() );
236 else // Update image if it positioned relatively to other image
244 bool HYDROData_Image::IsHas2dPrs() const
249 QVariant HYDROData_Image::GetDataVariant()
251 QTransform aTransform = Trsf();
253 ImageComposer_Image anImage = Image();
254 anImage.setTransform( aTransform );
257 aVarData.setValue<ImageComposer_Image>( anImage );
262 HYDROData_SequenceOfObjects HYDROData_Image::GetAllReferenceObjects() const
264 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
266 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
267 if ( !aRefImage.IsNull() )
268 aResSeq.Append( aRefImage );
270 HYDROData_SequenceOfObjects aSeqRefObjects = GetReferenceObjects( 0 );
271 aResSeq.Append( aSeqRefObjects );
276 void HYDROData_Image::SetImage(const QImage& theImage)
278 if ( theImage.isNull() )
280 // for empty image remove all previously stored attributes
281 myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
282 myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
288 // convert 8-bits images
289 if ( theImage.format() == QImage::Format_Indexed8 ) {
290 anImage = theImage.convertToFormat( QImage::Format_RGB32 );
295 // store width, height, bytes per line and format in integer array
296 Handle(TDataStd_IntegerArray) aParams;
297 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
298 aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
300 aParams->SetValue(1, anImage.width());
301 aParams->SetValue(2, anImage.height());
302 aParams->SetValue(3, anImage.bytesPerLine());
303 aParams->SetValue(4, (int)(anImage.format()));
304 // store data of image in byte array
305 const char* aData = (const char*)(anImage.bits());
306 SaveByteArray(0, aData, anImage.byteCount());
312 bool HYDROData_Image::LoadImage( const QString& theFilePath )
314 QFileInfo aFI(theFilePath);
316 HYDROData_Image::ECW_FileInfo* theECWInfo;
317 if (aFI.suffix().toLower() == "ecw")
319 theECWInfo = new HYDROData_Image::ECW_FileInfo;
320 HYDROData_Image::OpenECW(theFilePath.toLatin1().data(), anImage, theECWInfo);
323 anImage = QImage(theFilePath);
325 SetFilePath( theFilePath );
326 return !anImage.isNull();
329 bool HYDROData_Image::OpenECW(char* theFileName, QImage& theImage, ECW_FileInfo* theECWInfo)
331 NCSFileView *pNCSFileView;
332 NCSFileViewFileInfo *pNCSFileInfo;
333 NCSError eError = NCS_SUCCESS;
338 eError = NCScbmOpenFileView(theFileName, &pNCSFileView, NULL);
339 if(eError != NCS_SUCCESS)
342 eError = NCScbmGetViewFileInfo(pNCSFileView, &pNCSFileInfo);
343 if(eError != NCS_SUCCESS)
346 XSize = pNCSFileInfo->nSizeX;
347 YSize = pNCSFileInfo->nSizeY;
348 nBands = pNCSFileInfo->nBands;
351 //ECW_CellUnits myCellSizeUnits;
352 CellSizeUnits aCellUnits = pNCSFileInfo->eCellSizeUnits;
353 if (aCellUnits == ECW_CELL_UNITS_METERS)
354 theECWInfo->myCellSizeUnits = ECW_CellUnits_Meters;
355 else if (aCellUnits == ECW_CELL_UNITS_DEGREES)
356 theECWInfo->myCellSizeUnits = ECW_CellUnits_Deg;
357 else if (aCellUnits == ECW_CELL_UNITS_FEET)
358 theECWInfo->myCellSizeUnits = ECW_CellUnits_Feet;
360 theECWInfo->myCellSizeUnits = ECW_CellUnits_Unknown;
361 theECWInfo->myCellIncrementX = pNCSFileInfo->fCellIncrementX;
362 theECWInfo->myCellIncrementY = pNCSFileInfo->fCellIncrementY;
363 theECWInfo->myOriginX = pNCSFileInfo->fOriginX;
364 theECWInfo->myOriginY = pNCSFileInfo->fOriginY;
365 theECWInfo->myXSize = pNCSFileInfo->nSizeX;
366 theECWInfo->myYSize = pNCSFileInfo->nSizeY;
369 std::vector<UINT32> band_list(nBands);
370 for( band = 0; band < nBands; band++ )
371 band_list[band] = band;
373 eError = NCScbmSetFileView(pNCSFileView, nBands, &band_list[0], 0, 0, XSize - 1, YSize - 1, XSize, YSize); //view an image into the original size
375 if(eError != NCS_SUCCESS)
377 NCScbmCloseFileView(pNCSFileView);
382 pRGBTriplets = (UINT8 *) malloc(XSize*3);
384 QImage anImage(XSize, YSize, QImage::Format_RGB32);
386 for(UINT32 line = 0; line < YSize; line++)
388 NCSEcwReadStatus eStatus;
389 eStatus = NCScbmReadViewLineRGB(pNCSFileView, pRGBTriplets);
390 if(eStatus == NCSECW_READ_OK)
392 QRgb* crp = (QRgb*)anImage.scanLine(line);
393 for(UINT32 j = 0; j < XSize; j++)
395 QRgb val = qRgb((int)pRGBTriplets[j*3],(int)pRGBTriplets[j*3+1],(int)pRGBTriplets[j*3+2]);
396 memcpy((void*)(crp+j), &val, sizeof(QRgb));
402 NCScbmCloseFileView(pNCSFileView);
408 NCScbmCloseFileView(pNCSFileView);
413 bool HYDROData_Image::LoadImageECW( const QString& theFilePath )
416 if (HYDROData_Image::OpenECW(theFilePath.toLatin1().data(), anImage, NULL))
419 SetFilePath( theFilePath );
421 return !anImage.isNull();
424 QImage HYDROData_Image::Image()
426 Handle(TDataStd_IntegerArray) aParams;
427 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
428 return QImage(); // return empty image if there is no array
430 uchar* anArray = (uchar*)ByteArray(0, aLen);
432 return QImage(); // return empty image if there is no array
433 QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
434 aParams->Value(3), QImage::Format(aParams->Value(4)));
438 void HYDROData_Image::SetFilePath( const QString& theFilePath )
440 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
441 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
446 QString HYDROData_Image::GetFilePath() const
450 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
451 if ( !aLabel.IsNull() )
453 Handle(TDataStd_AsciiString) anAsciiStr;
454 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
455 aRes = QString( anAsciiStr->Get().ToCString() );
461 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
463 // locate 9 coeffs of matrix into the real array
464 Handle(TDataStd_RealArray) anArray;
465 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
466 if (theTrsf.isIdentity()) return; // no need to store identity transformation
467 anArray = TDataStd_RealArray::Set(myLab, 1, 9);
469 anArray->SetValue(1, theTrsf.m11());
470 anArray->SetValue(2, theTrsf.m12());
471 anArray->SetValue(3, theTrsf.m13());
472 anArray->SetValue(4, theTrsf.m21());
473 anArray->SetValue(5, theTrsf.m22());
474 anArray->SetValue(6, theTrsf.m23());
475 anArray->SetValue(7, theTrsf.m31());
476 anArray->SetValue(8, theTrsf.m32());
477 anArray->SetValue(9, theTrsf.m33());
482 QTransform HYDROData_Image::Trsf() const
484 // get 9 coeffs of matrix from the real array
485 Handle(TDataStd_RealArray) anArray;
486 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
487 return QTransform(); // return identity if there is no array
489 anArray->Value(1), anArray->Value(2), anArray->Value(3),
490 anArray->Value(4), anArray->Value(5), anArray->Value(6),
491 anArray->Value(7), anArray->Value(8), anArray->Value(9));
495 void HYDROData_Image::UpdateTrsf()
497 QPoint aPointA, aPointB, aPointC;
498 if ( !GetLocalPoints( aPointA, aPointB, aPointC ) )
501 TransformationMode aTrsfMode;
502 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
503 if ( !GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
506 QTransform aRefTransform;
507 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
509 bool anIsRefImage = aTrsfMode == ReferenceImage;
512 if ( aRefImage.IsNull() )
515 aRefTransform = aRefImage->Trsf();
518 bool anIsByTwoPoints = IsByTwoPoints();
520 // Convert lambert coordinates to cartesian
521 if ( aTrsfMode == ManualGeodesic )
523 double aXCart = 0, aYCart = 0;
525 HYDROData_Lambert93::toXY( aTrsfPointA.y(), aTrsfPointA.x(), aXCart, aYCart );
526 aTrsfPointA = QPointF( aXCart, aYCart );
528 HYDROData_Lambert93::toXY( aTrsfPointB.y(), aTrsfPointB.x(), aXCart, aYCart );
529 aTrsfPointB = QPointF( aXCart, aYCart );
531 if ( !anIsByTwoPoints )
533 HYDROData_Lambert93::toXY( aTrsfPointC.y(), aTrsfPointC.x(), aXCart, aYCart );
534 aTrsfPointC = QPointF( aXCart, aYCart );
538 // generate third points if needed
539 if ( anIsByTwoPoints )
541 aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
542 aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
545 int xa = aPointA.x();
546 int ya = aPointA.y();
547 int xb = aPointB.x();
548 int yb = aPointB.y();
549 int xc = aPointC.x();
550 int yc = aPointC.y();
552 double xta = aTrsfPointA.x();
553 double yta = aTrsfPointA.y();
554 double xtb = aTrsfPointB.x();
555 double ytb = aTrsfPointB.y();
556 double xtc = aTrsfPointC.x();
557 double ytc = aTrsfPointC.y();
559 // first, check that three input points don't belong to a single line
560 if( ( yb - ya ) * ( xc - xa ) == ( yc - ya ) * ( xb - xa ) )
563 // the same check for the reference points
564 if( anIsRefImage && ( ( ytb - yta ) * ( xtc - xta ) == ( ytc - yta ) * ( xtb - xta ) ) )
567 QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
568 QTransform aTransform2( xta, yta, 1, xtb, ytb, 1, xtc, ytc, 1 );
570 bool anIsInvertible = false;
571 QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
572 if( !anIsInvertible )
575 QTransform aResTransform = aTransform1Inverted * aTransform2;
577 aResTransform *= aRefTransform;
579 SetTrsf( aResTransform );
582 bool HYDROData_Image::IsByTwoPoints() const
584 if ( !HasLocalPoints() || !HasGlobalPoints() )
587 QPoint aPointA, aPointB, aPointC;
588 GetLocalPoints( aPointA, aPointB, aPointC );
590 return aPointC.x() < 0 && aPointC.y() < 0;
593 bool HYDROData_Image::HasReferences() const
595 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
596 int aNbReferences = NbReferences();
598 return !aRefImage.IsNull() || aNbReferences > 0;
601 void HYDROData_Image::RemoveAllReferences()
603 if ( !HasReferences() )
606 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
607 if ( !aRefImage.IsNull() )
609 RemoveTrsfReferenceImage();
614 SetOperatorName( "" );
616 SetIsSelfSplit( false );
619 bool anIsByTwoPoints = IsByTwoPoints();
621 QImage anImage = Image();
622 if ( anImage.isNull() )
628 // Set local points to default position
629 QPoint aLocalPointA = QPoint( 0, 0 );
630 QPoint aLocalPointB = QPoint( anImage.width(), 0 );
631 QPoint aLocalPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) : QPoint( 0, anImage.height() );
633 SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
635 // Calculate global points
636 QTransform aTransform = Trsf();
638 QPointF aTrsfPointA = QPointF( aTransform.map( aLocalPointA ) );
639 QPointF aTrsfPointB = QPointF( aTransform.map( aLocalPointB ) );
640 QPointF aTrsfPointC = anIsByTwoPoints ? QPointF( INT_MIN, INT_MIN ) :
641 QPointF( aTransform.map( aLocalPointC ) );
643 SetGlobalPoints( ManualCartesian, aTrsfPointA, aTrsfPointB, aTrsfPointC );
648 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
649 const QPoint& thePointB,
650 const QPoint& thePointC,
651 const bool theIsUpdate )
653 Handle(TDataStd_RealArray) anArray;
654 if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
655 anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
657 anArray->SetValue( 1, thePointA.x() );
658 anArray->SetValue( 2, thePointA.y() );
659 anArray->SetValue( 3, thePointB.x() );
660 anArray->SetValue( 4, thePointB.y() );
661 anArray->SetValue( 5, thePointC.x() );
662 anArray->SetValue( 6, thePointC.y() );
664 TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
672 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
674 QPoint& thePointC ) const
676 if ( !HasLocalPoints() )
679 Handle(TDataStd_RealArray) anArray;
680 myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
682 thePointA = QPointF( anArray->Value( 1 ), anArray->Value( 2 ) ).toPoint();
683 thePointB = QPointF( anArray->Value( 3 ), anArray->Value( 4 ) ).toPoint();
684 thePointC = QPointF( anArray->Value( 5 ), anArray->Value( 6 ) ).toPoint();
689 bool HYDROData_Image::HasLocalPoints() const
691 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
692 if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_LOCAL_POINTS ) )
695 Handle(TDataStd_RealArray) anArray;
696 return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
700 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
701 const QPointF& thePointA,
702 const QPointF& thePointB,
703 const QPointF& thePointC,
704 const bool theIsUpdate )
706 Handle(TDataStd_RealArray) anArray;
707 if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
708 anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
710 anArray->SetValue( 7, thePointA.x() );
711 anArray->SetValue( 8, thePointA.y() );
712 anArray->SetValue( 9, thePointB.x() );
713 anArray->SetValue( 10, thePointB.y() );
714 anArray->SetValue( 11, thePointC.x() );
715 anArray->SetValue( 12, thePointC.y() );
717 SetTrsfMode( theMode );
719 TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
727 bool HYDROData_Image::GetGlobalPoints( TransformationMode& theMode,
730 QPointF& thePointC ) const
732 if ( !HasGlobalPoints() )
735 theMode = GetTrsfMode();
737 Handle(TDataStd_RealArray) anArray;
738 myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
740 thePointA = QPointF( anArray->Value( 7 ), anArray->Value( 8 ) );
741 thePointB = QPointF( anArray->Value( 9 ), anArray->Value( 10 ) );
742 thePointC = QPointF( anArray->Value( 11 ), anArray->Value( 12 ) );
747 bool HYDROData_Image::SetGlobalPointsFromFile( const QString& theFileName )
751 // Try to open the file
752 QFile aFile( theFileName );
753 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) ) {
757 QPointF aPointA, aPointB;
758 double aXmin, anYmin, aXmax, anYmax;
759 aXmin = anYmin = aXmax = anYmax = -1;
761 while ( !aFile.atEnd() &&
762 ( aXmin < 0 || anYmin < 0 || aXmax < 0 || anYmax < 0 ) ) {
764 QString aLine = aFile.readLine().simplified();
765 aLine.replace( " ", "" );
766 if ( aLine.isEmpty() ) {
770 // Try to read double value after ":"
771 bool isDoubleOk = false;
772 double aDoubleValue = -1;
773 QStringList aValues = aLine.split( ":", QString::SkipEmptyParts );
774 if ( aValues.count() == 2 ) {
775 aDoubleValue = aValues.last().toDouble( &isDoubleOk );
780 HYDROData_Tool::IsNan( aDoubleValue ) ||
781 HYDROData_Tool::IsInf( aDoubleValue ) ) {
786 if ( aLine.startsWith( "Xminimum" ) ) {
787 aXmin = aDoubleValue;
789 else if ( aLine.startsWith( "Yminimum" ) ) {
790 anYmin = aDoubleValue;
792 else if ( aLine.startsWith( "Xmaximum" ) ) {
793 aXmax = aDoubleValue;
795 else if ( aLine.startsWith( "Ymaximum" ) ) {
796 anYmax = aDoubleValue;
803 if ( aXmin >= 0 && anYmin >= 0 ) {
804 aPointA.setX( aXmin );
805 aPointA.setY( anYmin );
808 if ( aXmax >= 0 && anYmax >= 0 ) {
809 aPointB.setX( aXmax );
810 aPointB.setY( anYmax );
813 if ( !aPointA.isNull() && !aPointB.isNull() ) {
814 SetGlobalPoints( ManualCartesian, aPointA, aPointB );
821 bool HYDROData_Image::HasGlobalPoints() const
823 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
824 if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_GLOBAL_POINTS ) )
827 Handle(TDataStd_RealArray) anArray;
828 return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
831 void HYDROData_Image::SetReferencePoints( const Handle(HYDROData_Image)& theRefImage,
832 const QPointF& thePointA,
833 const QPointF& thePointB,
834 const QPointF& thePointC,
835 const bool theIsUpdate )
837 SetTrsfReferenceImage( theRefImage );
838 SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC, theIsUpdate );
841 bool HYDROData_Image::GetReferencePoints( Handle(HYDROData_Image)& theRefImage,
844 QPointF& thePointC ) const
846 if ( !HasReferencePoints() )
849 theRefImage = GetTrsfReferenceImage();
851 TransformationMode aMode;
852 GetGlobalPoints( aMode, thePointA, thePointB, thePointC );
857 bool HYDROData_Image::HasReferencePoints() const
859 if ( !HasGlobalPoints() )
862 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
863 if ( aRefImage.IsNull() )
866 TransformationMode aTrsfMode = GetTrsfMode();
867 if ( aTrsfMode != ReferenceImage )
873 void HYDROData_Image::SetTrsfMode( const TransformationMode& theMode )
875 TDataStd_Integer::Set( myLab.FindChild( DataTag_TrsfMode ), (int)theMode );
879 HYDROData_Image::TransformationMode HYDROData_Image::GetTrsfMode() const
881 TransformationMode aResMode = ManualGeodesic;
883 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
884 if ( !aLabel.IsNull() )
886 Handle(TDataStd_Integer) aMode;
887 if ( myLab.FindChild( DataTag_TrsfMode ).FindAttribute( TDataStd_Integer::GetID(), aMode ) )
888 aResMode = (TransformationMode)aMode->Get();
894 void HYDROData_Image::SetTrsfReferenceImage( const Handle(HYDROData_Image)& theRefImage )
896 SetReferenceObject( theRefImage, DataTag_TrsfImage );
900 Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
902 return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
905 void HYDROData_Image::RemoveTrsfReferenceImage()
907 RemoveReferenceObject( DataTag_TrsfImage );
911 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
913 AddReferenceObject( theReferenced, 0 );
917 int HYDROData_Image::NbReferences() const
919 return NbReferenceObjects( 0 );
922 Handle(HYDROData_Entity) HYDROData_Image::Reference( const int theIndex ) const
924 return GetReferenceObject( 0, theIndex );
927 void HYDROData_Image::ChangeReference(
928 const int theIndex, Handle(HYDROData_Entity) theReferenced)
930 SetReferenceObject( theReferenced, 0, theIndex );
934 void HYDROData_Image::RemoveReference(const int theIndex)
936 RemoveReferenceObject( 0, theIndex );
940 void HYDROData_Image::ClearReferences()
942 ClearReferenceObjects( 0 );
946 void HYDROData_Image::SetOperatorName( const QString theOpName )
948 TCollection_AsciiString anAsciiStr( theOpName.toStdString().c_str() );
949 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_Operator ), anAsciiStr );
953 QString HYDROData_Image::OperatorName() const
957 TDF_Label aLabel = myLab.FindChild( DataTag_Operator, false );
958 if ( !aLabel.IsNull() )
960 Handle(TDataStd_AsciiString) anAsciiStr;
961 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
962 aRes = QString( anAsciiStr->Get().ToCString() );
968 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
970 SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
974 QByteArray HYDROData_Image::Args() const
977 const char* aData = ByteArray(DataTag_Operator, aLen);
980 return QByteArray(aData, aLen);
983 void HYDROData_Image::SetIsSelfSplit(bool theFlag)
986 TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLIT);
988 myLab.ForgetAttribute(GUID_SELF_SPLIT);
993 bool HYDROData_Image::IsSelfSplit() const
995 return myLab.IsAttribute(GUID_SELF_SPLIT);
998 QPointF HYDROData_Image::generateThirdPoint( const QPointF& thePointA,
999 const QPointF& thePointB,
1000 const bool& theIsLocal ) const
1002 // Rotate vector to 90 degrees : clockwise - for local
1003 // counterclockwise - for global
1004 const double aTheta = theIsLocal ? -M_PI_2 : M_PI_2;
1008 // Move to (0,0) for correct rotation
1009 double x = thePointB.x() - thePointA.x();
1010 double y = thePointB.y() - thePointA.y();
1012 aResPoint.setX( x * cos( aTheta ) - y * sin( aTheta ) );
1013 aResPoint.setY( x * sin( aTheta ) + y * cos( aTheta ) );
1015 // Move back to origin position
1016 aResPoint.setX( aResPoint.x() + thePointA.x() );
1017 aResPoint.setY( aResPoint.y() + thePointA.y() );