📄 grid.cpp
字号:
}
}
return( _Get_ValAtPos_BiLinear(x, y, dx, dy, bByteWise) );
}
//---------------------------------------------------------
inline bool CSG_Grid::_Get_ValAtPos_Fill4x4Submatrix(int x, int y, double z_xy[4][4]) const
{
int ix, iy, px, py;
for(iy=0, py=y-1; iy<4; iy++, py++)
{
for(ix=0, px=x-1; ix<4; ix++, px++)
{
if( !is_InGrid(px, py) )
{
return( false );
}
z_xy[ix][iy] = asDouble(px, py);
}
}
return( true );
}
inline bool CSG_Grid::_Get_ValAtPos_Fill4x4Submatrix(int x, int y, double z_xy[4][4][4]) const
{
for(int iy=0, py=y-1; iy<4; iy++, py++)
{
for(int ix=0, px=x-1; ix<4; ix++, px++)
{
if( !is_InGrid(px, py) )
{
return( false );
}
int v = asInt(px, py);
z_xy[0][ix][iy] = SG_GET_BYTE_0(v);
z_xy[1][ix][iy] = SG_GET_BYTE_1(v);
z_xy[2][ix][iy] = SG_GET_BYTE_2(v);
z_xy[3][ix][iy] = SG_GET_BYTE_3(v);
}
}
return( true );
}
///////////////////////////////////////////////////////////
// //
// Statistics //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
double CSG_Grid::Get_ZMin(bool bZFactor)
{
Update_Statistics();
return( bZFactor ? m_zFactor * m_zMin : m_zMin );
}
//---------------------------------------------------------
double CSG_Grid::Get_ZMax(bool bZFactor)
{
Update_Statistics();
return( bZFactor ? m_zFactor * m_zMax : m_zMax );
}
//---------------------------------------------------------
double CSG_Grid::Get_ZRange(bool bZFactor)
{
return( Get_ZMax(bZFactor) - Get_ZMin(bZFactor) );
}
//---------------------------------------------------------
double CSG_Grid::Get_ArithMean(bool bZFactor)
{
Update_Statistics();
return( bZFactor ? m_zFactor * m_ArithMean : m_ArithMean );
}
//---------------------------------------------------------
double CSG_Grid::Get_Variance(bool bZFactor)
{
Update_Statistics();
return( bZFactor ? m_zFactor * m_Variance : m_Variance);
}
//---------------------------------------------------------
bool CSG_Grid::Update_Statistics(bool bEnforce)
{
int x, y;
long nValues;
double z;
if( is_Valid() && (m_bUpdate || bEnforce) )
{
m_ArithMean = 0.0;
m_Variance = 0.0;
nValues = 0;
for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++)
{
for(x=0; x<Get_NX(); x++)
{
if( !is_NoData_Value(z = asDouble(x, y)) )
{
if( nValues == 0 )
{
m_zMin = m_zMax = z;
}
else if( m_zMin > z )
{
m_zMin = z;
}
else if( m_zMax < z )
{
m_zMax = z;
}
m_ArithMean += z;
m_Variance += z * z;
nValues++;
}
}
}
if( nValues > 0 )
{
m_ArithMean /= (double)nValues;
m_Variance = m_Variance / (double)nValues - m_ArithMean * m_ArithMean;
}
m_bUpdate = false;
SG_UI_Process_Set_Ready();
}
return( m_bUpdate == false );
}
///////////////////////////////////////////////////////////
// //
// Sort //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
double CSG_Grid::Get_Percentile(double Percent, bool bZFactor)
{
int x, y;
if( Percent < 0.0 )
{
Percent = 0.0;
}
else if( Percent > 100.0 )
{
Percent = 100.0;
}
Get_Sorted((int)(Percent * Get_NCells() / 100.0), x, y, true);
return( asDouble(x, y, bZFactor) );
}
//---------------------------------------------------------
bool CSG_Grid::_Sort_Execute(void)
{
long i, j, *Index;
//-----------------------------------------------------
SG_UI_Process_Set_Text(CSG_String::Format(SG_T("%s: %s"), LNG("Create index"), Get_Name()));
Index = (long *)SG_Calloc(Get_NCells(), sizeof(long));
//-----------------------------------------------------
if( (m_bSorted = _Sort_Index(Index)) == false )
{
Sort_Discard();
}
else if( Get_NX() < 65536 && Get_NY() < 65536 )
{
if( m_Sort_4b )
{
Sort_Discard();
}
if( !m_Sort_2b )
{
m_Sort_2b = (unsigned short **)SG_Calloc(2 , sizeof(unsigned short *));
m_Sort_2b[0] = (unsigned short *)SG_Calloc(Get_NCells(), sizeof(unsigned short));
m_Sort_2b[1] = (unsigned short *)SG_Calloc(Get_NCells(), sizeof(unsigned short));
}
for(i=0; i<Get_NCells(); i++)
{
j = Index[Get_NCells() - i - 1];
m_Sort_2b[0][i] = (unsigned short)(j % Get_NX());
m_Sort_2b[1][i] = (unsigned short)(j / Get_NX());
}
}
else
{
if( m_Sort_2b )
{
Sort_Discard();
}
if( !m_Sort_4b )
{
m_Sort_4b = (int **)SG_Calloc(2 , sizeof(int *));
m_Sort_4b[0] = (int *)SG_Calloc(Get_NCells(), sizeof(int));
m_Sort_4b[1] = (int *)SG_Calloc(Get_NCells(), sizeof(int));
}
for(i=0; i<Get_NCells(); i++)
{
j = Index[Get_NCells() - i - 1];
m_Sort_4b[0][i] = (int)(j % Get_NX());
m_Sort_4b[1][i] = (int)(j / Get_NX());
}
}
//-----------------------------------------------------
SG_Free(Index);
SG_UI_Process_Set_Ready();
SG_UI_Process_Set_Text(LNG("ready"));
return( m_bSorted );
}
//---------------------------------------------------------
void CSG_Grid::Sort_Discard(void)
{
m_bSorted = false;
if( m_Sort_2b )
{
SG_Free(m_Sort_2b[0]);
SG_Free(m_Sort_2b[1]);
SG_Free(m_Sort_2b);
m_Sort_2b = NULL;
}
if( m_Sort_4b )
{
SG_Free(m_Sort_4b[0]);
SG_Free(m_Sort_4b[1]);
SG_Free(m_Sort_4b);
m_Sort_4b = NULL;
}
}
//---------------------------------------------------------
#define SORT_SWAP(a,b) {itemp=(a);(a)=(b);(b)=itemp;}
bool CSG_Grid::_Sort_Index(long *Index)
{
const int M = 7;
int i, j, k, l, ir, n, nCells, *istack, jstack, nstack, indxt, itemp;
double a;
//-----------------------------------------------------
for(i=0, l=0; i<Get_NCells(); i++)
{
if( is_NoData(i) )
{
Index[l++] = i;
}
}
if( (nCells = Get_NCells() - l) > 1 )
{
for(i=0, j=l; i<Get_NCells(); i++)
{
if( !is_NoData(i) )
{
Index[j++] = i;
}
}
//-------------------------------------------------
n = 0;
ir = Get_NCells() - 1;
nstack = 64;
istack = (int *)SG_Malloc(nstack * sizeof(int));
jstack = 0;
for(;;)
{
if( ir - l < M )
{
if( !SG_UI_Process_Set_Progress(n += M - 1, nCells) )
{
SG_Free(istack);
return( false );
}
for(j=l+1; j<=ir; j++)
{
indxt = Index[j];
a = asDouble(indxt);
for(i=j-1; i>=0; i--)
{
if( asDouble(Index[i]) <= a )
{
break;
}
Index[i + 1] = Index[i];
}
Index[i + 1] = indxt;
}
if( jstack == 0 )
{
break;
}
ir = istack[jstack--];
l = istack[jstack--];
}
//---------------------------------------------
else
{
k = (l + ir) >> 1;
SORT_SWAP(Index[k], Index[l + 1]);
if( asDouble( Index[l + 1]) > asDouble(Index[ir]) )
SORT_SWAP(Index[l + 1], Index[ir]);
if( asDouble( Index[l ]) > asDouble(Index[ir]) )
SORT_SWAP(Index[l ], Index[ir]);
if( asDouble( Index[l + 1]) > asDouble(Index[l ]) )
SORT_SWAP(Index[l + 1], Index[l ]);
i = l + 1;
j = ir;
indxt = Index[l];
a = asDouble(indxt);
for(;;)
{
do i++; while(asDouble(Index[i]) < a);
do j--; while(asDouble(Index[j]) > a);
if( j < i )
{
break;
}
SORT_SWAP(Index[i], Index[j]);
}
Index[l] = Index[j];
Index[j] = indxt;
jstack += 2;
if( jstack >= nstack )
{
nstack += 64;
istack = (int *)SG_Realloc(istack, nstack * sizeof(int));
}
if( ir - i + 1 >= j - l )
{
istack[jstack] = ir;
istack[jstack - 1] = i;
ir = j - 1;
}
else
{
istack[jstack] = j - 1;
istack[jstack - 1] = l;
l = i;
}
}
}
//-------------------------------------------------
SG_Free(istack);
return( true );
}
return( false );
}
#undef SORT_SWAP
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -