Skip to content

Commit 9d816ad

Browse files
committed
Fix #833
1 parent e841f58 commit 9d816ad

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: lidR
22
Type: Package
33
Title: Airborne LiDAR Data Manipulation and Visualization for Forestry
44
Applications
5-
Version: 4.2.2
5+
Version: 4.2.3
66
Authors@R: c(
77
person("Jean-Romain", "Roussel", email = "[email protected]", role = c("aut", "cre", "cph")),
88
person("David", "Auty", email = "", role = c("aut", "ctb"), comment = "Reviews the documentation"),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
If you are viewing this file on CRAN, please check [the latest news on GitHub](https://github.com/r-lidar/lidR/blob/master/NEWS.md) where the formatting is also better
22

3+
## lidR v4.2.3 (Release date: ...)
4+
5+
- Fix #831 numerical accuracy issue in 3D grid partition.
6+
37
## lidR v4.2.2 (Release date: 2025-11-04)
48

59
- Use `nanoflann` for knn computation. Numerous functions are faster.

inst/include/lidR/Grid3D.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,16 @@ inline Grid3D::Grid3D(const Rcpp::S4 las, double res)
9191
if (z[i] > zmax) zmax = z[i];
9292
}
9393

94-
xmin = ROUNDANY(xmin - 0.5 * xres, xres);
95-
ymin = ROUNDANY(ymin - 0.5 * yres, yres);
96-
zmin = ROUNDANY(zmin - 0.5 * zres, zres);
97-
xmax = ROUNDANY(xmax + 0.5 * xres, xres);
98-
ymax = ROUNDANY(ymax + 0.5 * yres, yres);
99-
zmax = ROUNDANY(zmax + 0.5 * zres, zres);
100-
101-
xmin -= xres;
102-
xmax += xres;
103-
ymin -= yres;
104-
ymax += yres;
105-
zmin -= zres;
106-
zmax += zres;
107-
108-
ncols = static_cast<int64_t>((xmax - xmin) / xres);
109-
nrows = static_cast<int64_t>((ymax - ymin) / yres);
110-
nlayers = static_cast<int64_t>((zmax - zmin) / zres);
94+
xmin = std::floor(xmin / xres) * xres;
95+
xmax = std::ceil (xmax / xres) * xres;
96+
ymin = std::floor(ymin / yres) * yres;
97+
ymax = std::ceil (ymax / yres) * yres;
98+
zmin = std::floor(zmin / zres) * zres;
99+
zmax = std::ceil (zmax / zres) * zres;
100+
101+
ncols = (int64_t)std::floor((xmax - xmin) / xres) + 1;
102+
nrows = (int64_t)std::floor((ymax - ymin) / yres) + 1;
103+
nlayers = (int64_t)std::floor((zmax - zmin) / zres) + 1;
111104

112105
uint64_t max_cells =
113106
static_cast<uint64_t>(ncols) *
@@ -137,11 +130,12 @@ inline int64_t Grid3D::get_cell(double x, double y, double z)
137130
}
138131

139132
int64_t col = std::floor((x - xmin) / xres);
140-
int64_t row = std::floor((y - ymin) / yres); // Fixed formula for row
133+
int64_t row = std::floor((y - ymin) / yres);
141134
int64_t lay = std::floor((z - zmin) / zres);
142135

143136
if (col < 0 || col >= ncols || row < 0 || row >= nrows || lay < 0 || lay >= nlayers)
144137
{
138+
Rcpp::Rcout << col << row << lay << std::endl;
145139
Rcpp::stop("Internal error in spatial index: indices out of range.");
146140
}
147141

0 commit comments

Comments
 (0)