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);
412 bool HYDROData_Image::LoadImageECW( const QString& theFilePath )
415 if (HYDROData_Image::OpenECW(theFilePath.toLatin1().data(), anImage, NULL))
418 SetFilePath( theFilePath );
420 return !anImage.isNull();
423 QImage HYDROData_Image::Image()
425 Handle(TDataStd_IntegerArray) aParams;
426 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
427 return QImage(); // return empty image if there is no array
429 uchar* anArray = (uchar*)ByteArray(0, aLen);
431 return QImage(); // return empty image if there is no array
432 QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
433 aParams->Value(3), QImage::Format(aParams->Value(4)));
437 void HYDROData_Image::SetFilePath( const QString& theFilePath )
439 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
440 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
445 QString HYDROData_Image::GetFilePath() const
449 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
450 if ( !aLabel.IsNull() )
452 Handle(TDataStd_AsciiString) anAsciiStr;
453 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
454 aRes = QString( anAsciiStr->Get().ToCString() );
460 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
462 // locate 9 coeffs of matrix into the real array
463 Handle(TDataStd_RealArray) anArray;
464 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
465 if (theTrsf.isIdentity()) return; // no need to store identity transformation
466 anArray = TDataStd_RealArray::Set(myLab, 1, 9);
468 anArray->SetValue(1, theTrsf.m11());
469 anArray->SetValue(2, theTrsf.m12());
470 anArray->SetValue(3, theTrsf.m13());
471 anArray->SetValue(4, theTrsf.m21());
472 anArray->SetValue(5, theTrsf.m22());
473 anArray->SetValue(6, theTrsf.m23());
474 anArray->SetValue(7, theTrsf.m31());
475 anArray->SetValue(8, theTrsf.m32());
476 anArray->SetValue(9, theTrsf.m33());
481 QTransform HYDROData_Image::Trsf() const
483 // get 9 coeffs of matrix from the real array
484 Handle(TDataStd_RealArray) anArray;
485 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
486 return QTransform(); // return identity if there is no array
488 anArray->Value(1), anArray->Value(2), anArray->Value(3),
489 anArray->Value(4), anArray->Value(5), anArray->Value(6),
490 anArray->Value(7), anArray->Value(8), anArray->Value(9));
494 void HYDROData_Image::UpdateTrsf()
496 QPoint aPointA, aPointB, aPointC;
497 if ( !GetLocalPoints( aPointA, aPointB, aPointC ) )
500 TransformationMode aTrsfMode;
501 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
502 if ( !GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC ) )
505 QTransform aRefTransform;
506 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
508 bool anIsRefImage = aTrsfMode == ReferenceImage;
511 if ( aRefImage.IsNull() )
514 aRefTransform = aRefImage->Trsf();
517 bool anIsByTwoPoints = IsByTwoPoints();
519 // Convert lambert coordinates to cartesian
520 if ( aTrsfMode == ManualGeodesic )
522 double aXCart = 0, aYCart = 0;
524 HYDROData_Lambert93::toXY( aTrsfPointA.y(), aTrsfPointA.x(), aXCart, aYCart );
525 aTrsfPointA = QPointF( aXCart, aYCart );
527 HYDROData_Lambert93::toXY( aTrsfPointB.y(), aTrsfPointB.x(), aXCart, aYCart );
528 aTrsfPointB = QPointF( aXCart, aYCart );
530 if ( !anIsByTwoPoints )
532 HYDROData_Lambert93::toXY( aTrsfPointC.y(), aTrsfPointC.x(), aXCart, aYCart );
533 aTrsfPointC = QPointF( aXCart, aYCart );
537 // generate third points if needed
538 if ( anIsByTwoPoints )
540 aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
541 aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
544 int xa = aPointA.x();
545 int ya = aPointA.y();
546 int xb = aPointB.x();
547 int yb = aPointB.y();
548 int xc = aPointC.x();
549 int yc = aPointC.y();
551 double xta = aTrsfPointA.x();
552 double yta = aTrsfPointA.y();
553 double xtb = aTrsfPointB.x();
554 double ytb = aTrsfPointB.y();
555 double xtc = aTrsfPointC.x();
556 double ytc = aTrsfPointC.y();
558 // first, check that three input points don't belong to a single line
559 if( ( yb - ya ) * ( xc - xa ) == ( yc - ya ) * ( xb - xa ) )
562 // the same check for the reference points
563 if( anIsRefImage && ( ( ytb - yta ) * ( xtc - xta ) == ( ytc - yta ) * ( xtb - xta ) ) )
566 QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
567 QTransform aTransform2( xta, yta, 1, xtb, ytb, 1, xtc, ytc, 1 );
569 bool anIsInvertible = false;
570 QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
571 if( !anIsInvertible )
574 QTransform aResTransform = aTransform1Inverted * aTransform2;
576 aResTransform *= aRefTransform;
578 SetTrsf( aResTransform );
581 bool HYDROData_Image::IsByTwoPoints() const
583 if ( !HasLocalPoints() || !HasGlobalPoints() )
586 QPoint aPointA, aPointB, aPointC;
587 GetLocalPoints( aPointA, aPointB, aPointC );
589 return aPointC.x() < 0 && aPointC.y() < 0;
592 bool HYDROData_Image::HasReferences() const
594 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
595 int aNbReferences = NbReferences();
597 return !aRefImage.IsNull() || aNbReferences > 0;
600 void HYDROData_Image::RemoveAllReferences()
602 if ( !HasReferences() )
605 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
606 if ( !aRefImage.IsNull() )
608 RemoveTrsfReferenceImage();
613 SetOperatorName( "" );
615 SetIsSelfSplit( false );
618 bool anIsByTwoPoints = IsByTwoPoints();
620 QImage anImage = Image();
621 if ( anImage.isNull() )
627 // Set local points to default position
628 QPoint aLocalPointA = QPoint( 0, 0 );
629 QPoint aLocalPointB = QPoint( anImage.width(), 0 );
630 QPoint aLocalPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) : QPoint( 0, anImage.height() );
632 SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
634 // Calculate global points
635 QTransform aTransform = Trsf();
637 QPointF aTrsfPointA = QPointF( aTransform.map( aLocalPointA ) );
638 QPointF aTrsfPointB = QPointF( aTransform.map( aLocalPointB ) );
639 QPointF aTrsfPointC = anIsByTwoPoints ? QPointF( INT_MIN, INT_MIN ) :
640 QPointF( aTransform.map( aLocalPointC ) );
642 SetGlobalPoints( ManualCartesian, aTrsfPointA, aTrsfPointB, aTrsfPointC );
647 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
648 const QPoint& thePointB,
649 const QPoint& thePointC,
650 const bool theIsUpdate )
652 Handle(TDataStd_RealArray) anArray;
653 if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
654 anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
656 anArray->SetValue( 1, thePointA.x() );
657 anArray->SetValue( 2, thePointA.y() );
658 anArray->SetValue( 3, thePointB.x() );
659 anArray->SetValue( 4, thePointB.y() );
660 anArray->SetValue( 5, thePointC.x() );
661 anArray->SetValue( 6, thePointC.y() );
663 TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
671 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
673 QPoint& thePointC ) const
675 if ( !HasLocalPoints() )
678 Handle(TDataStd_RealArray) anArray;
679 myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
681 thePointA = QPointF( anArray->Value( 1 ), anArray->Value( 2 ) ).toPoint();
682 thePointB = QPointF( anArray->Value( 3 ), anArray->Value( 4 ) ).toPoint();
683 thePointC = QPointF( anArray->Value( 5 ), anArray->Value( 6 ) ).toPoint();
688 bool HYDROData_Image::HasLocalPoints() const
690 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
691 if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_LOCAL_POINTS ) )
694 Handle(TDataStd_RealArray) anArray;
695 return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
699 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
700 const QPointF& thePointA,
701 const QPointF& thePointB,
702 const QPointF& thePointC,
703 const bool theIsUpdate )
705 Handle(TDataStd_RealArray) anArray;
706 if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
707 anArray = TDataStd_RealArray::Set( myLab.FindChild( DataTag_TrsfPoints ), 1, 12 );
709 anArray->SetValue( 7, thePointA.x() );
710 anArray->SetValue( 8, thePointA.y() );
711 anArray->SetValue( 9, thePointB.x() );
712 anArray->SetValue( 10, thePointB.y() );
713 anArray->SetValue( 11, thePointC.x() );
714 anArray->SetValue( 12, thePointC.y() );
716 SetTrsfMode( theMode );
718 TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
726 bool HYDROData_Image::GetGlobalPoints( TransformationMode& theMode,
729 QPointF& thePointC ) const
731 if ( !HasGlobalPoints() )
734 theMode = GetTrsfMode();
736 Handle(TDataStd_RealArray) anArray;
737 myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray );
739 thePointA = QPointF( anArray->Value( 7 ), anArray->Value( 8 ) );
740 thePointB = QPointF( anArray->Value( 9 ), anArray->Value( 10 ) );
741 thePointC = QPointF( anArray->Value( 11 ), anArray->Value( 12 ) );
746 bool HYDROData_Image::SetGlobalPointsFromFile( const QString& theFileName )
750 // Try to open the file
751 QFile aFile( theFileName );
752 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) ) {
756 QPointF aPointA, aPointB;
757 double aXmin, anYmin, aXmax, anYmax;
758 aXmin = anYmin = aXmax = anYmax = -1;
760 while ( !aFile.atEnd() &&
761 ( aXmin < 0 || anYmin < 0 || aXmax < 0 || anYmax < 0 ) ) {
763 QString aLine = aFile.readLine().simplified();
764 aLine.replace( " ", "" );
765 if ( aLine.isEmpty() ) {
769 // Try to read double value after ":"
770 bool isDoubleOk = false;
771 double aDoubleValue = -1;
772 QStringList aValues = aLine.split( ":", QString::SkipEmptyParts );
773 if ( aValues.count() == 2 ) {
774 aDoubleValue = aValues.last().toDouble( &isDoubleOk );
779 HYDROData_Tool::IsNan( aDoubleValue ) ||
780 HYDROData_Tool::IsInf( aDoubleValue ) ) {
785 if ( aLine.startsWith( "Xminimum" ) ) {
786 aXmin = aDoubleValue;
788 else if ( aLine.startsWith( "Yminimum" ) ) {
789 anYmin = aDoubleValue;
791 else if ( aLine.startsWith( "Xmaximum" ) ) {
792 aXmax = aDoubleValue;
794 else if ( aLine.startsWith( "Ymaximum" ) ) {
795 anYmax = aDoubleValue;
802 if ( aXmin >= 0 && anYmin >= 0 ) {
803 aPointA.setX( aXmin );
804 aPointA.setY( anYmin );
807 if ( aXmax >= 0 && anYmax >= 0 ) {
808 aPointB.setX( aXmax );
809 aPointB.setY( anYmax );
812 if ( !aPointA.isNull() && !aPointB.isNull() ) {
813 SetGlobalPoints( ManualCartesian, aPointA, aPointB );
820 bool HYDROData_Image::HasGlobalPoints() const
822 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
823 if ( aLabel.IsNull() || !aLabel.IsAttribute( GUID_HAS_GLOBAL_POINTS ) )
826 Handle(TDataStd_RealArray) anArray;
827 return aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray );
830 void HYDROData_Image::SetReferencePoints( const Handle(HYDROData_Image)& theRefImage,
831 const QPointF& thePointA,
832 const QPointF& thePointB,
833 const QPointF& thePointC,
834 const bool theIsUpdate )
836 SetTrsfReferenceImage( theRefImage );
837 SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC, theIsUpdate );
840 bool HYDROData_Image::GetReferencePoints( Handle(HYDROData_Image)& theRefImage,
843 QPointF& thePointC ) const
845 if ( !HasReferencePoints() )
848 theRefImage = GetTrsfReferenceImage();
850 TransformationMode aMode;
851 GetGlobalPoints( aMode, thePointA, thePointB, thePointC );
856 bool HYDROData_Image::HasReferencePoints() const
858 if ( !HasGlobalPoints() )
861 Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
862 if ( aRefImage.IsNull() )
865 TransformationMode aTrsfMode = GetTrsfMode();
866 if ( aTrsfMode != ReferenceImage )
872 void HYDROData_Image::SetTrsfMode( const TransformationMode& theMode )
874 TDataStd_Integer::Set( myLab.FindChild( DataTag_TrsfMode ), (int)theMode );
878 HYDROData_Image::TransformationMode HYDROData_Image::GetTrsfMode() const
880 TransformationMode aResMode = ManualGeodesic;
882 TDF_Label aLabel = myLab.FindChild( DataTag_TrsfPoints, false );
883 if ( !aLabel.IsNull() )
885 Handle(TDataStd_Integer) aMode;
886 if ( myLab.FindChild( DataTag_TrsfMode ).FindAttribute( TDataStd_Integer::GetID(), aMode ) )
887 aResMode = (TransformationMode)aMode->Get();
893 void HYDROData_Image::SetTrsfReferenceImage( const Handle(HYDROData_Image)& theRefImage )
895 SetReferenceObject( theRefImage, DataTag_TrsfImage );
899 Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
901 return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
904 void HYDROData_Image::RemoveTrsfReferenceImage()
906 RemoveReferenceObject( DataTag_TrsfImage );
910 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
912 AddReferenceObject( theReferenced, 0 );
916 int HYDROData_Image::NbReferences() const
918 return NbReferenceObjects( 0 );
921 Handle(HYDROData_Entity) HYDROData_Image::Reference( const int theIndex ) const
923 return GetReferenceObject( 0, theIndex );
926 void HYDROData_Image::ChangeReference(
927 const int theIndex, Handle(HYDROData_Entity) theReferenced)
929 SetReferenceObject( theReferenced, 0, theIndex );
933 void HYDROData_Image::RemoveReference(const int theIndex)
935 RemoveReferenceObject( 0, theIndex );
939 void HYDROData_Image::ClearReferences()
941 ClearReferenceObjects( 0 );
945 void HYDROData_Image::SetOperatorName( const QString theOpName )
947 TCollection_AsciiString anAsciiStr( theOpName.toStdString().c_str() );
948 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_Operator ), anAsciiStr );
952 QString HYDROData_Image::OperatorName() const
956 TDF_Label aLabel = myLab.FindChild( DataTag_Operator, false );
957 if ( !aLabel.IsNull() )
959 Handle(TDataStd_AsciiString) anAsciiStr;
960 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
961 aRes = QString( anAsciiStr->Get().ToCString() );
967 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
969 SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
973 QByteArray HYDROData_Image::Args() const
976 const char* aData = ByteArray(DataTag_Operator, aLen);
979 return QByteArray(aData, aLen);
982 void HYDROData_Image::SetIsSelfSplit(bool theFlag)
985 TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLIT);
987 myLab.ForgetAttribute(GUID_SELF_SPLIT);
992 bool HYDROData_Image::IsSelfSplit() const
994 return myLab.IsAttribute(GUID_SELF_SPLIT);
997 QPointF HYDROData_Image::generateThirdPoint( const QPointF& thePointA,
998 const QPointF& thePointB,
999 const bool& theIsLocal ) const
1001 // Rotate vector to 90 degrees : clockwise - for local
1002 // counterclockwise - for global
1003 const double aTheta = theIsLocal ? -M_PI_2 : M_PI_2;
1007 // Move to (0,0) for correct rotation
1008 double x = thePointB.x() - thePointA.x();
1009 double y = thePointB.y() - thePointA.y();
1011 aResPoint.setX( x * cos( aTheta ) - y * sin( aTheta ) );
1012 aResPoint.setY( x * sin( aTheta ) + y * cos( aTheta ) );
1014 // Move back to origin position
1015 aResPoint.setX( aResPoint.x() + thePointA.x() );
1016 aResPoint.setY( aResPoint.y() + thePointA.y() );