2 #include "HYDROData_Bathymetry.h"
3 #include "HYDROData_Document.h"
4 #include "HYDROData_Tool.h"
5 #include "HYDROData_PolylineXY.h"
7 #include <boost/math/special_functions/fpclassify.hpp>
12 #include <TDataStd_RealArray.hxx>
13 #include <TDataStd_AsciiString.hxx>
14 #include <TDataStd_Integer.hxx>
20 #include <QStringList>
26 #include <OSD_Timer.hxx>
30 #include "HYDRO_trace.hxx"
32 IMPLEMENT_STANDARD_HANDLE(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
33 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Bathymetry, HYDROData_IAltitudeObject)
35 //HYDROData_QuadtreeNode* HYDROData_Bathymetry::myQuadtree = 0;
36 std::map<int, HYDROData_QuadtreeNode*> HYDROData_Bathymetry::myQuadtrees;
38 HYDROData_Bathymetry::HYDROData_Bathymetry()
39 : HYDROData_IAltitudeObject()
41 //DEBTRACE("HYDROData_Bathymetry constructor start " << this);
43 // myQuadtree = new HYDROData_QuadtreeNode(0, 30, 5, 0.);
44 //DEBTRACE("HYDROData_Bathymetry constructor end " << this);
47 HYDROData_Bathymetry::~HYDROData_Bathymetry()
49 //DEBTRACE("HYDROData_Bathymetry destructor start " << this);
52 // Nodes_3D::iterator it = myListOfNodes.begin();
53 // for( ; it != myListOfNodes.end(); ++it)
55 // myListOfNodes.clear();
58 QStringList HYDROData_Bathymetry::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
60 QStringList aResList = dumpObjectCreation( theTreatedObjects );
61 QString aBathymetryName = GetObjPyName();
63 aResList << QString( "%1.SetAltitudesInverted( %2 );" )
64 .arg( aBathymetryName ).arg( IsAltitudesInverted() );
66 TCollection_AsciiString aFilePath = GetFilePath();
67 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
68 .arg( aBathymetryName ).arg( aFilePath.ToCString() );
70 aResList << QString( "" );
71 aResList << QString( "%1.Update();" ).arg( aBathymetryName );
72 aResList << QString( "" );
77 void HYDROData_Bathymetry::SetAltitudePoints( const AltitudePoints& thePoints )
79 RemoveAltitudePoints();
81 if ( thePoints.IsEmpty() )
85 Handle(TDataStd_RealArray) aCoordsArray =
86 TDataStd_RealArray::Set( myLab.FindChild( DataTag_AltitudePoints ), 0, thePoints.Length() * 3 - 1 );
88 AltitudePoints::Iterator anIter( thePoints );
89 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
91 const AltitudePoint& aPoint = anIter.Value();
93 aCoordsArray->SetValue( i * 3, aPoint.X() );
94 aCoordsArray->SetValue( i * 3 + 1, aPoint.Y() );
95 aCoordsArray->SetValue( i * 3 + 2, aPoint.Z() );
101 HYDROData_Bathymetry::AltitudePoints HYDROData_Bathymetry::GetAltitudePoints() const
103 AltitudePoints aPoints;
105 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudePoints, false );
106 if ( aLabel.IsNull() )
109 Handle(TDataStd_RealArray) aCoordsArray;
110 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoordsArray ) )
113 for ( int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n; )
118 AltitudePoint aPoint;
119 aPoint.SetX( aCoordsArray->Value( i++ ) );
120 aPoint.SetY( aCoordsArray->Value( i++ ) );
121 aPoint.SetZ( aCoordsArray->Value( i++ ) );
123 aPoints.Append( aPoint );
129 HYDROData_QuadtreeNode* HYDROData_Bathymetry::GetQuadtreeNodes() const
131 TDF_Label aLabel = myLab.FindChild(DataTag_AltitudePoints, false);
134 int labkey = myLab.Tag();
135 int altkey = aLabel.Tag();
136 //DEBTRACE("GetQuadtreeNodes this labkey altkey "<<this<<" "<<labkey<<" "<<altkey);
137 // if (myQuadtree->isEmpty() )
138 if (myQuadtrees.find(labkey) == myQuadtrees.end())
140 DEBTRACE("GetQuadtreeNodes init " << this << " " << labkey);
141 HYDROData_QuadtreeNode* aQuadtree = new HYDROData_QuadtreeNode(0, 30, 5, 0.);
142 myQuadtrees[labkey] = aQuadtree;
143 TDF_Label aLabel = myLab.FindChild(DataTag_AltitudePoints, false);
147 Handle(TDataStd_RealArray) aCoordsArray;
148 if (!aLabel.FindAttribute(TDataStd_RealArray::GetID(), aCoordsArray))
151 Nodes_3D* aListOfNodes = new Nodes_3D();
153 for (int i = aCoordsArray->Lower(), n = aCoordsArray->Upper(); i <= n;)
158 double x = aCoordsArray->Value(i++);
159 double y = aCoordsArray->Value(i++);
160 double z = aCoordsArray->Value(i++);
161 gp_XYZ* aPoint = new gp_XYZ(x, y, z);
162 aListOfNodes->push_back(aPoint);
164 DEBTRACE(" GetQuadtreeNodes call setNodesAndCompute");
165 aQuadtree->setNodesAndCompute(aListOfNodes);
169 return myQuadtrees[labkey];
172 void HYDROData_Bathymetry::RemoveAltitudePoints()
174 TDF_Label aLabel = myLab.FindChild(DataTag_AltitudePoints, false);
175 if (!aLabel.IsNull())
177 aLabel.ForgetAllAttributes();
182 void interpolateAltitudeForPoints( const gp_XY& thePoint,
183 const HYDROData_Bathymetry::AltitudePoint& theFirstPoint,
184 const HYDROData_Bathymetry::AltitudePoint& theSecPoint,
185 HYDROData_Bathymetry::AltitudePoint& theResPoint,
186 const bool& theIsVertical )
188 double aCoordX = thePoint.X();
189 double aCoordY = thePoint.Y();
193 aCoordX = theFirstPoint.X();
195 if ( !ValuesEquals( theFirstPoint.X(), theSecPoint.X() ) )
197 // Recalculate X coordinate by equation of line from two points
198 aCoordX = ( ( ( thePoint.Y() - theFirstPoint.Y() ) * ( theSecPoint.X() - theFirstPoint.X() ) ) /
199 ( theSecPoint.Y() - theFirstPoint.Y() ) ) + theFirstPoint.X();
204 aCoordY = theFirstPoint.Y();
206 if ( !ValuesEquals( theFirstPoint.Y(), theSecPoint.Y() ) )
208 // Recalculate y by equation of line from two points
209 aCoordY = ( ( ( thePoint.X() - theFirstPoint.X() ) * ( theSecPoint.Y() - theFirstPoint.Y() ) ) /
210 ( theSecPoint.X() - theFirstPoint.X() ) ) + theFirstPoint.Y();
214 theResPoint.SetX( aCoordX );
215 theResPoint.SetY( aCoordY );
217 // Calculate coefficient for interpolation
218 double aLength = Sqrt( Pow( theSecPoint.Y() - theFirstPoint.Y(), 2 ) +
219 Pow( theSecPoint.X() - theFirstPoint.X(), 2 ) );
221 double aInterCoeff = 0;
223 aInterCoeff = ( theSecPoint.Z() - theFirstPoint.Z() ) / aLength;
226 double aNewLength = Sqrt( Pow( theResPoint.Y() - theFirstPoint.Y(), 2 ) +
227 Pow( theResPoint.X() - theFirstPoint.X(), 2 ) );
229 // Calculate interpolated value
230 double aResVal = theFirstPoint.Z() + aInterCoeff * aNewLength;
232 theResPoint.SetZ( aResVal );
235 double HYDROData_Bathymetry::GetAltitudeForPoint(const gp_XY& thePoint) const
237 //DEBTRACE("GetAltitudeForPoint p(" << thePoint.X() << ", " << thePoint.Y() << ")");
238 double anInvalidAltitude = GetInvalidAltitude();
239 double aResAltitude = anInvalidAltitude;
241 HYDROData_QuadtreeNode* aQuadtree = GetQuadtreeNodes();
244 DEBTRACE(" no Quadtree");
248 std::map<double, const gp_XYZ*> dist2nodes;
249 aQuadtree->NodesAround(thePoint, dist2nodes, 1.0);
250 if (dist2nodes.size())
252 std::map<double, const gp_XYZ*>::const_iterator it = dist2nodes.begin();
253 aResAltitude = it->second->Z();
254 //DEBTRACE(" number of points found: " << dist2nodes.size() << " nearest z: " << aResAltitude);
260 // AltitudePoints anAltitudePoints = GetAltitudePoints();
261 // if ( anAltitudePoints.IsEmpty() )
262 // return aResAltitude;
264 // QPolygonF aBoundingRect;
267 // // [ 0 (top-left) ] [ 1 (top-right) ]
269 // // [ 2 (bot-left) ] [ 3 (bot-right) ]
270 // AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
271 // AltitudePoint( DBL_MAX, -DBL_MAX, anInvalidAltitude ),
272 // AltitudePoint( -DBL_MAX, DBL_MAX, anInvalidAltitude ),
273 // AltitudePoint( DBL_MAX, DBL_MAX, anInvalidAltitude ) };
275 // AltitudePoints::Iterator anIter( anAltitudePoints );
276 // for ( ; anIter.More(); anIter.Next() )
278 // const AltitudePoint& aPoint = anIter.Value();
280 // double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
281 // double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
283 // if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
285 // if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
287 // aResAltitude = aPoint.Z();
288 // return aResAltitude;
290 // else if ( aDeltaY < 0 ) // top side
293 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
294 // aBounds[ 0 ] = aPoint;
295 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
296 // aBounds[ 1 ] = aPoint;
301 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
302 // aBounds[ 2 ] = aPoint;
303 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
304 // aBounds[ 3 ] = aPoint;
307 // else if ( aDeltaX < 0 ) // left side
309 // if ( ValuesEquals( aDeltaY, 0.0 ) )
312 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
313 // aBounds[ 0 ] = aPoint;
314 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
315 // aBounds[ 2 ] = aPoint;
317 // else if ( aDeltaY < 0 )
319 // // top left corner
320 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
321 // aBounds[ 0 ] = aPoint;
325 // // bottom left corner
326 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
327 // aBounds[ 2 ] = aPoint;
330 // else // right side
332 // if ( ValuesEquals( aDeltaY, 0.0 ) )
335 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
336 // aBounds[ 1 ] = aPoint;
337 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
338 // aBounds[ 3 ] = aPoint;
340 // else if ( aDeltaY < 0 )
342 // // top right corner
343 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
344 // aBounds[ 1 ] = aPoint;
348 // // bottom right corner
349 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
350 // aBounds[ 3 ] = aPoint;
354 // // Update bounding rectangle of our global grid
355 // aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
358 // const double LIMIT = 1E300;
359 // if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
360 // fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
361 // fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
362 // fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
363 // return anInvalidAltitude;
366 // // Check if requested point is inside of our bounding rectangle
367 // if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
368 // return aResAltitude;
370 // // Calculate result altitude for point
371 // AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
373 // // At first we merge top and bottom borders
374 // if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
375 // interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
377 // if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
378 // interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
380 // AltitudePoint aResPoint( aFirstPoint );
382 // // At last we merge left and right borders
383 // if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
384 // interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
386 // aResAltitude = aResPoint.Z();
388 // return aResAltitude;
391 void HYDROData_Bathymetry::SetFilePath( const TCollection_AsciiString& theFilePath )
393 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
396 TCollection_AsciiString HYDROData_Bathymetry::GetFilePath() const
398 TCollection_AsciiString aRes;
400 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
401 if ( !aLabel.IsNull() )
403 Handle(TDataStd_AsciiString) anAsciiStr;
404 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
405 aRes = anAsciiStr->Get();
411 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
412 const bool theIsUpdate )
414 bool anIsAltitudesInverted = IsAltitudesInverted();
415 if ( anIsAltitudesInverted == theIsInverted )
418 TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
425 // Update altitude points
426 AltitudePoints anAltitudePoints = GetAltitudePoints();
427 if ( anAltitudePoints.IsEmpty() )
430 AltitudePoints::Iterator anIter( anAltitudePoints );
431 for ( ; anIter.More(); anIter.Next() )
433 AltitudePoint& aPoint = anIter.ChangeValue();
434 aPoint.SetZ( aPoint.Z() * -1 );
437 SetAltitudePoints( anAltitudePoints );
440 bool HYDROData_Bathymetry::IsAltitudesInverted() const
444 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
445 if ( !aLabel.IsNull() )
447 Handle(TDataStd_Integer) anIntVal;
448 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
449 aRes = (bool)anIntVal->Get();
455 bool HYDROData_Bathymetry::ImportFromFile( const TCollection_AsciiString& theFileName )
457 // Try to open the file
458 QFile aFile( theFileName.ToCString() );
459 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
464 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
466 AltitudePoints aPoints;
468 // Try to import the file
469 if ( aFileSuf == "xyz" )
470 aRes = importFromXYZFile( aFile, aPoints );
476 // Convert from global to local CS
477 Handle_HYDROData_Document aDoc = HYDROData_Document::Document( myLab );
478 AltitudePoints::Iterator anIter( aPoints );
479 for ( ; anIter.More(); anIter.Next() )
481 AltitudePoint& aPoint = anIter.ChangeValue();
482 aDoc->Transform( aPoint, true );
487 // Update file path and altitude points of this Bathymetry
488 SetFilePath( theFileName );
489 SetAltitudePoints( aPoints );
492 return aRes && !aPoints.IsEmpty();
495 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
496 AltitudePoints& thePoints ) const
498 if ( !theFile.isOpen() )
501 // Strings in file is written as:
502 // 1. X(float) Y(float) Z(float)
503 // 2. X(float) Y(float) Z(float)
511 bool anIsAltitudesInverted = IsAltitudesInverted();
512 while ( !theFile.atEnd() )
514 QString aLine = theFile.readLine().simplified();
515 if ( aLine.isEmpty() )
518 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
519 if ( aValues.length() < 3 )
522 AltitudePoint aPoint;
524 QString anX = aValues.value( 0 );
525 QString anY = aValues.value( 1 );
526 QString aZ = aValues.value( 2 );
528 bool isXOk = false, isYOk = false, isZOk = false;
530 aPoint.SetX( anX.toDouble( &isXOk ) );
531 aPoint.SetY( anY.toDouble( &isYOk ) );
532 aPoint.SetZ( aZ.toDouble( &isZOk ) );
534 if ( !isXOk || !isYOk || !isZOk )
537 if ( boost::math::isnan( aPoint.X() ) || boost::math::isinf( aPoint.X() ) ||
538 boost::math::isnan( aPoint.Y() ) || boost::math::isinf( aPoint.Y() ) ||
539 boost::math::isnan( aPoint.Z() ) || boost::math::isinf( aPoint.Z() ) )
542 // Invert the z value if requested
543 if ( anIsAltitudesInverted )
544 aPoint.SetZ( -aPoint.Z() );
546 thePoints.Append( aPoint );
551 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
552 aTimer.Show( stream );
559 Handle_HYDROData_PolylineXY HYDROData_Bathymetry::CreateBoundaryPolyline() const
561 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
562 Handle_HYDROData_PolylineXY aResult =
563 Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
565 if( aResult.IsNull() )
569 QString aPolylinePref = GetName() + "_Boundary";
570 QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
571 aResult->SetName( aPolylineName );
573 double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
575 AltitudePoints aPoints = GetAltitudePoints();
577 AltitudePoints::Iterator anIter( aPoints );
578 for ( ; anIter.More(); anIter.Next() )
580 const AltitudePoint& aPoint = anIter.Value();
582 double x = aPoint.X(), y = aPoint.Y();
583 if( isFirst || x<Xmin )
585 if( isFirst || x>Xmax )
587 if( isFirst || y<Ymin )
589 if( isFirst || y>Ymax )
594 aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
595 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
596 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
597 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
598 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );
604 void HYDROData_Bathymetry::UpdateLocalCS( double theDx, double theDy )
606 gp_XYZ aDelta( theDx, theDy, 0 );
607 AltitudePoints aPoints = GetAltitudePoints();
608 AltitudePoints::Iterator anIter( aPoints );
609 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
611 AltitudePoint& aPoint = anIter.ChangeValue();
614 SetAltitudePoints( aPoints );