Heightmaps can now be loaded.

This commit is contained in:
2023-05-10 11:13:55 +03:00
parent 5d7acafefa
commit 8a1c7d7d89
2 changed files with 63 additions and 9 deletions
+62 -8
View File
@@ -459,10 +459,10 @@ public:
TextBox *txt_heightmap_from_x, *txt_heightmap_from_y;
TextBox *txt_heightmap_to_x, *txt_heightmap_to_y;
TextBox *txt_grid_res;
TextBox *txt_heightmap_res;
TextBox *txt_clearance_height, *txt_start_probing_at, *txt_max_negative_z, *txt_final_z_height;
void save_heightmap(const grbl::heightmap& grid, std::string path) {
static void save_heightmap(const grbl::heightmap& grid, std::string path) {
std::ofstream out(path);
if (out) {
out << "from_x:" << grid.from_x << std::endl;
@@ -477,6 +477,50 @@ public:
}
}
grbl::heightmap load_heightmap(std::string path) {
grbl::heightmap result{};
std::ifstream in_file(path);
if (in_file) {
float from_x, from_y;
float to_x, to_y;
float resolution;
size_t probe_location = 0;
for (std::string line; std::getline(in_file, line);) {
auto pieces = split_string(line, ":");
if (pieces[0] == "from_x") {
from_x = std::stof(pieces[1]);
} else if (pieces[0] == "from_y") {
from_y = std::stof(pieces[1]);
} else if (pieces[0] == "to_x") {
to_x = std::stof(pieces[1]);
} else if (pieces[0] == "to_y") {
to_y = std::stof(pieces[1]);
} else if (pieces[0] == "resolution") {
resolution = std::stof(pieces[1]);
} else if (pieces[0] == "probes") {
result = grbl::heightmap::from_params(from_x, from_y, to_x, to_y, resolution);
} else {
result.vertices[probe_location].x = std::stof(pieces[0]);
result.vertices[probe_location].y = std::stof(pieces[1]);
result.vertices[probe_location].z = std::stof(pieces[2]);
probe_location++;
}
}
}
return result;
}
void fill_heightmap_from_grid(const grbl::heightmap& grid) {
txt_heightmap_from_x->set_value(std::to_string(grid.from_x));
txt_heightmap_from_y->set_value(std::to_string(grid.from_y));
txt_heightmap_to_x->set_value(std::to_string(grid.to_x));
txt_heightmap_to_y->set_value(std::to_string(grid.to_y));
txt_heightmap_res->set_value(std::to_string(grid.resolution));
}
void add_heightmap_markup() {
heightmap_layer->add<Label>("Grid definition", "sans-bold", 20);
auto heightmap_params_holder = heightmap_layer->add<Widget>();
@@ -511,7 +555,17 @@ public:
btn->set_callback([&]() {
fill_heightmap_from_model();
});
grid_size_holder->add<Label>("");
auto btn_load_heightmap = grid_size_holder->add<Button>("Load from file");
btn_load_heightmap->set_callback([&]() {
auto path = file_dialog(
{{"hmap", "Heightmap files"}
}, true);
if (!path.empty()) {
heightmap_grid = load_heightmap(path);
fill_heightmap_from_grid(heightmap_grid);
renderer.update_grid(heightmap_grid);
}
});
txt_heightmap_from_x->set_callback([&](const std::string& new_value) {
update_grid();
@@ -536,10 +590,10 @@ public:
auto grid_res_holder = heightmap_params_holder->add<Widget>();
grid_res_holder->set_layout(new BoxLayout(nanogui::Orientation::Horizontal, Alignment::Middle, 4, 4));
grid_res_holder->add<Label>("Grid resolution", "sans-bold", 20);
txt_grid_res = grid_res_holder->add<TextBox>("5");
txt_grid_res->set_editable(true);
txt_grid_res->set_fixed_width(150);
txt_grid_res->set_callback([&](const std::string& new_value) {
txt_heightmap_res = grid_res_holder->add<TextBox>("5");
txt_heightmap_res->set_editable(true);
txt_heightmap_res->set_fixed_width(150);
txt_heightmap_res->set_callback([&](const std::string& new_value) {
update_grid();
return true;
});
@@ -599,7 +653,7 @@ public:
auto from_y = std::stof(txt_heightmap_from_y->value());
auto to_x = std::stof(txt_heightmap_to_x->value());
auto to_y = std::stof(txt_heightmap_to_y->value());
auto res = std::stof(txt_grid_res->value());
auto res = std::stof(txt_heightmap_res->value());
if (from_x != heightmap_grid.from_x ||
from_y != heightmap_grid.from_y ||
+1 -1
View File
@@ -442,7 +442,7 @@ void grbl::program_renderer::update_grid(const grbl::heightmap& grid) {
auto p3 = grid.vertices[next];
// exaggerate Z
auto exaggeration_factor = 1000.0f;
auto exaggeration_factor = 100.0f;
p1.z *= exaggeration_factor;
p2.z *= exaggeration_factor;
p3.z *= exaggeration_factor;