|
| 1 | +#include <jse/jse.h> |
| 2 | +#include <CLI/CLI.hpp> |
| 3 | +#include <filesystem> |
| 4 | +#include <nlohmann/json.hpp> |
| 5 | + |
| 6 | +#include <wmtk/Mesh.hpp> |
| 7 | +#include <wmtk/utils/Logger.hpp> |
| 8 | + |
| 9 | +#include <wmtk/components/input/input.hpp> |
| 10 | +#include <wmtk/components/output/output.hpp> |
| 11 | +#include <wmtk/components/shortest_edge_collapse/shortest_edge_collapse.hpp> |
| 12 | +#include <wmtk/components/utils/resolve_path.hpp> |
| 13 | + |
| 14 | +#include "shortest_edge_collapse_spec.hpp" |
| 15 | + |
| 16 | +using namespace wmtk; |
| 17 | +namespace fs = std::filesystem; |
| 18 | + |
| 19 | +using wmtk::components::utils::resolve_paths; |
| 20 | + |
| 21 | +int main(int argc, char* argv[]) |
| 22 | +{ |
| 23 | + CLI::App app{argv[0]}; |
| 24 | + |
| 25 | + app.ignore_case(); |
| 26 | + |
| 27 | + fs::path json_input_file; |
| 28 | + app.add_option("-j, --json", json_input_file, "json specification file") |
| 29 | + ->required(true) |
| 30 | + ->check(CLI::ExistingFile); |
| 31 | + CLI11_PARSE(app, argc, argv); |
| 32 | + |
| 33 | + nlohmann::json j; |
| 34 | + { |
| 35 | + std::ifstream ifs(json_input_file); |
| 36 | + j = nlohmann::json::parse(ifs); |
| 37 | + |
| 38 | + jse::JSE spec_engine; |
| 39 | + bool r = spec_engine.verify_json(j, shortest_edge_collapse_spec); |
| 40 | + if (!r) { |
| 41 | + wmtk::logger().error("{}", spec_engine.log2str()); |
| 42 | + return 1; |
| 43 | + } else { |
| 44 | + j = spec_engine.inject_defaults(j, shortest_edge_collapse_spec); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const fs::path input_file = resolve_paths(json_input_file, {j["input_path"], j["input"]}); |
| 49 | + |
| 50 | + std::shared_ptr<Mesh> mesh_in = wmtk::components::input::input(input_file); |
| 51 | + Mesh& mesh = *mesh_in; |
| 52 | + |
| 53 | + |
| 54 | + attribute::MeshAttributeHandle pos_handle = |
| 55 | + mesh.get_attribute_handle<double>("vertices", PrimitiveType::Vertex); |
| 56 | + |
| 57 | + // shortest-edge collapse |
| 58 | + { |
| 59 | + using namespace components::shortest_edge_collapse; |
| 60 | + ShortestEdgeCollapseOptions options; |
| 61 | + options.position_handle = pos_handle; |
| 62 | + options.length_rel = j["length_rel"]; |
| 63 | + const double env_size = j["envelope_size"]; |
| 64 | + if (env_size >= 0) { |
| 65 | + options.envelope_size = j["envelope_size"]; |
| 66 | + } |
| 67 | + options.lock_boundary = j["lock_boundary"]; |
| 68 | + |
| 69 | + shortest_edge_collapse(static_cast<TriMesh&>(mesh), options); |
| 70 | + } |
| 71 | + |
| 72 | + wmtk::components::output::output(mesh, j["output"], pos_handle); |
| 73 | + |
| 74 | + const std::string report = j["report"]; |
| 75 | + if (!report.empty()) { |
| 76 | + nlohmann::json out_json; |
| 77 | + out_json["stats"]["vertices"] = mesh.get_all(PrimitiveType::Vertex).size(); |
| 78 | + out_json["stats"]["edges"] = mesh.get_all(PrimitiveType::Edge).size(); |
| 79 | + out_json["stats"]["triangles"] = mesh.get_all(PrimitiveType::Triangle).size(); |
| 80 | + out_json["stats"]["tets"] = mesh.get_all(PrimitiveType::Tetrahedron).size(); |
| 81 | + |
| 82 | + out_json["input"] = j; |
| 83 | + |
| 84 | + std::ofstream ofs(report); |
| 85 | + ofs << std::setw(4) << out_json; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + return 0; |
| 90 | +} |
0 commit comments