From 8a92660d6e903427ed12637e829808583743cd85 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sun, 20 May 2018 21:47:53 -0400 Subject: [PATCH] - Fixed out of bounds memory issue - Propogated remove_edges argument to Lambert term in rayshade --- R/lambshade.R | 6 ++++-- R/rayshade.R | 4 ++-- src/lambshade.cpp | 16 ++++++++++++---- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/R/lambshade.R b/R/lambshade.R index ac8232b2..b8d8c744 100644 --- a/R/lambshade.R +++ b/R/lambshade.R @@ -28,7 +28,9 @@ lambshade = function(heightmap, rayangle, sunangle, zscale = 1, zero_negative = shadowmatrix[shadowmatrix < 0] = 0 } if(remove_edges) { - shadowmatrix = shadowmatrix[c(-1,-nrow(shadowmatrix)),c(-1,-ncol(shadowmatrix))] + shadowmatrixremove = shadowmatrix[c(-1,-nrow(shadowmatrix)),c(-1,-ncol(shadowmatrix))] + return(shadowmatrixremove) + } else { + return(shadowmatrix) } - return(shadowmatrix) } \ No newline at end of file diff --git a/R/rayshade.R b/R/rayshade.R index 0c2c6e9f..6e3bad93 100644 --- a/R/rayshade.R +++ b/R/rayshade.R @@ -51,7 +51,7 @@ rayshade = function(heightmap, anglebreaks, sunangle, maxsearch, lambert=TRUE, z shadowmatrix[shadowmatrix<0] = 0 if(lambert) { shadowmatrix = shadowmatrix * lambshade(heightmap, rayangle = mean(anglebreaks), - sunangle = sunangle, zscale = zscale) + sunangle = sunangle, zscale = zscale, remove_edges=remove_edges) } return(shadowmatrix) } else { @@ -79,7 +79,7 @@ rayshade = function(heightmap, anglebreaks, sunangle, maxsearch, lambert=TRUE, z } if(lambert) { shadowmatrix = shadowmatrix * lambshade(heightmap, rayangle = mean(anglebreaks), - sunangle = sunangle, zscale = zscale) + sunangle = sunangle, zscale = zscale, remove_edges=remove_edges) } return(shadowmatrix) } diff --git a/src/lambshade.cpp b/src/lambshade.cpp index 4ea7f273..86164cab 100644 --- a/src/lambshade.cpp +++ b/src/lambshade.cpp @@ -11,7 +11,7 @@ NumericVector vcrossnorm(NumericVector a, NumericVector b) { return(result); } -double dot_product(NumericVector x, NumericVector y) { +double dot_product(const NumericVector x, const NumericVector y) { return std::inner_product(x.begin(), x.end(), y.begin(), 0.0); } @@ -26,7 +26,7 @@ NumericMatrix lambshade_cpp(NumericMatrix heightmap, NumericVector rayvector) { NumericVector tempvector4 = NumericVector::create(0,-1,0); for(int col = 0; col < heightmap.ncol(); col++) { for(int row = 0; row < heightmap.nrow(); row++) { - if(row != 0 || col != 0 || row != heightmap.nrow() - 1 || col != heightmap.ncol() - 1) { + if(row != 0 && col != 0 && row < heightmap.nrow() - 1 && col < heightmap.ncol() - 1) { tempvector1(2) = heightmap(row,col)-heightmap(row,col+1); tempvector2(2) = heightmap(row,col)-heightmap(row+1,col); tempvector3(2) = heightmap(row,col)-heightmap(row,col-1); @@ -35,11 +35,12 @@ NumericMatrix lambshade_cpp(NumericMatrix heightmap, NumericVector rayvector) { storevector2 = vcrossnorm(tempvector3,tempvector4); shaded_matrix(row,col) = dot_product((storevector1 + storevector2)/2, rayvector); } else { - if(row == 0 || col == 0) { + if((row == 0 || col == 0) && (row != heightmap.nrow() - 1 || col != heightmap.ncol() - 1)) { tempvector1(2) = heightmap(row,col)-heightmap(row,col+1); tempvector2(2) = heightmap(row,col)-heightmap(row+1,col); shaded_matrix(row,col) = dot_product(vcrossnorm(tempvector1,tempvector2), rayvector); - } else { + } + if((row != 0 || col != 0) && (row == heightmap.nrow() - 1 || col == heightmap.ncol() - 1)) { tempvector3(2) = heightmap(row,col)-heightmap(row,col-1); tempvector4(2) = heightmap(row,col)-heightmap(row-1,col); shaded_matrix(row,col) = dot_product(vcrossnorm(tempvector3,tempvector4), rayvector); @@ -47,5 +48,12 @@ NumericMatrix lambshade_cpp(NumericMatrix heightmap, NumericVector rayvector) { } } } + tempvector2(2) = heightmap(0,heightmap.ncol())-heightmap(1,heightmap.ncol()); + tempvector3(2) = heightmap(0,heightmap.ncol())-heightmap(0,heightmap.ncol()-1); + shaded_matrix(0,heightmap.ncol()-1) = dot_product(vcrossnorm(tempvector3,tempvector2), rayvector); + tempvector1(2) = heightmap(heightmap.nrow(),0)-heightmap(heightmap.nrow(),1); + tempvector4(2) = heightmap(heightmap.nrow(),0)-heightmap(heightmap.nrow()-1,0); + shaded_matrix(heightmap.nrow()-1,0) = dot_product(vcrossnorm(tempvector4,tempvector1), rayvector); + return(shaded_matrix); } \ No newline at end of file