2 #include "HYDROData_Bathymetry.h"
3 #include "HYDROData_Document.h"
4 #include "HYDROData_Tool.h"
9 #include <TDataStd_RealArray.hxx>
10 #include <TDataStd_AsciiString.hxx>
16 #include <QStringList>
18 #define PYTHON_BATHYMETRY_ID "KIND_BATHYMETRY"
21 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
22 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
24 HYDROData_Bathymetry::HYDROData_Bathymetry()
25 : HYDROData_IAltitudeObject()
29 HYDROData_Bathymetry::~HYDROData_Bathymetry()
33 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
37 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
38 if ( aDocument.IsNull() )
41 QString aDocName = aDocument->GetDocPyName();
42 QString aBathymetryName = GetName();
44 aResList << QString( "%1 = %2.CreateObject( %3 );" )
45 .arg( aBathymetryName ).arg( aDocName ).arg( PYTHON_BATHYMETRY_ID );
46 aResList << QString( "%1.SetName( \"%2\" );" )
47 .arg( aBathymetryName ).arg( aBathymetryName );
49 QString aFilePath = GetFilePath();
50 if ( !aFilePath.isEmpty() )
52 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
53 .arg( aBathymetryName ).arg( aFilePath );
57 // TODO : bathymetry is composed from other bathymetry(ies)
63 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
65 RemoveAltitudePoints();
67 if ( thePoints.isEmpty() )
71 Handle(TDataStd_RealArray) aCoordsArray =
72 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.size() * 3 - 1 );
74 AltitudePoints::const_iterator aListItBeg = thePoints.constBegin();
75 AltitudePoints::const_iterator aListItEnd = thePoints.constEnd();
76 for ( int i = 0 ; aListItBeg != aListItEnd; ++i, ++aListItBeg )
78 const AltitudePoint& aPoint = *aListItBeg;
80 aCoordsArray->SetValue( i * 3, aPoint.X() );
81 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
82 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
86 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
88 AltitudePoints aPoints;
90 Handle(TDataStd_RealArray) aCoordsArray;
91 if ( !myLab.FindChild( DataTag_AltitudePoints ).FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
94 int aLowerIdx = aCoordsArray->Lower();
95 int anUpperIdx = aCoordsArray->Upper();
96 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
101 AltitudePoint aPoint;
102 aPoint.SetX( aCoordsArray->Value( i++ ) );
103 aPoint.SetY( aCoordsArray->Value( i++ ) );
104 aPoint.SetZ( aCoordsArray->Value( i++ ) );
112 void HYDROData_Bathymetry::RemoveAltitudePoints()
114 TDF_Label aLab = myLab.FindChild( DataTag_AltitudePoints );
115 aLab.ForgetAllAttributes();
118 void interpolateAltitudeForPoints( const gp_XY& thePoint,
119 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
120 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
121 HYDROData_Bathymetry::AltitudePoint& theResPoint,
122 const bool& theIsVertical )
124 double aCoordX = thePoint.X();
125 double aCoordY = thePoint.Y();
129 aCoordX = theFirstPoint.X();
131 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
133 // Recalculate X coordinate by equation of line from two points
134 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
135 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
140 aCoordY = theFirstPoint.Y();
142 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
144 // Recalculate y by equation of line from two points
145 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
146 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
150 theResPoint.SetX( aCoordX );
151 theResPoint.SetY( aCoordY );
153 // Calculate coefficient for interpolation
154 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
155 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
157 double aInterCoeff = 0;
159 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
162 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
163 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
165 // Calculate interpolated value
166 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
168 theResPoint.SetZ( aResVal );
171 double HYDROData_Bathymetry::GetAltitudeForPoint( const gp_XY& thePoint ) const
173 double aResAltitude = GetInvalidAltitude();
175 AltitudePoints anAltitudePoints = GetAltitudePoints();
176 if ( anAltitudePoints.isEmpty() )
179 QPolygonF aBoundingRect;
182 // [ 0 (top-left) ] [ 1 (top-right) ]
184 // [ 2 (bot-left) ] [ 3 (bot-right) ]
185 AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, GetInvalidAltitude() ),
186 AltitudePoint( DBL_MAX, -DBL_MAX, GetInvalidAltitude() ),
187 AltitudePoint( -DBL_MAX, DBL_MAX, GetInvalidAltitude() ),
188 AltitudePoint( DBL_MAX, DBL_MAX, GetInvalidAltitude() ) };
190 AltitudePoints::const_iterator aListItBeg = anAltitudePoints.constBegin();
191 AltitudePoints::const_iterator aListItEnd = anAltitudePoints.constEnd();
192 for ( ; aListItBeg != aListItEnd; ++aListItBeg )
194 const AltitudePoint& aPoint = *aListItBeg;
196 double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
197 double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
199 if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
201 if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
203 aResAltitude = aPoint.Z();
206 else if ( aDeltaY < 0 ) // top side
209 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
210 aBounds[ 0 ] = aPoint;
211 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
212 aBounds[ 1 ] = aPoint;
217 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
218 aBounds[ 2 ] = aPoint;
219 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
220 aBounds[ 3 ] = aPoint;
223 else if ( aDeltaX < 0 ) // left side
225 if ( ValuesEquals( aDeltaY, 0.0 ) )
228 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
229 aBounds[ 0 ] = aPoint;
230 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
231 aBounds[ 2 ] = aPoint;
233 else if ( aDeltaY < 0 )
236 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
237 aBounds[ 0 ] = aPoint;
241 // bottom left corner
242 if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
243 aBounds[ 2 ] = aPoint;
248 if ( ValuesEquals( aDeltaY, 0.0 ) )
251 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
252 aBounds[ 1 ] = aPoint;
253 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
254 aBounds[ 3 ] = aPoint;
256 else if ( aDeltaY < 0 )
259 if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
260 aBounds[ 1 ] = aPoint;
264 // bottom right corner
265 if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
266 aBounds[ 3 ] = aPoint;
270 // Update bounding rectangle of our global grid
271 aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
274 // Check if requested point is inside of our bounding rectangle
275 if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
278 // Calculate result altitude for point
279 AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
281 // At first we merge top and bottom borders
282 if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
283 interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
285 if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
286 interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
288 AltitudePoint aResPoint( aFirstPoint );
290 // At last we merge left and right borders
291 if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
292 interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
294 aResAltitude = aResPoint.Z();
299 void HYDROData_Bathymetry::SetFilePath(const QString& theFilePath)
301 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
302 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
305 QString HYDROData_Bathymetry::GetFilePath() const
309 Handle(TDataStd_AsciiString) anAsciiStr;
310 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
311 aRes = QString( anAsciiStr->Get().ToCString() );
316 bool HYDROData_Bathymetry::ImportFromFile( const QString& theFileName )
318 // Try to open the file
319 QFile aFile( theFileName );
320 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
325 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
327 AltitudePoints aPoints;
329 // Try to import the file
330 if ( aFileSuf == "xyz" )
331 aRes = importFromXYZFile( aFile, aPoints );
338 // Update file path and altitude points of this Bathymetry
339 SetFilePath( theFileName );
340 SetAltitudePoints( aPoints );
343 return aRes && !aPoints.isEmpty();
346 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
347 AltitudePoints& thePoints )
349 if ( !theFile.isOpen() )
352 // Strings in file is written as:
353 // 1. X(float) Y(float) Z(float)
354 // 2. X(float) Y(float) Z(float)
357 while ( !theFile.atEnd() )
359 QString aLine = theFile.readLine().simplified();
360 if ( aLine.isEmpty() )
363 QStringList aValues = aLine.split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
364 if ( aValues.length() < 3 )
367 AltitudePoint aPoint;
369 QString anX = aValues.value( 0 );
370 QString anY = aValues.value( 1 );
371 QString aZ = aValues.value( 2 );
373 bool isXOk = false, isYOk = false, isZOk = false;
375 aPoint.SetX( anX.toDouble( &isXOk ) );
376 aPoint.SetY( anY.toDouble( &isYOk ) );
377 aPoint.SetZ( aZ.toDouble( &isZOk ) );
379 if ( !isXOk || !isYOk || !isZOk )