+ if ( anAltitudePoints.IsEmpty() )
+ return aResAltitude;
+
+ QPolygonF aBoundingRect;
+
+ // Boundary plane
+ // [ 0 (top-left) ] [ 1 (top-right) ]
+ // thePoint
+ // [ 2 (bot-left) ] [ 3 (bot-right) ]
+ AltitudePoint aBounds[ 4 ] = { AltitudePoint( -DBL_MAX, -DBL_MAX, anInvalidAltitude ),
+ AltitudePoint( DBL_MAX, -DBL_MAX, anInvalidAltitude ),
+ AltitudePoint( -DBL_MAX, DBL_MAX, anInvalidAltitude ),
+ AltitudePoint( DBL_MAX, DBL_MAX, anInvalidAltitude ) };
+
+ AltitudePoints::Iterator anIter( anAltitudePoints );
+ for ( ; anIter.More(); anIter.Next() )
+ {
+ const AltitudePoint& aPoint = anIter.Value();
+
+ double aDeltaX = Abs( aPoint.X() ) - Abs( thePoint.X() );
+ double aDeltaY = Abs( aPoint.Y() ) - Abs( thePoint.Y() );
+
+ if ( ValuesEquals( aDeltaX, 0.0 ) ) // Both left and right sides
+ {
+ if ( ValuesEquals( aDeltaY, 0.0 ) ) // Both top and bottom sides
+ {
+ aResAltitude = aPoint.Z();
+ return aResAltitude;
+ }
+ else if ( aDeltaY < 0 ) // top side
+ {
+ // top border
+ if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
+ aBounds[ 0 ] = aPoint;
+ if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
+ aBounds[ 1 ] = aPoint;
+ }
+ else
+ {
+ // bottom border
+ if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
+ aBounds[ 2 ] = aPoint;
+ if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
+ aBounds[ 3 ] = aPoint;
+ }
+ }
+ else if ( aDeltaX < 0 ) // left side
+ {
+ if ( ValuesEquals( aDeltaY, 0.0 ) )
+ {
+ // Left border
+ if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
+ aBounds[ 0 ] = aPoint;
+ if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
+ aBounds[ 2 ] = aPoint;
+ }
+ else if ( aDeltaY < 0 )
+ {
+ // top left corner
+ if ( ValuesMoreEquals( aPoint.X(), aBounds[ 0 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 0 ].Y() ) )
+ aBounds[ 0 ] = aPoint;
+ }
+ else
+ {
+ // bottom left corner
+ if ( ValuesMoreEquals( aPoint.X(), aBounds[ 2 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 2 ].Y() ) )
+ aBounds[ 2 ] = aPoint;
+ }
+ }
+ else // right side
+ {
+ if ( ValuesEquals( aDeltaY, 0.0 ) )
+ {
+ // Right border
+ if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
+ aBounds[ 1 ] = aPoint;
+ if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
+ aBounds[ 3 ] = aPoint;
+ }
+ else if ( aDeltaY < 0 )
+ {
+ // top right corner
+ if ( ValuesLessEquals( aPoint.X(), aBounds[ 1 ].X() ) && ValuesMoreEquals( aPoint.Y(), aBounds[ 1 ].Y() ) )
+ aBounds[ 1 ] = aPoint;
+ }
+ else
+ {
+ // bottom right corner
+ if ( ValuesLessEquals( aPoint.X(), aBounds[ 3 ].X() ) && ValuesLessEquals( aPoint.Y(), aBounds[ 3 ].Y() ) )
+ aBounds[ 3 ] = aPoint;
+ }
+ }
+
+ // Update bounding rectangle of our global grid
+ aBoundingRect << QPointF( aPoint.X(), aPoint.Y() );
+ }
+
+ const double LIMIT = 1E300;
+ if( fabs( aBounds[ 0 ].X() ) > LIMIT || fabs( aBounds[ 0 ].Y() ) > LIMIT ||
+ fabs( aBounds[ 1 ].X() ) > LIMIT || fabs( aBounds[ 1 ].Y() ) > LIMIT ||
+ fabs( aBounds[ 2 ].X() ) > LIMIT || fabs( aBounds[ 2 ].Y() ) > LIMIT ||
+ fabs( aBounds[ 3 ].X() ) > LIMIT || fabs( aBounds[ 3 ].Y() ) > LIMIT )
+ return anInvalidAltitude;
+
+
+ // Check if requested point is inside of our bounding rectangle
+ if ( !aBoundingRect.boundingRect().contains( thePoint.X(), thePoint.Y() ) )
+ return aResAltitude;
+
+ // Calculate result altitude for point
+ AltitudePoint aFirstPoint( aBounds[ 0 ] ), aSecPoint( aBounds[ 1 ] );
+
+ // At first we merge top and bottom borders
+ if ( aBounds[ 0 ].Y() != aBounds[ 2 ].Y() || aBounds[ 0 ].X() != aBounds[ 2 ].X() )
+ interpolateAltitudeForPoints( thePoint, aBounds[ 0 ], aBounds[ 2 ], aFirstPoint, true );
+
+ if ( aBounds[ 1 ].Y() != aBounds[ 3 ].Y() || aBounds[ 1 ].X() != aBounds[ 3 ].X() )
+ interpolateAltitudeForPoints( thePoint, aBounds[ 1 ], aBounds[ 3 ], aSecPoint, true );
+
+ AltitudePoint aResPoint( aFirstPoint );