62 BYTE CSG_Grid::m_Bitmask[8] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };
82 if( !pGrid->
is_Valid() ) {
delete(pGrid);
return( NULL ); }
return( pGrid );
92 if( !pGrid->
is_Valid() ) {
delete(pGrid);
return( NULL ); }
return( pGrid );
98 pGrid =
new CSG_Grid(pGrid, Type, bCached);
100 if( !pGrid->
is_Valid() ) {
delete(pGrid);
return( NULL ); }
return( pGrid );
108 if( !pGrid->
is_Valid() ) {
delete(pGrid);
return( NULL ); }
return( pGrid );
114 CSG_Grid *pGrid =
new CSG_Grid(Type, NX, NY, Cellsize, xMin, yMin, bCached);
116 if( !pGrid->
is_Valid() ) {
delete(pGrid);
return( NULL ); }
return( pGrid );
203 void CSG_Grid::_On_Construction(
void)
209 m_Cache_Stream = NULL;
211 m_Cache_bSwap =
false;
212 m_Cache_bFlip =
false;
242 #pragma omp parallel for
243 for(
int y=0; y<
Get_NY(); y++)
for(
int x=0; x<
Get_NX(); x++)
276 switch( (m_Type = Type) )
296 if( m_System.
Create(System) ==
false )
308 return( _Memory_Create(bCached) );
322 if( _Load_PGSQL (File, bCached, bLoadData)
323 || _Load_Native (File, bCached, bLoadData)
324 || _Load_Compressed(File, bCached, bLoadData)
325 || _Load_Surfer (File, bCached, bLoadData)
326 || _Load_External (File, bCached, bLoadData) )
403 if( (Scale != m_zScale && Scale != 0.) || Offset != m_zOffset )
440 return( m_Values != NULL ||
is_Cached() );
470 return( m_System == System );
490 if( y >= 0 && y <
Get_NY() )
494 for(
int x=0; x<
Get_NX(); x++)
508 for(
int x=0; x<
Get_NX(); x++)
543 return(
Get_Value(p.
x, p.
y, Value, Resampling, bNoData, bByteWise) );
554 if( bNoData ||
is_InGrid(ix + (
int)(0.5 + dx), iy + (
int)(0.5 + dy)) )
577 inline bool CSG_Grid::_Get_ValAtPos_NearestNeighbour(
double &Value,
int x,
int y,
double dx,
double dy)
const
579 if(
is_InGrid(x = x + (
int)(0.5 + dx), y = y + (
int)(0.5 + dy)) )
595 #define BILINEAR_ADD(ix, iy, d) if( is_InGrid(ix, iy) ) {\
596 n += d; z += d * asDouble(ix, iy);\
600 #define BILINEAR_ADD_BYTE(ix, iy, d) if( is_InGrid(ix, iy) ) {\
601 n += d; sLong v = asInt(ix, iy);\
602 z[0] += d * SG_GET_BYTE_0(v);\
603 z[1] += d * SG_GET_BYTE_1(v);\
604 z[2] += d * SG_GET_BYTE_2(v);\
605 z[3] += d * SG_GET_BYTE_3(v);\
609 inline bool CSG_Grid::_Get_ValAtPos_BiLinear(
double &Value,
int x,
int y,
double dx,
double dy,
bool bByteWise)
const
613 double z = 0., n = 0.;
658 inline double CSG_Grid::_Get_ValAtPos_BiCubicSpline(
double dx,
double dy,
double v_xy[4][4])
const
660 #define BiCubicSpline(d, v) (v[1] + 0.5 * d * (v[2] - v[0] + d * (2 * v[0] - 5 * v[1] + 4 * v[2] - v[3] + d * (3 * (v[1] - v[2]) + v[3] - v[0]))))
664 for(
int ix=0; ix<4; ix++)
673 inline bool CSG_Grid::_Get_ValAtPos_BiCubicSpline(
double &Value,
int x,
int y,
double dx,
double dy,
bool bByteWise)
const
679 if( _Get_ValAtPos_Fill4x4Submatrix(x, y, v_xy) )
681 Value = _Get_ValAtPos_BiCubicSpline(dx, dy, v_xy);
688 double v_xy[4][4][4];
690 if( _Get_ValAtPos_Fill4x4Submatrix(x, y, v_xy) )
693 _Get_ValAtPos_BiCubicSpline(dx, dy, v_xy[0]),
694 _Get_ValAtPos_BiCubicSpline(dx, dy, v_xy[1]),
695 _Get_ValAtPos_BiCubicSpline(dx, dy, v_xy[2]),
696 _Get_ValAtPos_BiCubicSpline(dx, dy, v_xy[3])
712 inline double CSG_Grid::_Get_ValAtPos_BSpline(
double dx,
double dy,
double v_xy[4][4])
const
716 for(
int i=0; i<4; i++)
721 if( (d = i - dx + 1.) > 0. ) s += d*d*d;
722 if( (d = i - dx + 0.) > 0. ) s += -4. * d*d*d;
723 if( (d = i - dx - 1.) > 0. ) s += 6. * d*d*d;
724 if( (d = i - dx - 2.) > 0. ) s += -4. * d*d*d;
729 if( (d = i - dy + 1.) > 0. ) s += d*d*d;
730 if( (d = i - dy + 0.) > 0. ) s += -4. * d*d*d;
731 if( (d = i - dy - 1.) > 0. ) s += 6. * d*d*d;
732 if( (d = i - dy - 2.) > 0. ) s += -4. * d*d*d;
739 for(
int iy=0; iy<4; iy++)
741 for(
int ix=0; ix<4; ix++)
743 z += v_xy[ix][iy] * Rx[ix] * Ry[iy];
751 inline bool CSG_Grid::_Get_ValAtPos_BSpline(
double &Value,
int x,
int y,
double dx,
double dy,
bool bByteWise)
const
757 if( _Get_ValAtPos_Fill4x4Submatrix(x, y, v_xy) )
759 Value = _Get_ValAtPos_BSpline(dx, dy, v_xy);
766 double v_xy[4][4][4];
768 if( _Get_ValAtPos_Fill4x4Submatrix(x, y, v_xy) )
771 _Get_ValAtPos_BSpline(dx, dy, v_xy[0]),
772 _Get_ValAtPos_BSpline(dx, dy, v_xy[1]),
773 _Get_ValAtPos_BSpline(dx, dy, v_xy[2]),
774 _Get_ValAtPos_BSpline(dx, dy, v_xy[3])
790 inline bool CSG_Grid::_Get_ValAtPos_Fill4x4Submatrix(
int x,
int y,
double v_xy[4][4])
const
792 int ix, iy, jx, jy, nNoData = 0;
795 for(iy=0, jy=y-1; iy<4; iy++, jy++)
797 for(ix=0, jx=x-1; ix<4; ix++, jx++)
813 for(
int i=0; nNoData>0 && nNoData<16 && i<16; i++)
817 for(iy=0; iy<4; iy++)
for(ix=0; ix<4; ix++)
819 t_xy[ix][iy] = v_xy[ix][iy];
822 for(iy=0; iy<4; iy++)
for(ix=0; ix<4; ix++)
829 for(jy=iy-1; jy<=iy+1; jy++)
for(jx=ix-1; jx<=ix+1; jx++)
833 s +=
asDouble(jx + x - 1, jy + y - 1);
836 else if( jy >= 0 && jy < 4 && jx >= 0 && jx < 4 && !
is_NoData_Value(t_xy[jx][jy]) )
845 v_xy[ix][iy] = s / n;
854 return( nNoData == 0 );
858 inline bool CSG_Grid::_Get_ValAtPos_Fill4x4Submatrix(
int x,
int y,
double v_xy[4][4][4])
const
860 int ix, iy, jx, jy, nNoData = 0;
863 for(iy=0, jy=y-1; iy<4; iy++, jy++)
865 for(ix=0, jx=x-1; ix<4; ix++, jx++)
869 int z =
asInt(jx, jy);
878 v_xy[0][ix][iy] = -1;
886 for(
int i=0; nNoData>0 && nNoData<16 && i<16; i++)
888 double t_xy[4][4][4];
890 for(iy=0; iy<4; iy++)
for(ix=0; ix<4; ix++)
892 t_xy[0][ix][iy] = v_xy[0][ix][iy];
893 t_xy[1][ix][iy] = v_xy[1][ix][iy];
894 t_xy[2][ix][iy] = v_xy[2][ix][iy];
895 t_xy[3][ix][iy] = v_xy[3][ix][iy];
898 for(iy=0; iy<4; iy++)
for(ix=0; ix<4; ix++)
900 if( t_xy[0][ix][iy] < 0 )
903 double s[4]; s[0] = s[1] = s[2] = s[3] = 0.;
905 for(jy=iy-1; jy<=iy+1; jy++)
for(jx=ix-1; jx<=ix+1; jx++)
909 int z =
asInt(jx + x - 1, jy + y - 1);
917 else if( jy >= 0 && jy < 4 && jx >= 0 && jx < 4 && !
is_NoData_Value(t_xy[0][jx][jy]) )
919 s[0] += t_xy[0][jx][jy];
920 s[1] += t_xy[1][jx][jy];
921 s[2] += t_xy[2][jx][jy];
922 s[3] += t_xy[3][jx][jy];
929 v_xy[0][ix][iy] = s[0] / n;
930 v_xy[1][ix][iy] = s[1] / n;
931 v_xy[2][ix][iy] = s[2] / n;
932 v_xy[3][ix][iy] = s[3] / n;
941 return( nNoData == 0 );
976 m_Statistics += Scaling ? Offset + Scaling * Value : Value;
988 for(
int x=0; x<
Get_NX(); x++)
990 double Value =
asDouble(x, y,
false);
994 m_Statistics += Scaling ? Offset + Scaling * Value : Value;
1050 if( Quantile <= 0. ) {
return(
Get_Min() ); }
1051 if( Quantile >= 1. ) {
return(
Get_Max() ); }
1053 if( bFromHistogram )
1073 return(
Get_Quantile(0.01 * Percentile, bFromHistogram) );
1090 Update();
return( m_Statistics );
1107 if( xMin > xMax || yMin > yMax )
1112 Statistics.
Create(bHoldValues);
1114 int nx = 1 + (xMax - xMin);
1115 int ny = 1 + (yMax - yMin);
1116 sLong nCells = nx * ny;
1124 for(
double i=0; i<(double)nCells; i+=d)
1126 int y = yMin + (int)i / nx;
1127 int x = xMin + (int)i % nx;
1129 double Value =
asDouble(x, y,
false);
1133 Statistics += Scaling ? Offset + Scaling * Value : Value;
1139 for(
int y=yMin; y<=yMax; y++)
1141 for(
int x=xMin; x<=xMax; x++)
1143 double Value =
asDouble(x, y,
false);
1147 Statistics += Scaling ? Offset + Scaling * Value : Value;
1157 #define SG_GRID_HISTOGRAM_CLASSES_DEFAULT 255
1178 return( m_Histogram );
1196 if( xMin > xMax || yMin > yMax )
1203 int nx = 1 + (xMax - xMin);
1204 int ny = 1 + (yMax - yMin);
1205 sLong nCells = nx * ny;
1213 for(
double i=0; i<(double)nCells; i+=d)
1215 int y = yMin + (int)i / nx;
1216 int x = xMin + (int)i % nx;
1218 double Value =
asDouble(x, y,
false);
1222 Histogram += Scaling ? Offset + Scaling * Value : Value;
1228 for(
int y=yMin; y<=yMax; y++)
1230 for(
int x=xMin; x<=xMax; x++)
1232 double Value =
asDouble(x, y,
false);
1236 Histogram += Scaling ? Offset + Scaling * Value : Value;
1242 return( Histogram.
Update() );
1253 bool CSG_Grid::_Set_Index(
void)
1266 sLong i, j, k, l, ir, n, *istack, jstack, nstack, indxt, itemp, nData;
1276 m_Index[--nData] = i;
1315 for(j=l+1; j<=ir; j++)
1320 for(i=j-1; i>=0; i--)
1327 m_Index[i + 1] = m_Index[i];
1330 m_Index[i + 1] = indxt;
1338 ir = istack[jstack--];
1339 l = istack[jstack--];
1345 #define SORT_SWAP(a,b) {itemp=(a);(a)=(b);(b)=itemp;}
1367 do i++;
while(
asDouble(m_Index[i]) < a);
1368 do j--;
while(
asDouble(m_Index[j]) > a);
1380 m_Index[l] = m_Index[j];
1384 if( jstack >= nstack )
1390 if( ir - i + 1 >= j - l )
1392 istack[jstack] = ir;
1393 istack[jstack - 1] = i;
1398 istack[jstack] = j - 1;
1399 istack[jstack - 1] = l;