Skip to content

Commit ceff863

Browse files
committed
generalize save_raw_volumes for ui an terminal use
1 parent bb24859 commit ceff863

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

include/neural-graphics-primitives/testbed.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ class Testbed {
485485
vec2 fov_xy() const ;
486486
void set_fov_xy(const vec2& val);
487487
void save_snapshot(const fs::path& path, bool include_optimizer_state, bool compress);
488-
void save_raw_volumes();
488+
void save_raw_volumes(const fs::path &filename, int res, BoundingBox aabb, bool flip_y_and_z_axes);
489489
void load_snapshot(nlohmann::json config);
490490
void load_snapshot(const fs::path& path);
491491
void load_snapshot(std::istream& stream, bool is_compressed = true);

src/testbed.cu

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,24 +1624,7 @@ void Testbed::imgui() {
16241624
save_rgba_grid_to_png_sequence(rgba, dir, res3d, flip_y_and_z_axes);
16251625
}
16261626
if (imgui_colored_button("Save raw volumes", 0.4f)) {
1627-
auto effective_view_dir = flip_y_and_z_axes ? vec3{0.0f, 1.0f, 0.0f} : vec3{0.0f, 0.0f, 1.0f};
1628-
auto old_local = m_render_aabb_to_local;
1629-
auto old_aabb = m_render_aabb;
1630-
m_render_aabb_to_local = mat3::identity();
1631-
auto dir = m_data_path.is_directory() || m_data_path.empty() ? (m_data_path / "volume_raw") : (m_data_path.parent_path() / fmt::format("{}_volume_raw", m_data_path.filename()));
1632-
if (!dir.exists()) {
1633-
fs::create_directory(dir);
1634-
}
1635-
1636-
for (int cascade = 0; (1<<cascade)<= m_aabb.diag().x+0.5f; ++cascade) {
1637-
float radius = (1<<cascade) * 0.5f;
1638-
m_render_aabb = BoundingBox(vec3(0.5f-radius), vec3(0.5f+radius));
1639-
// Dump raw density values that the user can then convert to alpha as they please.
1640-
GPUMemory<vec4> rgba = get_rgba_on_grid(res3d, effective_view_dir, true, 0.0f, true);
1641-
save_rgba_grid_to_raw_file(rgba, dir, res3d, flip_y_and_z_axes, cascade);
1642-
}
1643-
m_render_aabb_to_local = old_local;
1644-
m_render_aabb = old_aabb;
1627+
save_raw_volumes(m_data_path, m_mesh.res, {}, flip_y_and_z_axes);
16451628
}
16461629
}
16471630

@@ -4754,30 +4737,39 @@ void Testbed::save_snapshot(const fs::path& path, bool include_optimizer_state,
47544737
tlog::success() << "Saved snapshot '" << path.str() << "'";
47554738
}
47564739

4757-
void Testbed::save_raw_volumes()
4740+
void Testbed::save_raw_volumes(const fs::path &filename, int res, BoundingBox aabb, bool flip_y_and_z_axes)
47584741
{
4759-
static bool flip_y_and_z_axes = false;
4760-
BoundingBox aabb = (m_testbed_mode == ETestbedMode::Nerf) ? m_render_aabb : m_aabb;
4761-
4762-
auto res3d = get_marching_cubes_res(m_mesh.res, aabb);
47634742
auto effective_view_dir = flip_y_and_z_axes ? vec3{0.0f, 1.0f, 0.0f} : vec3{0.0f, 0.0f, 1.0f};
4764-
auto old_local = m_render_aabb_to_local;
4765-
auto old_aabb = m_render_aabb;
4766-
m_render_aabb_to_local = mat3(1.0f);
4767-
auto dir = m_data_path / "volume_raw";
4743+
mat3 render_aabb_to_local = mat3(1.0f);
4744+
4745+
if (aabb.is_empty())
4746+
{
4747+
aabb = m_testbed_mode == ETestbedMode::Nerf ? m_render_aabb : m_aabb;
4748+
render_aabb_to_local = m_render_aabb_to_local;
4749+
}
4750+
4751+
if (m_testbed_mode != ETestbedMode::Nerf)
4752+
{
4753+
throw std::runtime_error{"Raw volume export is only supported for NeRF."};
4754+
}
4755+
4756+
auto res3d = get_marching_cubes_res(res, aabb);
4757+
4758+
std::string flipped = flip_y_and_z_axes ? "_flipedYZ" : "";
4759+
auto dir =( filename.is_directory() || filename.empty() ? (filename / fmt::format("volume_raw{}",flipped)) : (filename.parent_path() / fmt::format("{}_volume_raw{}", filename.filename(),flipped))) ;
47684760
if (!dir.exists())
47694761
{
47704762
fs::create_directory(dir);
47714763
}
4764+
47724765
for (int cascade = 0; (1 << cascade) <= m_aabb.diag().x + 0.5f; ++cascade)
47734766
{
47744767
float radius = (1 << cascade) * 0.5f;
47754768
m_render_aabb = BoundingBox(vec3(0.5f - radius), vec3(0.5f + radius));
4769+
// Dump raw density values that the user can then convert to alpha as they please.
47764770
GPUMemory<vec4> rgba = get_rgba_on_grid(res3d, effective_view_dir, true, 0.0f, true);
4777-
save_rgba_grid_to_raw_file(rgba, dir.str().c_str(), res3d, flip_y_and_z_axes, cascade);
4771+
save_rgba_grid_to_raw_file(rgba, dir, res3d, flip_y_and_z_axes, cascade);
47784772
}
4779-
m_render_aabb_to_local = old_local;
4780-
m_render_aabb = old_aabb;
47814773
}
47824774

47834775
void Testbed::load_snapshot(nlohmann::json config) {

0 commit comments

Comments
 (0)