//--------------------------------------------------------- bool SG_Get_Crossing(TSG_Point &Crossing, const TSG_Point &a1, const TSG_Point &a2, const TSG_Point &b1, const TSG_Point &b2, bool bExactMatch) { //----------------------------------------------------- if( bExactMatch && ( (M_GET_MAX(a1.x, a2.x) < M_GET_MIN(b1.x, b2.x)) || (M_GET_MIN(a1.x, a2.x) > M_GET_MAX(b1.x, b2.x)) || (M_GET_MAX(a1.y, a2.y) < M_GET_MIN(b1.y, b2.y)) || (M_GET_MIN(a1.y, a2.y) > M_GET_MAX(b1.y, b2.y)) ) ) { return( false ); } //----------------------------------------------------- if( (a1.x == b1.x && a1.y == b1.y) || (a1.x == b2.x && a1.y == b2.y) ) { Crossing = a1; return( true ); } if( (a2.x == b1.x && a2.y == b1.y) || (a2.x == b2.x && a2.y == b2.y) ) { Crossing = a2; return( true ); } //----------------------------------------------------- double lambda, div, a_dx, a_dy, b_dx, b_dy; a_dx = a2.x - a1.x; a_dy = a2.y - a1.y; b_dx = b2.x - b1.x; b_dy = b2.y - b1.y; if( (div = a_dx * b_dy - b_dx * a_dy) != 0.0 ) { lambda = ((b1.x - a1.x) * b_dy - b_dx * (b1.y - a1.y)) / div; Crossing.x = a1.x + lambda * a_dx; Crossing.y = a1.y + lambda * a_dy; if( !bExactMatch ) { return( true ); } else if( 0.0 <= lambda && lambda <= 1.0 ) { lambda = ((b1.x - a1.x) * a_dy - a_dx * (b1.y - a1.y)) / div; if( 0.0 <= lambda && lambda <= 1.0 ) { return( true ); } } } return( false ); }
bool CGrid_Proximity_Buffer::On_Execute(void){ CSG_Grid *pSource, *pDistance, *pAlloc, *pBuffer; double dBufDist, dDist, cellSize; int x, y, i, j, imin, imax, jmin, jmax, iBufDist, alloc, ival; pSource = Parameters("SOURCE")->asGrid(); pDistance = Parameters("DISTANCE")->asGrid(); pAlloc = Parameters("ALLOC")->asGrid(); pBuffer = Parameters("BUFFER")->asGrid(); ival = Parameters("IVAL")->asInt(); cellSize = pSource->Get_Cellsize(); dBufDist = Parameters("DIST")->asDouble() / cellSize; iBufDist = (int) (dBufDist + 2.0); dBufDist = pow(dBufDist, 2); pDistance->Assign_NoData(); pAlloc->Assign_NoData(); pBuffer->Assign_NoData(); for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( !pSource->is_NoData(x, y) ) { alloc = pSource->asInt(x, y); pAlloc->Set_Value(x, y, alloc); pDistance->Set_Value(x, y, 0.0); imin = M_GET_MAX(0, x-iBufDist); imax = M_GET_MIN(x+iBufDist, Get_NX()); jmin = M_GET_MAX(0, y-iBufDist); jmax = M_GET_MIN(y+iBufDist, Get_NY()); for(i=imin; i<imax; i++) { for(j=jmin; j<jmax; j++) { if( pSource->is_NoData(i, j) ) { dDist= (x-i)*(x-i)+(y-j)*(y-j); if( dDist <= dBufDist && (pDistance->is_NoData(i, j) || pDistance->asDouble(i, j) > dDist) ) { pDistance->Set_Value(i, j, dDist); pAlloc->Set_Value(i, j, alloc); } } }//for }//for }//if }//for }//for for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( !pDistance->is_NoData(x, y) ) { dDist = sqrt(pDistance->asDouble(x, y)) * cellSize; pDistance->Set_Value(x, y, dDist); i = 0; while( i< dDist ) i += ival; pBuffer->Set_Value(x, y, i); } } } return( true ); }