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);
299 aParams->SetID(TDataStd_IntegerArray::GetID());
301 aParams->SetValue(1, anImage.width());
302 aParams->SetValue(2, anImage.height());
303 aParams->SetValue(3, anImage.bytesPerLine());
304 aParams->SetValue(4, (int)(anImage.format()));
305 // store data of image in byte array
306 const char* aData = (const char*)(anImage.bits());
307 SaveByteArray(0, aData, anImage.byteCount());
313 bool HYDROData_Image::LoadImage( const QString& theFilePath )
315 QFileInfo aFI(theFilePath);
317 HYDROData_Image::ECW_FileInfo* theECWInfo;
318 if (aFI.suffix().toLower() == "ecw")
320 theECWInfo = new HYDROData_Image::ECW_FileInfo;
321 HYDROData_Image::OpenECW(theFilePath.toLatin1().data(), anImage, theECWInfo);
324 anImage = QImage(theFilePath);
326 SetFilePath( theFilePath );
327 return !anImage.isNull();
330 bool HYDROData_Image::OpenECW(char* theFileName, QImage& theImage, ECW_FileInfo* theECWInfo)
332 NCSFileView *pNCSFileView;
333 NCSFileViewFileInfo *pNCSFileInfo;
334 NCSError eError = NCS_SUCCESS;
339 eError = NCScbmOpenFileView(theFileName, &pNCSFileView, NULL);
340 if(eError != NCS_SUCCESS)
343 eError = NCScbmGetViewFileInfo(pNCSFileView, &pNCSFileInfo);
344 if(eError != NCS_SUCCESS)
347 XSize = pNCSFileInfo->nSizeX;
348 YSize = pNCSFileInfo->nSizeY;
349 nBands = pNCSFileInfo->nBands;
352 //ECW_CellUnits myCellSizeUnits;
353 CellSizeUnits aCellUnits = pNCSFileInfo->eCellSizeUnits;
354 if (aCellUnits == ECW_CELL_UNITS_METERS)
355 theECWInfo->myCellSizeUnits = ECW_CellUnits_Meters;
356 else if (aCellUnits == ECW_CELL_UNITS_DEGREES)
357 theECWInfo->myCellSizeUnits = ECW_CellUnits_Deg;
358 else if (aCellUnits == ECW_CELL_UNITS_FEET)
359 theECWInfo->myCellSizeUnits = ECW_CellUnits_Feet;
361 theECWInfo->myCellSizeUnits = ECW_CellUnits_Unknown;
362 theECWInfo->myCellIncrementX = pNCSFileInfo->fCellIncrementX;
363 theECWInfo->myCellIncrementY = pNCSFileInfo->fCellIncrementY;
364 theECWInfo->myOriginX = pNCSFileInfo->fOriginX;
365 theECWInfo->myOriginY = pNCSFileInfo->fOriginY;
366 theECWInfo->myXSize = pNCSFileInfo->nSizeX;
367 theECWInfo->myYSize = pNCSFileInfo->nSizeY;
370 std::vector<UINT32> band_list(nBands);
371 for( band = 0; band < nBands; band++ )
372 band_list[band] = band;
374 eError = NCScbmSetFileView(pNCSFileView, nBands, &band_list[0], 0, 0, XSize - 1, YSize - 1, XSize, YSize); //view an image into the original size
376 if(eError != NCS_SUCCESS)
378 NCScbmCloseFileView(pNCSFileView);
383 pRGBTriplets = (UINT8 *) malloc(XSize*3);
385 QImage anImage(XSize, YSize, QImage::Format_RGB32);
387 for(UINT32 line = 0; line < YSize; line++)
389 NCSEcwReadStatus eStatus;
390 eStatus = NCScbmReadViewLineRGB(pNCSFileView, pRGBTriplets);
391 if(eStatus == NCSECW_READ_OK)
393 QRgb* crp = (QRgb*)anImage.scanLine(line);
394 for(UINT32 j = 0; j < XSize; j++)
396 QRgb val = qRgb((int)pRGBTriplets[j*3],(int)pRGBTriplets[j*3+1],(int)pRGBTriplets[j*3+2]);
397 memcpy((void*)(crp+j), &val, sizeof(QRgb));
403 NCScbmCloseFileView(pNCSFileView);
409 NCScbmCloseFileView(pNCSFileView);
414 bool HYDROData_Image::LoadImageECW( const QString& theFilePath )
417 if (HYDROData_Image::OpenECW(theFilePath.toLatin1().data(), anImage, NULL))
420 SetFilePath( theFilePath );
422 return !anImage.isNull();
425 QImage HYDROData_Image::Image()
427 Handle(TDataStd_IntegerArray) aParams;
428 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
429 return QImage(); // return empty image if there is no array
431 uchar* anArray = (uchar*)ByteArray(0, aLen);
433 return QImage(); // return empty image if there is no array
434 QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
435 aParams->Value(3), QImage::Format(aParams->Value(4)));
439 void HYDROData_Image::SetFilePath( const QString& theFilePath )
441 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
442 Handle(TDataStd_AsciiString) anAttr = TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
443 anAttr->SetID(TDataStd_AsciiString::GetID());
447 QString HYDROData_Image::GetFilePath() const
451 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
452 if ( !aLabel.IsNull() )
454 Handle(TDataStd_AsciiString) anAsciiStr;
455 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
456 aRes = QString( anAsciiStr->Get().ToCString() );
462 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
464 // locate 9 coeffs of matrix into the real array
465 Handle(TDataStd_RealArray) anArray;
466 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
467 if (theTrsf.isIdentity()) return; // no need to store identity transformation
468 anArray = TDataStd_RealArray::Set(myLab, 1, 9);
469 anArray->SetID(TDataStd_RealArray::GetID());
471 anArray->SetValue(1, theTrsf.m11());
472 anArray->SetValue(2, theTrsf.m12());
473 anArray->SetValue(3, theTrsf.m13());
474 anArray->SetValue(4, theTrsf.m21());
475 anArray->SetValue(5, theTrsf.m22());
476 anArray->SetValue(6, theTrsf.m23());
477 anArray->SetValue(7, theTrsf.m31());
478 anArray->SetValue(8, theTrsf.m32());
479 anArray->SetValue(9, theTrsf.m33());
484 QTransform HYDROData_Image::Trsf() const
486 // get 9 coeffs of matrix from the real array
487 Handle(TDataStd_RealArray) anArray;
488 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
489 return QTransform(); // return identity if there is no array
491 anArray->Value(1), anArray->Value(2), anArray->Value(3),
492 anArray->Value(4), anArray->Value(5), anArray->Value(6),
493 anArray->Value(7), anArray->Value(8), anArray->Value(9));
497 void HYDROData_Image::UpdateTrsf()
499 QPoint aPointA, aPointB, aPointC;
500 if ( !GetLocalPoints( aPointA, aPointB, aPointC ) )
503 TransformationMode aTrsfMode;
504 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
505 if ( !GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
508 QTransform aRefTransform;
509 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
511 bool anIsRefImage = aTrsfMode == ReferenceImage;
514 if ( aRefImage.IsNull() )
517 aRefTransform = aRefImage->Trsf();
520 bool anIsByTwoPoints = IsByTwoPoints();
522 // Convert lambert coordinates to cartesian
523 if ( aTrsfMode == ManualGeodesic )
525 double aXCart = 0, aYCart = 0;
527 HYDROData_Lambert93::toXY( aTrsfPointA.y(), aTrsfPointA.x(), aXCart, aYCart );
528 aTrsfPointA = QPointF( aXCart, aYCart );
530 HYDROData_Lambert93::toXY( aTrsfPointB.y(), aTrsfPointB.x(), aXCart, aYCart );
531 aTrsfPointB = QPointF( aXCart, aYCart );
533 if ( !anIsByTwoPoints )
535 HYDROData_Lambert93::toXY( aTrsfPointC.y(), aTrsfPointC.x(), aXCart, aYCart );
536 aTrsfPointC = QPointF( aXCart, aYCart );
540 // generate third points if needed
541 if ( anIsByTwoPoints )
543 aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
544 aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
547 int xa = aPointA.x();
548 int ya = aPointA.y();
549 int xb = aPointB.x();
550 int yb = aPointB.y();
551 int xc = aPointC.x();
552 int yc = aPointC.y();
554 double xta = aTrsfPointA.x();
555 double yta = aTrsfPointA.y();
556 double xtb = aTrsfPointB.x();
557 double ytb = aTrsfPointB.y();
558 double xtc = aTrsfPointC.x();
559 double ytc = aTrsfPointC.y();
561 // first, check that three input points don't belong to a single line
562 if( ( yb - ya ) * ( xc - xa ) == ( yc - ya ) * ( xb - xa ) )
565 // the same check for the reference points
566 if( anIsRefImage && ( ( ytb - yta ) * ( xtc - xta ) == ( ytc - yta ) * ( xtb - xta ) ) )
569 QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
570 QTransform aTransform2( xta, yta, 1, xtb, ytb, 1, xtc, ytc, 1 );
572 bool anIsInvertible = false;
573 QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
574 if( !anIsInvertible )
577 QTransform aResTransform = aTransform1Inverted * aTransform2;
579 aResTransform *= aRefTransform;
581 SetTrsf( aResTransform );
584 bool HYDROData_Image::IsByTwoPoints() const
586 if ( !HasLocalPoints() || !HasGlobalPoints() )
589 QPoint aPointA, aPointB, aPointC;
590 GetLocalPoints( aPointA, aPointB, aPointC );
592 return aPointC.x() < 0 && aPointC.y() < 0;
595 bool HYDROData_Image::HasReferences() const
597 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
598 int aNbReferences = NbReferences();
600 return !aRefImage.IsNull() || aNbReferences > 0;
603 void HYDROData_Image::RemoveAllReferences()
605 if ( !HasReferences() )
608 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
609 if ( !aRefImage.IsNull() )
611 RemoveTrsfReferenceImage();
616 SetOperatorName( "" );
618 SetIsSelfSplit( false );
621 bool anIsByTwoPoints = IsByTwoPoints();
623 QImage anImage = Image();
624 if ( anImage.isNull() )
630 // Set local points to default position
631 QPoint aLocalPointA = QPoint( 0, 0 );
632 QPoint aLocalPointB = QPoint( anImage.width(), 0 );
633 QPoint aLocalPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) : QPoint( 0, anImage.height() );
635 SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
637 // Calculate global points
638 QTransform aTransform = Trsf();
640 QPointF aTrsfPointA = QPointF( aTransform.map( aLocalPointA ) );
641 QPointF aTrsfPointB = QPointF( aTransform.map( aLocalPointB ) );
642 QPointF aTrsfPointC = anIsByTwoPoints ? QPointF( INT_MIN, INT_MIN ) :
643 QPointF( aTransform.map( aLocalPointC ) );
645 SetGlobalPoints( ManualCartesian, aTrsfPointA, aTrsfPointB, aTrsfPointC );
650 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
651 const QPoint& thePointB,
652 const QPoint& thePointC,
653 const bool theIsUpdate )
655 Handle(TDataStd_RealArray) anArray;
656 if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
658 anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
659 anArray->SetID(TDataStd_RealArray::GetID());
662 anArray->SetValue( 1, thePointA.x() );
663 anArray->SetValue( 2, thePointA.y() );
664 anArray->SetValue( 3, thePointB.x() );
665 anArray->SetValue( 4, thePointB.y() );
666 anArray->SetValue( 5, thePointC.x() );
667 anArray->SetValue( 6, thePointC.y() );
669 TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
677 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
679 QPoint& thePointC ) const
681 if ( !HasLocalPoints() )
684 Handle(TDataStd_RealArray) anArray;
685 myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
687 thePointA = QPointF( anArray->Value( 1 ), anArray->Value( 2 ) ).toPoint();
688 thePointB = QPointF( anArray->Value( 3 ), anArray->Value( 4 ) ).toPoint();
689 thePointC = QPointF( anArray->Value( 5 ), anArray->Value( 6 ) ).toPoint();
694 bool HYDROData_Image::HasLocalPoints() const
696 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
697 if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_LOCAL_POINTS ) )
700 Handle(TDataStd_RealArray) anArray;
701 return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
705 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
706 const QPointF& thePointA,
707 const QPointF& thePointB,
708 const QPointF& thePointC,
709 const bool theIsUpdate )
711 Handle(TDataStd_RealArray) anArray;
712 if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
714 anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
715 anArray->SetID(TDataStd_RealArray::GetID());
718 anArray->SetValue( 7, thePointA.x() );
719 anArray->SetValue( 8, thePointA.y() );
720 anArray->SetValue( 9, thePointB.x() );
721 anArray->SetValue( 10, thePointB.y() );
722 anArray->SetValue( 11, thePointC.x() );
723 anArray->SetValue( 12, thePointC.y() );
725 SetTrsfMode( theMode );
727 TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
735 bool HYDROData_Image::GetGlobalPoints( TransformationMode& theMode,
738 QPointF& thePointC ) const
740 if ( !HasGlobalPoints() )
743 theMode = GetTrsfMode();
745 Handle(TDataStd_RealArray) anArray;
746 myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
748 thePointA = QPointF( anArray->Value( 7 ), anArray->Value( 8 ) );
749 thePointB = QPointF( anArray->Value( 9 ), anArray->Value( 10 ) );
750 thePointC = QPointF( anArray->Value( 11 ), anArray->Value( 12 ) );
755 bool HYDROData_Image::SetGlobalPointsFromFile( const QString& theFileName )
759 // Try to open the file
760 QFile aFile( theFileName );
761 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) ) {
765 QPointF aPointA, aPointB;
766 double aXmin, anYmin, aXmax, anYmax;
767 aXmin = anYmin = aXmax = anYmax = -1;
769 while ( !aFile.atEnd() &&
770 ( aXmin < 0 || anYmin < 0 || aXmax < 0 || anYmax < 0 ) ) {
772 QString aLine = aFile.readLine().simplified();
773 aLine.replace( " ", "" );
774 if ( aLine.isEmpty() ) {
778 // Try to read double value after ":"
779 bool isDoubleOk = false;
780 double aDoubleValue = -1;
781 QStringList aValues = aLine.split( ":", QString::SkipEmptyParts );
782 if ( aValues.count() == 2 ) {
783 aDoubleValue = aValues.last().toDouble( &isDoubleOk );
788 HYDROData_Tool::IsNan( aDoubleValue ) ||
789 HYDROData_Tool::IsInf( aDoubleValue ) ) {
794 if ( aLine.startsWith( "Xminimum" ) ) {
795 aXmin = aDoubleValue;
797 else if ( aLine.startsWith( "Yminimum" ) ) {
798 anYmin = aDoubleValue;
800 else if ( aLine.startsWith( "Xmaximum" ) ) {
801 aXmax = aDoubleValue;
803 else if ( aLine.startsWith( "Ymaximum" ) ) {
804 anYmax = aDoubleValue;
811 if ( aXmin >= 0 && anYmin >= 0 ) {
812 aPointA.setX( aXmin );
813 aPointA.setY( anYmin );
816 if ( aXmax >= 0 && anYmax >= 0 ) {
817 aPointB.setX( aXmax );
818 aPointB.setY( anYmax );
821 if ( !aPointA.isNull() && !aPointB.isNull() ) {
822 SetGlobalPoints( ManualCartesian, aPointA, aPointB );
829 bool HYDROData_Image::HasGlobalPoints() const
831 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
832 if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_GLOBAL_POINTS ) )
835 Handle(TDataStd_RealArray) anArray;
836 return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
839 void HYDROData_Image::SetReferencePoints( const Handle(HYDROData_Image)& theRefImage,
840 const QPointF& thePointA,
841 const QPointF& thePointB,
842 const QPointF& thePointC,
843 const bool theIsUpdate )
845 SetTrsfReferenceImage( theRefImage );
846 SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC, theIsUpdate );
849 bool HYDROData_Image::GetReferencePoints( Handle(HYDROData_Image)& theRefImage,
852 QPointF& thePointC ) const
854 if ( !HasReferencePoints() )
857 theRefImage = GetTrsfReferenceImage();
859 TransformationMode aMode;
860 GetGlobalPoints( aMode, thePointA, thePointB, thePointC );
865 bool HYDROData_Image::HasReferencePoints() const
867 if ( !HasGlobalPoints() )
870 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
871 if ( aRefImage.IsNull() )
874 TransformationMode aTrsfMode = GetTrsfMode();
875 if ( aTrsfMode != ReferenceImage )
881 void HYDROData_Image::SetTrsfMode( const TransformationMode& theMode )
883 Handle(TDataStd_Integer) anAttr = TDataStd_Integer::Set( myLab.FindChild( DataTag_TrsfMode ), (int)theMode );
884 anAttr->SetID(TDataStd_Integer::GetID());
888 HYDROData_Image::TransformationMode HYDROData_Image::GetTrsfMode() const
890 TransformationMode aResMode = ManualGeodesic;
892 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
893 if ( !aLabel.IsNull() )
895 Handle(TDataStd_Integer) aMode;
896 if ( myLab.FindChild( DataTag_TrsfMode ).FindAttribute( TDataStd_Integer::GetID(), aMode ) )
897 aResMode = (TransformationMode)aMode->Get();
903 void HYDROData_Image::SetTrsfReferenceImage( const Handle(HYDROData_Image)& theRefImage )
905 SetReferenceObject( theRefImage, DataTag_TrsfImage );
909 Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
911 return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
914 void HYDROData_Image::RemoveTrsfReferenceImage()
916 RemoveReferenceObject( DataTag_TrsfImage );
920 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
922 AddReferenceObject( theReferenced, 0 );
926 int HYDROData_Image::NbReferences() const
928 return NbReferenceObjects( 0 );
931 Handle(HYDROData_Entity) HYDROData_Image::Reference( const int theIndex ) const
933 return GetReferenceObject( 0, theIndex );
936 void HYDROData_Image::ChangeReference(
937 const int theIndex, Handle(HYDROData_Entity) theReferenced)
939 SetReferenceObject( theReferenced, 0, theIndex );
943 void HYDROData_Image::RemoveReference(const int theIndex)
945 RemoveReferenceObject( 0, theIndex );
949 void HYDROData_Image::ClearReferences()
951 ClearReferenceObjects( 0 );
955 void HYDROData_Image::SetOperatorName( const QString theOpName )
957 TCollection_AsciiString anAsciiStr( theOpName.toStdString().c_str() );
958 Handle(TDataStd_AsciiString) anAttr = TDataStd_AsciiString::Set( myLab.FindChild( DataTag_Operator ), anAsciiStr );
959 anAttr->SetID(TDataStd_AsciiString::GetID());
963 QString HYDROData_Image::OperatorName() const
967 TDF_Label aLabel = myLab.FindChild( DataTag_Operator, false );
968 if ( !aLabel.IsNull() )
970 Handle(TDataStd_AsciiString) anAsciiStr;
971 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
972 aRes = QString( anAsciiStr->Get().ToCString() );
978 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
980 SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
984 QByteArray HYDROData_Image::Args() const
987 const char* aData = ByteArray(DataTag_Operator, aLen);
990 return QByteArray(aData, aLen);
993 void HYDROData_Image::SetIsSelfSplit(bool theFlag)
996 TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLIT);
998 myLab.ForgetAttribute(GUID_SELF_SPLIT);
1003 bool HYDROData_Image::IsSelfSplit() const
1005 return myLab.IsAttribute(GUID_SELF_SPLIT);
1008 QPointF HYDROData_Image::generateThirdPoint( const QPointF& thePointA,
1009 const QPointF& thePointB,
1010 const bool& theIsLocal ) const
1012 // Rotate vector to 90 degrees : clockwise - for local
1013 // counterclockwise - for global
1014 const double aTheta = theIsLocal ? -M_PI_2 : M_PI_2;
1018 // Move to (0,0) for correct rotation
1019 double x = thePointB.x() - thePointA.x();
1020 double y = thePointB.y() - thePointA.y();
1022 aResPoint.setX( x * cos( aTheta ) - y * sin( aTheta ) );
1023 aResPoint.setY( x * sin( aTheta ) + y * cos( aTheta ) );
1025 // Move back to origin position
1026 aResPoint.setX( aResPoint.x() + thePointA.x() );
1027 aResPoint.setY( aResPoint.y() + thePointA.y() );