7#include <boost/tokenizer.hpp>
23using Bitset = boost::dynamic_bitset<>;
29 :
BaseLib::extractPath(fname) +
'/'),
35 std::ifstream in(
_fname.c_str());
43 bool pnts_read(
false);
47 while (std::getline(in, line))
53 if (line.back() ==
'\r')
57 if (line.substr(0, 8) ==
"HEADER {")
61 if (line.substr(0, 32) ==
"GOCAD_ORIGINAL_COORDINATE_SYSTEM")
66 if (line.substr(0, 7) ==
"AXIS_N ")
70 else if (line.substr(0, 12) ==
"POINTS_FILE ")
74 else if (line.substr(0, 9) ==
"PROPERTY ")
79 else if (line.substr(0, 35) ==
"BINARY_POINTS_IN_DOUBLE_PRECISION 1")
83 else if (line.substr(0, 11) ==
"FLAGS_FILE ")
87 else if (line.substr(0, 18) ==
"REGION_FLAGS_FILE ")
91 else if (line.substr(0, 7) ==
"REGION " ||
92 line.substr(0, 13) ==
"MODEL_REGION ")
96 else if (line.substr(0, 12) ==
"MODEL_LAYER ")
100 else if (line.substr(0, 24) ==
"REGION_FLAGS_BIT_LENGTH ")
102 std::istringstream iss(line);
103 std::istream_iterator<std::string> it(iss);
105 std::size_t bit_length = std::atoi(it->c_str());
106 if (
regions.size() != bit_length)
108 ERR(
"{:d} regions read but {:d} expected.\n",
regions.size(),
110 throw std::runtime_error(
111 "Number of read regions differs from expected.\n");
114 else if (line.substr(0, 9) ==
"FACE_SET ")
127 std::stringstream regions_ss;
128 regions_ss <<
regions.size() <<
" regions read:\n";
130 std::ostream_iterator<Gocad::Region>(regions_ss,
"\t"));
131 DBUG(
"{:s}", regions_ss.str());
133 std::stringstream layers_ss;
134 layers_ss <<
layers.size() <<
" layers read:\n";
136 std::ostream_iterator<Gocad::Layer>(layers_ss,
"\n"));
137 DBUG(
"{:s}", layers_ss.str());
139 std::stringstream properties_ss;
141 <<
" properties read:\n";
143 std::ostream_iterator<Gocad::Property>(properties_ss,
"\n"));
144 DBUG(
"{:s}", properties_ss.str());
175 std::vector<MeshLib::Node*> nodes;
176 std::transform(
_nodes.cbegin(),
_nodes.cend(), std::back_inserter(nodes),
178 { return new MeshLib::Node(*node); });
183 DBUG(
"Creating mesh from Gocad SGrid.");
188 DBUG(
"Mesh created.");
196 for (
auto const& name : prop_names)
204 DBUG(
"Adding Gocad property '{:s}' with {:d} values.", name,
205 prop->_property_data.size());
211 ERR(
"Could not create mesh property '{:s}'.", name);
214 pv->assign(prop->_property_data);
221 while (std::getline(in, line))
223 if (line.front() ==
'}')
227 if (line.substr(0, 27) ==
"double_precision_binary: on")
235 "GocadSGridReader::parseHeader(): _double_precision_binary == "
242 std::size_t x_dim(0);
243 std::size_t y_dim(0);
244 std::size_t z_dim(0);
245 boost::tokenizer<> tok(line);
246 auto it(tok.begin());
249 std::stringstream ssx(*(it),
250 std::stringstream::in | std::stringstream::out);
253 std::stringstream ssy(*it, std::stringstream::in | std::stringstream::out);
256 std::stringstream ssz(*it, std::stringstream::in | std::stringstream::out);
260 "x_dim = {:d}, y_dim = {:d}, z_dim = {:d} => #nodes = {:d}, #cells = "
267 std::string& result_string)
const
269 boost::char_separator<char> sep(
" \r");
270 boost::tokenizer<boost::char_separator<char>> tok(line, sep);
271 auto it(tok.begin());
273 result_string =
_path + *it;
296 std::istringstream iss(line);
297 std::istream_iterator<std::string> it(iss);
299 if (*it != std::string(
"FACE_SET"))
301 ERR(
"Expected FACE_SET keyword but '{:s}' found.", it->c_str());
302 throw std::runtime_error(
303 "In GocadSGridReader::parseFaceSet() expected FACE_SET keyword not "
309 auto const n_of_face_set_ids(
310 static_cast<std::size_t
>(std::atoi(it->c_str())));
311 std::size_t face_set_id_cnt(0);
313 while (std::getline(in, line) && face_set_id_cnt < n_of_face_set_ids)
315 if (line.back() ==
'\r')
319 boost::char_separator<char> sep(
"\t ");
320 boost::tokenizer<boost::char_separator<char>> tokens(line, sep);
322 for (
auto tok_it = tokens.begin(); tok_it != tokens.end();)
324 auto const id(
static_cast<std::size_t
>(std::atoi(tok_it->c_str())));
326 auto const face_indicator(
327 static_cast<std::size_t
>(std::atoi(tok_it->c_str())));
332 ERR(
"Face set id {:d} is greater than the number of nodes "
340 std::array<std::size_t, 3>
const c(
344 ERR(
"****** i coord {:d} to big for id {:d}.", c[0],
id);
348 ERR(
"****** j coord {:d} to big for id {:d}.", c[1],
id);
352 ERR(
"****** k coord {:d} to big for id {:d}.", c[2],
id);
354 std::size_t
const cell_id(
357 static_cast<double>(face_indicator);
363 if (face_set_id_cnt != n_of_face_set_ids)
365 ERR(
"Expected {:d} number of face set ids, read {:d}.",
366 n_of_face_set_ids, face_set_id_cnt);
367 throw std::runtime_error(
368 "Expected number of face set points does not match number of read "
377 split_node->transmitFaceDirections(*
_nodes[
id]);
388 using block_t = Bitset::block_type;
389 auto const bytes =
static_cast<std::size_t
>(std::ceil(bits / 8.));
390 std::size_t
const blocks =
391 bytes + 1 <
sizeof(block_t) ? 1 : (bytes + 1) /
sizeof(block_t);
393 std::vector<block_t> data;
395 std::fill_n(data.data(), blocks, 0);
396 in.read(
reinterpret_cast<char*
>(data.data()), bytes);
398 return Bitset(data.begin(), data.end());
403 std::ifstream in(
_pnts_fname.c_str(), std::ios::binary);
407 throw std::runtime_error(
"Could not open points file.");
416 while (in && k < n * 3)
428 if ((k + 1) % 3 == 0)
430 const std::size_t layer_transition_idx(
436 if (k != n * 3 && !in.eof())
438 ERR(
"Read different number of points. Expected {:d} floats, got "
445 std::vector<Bitset>
const& rf)
448 "GocadSGridReader::mapRegionFlagsToCellProperties region_flags.size: "
473 std::size_t
const cell_id(
478 if (rf[node_id].test(region.bit))
494 std::string
const& fname(property._property_data_fname);
495 if (property._property_data_fname.empty())
497 WARN(
"Empty filename for property {:s}.", property._property_name);
503 "GocadSGridReader::readElementPropertiesBinary(): Read {:d} float "
504 "properties from binary file.",
507 std::transform(float_properties.cbegin(), float_properties.cend(),
508 float_properties.begin(),
510 { return BaseLib::swapEndianness(val); });
512 property._property_data.resize(float_properties.size());
513 std::copy(float_properties.begin(), float_properties.end(),
514 property._property_data.begin());
515 if (property._property_data.empty())
517 ERR(
"Reading of element properties file '{:s}' failed.", fname);
524 std::vector<Bitset> result;
529 ERR(
"readRegionFlagsBinary(): Could not open file '{:s}' for input.\n",
543 if (k != n && !in.eof())
545 ERR(
"Read different number of values. Expected {:d}, got {:d}.\n", n,
551 std::vector<MeshLib::Node*>
const& nodes)
const
553 std::vector<MeshLib::Element*> elements;
555 std::array<MeshLib::Node*, 8> element_nodes{};
583 std::ifstream in(
_fname.c_str());
593 std::stringstream ss;
594 while (std::getline(in, line))
596 std::size_t pos(line.find(
"SPLIT "));
597 if (pos != std::string::npos)
599 ss << line.substr(pos + 6, line.size() - (pos + 6));
601 std::array<std::size_t, 3> grid_coords{};
602 ss >> grid_coords[0];
603 ss >> grid_coords[1];
604 ss >> grid_coords[2];
614 std::bitset<8> affected_cells{};
615 for (std::size_t ac = 0; ac < 8; ++ac)
619 affected_cells[ac] = bit !=
'0';
621 const std::size_t layer_transition_index(
622 _nodes[
id]->getLayerTransitionIndex());
625 layer_transition_index));
631 std::vector<MeshLib::Node*>& nodes,
632 std::vector<MeshLib::Element*>
const& elements)
const
636 std::size_t
const new_node_pos(nodes.size());
637 nodes.push_back(
new MeshLib::Node(split_node->data(), new_node_pos));
640 std::array<std::size_t, 3>
const& gc(split_node->getGridCoords());
642 auto const& affected_cells(split_node->getAffectedCells());
650 const std::size_t idx(
654 if (affected_cells[1] && gc[0] > 0 &&
658 const std::size_t idx(
662 if (affected_cells[2] && gc[1] > 0 &&
666 const std::size_t idx(
670 if (affected_cells[3] && gc[0] > 0 && gc[1] > 0 &&
673 const std::size_t idx(
677 if (affected_cells[4] && gc[2] > 0 &&
681 const std::size_t idx(
685 if (affected_cells[5] && gc[0] > 0 && gc[2] > 0 &&
688 const std::size_t idx(
692 if (affected_cells[6] && gc[1] > 0 && gc[2] > 0 &&
695 const std::size_t idx(
699 if (affected_cells[7] && gc[0] > 0 && gc[1] > 0 && gc[2] > 0)
701 const std::size_t idx(
716 std::find(hex_nodes, hex_nodes + 8, node2sub));
718 if (node_pos != hex_nodes + 8)
721 hex_nodes)[std::distance(hex_nodes, node_pos)] = substitute_node;
726 std::string
const& name)
const
731 { return p._property_name == name; }));
741 std::vector<std::string> names;
744 std::back_inserter(names),
750 std::size_t
const face_set_number)
const
752 std::vector<MeshLib::Node*> face_set_nodes;
753 std::vector<MeshLib::Element*> face_set_elements;
755 for (
auto const node :
_nodes)
757 if (node->isMemberOfFaceSet(face_set_number))
764 if (face_set_nodes.empty())
771 if (node->isMemberOfFaceSet(face_set_number))
773 if (node->getAffectedCells()[0])
781 std::string
const mesh_name(
"FaceSet-" + std::to_string(face_set_number));
782 return std::make_unique<MeshLib::Mesh>(
783 mesh_name, face_set_nodes, face_set_elements,
788 GocadNode* face_set_node, std::size_t face_set_number,
789 std::vector<MeshLib::Node*>& face_set_nodes,
790 std::vector<MeshLib::Element*>& face_set_elements)
const
792 std::array<MeshLib::Node*, 4> quad_nodes{};
793 quad_nodes[0] =
new GocadNode(*face_set_node);
794 const std::size_t id(face_set_node->
getID());
825 ERR(
"Could not create face for node with id {:d}.",
id);
827 std::copy(begin(quad_nodes), end(quad_nodes),
828 back_inserter(face_set_nodes));
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
FaceDirection getFaceDirection(std::size_t const face_set_number) const
Gocad::Property const * getProperty(std::string const &name) const
std::vector< Gocad::Property > _property_meta_data_vecs
std::string const & _fname
std::vector< MeshLib::Element * > createElements(std::vector< MeshLib::Node * > const &nodes) const
std::unique_ptr< MeshLib::Mesh > getFaceSetMesh(std::size_t const face_set_number) const
std::vector< GocadNode * > _nodes
std::vector< Gocad::Region > regions
std::string _region_flags_fname
std::unique_ptr< MeshLib::Mesh > getMesh() const
GocadSGridReader()=delete
void readElementPropertiesBinary()
std::vector< Bitset > readRegionFlagsBinary() const
void addGocadPropertiesToMesh(MeshLib::Mesh &mesh) const
void applySplitInformation(std::vector< MeshLib::Node * > &nodes, std::vector< MeshLib::Element * > const &elements) const
void mapRegionFlagsToCellProperties(std::vector< Bitset > const &rf)
Gocad::CoordinateSystem _coordinate_system
static void modifyElement(MeshLib::Element const *hex, MeshLib::Node const *node2sub, MeshLib::Node *substitute_node)
void readSplitInformation()
void parseFaceSet(std::string &line, std::istream &in)
void parseFileName(std::string const &line, std::string &result_string) const
bool _bin_pnts_in_double_precision
Gocad::IndexCalculator _index_calculator
std::vector< GocadSplitNode * > _split_nodes
void parseDims(std::string const &line)
std::vector< std::string > getPropertyNames() const
bool _double_precision_binary
void addFaceSetQuad(GocadNode *face_set_node, std::size_t face_set_number, std::vector< MeshLib::Node * > &face_set_nodes, std::vector< MeshLib::Element * > &face_set_elements) const
void parseHeader(std::istream &in)
std::vector< Gocad::Layer > layers
std::size_t getID() const
virtual Node *const * getNodes() const =0
Get array of element nodes.
T readBinaryValue(std::istream &in)
std::string extractBaseNameWithoutExtension(std::string const &pathname)
double swapEndianness(double const &v)
template std::vector< float > readBinaryVector< float >(std::string const &, std::size_t const, std::size_t const)
boost::dynamic_bitset<> Bitset
Property parseGocadPropertyMetaData(std::string &line, std::istream &in, std::string const &path)
Bitset readBits(std::ifstream &in, const std::size_t bits)
Layer parseLayer(std::string const &line, std::vector< Region > const ®ions)
Region parseRegion(std::string const &line)
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshItemType const item_type, int const number_of_components)
TemplateElement< MeshLib::QuadRule4 > Quad
TemplateElement< MeshLib::HexRule8 > Hex
double _property_no_data_value
std::string _property_data_fname
std::string _property_class_name
std::string _property_unit
std::string _property_name
std::string _property_data_type
std::vector< double > _property_data