Skip to content

Commit

Permalink
- Fixed out of bounds memory issue
Browse files Browse the repository at this point in the history
- Propogated remove_edges argument to Lambert term in rayshade
  • Loading branch information
tylermorganwall committed May 21, 2018
1 parent ee9c575 commit 8a92660
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
6 changes: 4 additions & 2 deletions R/lambshade.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions R/rayshade.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 12 additions & 4 deletions src/lambshade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
Expand All @@ -35,17 +35,25 @@ 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);
}
}
}
}
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);
}

0 comments on commit 8a92660

Please sign in to comment.