diff --git a/src/csg/csg.hpp b/src/csg/csg.hpp index 1862b8305afc484ea004a513157274cc937b4454..7bce19472eeb6c2faef5594eb568050873080b8a 100644 --- a/src/csg/csg.hpp +++ b/src/csg/csg.hpp @@ -47,6 +47,8 @@ private: std::unique_ptr m_pimpl; }; +std::unique_ptr get_csgif_from_filename(std::string geom_file); + } // namespace csg #endif diff --git a/src/csg/impl/csg.cpp b/src/csg/impl/csg.cpp index 1cb62eee5132ad1b2969f1f48d659fbb122fb937..7ea6c38ee30693a6c0321b2bea4b36fda775f0eb 100644 --- a/src/csg/impl/csg.cpp +++ b/src/csg/impl/csg.cpp @@ -1,7 +1,36 @@ +#include +#include +#include + #include "../csg.hpp" #include "csg_types.hpp" +namespace { + +const std::string csg_ext = ".csg"; + +bool has_ext(std::string filename, std::string extension) { + return filename.rfind(extension) == (filename.size() - extension.size()); +} + +std::optional read_csg(std::string geom_file) { + if (!has_ext(geom_file, csg_ext)) { + std::cout << "Filename: " << geom_file << " must has extension .csg" + << std::endl; + return std::nullopt; + } + + std::ifstream in_stream; + in_stream.open(geom_file.c_str()); + + std::stringstream str_stream; + str_stream << in_stream.rdbuf(); + return str_stream.str(); +} + +} // namespace + namespace csg { class CsgIF::Impl { @@ -25,4 +54,22 @@ double CsgIF::operator()(double xx, double yy, double zz) const noexcept { return m_pimpl->call_signed_distance(m_state, xx, yy, zz); } +std::unique_ptr get_csgif_from_filename(std::string geom_file) { + + auto csg_str = read_csg(geom_file); + if (!csg_str) { + std::cout << "Unable to read .csg file: " << geom_file << std::endl; + return nullptr; + } + auto csg_obj = csg::parse_csg(csg_str.value()); + + if (!csg_obj) { + std::cout << "Failed to parse .csg file: " << geom_file << std::endl; + return nullptr; + } + + csg::CsgIF csg_if(csg_obj); + return std::make_unique(csg_if); +} + } // namespace csg