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, aQuadtree->getPrecision());
250 while (dist2nodes.size() == 0)
252 aQuadtree->setPrecision(aQuadtree->getPrecision() *2);
253 DEBTRACE("adjust precision to: " << aQuadtree->getPrecision());
254 aQuadtree->NodesAround(thePoint, dist2nodes, aQuadtree->getPrecision());
256 aQuadtree->NodesAround(thePoint, dist2nodes, 5.0);
257 if (dist2nodes.size())
259 std::map<double, const gp_XYZ*>::const_iterator it = dist2nodes.begin();
260 aResAltitude = it->second->Z();
261 DEBTRACE(" number of points found: " << dist2nodes.size() << " nearest z: " << aResAltitude);
265 DEBTRACE(" number of points found: 0");
271 // AltitudePoints anAltitudePoints = GetAltitudePoints();
272 // if ( anAltitudePoints.IsEmpty() )
273 // return aResAltitude;
275 // QPolygonF aBoundingRect;
278 // // [ 0 (top-left) ] [ 1 (top-right) ]
280 // // [ 2 (bot-left) ] [ 3 (bot-right) ]
281 // AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
282 // AltitudePoint( DBL_MAX, -DBL_MAX, anInvalidAltitude ),
283 // AltitudePoint( -DBL_MAX, DBL_MAX, anInvalidAltitude ),
284 // AltitudePoint( DBL_MAX, DBL_MAX, anInvalidAltitude ) };
286 // AltitudePoints::Iterator anIter( anAltitudePoints );
287 // for ( ; anIter.More(); anIter.Next() )
289 // const AltitudePoint& aPoint = anIter.Value();
291 // double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
292 // double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
294 // if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
296 // if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
298 // aResAltitude = aPoint.Z();
299 // return aResAltitude;
301 // else if ( aDeltaY < 0 ) // top side
304 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
305 // aBounds[ 0 ] = aPoint;
306 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
307 // aBounds[ 1 ] = aPoint;
312 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
313 // aBounds[ 2 ] = aPoint;
314 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
315 // aBounds[ 3 ] = aPoint;
318 // else if ( aDeltaX < 0 ) // left side
320 // if ( ValuesEquals( aDeltaY, 0.0 ) )
323 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
324 // aBounds[ 0 ] = aPoint;
325 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
326 // aBounds[ 2 ] = aPoint;
328 // else if ( aDeltaY < 0 )
330 // // top left corner
331 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
332 // aBounds[ 0 ] = aPoint;
336 // // bottom left corner
337 // if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
338 // aBounds[ 2 ] = aPoint;
341 // else // right side
343 // if ( ValuesEquals( aDeltaY, 0.0 ) )
346 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
347 // aBounds[ 1 ] = aPoint;
348 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
349 // aBounds[ 3 ] = aPoint;
351 // else if ( aDeltaY < 0 )
353 // // top right corner
354 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
355 // aBounds[ 1 ] = aPoint;
359 // // bottom right corner
360 // if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
361 // aBounds[ 3 ] = aPoint;
365 // // Update bounding rectangle of our global grid
366 // aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
369 // const double LIMIT = 1E300;
370 // if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
371 // fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
372 // fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
373 // fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
374 // return anInvalidAltitude;
377 // // Check if requested point is inside of our bounding rectangle
378 // if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
379 // return aResAltitude;
381 // // Calculate result altitude for point
382 // AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
384 // // At first we merge top and bottom borders
385 // if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
386 // interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
388 // if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
389 // interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
391 // AltitudePoint aResPoint( aFirstPoint );
393 // // At last we merge left and right borders
394 // if ( aFirstPoint.Y() != aSecPoint.Y() || aFirstPoint.X() != aSecPoint.X() )
395 // interpolateAltitudeForPoints( thePoint, aFirstPoint, aSecPoint, aResPoint, false );
397 // aResAltitude = aResPoint.Z();
399 // return aResAltitude;
402 void HYDROData_Bathymetry::SetFilePath( const TCollection_AsciiString& theFilePath )
404 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
407 TCollection_AsciiString HYDROData_Bathymetry::GetFilePath() const
409 TCollection_AsciiString aRes;
411 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
412 if ( !aLabel.IsNull() )
414 Handle(TDataStd_AsciiString) anAsciiStr;
415 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
416 aRes = anAsciiStr->Get();
422 void HYDROData_Bathymetry::SetAltitudesInverted( const bool theIsInverted,
423 const bool theIsUpdate )
425 bool anIsAltitudesInverted = IsAltitudesInverted();
426 if ( anIsAltitudesInverted == theIsInverted )
429 TDataStd_Integer::Set( myLab.FindChild( DataTag_AltitudesInverted ), (Standard_Integer)theIsInverted );
436 // Update altitude points
437 AltitudePoints anAltitudePoints = GetAltitudePoints();
438 if ( anAltitudePoints.IsEmpty() )
441 AltitudePoints::Iterator anIter( anAltitudePoints );
442 for ( ; anIter.More(); anIter.Next() )
444 AltitudePoint& aPoint = anIter.ChangeValue();
445 aPoint.SetZ( aPoint.Z() * -1 );
448 SetAltitudePoints( anAltitudePoints );
451 bool HYDROData_Bathymetry::IsAltitudesInverted() const
455 TDF_Label aLabel = myLab.FindChild( DataTag_AltitudesInverted, false );
456 if ( !aLabel.IsNull() )
458 Handle(TDataStd_Integer) anIntVal;
459 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
460 aRes = (bool)anIntVal->Get();
466 bool HYDROData_Bathymetry::ImportFromFile( const TCollection_AsciiString& theFileName )
468 // Try to open the file
469 QFile aFile( theFileName.ToCString() );
470 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
475 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
477 AltitudePoints aPoints;
479 // Try to import the file
480 if ( aFileSuf == "xyz" )
481 aRes = importFromXYZFile( aFile, aPoints );
487 // Convert from global to local CS
488 Handle_HYDROData_Document aDoc = HYDROData_Document::Document( myLab );
489 AltitudePoints::Iterator anIter( aPoints );
490 for ( ; anIter.More(); anIter.Next() )
492 AltitudePoint& aPoint = anIter.ChangeValue();
493 aDoc->Transform( aPoint, true );
498 // Update file path and altitude points of this Bathymetry
499 SetFilePath( theFileName );
500 SetAltitudePoints( aPoints );
503 return aRes && !aPoints.IsEmpty();
506 bool HYDROData_Bathymetry::importFromXYZFile( QFile& theFile,
507 AltitudePoints& thePoints ) const
509 if ( !theFile.isOpen() )
512 // Strings in file is written as:
513 // 1. X(float) Y(float) Z(float)
514 // 2. X(float) Y(float) Z(float)
522 bool anIsAltitudesInverted = IsAltitudesInverted();
523 while ( !theFile.atEnd() )
525 QString aLine = theFile.readLine().simplified();
526 if ( aLine.isEmpty() )
529 QStringList aValues = aLine.split( ' ', QString::SkipEmptyParts );
530 if ( aValues.length() < 3 )
533 AltitudePoint aPoint;
535 QString anX = aValues.value( 0 );
536 QString anY = aValues.value( 1 );
537 QString aZ = aValues.value( 2 );
539 bool isXOk = false, isYOk = false, isZOk = false;
541 aPoint.SetX( anX.toDouble( &isXOk ) );
542 aPoint.SetY( anY.toDouble( &isYOk ) );
543 aPoint.SetZ( aZ.toDouble( &isZOk ) );
545 if ( !isXOk || !isYOk || !isZOk )
548 if ( boost::math::isnan( aPoint.X() ) || boost::math::isinf( aPoint.X() ) ||
549 boost::math::isnan( aPoint.Y() ) || boost::math::isinf( aPoint.Y() ) ||
550 boost::math::isnan( aPoint.Z() ) || boost::math::isinf( aPoint.Z() ) )
553 // Invert the z value if requested
554 if ( anIsAltitudesInverted )
555 aPoint.SetZ( -aPoint.Z() );
557 thePoints.Append( aPoint );
562 std::ofstream stream( "W:/HYDRO/WORK/log.txt", std::ofstream::out );
563 aTimer.Show( stream );
570 Handle_HYDROData_PolylineXY HYDROData_Bathymetry::CreateBoundaryPolyline() const
572 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
573 Handle_HYDROData_PolylineXY aResult =
574 Handle_HYDROData_PolylineXY::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
576 if( aResult.IsNull() )
580 QString aPolylinePref = GetName() + "_Boundary";
581 QString aPolylineName = HYDROData_Tool::GenerateObjectName( aDocument, aPolylinePref );
582 aResult->SetName( aPolylineName );
584 double Xmin = 0.0, Xmax = 0.0, Ymin = 0.0, Ymax = 0.0;
586 AltitudePoints aPoints = GetAltitudePoints();
588 AltitudePoints::Iterator anIter( aPoints );
589 for ( ; anIter.More(); anIter.Next() )
591 const AltitudePoint& aPoint = anIter.Value();
593 double x = aPoint.X(), y = aPoint.Y();
594 if( isFirst || x<Xmin )
596 if( isFirst || x>Xmax )
598 if( isFirst || y<Ymin )
600 if( isFirst || y>Ymax )
605 aResult->AddSection( "bound", HYDROData_IPolyline::SECTION_POLYLINE, true );
606 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymin ) );
607 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmin, Ymax ) );
608 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymax ) );
609 aResult->AddPoint( 0, HYDROData_IPolyline::Point( Xmax, Ymin ) );
615 void HYDROData_Bathymetry::UpdateLocalCS( double theDx, double theDy )
617 gp_XYZ aDelta( theDx, theDy, 0 );
618 AltitudePoints aPoints = GetAltitudePoints();
619 AltitudePoints::Iterator anIter( aPoints );
620 for ( int i = 0 ; anIter.More(); ++i, anIter.Next() )
622 AltitudePoint& aPoint = anIter.ChangeValue();
625 SetAltitudePoints( aPoints );