Commit b8f18a61 authored by Mark Meredith's avatar Mark Meredith
Browse files

Merge branch 'mwm/read_csg' into 'master'

Read .csg file in mfix-parser

See merge request exa/mfix-parser!12
parents 2b795fc6 da1482e2
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -47,6 +47,8 @@ private:
  std::unique_ptr<Impl> m_pimpl;
};

std::unique_ptr<CsgIF> get_csgif_from_filename(std::string geom_file);

} // namespace csg

#endif
+47 −0
Original line number Diff line number Diff line
#include <fstream>
#include <iostream>
#include <sstream>

#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<std::string> 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<CsgIF> 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<CsgIF>(csg_if);
}

} // namespace csg