int VizGeorefSpline2D::get_point( const double Px, const double Py, double *vars ) { int v, r; double tmp, Pu; double fact; int leftP=0, rightP=0, found = 0; switch ( type ) { case VIZ_GEOREF_SPLINE_ZERO_POINTS : for ( v = 0; v < _nof_vars; v++ ) vars[v] = 0.0; break; case VIZ_GEOREF_SPLINE_ONE_POINT : for ( v = 0; v < _nof_vars; v++ ) vars[v] = rhs[v][3]; break; case VIZ_GEOREF_SPLINE_TWO_POINTS : fact = _dx * ( Px - x[0] ) + _dy * ( Py - y[0] ); for ( v = 0; v < _nof_vars; v++ ) vars[v] = ( 1 - fact ) * rhs[v][3] + fact * rhs[v][4]; break; case VIZ_GEOREF_SPLINE_ONE_DIMENSIONAL : Pu = _dx * ( Px - x[0] ) + _dy * ( Py - y[0] ); if ( Pu <= u[index[0]] ) { leftP = index[0]; rightP = index[1]; } else if ( Pu >= u[index[_nof_points-1]] ) { leftP = index[_nof_points-2]; rightP = index[_nof_points-1]; } else { for ( r = 1; !found && r < _nof_points; r++ ) { leftP = index[r-1]; rightP = index[r]; if ( Pu >= u[leftP] && Pu <= u[rightP] ) found = 1; } } fact = ( Pu - u[leftP] ) / ( u[rightP] - u[leftP] ); for ( v = 0; v < _nof_vars; v++ ) vars[v] = ( 1.0 - fact ) * rhs[v][leftP+3] + fact * rhs[v][rightP+3]; break; case VIZ_GEOREF_SPLINE_FULL : for ( v = 0; v < _nof_vars; v++ ) vars[v] = coef[v][0] + coef[v][1] * Px + coef[v][2] * Py; for ( r = 0; r < _nof_points; r++ ) { tmp = base_func( Px, Py, x[r], y[r] ); for ( v= 0; v < _nof_vars; v++ ) vars[v] += coef[v][r+3] * tmp; } break; case VIZ_GEOREF_SPLINE_POINT_WAS_ADDED : fprintf(stderr, " A point was added after the last solve\n"); fprintf(stderr, " NO interpolation - return values are zero\n"); for ( v = 0; v < _nof_vars; v++ ) vars[v] = 0.0; return(0); break; case VIZ_GEOREF_SPLINE_POINT_WAS_DELETED : fprintf(stderr, " A point was deleted after the last solve\n"); fprintf(stderr, " NO interpolation - return values are zero\n"); for ( v = 0; v < _nof_vars; v++ ) vars[v] = 0.0; return(0); break; default : return(0); break; } return(1); }
const base_type* base() const { return &base_func(); }
int VizGeorefSpline2D::solve(void) { int r, c, v; int p; // No points at all if ( _nof_points < 1 ) { type = VIZ_GEOREF_SPLINE_ZERO_POINTS; return(0); } // Only one point if ( _nof_points == 1 ) { type = VIZ_GEOREF_SPLINE_ONE_POINT; return(1); } // Just 2 points - it is necessarily 1D case if ( _nof_points == 2 ) { _dx = x[1] - x[0]; _dy = y[1] - y[0]; double fact = 1.0 / ( _dx * _dx + _dy * _dy ); _dx *= fact; _dy *= fact; type = VIZ_GEOREF_SPLINE_TWO_POINTS; return(2); } // More than 2 points - first we have to check if it is 1D or 2D case double xmax = x[0], xmin = x[0], ymax = y[0], ymin = y[0]; double delx, dely; double xx, yy; double sumx = 0.0f, sumy= 0.0f, sumx2 = 0.0f, sumy2 = 0.0f, sumxy = 0.0f; double SSxx, SSyy, SSxy; for ( p = 0; p < _nof_points; p++ ) { xx = x[p]; yy = y[p]; xmax = MAX( xmax, xx ); xmin = MIN( xmin, xx ); ymax = MAX( ymax, yy ); ymin = MIN( ymin, yy ); sumx += xx; sumx2 += xx * xx; sumy += yy; sumy2 += yy * yy; sumxy += xx * yy; } delx = xmax - xmin; dely = ymax - ymin; SSxx = sumx2 - sumx * sumx / _nof_points; SSyy = sumy2 - sumy * sumy / _nof_points; SSxy = sumxy - sumx * sumy / _nof_points; if ( delx < 0.001 * dely || dely < 0.001 * delx || fabs ( SSxy * SSxy / ( SSxx * SSyy ) ) > 0.99 ) { int p1; type = VIZ_GEOREF_SPLINE_ONE_DIMENSIONAL; _dx = _nof_points * sumx2 - sumx * sumx; _dy = _nof_points * sumy2 - sumy * sumy; double fact = 1.0 / sqrt( _dx * _dx + _dy * _dy ); _dx *= fact; _dy *= fact; for ( p = 0; p < _nof_points; p++ ) { double dxp = x[p] - x[0]; double dyp = y[p] - y[0]; u[p] = _dx * dxp + _dy * dyp; unused[p] = 1; } for ( p = 0; p < _nof_points; p++ ) { int min_index = -1; double min_u = 0; for ( p1 = 0; p1 < _nof_points; p1++ ) { if ( unused[p1] ) { if ( min_index < 0 || u[p1] < min_u ) { min_index = p1; min_u = u[p1]; } } } index[p] = min_index; unused[min_index] = 0; } return(3); } type = VIZ_GEOREF_SPLINE_FULL; // Make the necessary memory allocations if ( _AA ) CPLFree(_AA); if ( _Ainv ) CPLFree(_Ainv); _nof_eqs = _nof_points + 3; _AA = ( double * )CPLCalloc( _nof_eqs * _nof_eqs, sizeof( double ) ); _Ainv = ( double * )CPLCalloc( _nof_eqs * _nof_eqs, sizeof( double ) ); // Calc the values of the matrix A for ( r = 0; r < 3; r++ ) for ( c = 0; c < 3; c++ ) A(r,c) = 0.0; for ( c = 0; c < _nof_points; c++ ) { A(0,c+3) = 1.0; A(1,c+3) = x[c]; A(2,c+3) = y[c]; A(c+3,0) = 1.0; A(c+3,1) = x[c]; A(c+3,2) = y[c]; } for ( r = 0; r < _nof_points; r++ ) for ( c = r; c < _nof_points; c++ ) { A(r+3,c+3) = base_func( x[r], y[r], x[c], y[c] ); if ( r != c ) A(c+3,r+3 ) = A(r+3,c+3); } #if VIZ_GEOREF_SPLINE_DEBUG for ( r = 0; r < _nof_eqs; r++ ) { for ( c = 0; c < _nof_eqs; c++ ) fprintf(stderr, "%f", A(r,c)); fprintf(stderr, "\n"); } #endif // Invert the matrix int status = matrixInvert( _nof_eqs, _AA, _Ainv ); if ( !status ) { fprintf(stderr, " There is a problem to invert the interpolation matrix\n"); return 0; } // calc the coefs for ( v = 0; v < _nof_vars; v++ ) for ( r = 0; r < _nof_eqs; r++ ) { coef[v][r] = 0.0; for ( c = 0; c < _nof_eqs; c++ ) coef[v][r] += Ainv(r,c) * rhs[v][c]; } return(4); }