Last active
December 21, 2020 15:41
-
-
Save mxxo/df3c7d14a7aa941f2b2d6aa2f91ad2dc to your computer and use it in GitHub Desktop.
ICRP 145 EGS_Mesh parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ICRP 145 mesh-type computational reference phantom (MCRP) node and element parser | |
// -- Max Orok 2020 | |
// | |
// g++ -std=c++11 -Wall -Wextra -O2 -o mrcp-parser mrcp-parser.cpp | |
#include "msh_parser.h" | |
#include <fstream> | |
#include <sstream> | |
#include <stdexcept> | |
#include <vector> | |
std::vector<EGS_Mesh::Tetrahedron> parse_mrcp_ele(std::istream& input) { | |
std::vector<EGS_Mesh::Tetrahedron> elts; | |
int num_elts = -1; | |
std::string line; | |
{ | |
int num_nodes = -1; | |
int num_organs = -1; | |
std::getline(input, line); | |
std::istringstream line_stream(line); | |
line_stream >> num_elts >> num_nodes >> num_organs; | |
if (line_stream.fail() || num_elts == -1 || num_nodes != 4 || num_organs != 1) { | |
throw std::runtime_error("ele file parsing failed"); | |
} | |
} | |
for (int i = 0; i < num_elts; ++i) { | |
std::getline(input, line); | |
std::istringstream line_stream(line); | |
int tag = -1; | |
int a = -1; | |
int b = -1; | |
int c = -1; | |
int d = -1; | |
int organ_id = -1; | |
line_stream >> tag >> a >> b >> c >> d >> organ_id; | |
if (line_stream.fail() || tag == -1 || a == -1 || b == -1 || | |
c == -1 || d == -1 || organ_id == -1) | |
{ | |
throw std::runtime_error("ele file parsing failed"); | |
} | |
elts.push_back(EGS_Mesh::Tetrahedron(organ_id, a, b, c, d)); | |
} | |
return elts; | |
} | |
std::vector<EGS_Mesh::Node> parse_mrcp_node(std::istream& input) { | |
std::vector<EGS_Mesh::Node> nodes; | |
int num_nodes = -1; | |
std::string line; | |
{ | |
int num_coords = -1; | |
int unused0 = -1; | |
int unused1 = -1; | |
std::getline(input, line); | |
std::istringstream line_stream(line); | |
line_stream >> num_nodes >> num_coords >> unused0 >> unused1; | |
if (line_stream.fail() || num_nodes == -1 || num_coords != 3 || unused0 != 0 | |
|| unused1 != 0) { | |
throw std::runtime_error("node file parsing failed"); | |
} | |
} | |
for (int i = 0; i < num_nodes; ++i) { | |
std::getline(input, line); | |
std::istringstream line_stream(line); | |
int tag = -1; | |
double x = 0.0; | |
double y = 0.0; | |
double z = 0.0; | |
line_stream >> tag >> x >> y >> z; | |
if (line_stream.fail() || tag == -1) | |
{ | |
throw std::runtime_error("node file parsing failed"); | |
} | |
nodes.push_back(EGS_Mesh::Node(tag, x, y , z)); | |
} | |
return nodes; | |
} | |
int main(int argc, char** argv) { | |
if (argc != 2) { | |
std::cout << "usage: " << std::string(argv[0]) << " <mrcp_model_name>\n"; | |
exit(1); | |
} | |
auto input_file_stem = std::string(argv[1]); | |
std::cout << input_file_stem << "\n"; | |
auto node_file_name = input_file_stem + ".node"; | |
auto elt_file_name = input_file_stem + ".ele"; | |
auto node_file = std::ifstream(node_file_name); | |
auto elt_file = std::ifstream(elt_file_name); | |
if (!node_file) { | |
std::cout << "couldn't open node file `" << node_file_name << "`, exiting\n"; | |
exit(1); | |
} | |
if (!elt_file) { | |
std::cout << "couldn't open element file `" << elt_file_name << "`, exiting\n"; | |
exit(1); | |
} | |
auto nodes = parse_mrcp_node(node_file); | |
auto elts = parse_mrcp_ele(elt_file); | |
std::cout << "read " << nodes.size() << " nodes\n"; | |
auto last_node = nodes.back(); | |
std::cout << "node " << last_node.tag << | |
":\n x: " << last_node.x << | |
"\n y: " << last_node.y << | |
"\n z: " << last_node.z << "\n"; | |
std::cout << "read " << elts.size() << " tetrahedrons\n"; | |
auto last_elt = elts.back(); | |
std::cout << "element " << elts.size() - 1 << | |
":\n organ_id: " << last_elt.medium_tag << | |
"\n a: " << last_elt.a << | |
"\n b: " << last_elt.b << | |
"\n c: " << last_elt.c << | |
"\n d: " << last_elt.d << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment