Skip to content

Commit

Permalink
Fix bug with nanovdb indexing in cmd/nanovdb2pbrt.cpp
Browse files Browse the repository at this point in the history
(Removed the downsampling functinoality for simplicity along the way.)

Fixes #452.
  • Loading branch information
mmp committed Oct 26, 2024
1 parent 20d85f0 commit f9a38ba
Showing 1 changed file with 7 additions and 53 deletions.
60 changes: 7 additions & 53 deletions src/pbrt/cmd/nanovdb2pbrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ static void usage(const std::string &msg = {}) {
R"(usage: nanovdb2pbrt [<options>] <filename.nvdb>
Options:
--downsample <n> Number of times to 2x downsample the volume.
Default: 0
--grid <name> Name of grid to extract. Default: "density"
)");
exit(msg.empty() ? 0 : 1);
Expand Down Expand Up @@ -100,63 +98,19 @@ int main(int argc, char *argv[]) {
Bounds3f bounds(Point3f(bbox.min()[0], bbox.min()[1], bbox.min()[2]),
Point3f(bbox.max()[0], bbox.max()[1], bbox.max()[2]));

int nx = floatGrid->indexBBox().dim()[0];
int ny = floatGrid->indexBBox().dim()[1];
int nz = floatGrid->indexBBox().dim()[2];

std::vector<Float> values;

int z0 = 0, z1 = nz;
int y0 = 0, y1 = ny;
int x0 = 0, x1 = nx;

// Fix the resolution to be a multiple of 2^downsample just to make
// downsampling easy. Chop off one at a time from the bottom and top
// of the range until we get there; the bounding box is updated as
// well so that the remaining volume doesn't shift spatially.
auto round = [=](int &low, int &high, Float &c0, Float &c1) {
Float delta = (c1-c0) / (high-low);
int mult = 1 << downsample; // want a multiple of this in resolution
while ((high - low) % mult) {
++low;
c0 += delta;
if ((high - low) % mult) {
--high;
c1 -= delta;
}
}
return high - low;
};
nz = round(z0, z1, bounds.pMin.z, bounds.pMax.z);
ny = round(y0, y1, bounds.pMin.y, bounds.pMax.y);
nx = round(x0, x1, bounds.pMin.x, bounds.pMax.x);
int x0 = floatGrid->indexBBox().min()[0], x1 = floatGrid->indexBBox().max()[0]+1;
int y0 = floatGrid->indexBBox().min()[1], y1 = floatGrid->indexBBox().max()[1]+1;
int z0 = floatGrid->indexBBox().min()[2], z1 = floatGrid->indexBBox().max()[2]+1;

for (int z = z0; z < z1; ++z)
for (int y = y0; y < y1; ++y)
for (int x = x0; x < x1; ++x) {
for (int z = z0; z <= z1; ++z)
for (int y = y0; y <= y1; ++y)
for (int x = x0; x <= x1; ++x) {
values.push_back(floatGrid->tree().getValue({x, y, z}));
}

while (downsample > 0) {
std::vector<Float> v2;
for (int z = 0; z < nz/2; ++z)
for (int y = 0; y < ny/2; ++y)
for (int x = 0; x < nx/2; ++x) {
auto v = [&](int dx, int dy, int dz) -> Float{
return values[(2*x+dx) + nx * ((2*y+dy) + ny * (2*z+dz))];
};
v2.push_back((v(0,0,0) + v(1,0,0) + v(0,1,0) + v(1,1,0) +
v(0,0,1) + v(1,0,1) + v(0,1,1) + v(1,1,1))/8);
}

values = std::move(v2);
nx /= 2;
ny /= 2;
nz /= 2;
--downsample;
}

printf("\"integer nx\" %d \"integer ny\" %d \"integer nz\" %d\n", nx, ny, nz);
printf("\"integer nx\" %d \"integer ny\" %d \"integer nz\" %d\n", 1+x1-x0, 1+y1-y0, 1+z1-z0);
printf("\t\"point3 p0\" [ %f %f %f ] \"point3 p1\" [ %f %f %f ]\n",
bounds.pMin.x, bounds.pMin.y, bounds.pMin.z,
bounds.pMax.x, bounds.pMax.y, bounds.pMax.z);
Expand Down

0 comments on commit f9a38ba

Please sign in to comment.