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>
48 #pragma warning ( default: 4251 )
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");
55 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Entity)
57 HYDROData_Image::HYDROData_Image()
58 : HYDROData_Entity( Geom_2d )
62 HYDROData_Image::~HYDROData_Image()
66 QStringList HYDROData_Image::DumpToPython( const QString& thePyScriptPath,
67 MapOfTreatedObjects& theTreatedObjects ) const
69 QStringList aResList = dumpObjectCreation( theTreatedObjects );
70 QString anImageName = GetObjPyName();
72 QString aFilePath = GetFilePath();
73 if ( !aFilePath.isEmpty() )
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( "" );
81 // Dump transformation points for image
83 bool anIsByTwoPoints = IsByTwoPoints();
85 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
86 if ( GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC ) )
88 QString aGap = QString().fill( ' ', anImageName.size() + 17 );
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 )
96 aResList.last().append( "," );
97 aResList << QString( aGap + "QPoint( %1, %2 ) )" )
98 .arg( aLocalPointC.x() ).arg( aLocalPointC.y() );
102 aResList.last().append( " )" );
104 aResList << QString( "" );
107 HYDROData_Image::TransformationMode aTransformationMode;
108 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
109 if ( GetGlobalPoints( aTransformationMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
111 QString aGap = QString().fill( ' ', anImageName.size() + 18 );
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 )
121 aResList.last().append( "," );
122 aResList << QString( aGap + "QPointF( %1, %2 ) )" )
123 .arg( aTrsfPointC.x(), 0, 'f', 3 ).arg( aTrsfPointC.y(), 0, 'f', 3 );
127 aResList.last().append( " )" );
130 if ( aTransformationMode == ReferenceImage )
132 Handle(HYDROData_Image) aRefImg = GetTrsfReferenceImage();
133 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefImg, "SetTrsfReferenceImage" );
139 // Image is composed from other image(s)
141 QString anOperatorName = OperatorName();
142 if ( !anOperatorName.isEmpty() )
144 aResList << QString( "" );
146 aResList << QString( "%1.SetOperatorName( \"%2\" )" )
147 .arg( anImageName ).arg( anOperatorName );
149 ImageComposer_Operator* anImageOp =
150 HYDROData_OperationsFactory::Factory()->Operator( OperatorName() );
153 // Dump operation arguments
154 QString anOpArgsArrayName;
155 QStringList anOpArgs = anImageOp->dumpArgsToPython( anOpArgsArrayName );
156 if ( !anOpArgs.isEmpty() )
158 aResList << QString( "" );
159 aResList << anOpArgs;
161 aResList << QString( "" );
162 aResList << QString( "%1.SetArgs( %2 )" )
163 .arg( anImageName ).arg( anOpArgsArrayName );
168 int aNbReferences = NbReferences();
169 if ( aNbReferences > 0 )
171 aResList << QString( "" );
173 for ( int i = 0; i < aNbReferences; ++i )
175 Handle(HYDROData_Image) aRefImg = Handle(HYDROData_Image)::DownCast( Reference( i ) );
176 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefImg, "AppendReference" );
181 aResList << QString( "" );
182 aResList << QString( "%1.Update()" ).arg( anImageName );
187 void HYDROData_Image::Update()
189 bool anIsToUpdate = IsMustBeUpdated( Geom_2d );
191 HYDROData_Entity::Update();
196 HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
198 ImageComposer_Operator* anOp = aFactory->Operator( OperatorName() );
199 if ( anOp ) // Update image if there is an operation
201 // Fill by arguments and process the operation
202 anOp->setBinArgs( Args() );
204 QVariant anObj1, anObj2;
205 int aNbReferences = NbReferences();
207 if ( aNbReferences > 0 )
209 // First referenced object
210 Handle(HYDROData_Entity) aRefObj = Reference( 0 );
211 if ( !aRefObj.IsNull() )
213 anObj1 = aRefObj->GetDataVariant();
214 if ( !anObj1.isNull() && anObj1.canConvert<ImageComposer_Image>() )
216 ImageComposer_Image anImage = anObj1.value<ImageComposer_Image>();
217 QTransform aTransform = anImage.transform();
218 SetTrsf( aTransform );
223 if ( aNbReferences > 1 )
225 // Second referenced object
226 Handle(HYDROData_Entity) aRefObj = Reference( 1 );
227 if ( !aRefObj.IsNull() )
228 anObj2 = aRefObj->GetDataVariant();
231 ImageComposer_Image aResImg = anOp->process( anObj1, anObj2 );
233 SetTrsf( aResImg.transform() );
235 else // Update image if it positioned relatively to other image
243 bool HYDROData_Image::IsHas2dPrs() const
248 QVariant HYDROData_Image::GetDataVariant()
250 QTransform aTransform = Trsf();
252 ImageComposer_Image anImage = Image();
253 anImage.setTransform( aTransform );
256 aVarData.setValue<ImageComposer_Image>( anImage );
261 HYDROData_SequenceOfObjects HYDROData_Image::GetAllReferenceObjects() const
263 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
265 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
266 if ( !aRefImage.IsNull() )
267 aResSeq.Append( aRefImage );
269 HYDROData_SequenceOfObjects aSeqRefObjects = GetReferenceObjects( 0 );
270 aResSeq.Append( aSeqRefObjects );
275 void HYDROData_Image::SetImage(const QImage& theImage)
277 if ( theImage.isNull() )
279 // for empty image remove all previously stored attributes
280 myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
281 myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
287 // convert 8-bits images
288 if ( theImage.format() == QImage::Format_Indexed8 ) {
289 anImage = theImage.convertToFormat( QImage::Format_RGB32 );
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);
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());
311 bool HYDROData_Image::LoadImage( const QString& theFilePath )
313 QImage anImage( theFilePath );
315 SetFilePath( theFilePath );
316 return !anImage.isNull(); //TODO open ecw from this func!
319 bool HYDROData_Image::OpenECW(char* theFileName, QImage& theImage, ECW_FileInfo* theECWInfo)
321 NCSFileView *pNCSFileView;
322 NCSFileViewFileInfo *pNCSFileInfo;
323 NCSError eError = NCS_SUCCESS;
328 eError = NCScbmOpenFileView(theFileName, &pNCSFileView, NULL);
329 if(eError != NCS_SUCCESS)
332 eError = NCScbmGetViewFileInfo(pNCSFileView, &pNCSFileInfo);
333 if(eError != NCS_SUCCESS)
336 XSize = pNCSFileInfo->nSizeX;
337 YSize = pNCSFileInfo->nSizeY;
338 nBands = pNCSFileInfo->nBands;
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;
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;
359 std::vector<UINT32> band_list(nBands);
360 for( band = 0; band < nBands; band++ )
361 band_list[band] = band;
363 eError = NCScbmSetFileView(pNCSFileView, nBands, &band_list[0], 0, 0, XSize - 1, YSize - 1, XSize, YSize); //view an image into the original size
365 if(eError != NCS_SUCCESS)
367 NCScbmCloseFileView(pNCSFileView);
372 pRGBTriplets = (UINT8 *) malloc(XSize*3);
374 QImage anImage(XSize, YSize, QImage::Format_RGB32);
376 for(UINT32 line = 0; line < YSize; line++)
378 NCSEcwReadStatus eStatus;
379 eStatus = NCScbmReadViewLineRGB(pNCSFileView, pRGBTriplets);
380 if(eStatus == NCSECW_READ_OK)
382 QRgb* crp = (QRgb*)anImage.scanLine(line);
383 for(UINT32 j = 0; j < XSize; j++)
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));
392 NCScbmCloseFileView(pNCSFileView);
398 NCScbmCloseFileView(pNCSFileView);
402 bool HYDROData_Image::LoadImageECW( const QString& theFilePath )
405 if (HYDROData_Image::OpenECW(theFilePath.toLatin1().data(), anImage, NULL))
408 SetFilePath( theFilePath );
410 return !anImage.isNull();
413 QImage HYDROData_Image::Image()
415 Handle(TDataStd_IntegerArray) aParams;
416 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
417 return QImage(); // return empty image if there is no array
419 uchar* anArray = (uchar*)ByteArray(0, 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)));
427 void HYDROData_Image::SetFilePath( const QString& theFilePath )
429 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
430 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
435 QString HYDROData_Image::GetFilePath() const
439 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
440 if ( !aLabel.IsNull() )
442 Handle(TDataStd_AsciiString) anAsciiStr;
443 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
444 aRes = QString( anAsciiStr->Get().ToCString() );
450 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
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);
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());
471 QTransform HYDROData_Image::Trsf() const
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
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));
484 void HYDROData_Image::UpdateTrsf()
486 QPoint aPointA, aPointB, aPointC;
487 if ( !GetLocalPoints( aPointA, aPointB, aPointC ) )
490 TransformationMode aTrsfMode;
491 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
492 if ( !GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
495 QTransform aRefTransform;
496 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
498 bool anIsRefImage = aTrsfMode == ReferenceImage;
501 if ( aRefImage.IsNull() )
504 aRefTransform = aRefImage->Trsf();
507 bool anIsByTwoPoints = IsByTwoPoints();
509 // Convert lambert coordinates to cartesian
510 if ( aTrsfMode == ManualGeodesic )
512 double aXCart = 0, aYCart = 0;
514 HYDROData_Lambert93::toXY( aTrsfPointA.y(), aTrsfPointA.x(), aXCart, aYCart );
515 aTrsfPointA = QPointF( aXCart, aYCart );
517 HYDROData_Lambert93::toXY( aTrsfPointB.y(), aTrsfPointB.x(), aXCart, aYCart );
518 aTrsfPointB = QPointF( aXCart, aYCart );
520 if ( !anIsByTwoPoints )
522 HYDROData_Lambert93::toXY( aTrsfPointC.y(), aTrsfPointC.x(), aXCart, aYCart );
523 aTrsfPointC = QPointF( aXCart, aYCart );
527 // generate third points if needed
528 if ( anIsByTwoPoints )
530 aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
531 aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
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();
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();
548 // first, check that three input points don't belong to a single line
549 if( ( yb - ya ) * ( xc - xa ) == ( yc - ya ) * ( xb - xa ) )
552 // the same check for the reference points
553 if( anIsRefImage && ( ( ytb - yta ) * ( xtc - xta ) == ( ytc - yta ) * ( xtb - xta ) ) )
556 QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
557 QTransform aTransform2( xta, yta, 1, xtb, ytb, 1, xtc, ytc, 1 );
559 bool anIsInvertible = false;
560 QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
561 if( !anIsInvertible )
564 QTransform aResTransform = aTransform1Inverted * aTransform2;
566 aResTransform *= aRefTransform;
568 SetTrsf( aResTransform );
571 bool HYDROData_Image::IsByTwoPoints() const
573 if ( !HasLocalPoints() || !HasGlobalPoints() )
576 QPoint aPointA, aPointB, aPointC;
577 GetLocalPoints( aPointA, aPointB, aPointC );
579 return aPointC.x() < 0 && aPointC.y() < 0;
582 bool HYDROData_Image::HasReferences() const
584 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
585 int aNbReferences = NbReferences();
587 return !aRefImage.IsNull() || aNbReferences > 0;
590 void HYDROData_Image::RemoveAllReferences()
592 if ( !HasReferences() )
595 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
596 if ( !aRefImage.IsNull() )
598 RemoveTrsfReferenceImage();
603 SetOperatorName( "" );
605 SetIsSelfSplit( false );
608 bool anIsByTwoPoints = IsByTwoPoints();
610 QImage anImage = Image();
611 if ( anImage.isNull() )
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() );
622 SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
624 // Calculate global points
625 QTransform aTransform = Trsf();
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 ) );
632 SetGlobalPoints( ManualCartesian, aTrsfPointA, aTrsfPointB, aTrsfPointC );
637 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
638 const QPoint& thePointB,
639 const QPoint& thePointC,
640 const bool theIsUpdate )
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 );
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() );
653 TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
661 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
663 QPoint& thePointC ) const
665 if ( !HasLocalPoints() )
668 Handle(TDataStd_RealArray) anArray;
669 myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
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();
678 bool HYDROData_Image::HasLocalPoints() const
680 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
681 if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_LOCAL_POINTS ) )
684 Handle(TDataStd_RealArray) anArray;
685 return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
689 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
690 const QPointF& thePointA,
691 const QPointF& thePointB,
692 const QPointF& thePointC,
693 const bool theIsUpdate )
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 );
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() );
706 SetTrsfMode( theMode );
708 TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
716 bool HYDROData_Image::GetGlobalPoints( TransformationMode& theMode,
719 QPointF& thePointC ) const
721 if ( !HasGlobalPoints() )
724 theMode = GetTrsfMode();
726 Handle(TDataStd_RealArray) anArray;
727 myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
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 ) );
736 bool HYDROData_Image::SetGlobalPointsFromFile( const QString& theFileName )
740 // Try to open the file
741 QFile aFile( theFileName );
742 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) ) {
746 QPointF aPointA, aPointB;
747 double aXmin, anYmin, aXmax, anYmax;
748 aXmin = anYmin = aXmax = anYmax = -1;
750 while ( !aFile.atEnd() &&
751 ( aXmin < 0 || anYmin < 0 || aXmax < 0 || anYmax < 0 ) ) {
753 QString aLine = aFile.readLine().simplified();
754 aLine.replace( " ", "" );
755 if ( aLine.isEmpty() ) {
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 );
769 HYDROData_Tool::IsNan( aDoubleValue ) ||
770 HYDROData_Tool::IsInf( aDoubleValue ) ) {
775 if ( aLine.startsWith( "Xminimum" ) ) {
776 aXmin = aDoubleValue;
778 else if ( aLine.startsWith( "Yminimum" ) ) {
779 anYmin = aDoubleValue;
781 else if ( aLine.startsWith( "Xmaximum" ) ) {
782 aXmax = aDoubleValue;
784 else if ( aLine.startsWith( "Ymaximum" ) ) {
785 anYmax = aDoubleValue;
792 if ( aXmin >= 0 && anYmin >= 0 ) {
793 aPointA.setX( aXmin );
794 aPointA.setY( anYmin );
797 if ( aXmax >= 0 && anYmax >= 0 ) {
798 aPointB.setX( aXmax );
799 aPointB.setY( anYmax );
802 if ( !aPointA.isNull() && !aPointB.isNull() ) {
803 SetGlobalPoints( ManualCartesian, aPointA, aPointB );
810 bool HYDROData_Image::HasGlobalPoints() const
812 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
813 if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_GLOBAL_POINTS ) )
816 Handle(TDataStd_RealArray) anArray;
817 return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
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 )
826 SetTrsfReferenceImage( theRefImage );
827 SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC, theIsUpdate );
830 bool HYDROData_Image::GetReferencePoints( Handle(HYDROData_Image)& theRefImage,
833 QPointF& thePointC ) const
835 if ( !HasReferencePoints() )
838 theRefImage = GetTrsfReferenceImage();
840 TransformationMode aMode;
841 GetGlobalPoints( aMode, thePointA, thePointB, thePointC );
846 bool HYDROData_Image::HasReferencePoints() const
848 if ( !HasGlobalPoints() )
851 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
852 if ( aRefImage.IsNull() )
855 TransformationMode aTrsfMode = GetTrsfMode();
856 if ( aTrsfMode != ReferenceImage )
862 void HYDROData_Image::SetTrsfMode( const TransformationMode& theMode )
864 TDataStd_Integer::Set( myLab.FindChild( DataTag_TrsfMode ), (int)theMode );
868 HYDROData_Image::TransformationMode HYDROData_Image::GetTrsfMode() const
870 TransformationMode aResMode = ManualGeodesic;
872 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
873 if ( !aLabel.IsNull() )
875 Handle(TDataStd_Integer) aMode;
876 if ( myLab.FindChild( DataTag_TrsfMode ).FindAttribute( TDataStd_Integer::GetID(), aMode ) )
877 aResMode = (TransformationMode)aMode->Get();
883 void HYDROData_Image::SetTrsfReferenceImage( const Handle(HYDROData_Image)& theRefImage )
885 SetReferenceObject( theRefImage, DataTag_TrsfImage );
889 Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
891 return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
894 void HYDROData_Image::RemoveTrsfReferenceImage()
896 RemoveReferenceObject( DataTag_TrsfImage );
900 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
902 AddReferenceObject( theReferenced, 0 );
906 int HYDROData_Image::NbReferences() const
908 return NbReferenceObjects( 0 );
911 Handle(HYDROData_Entity) HYDROData_Image::Reference( const int theIndex ) const
913 return GetReferenceObject( 0, theIndex );
916 void HYDROData_Image::ChangeReference(
917 const int theIndex, Handle(HYDROData_Entity) theReferenced)
919 SetReferenceObject( theReferenced, 0, theIndex );
923 void HYDROData_Image::RemoveReference(const int theIndex)
925 RemoveReferenceObject( 0, theIndex );
929 void HYDROData_Image::ClearReferences()
931 ClearReferenceObjects( 0 );
935 void HYDROData_Image::SetOperatorName( const QString theOpName )
937 TCollection_AsciiString anAsciiStr( theOpName.toStdString().c_str() );
938 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_Operator ), anAsciiStr );
942 QString HYDROData_Image::OperatorName() const
946 TDF_Label aLabel = myLab.FindChild( DataTag_Operator, false );
947 if ( !aLabel.IsNull() )
949 Handle(TDataStd_AsciiString) anAsciiStr;
950 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
951 aRes = QString( anAsciiStr->Get().ToCString() );
957 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
959 SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
963 QByteArray HYDROData_Image::Args() const
966 const char* aData = ByteArray(DataTag_Operator, aLen);
969 return QByteArray(aData, aLen);
972 void HYDROData_Image::SetIsSelfSplit(bool theFlag)
975 TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLIT);
977 myLab.ForgetAttribute(GUID_SELF_SPLIT);
982 bool HYDROData_Image::IsSelfSplit() const
984 return myLab.IsAttribute(GUID_SELF_SPLIT);
987 QPointF HYDROData_Image::generateThirdPoint( const QPointF& thePointA,
988 const QPointF& thePointB,
989 const bool& theIsLocal ) const
991 // Rotate vector to 90 degrees : clockwise - for local
992 // counterclockwise - for global
993 const double aTheta = theIsLocal ? -M_PI_2 : M_PI_2;
997 // Move to (0,0) for correct rotation
998 double x = thePointB.x() - thePointA.x();
999 double y = thePointB.y() - thePointA.y();
1001 aResPoint.setX( x * cos( aTheta ) - y * sin( aTheta ) );
1002 aResPoint.setY( x * sin( aTheta ) + y * cos( aTheta ) );
1004 // Move back to origin position
1005 aResPoint.setX( aResPoint.x() + thePointA.x() );
1006 aResPoint.setY( aResPoint.y() + thePointA.y() );