From d499b9feccbe3622ea1392c5c3031e7f51ed5dd8 Mon Sep 17 00:00:00 2001 From: Tommaso Sorgente Date: Tue, 7 Nov 2023 10:13:35 +0100 Subject: [PATCH] read/write RF --- include/cinolib/io/read_RF.cpp | 118 ++++++++++++++++++++++++++++++++ include/cinolib/io/read_RF.h | 60 ++++++++++++++++ include/cinolib/io/write_RF.cpp | 87 +++++++++++++++++++++++ include/cinolib/io/write_RF.h | 57 +++++++++++++++ 4 files changed, 322 insertions(+) create mode 100644 include/cinolib/io/read_RF.cpp create mode 100644 include/cinolib/io/read_RF.h create mode 100644 include/cinolib/io/write_RF.cpp create mode 100644 include/cinolib/io/write_RF.h diff --git a/include/cinolib/io/read_RF.cpp b/include/cinolib/io/read_RF.cpp new file mode 100644 index 00000000..a55dcc26 --- /dev/null +++ b/include/cinolib/io/read_RF.cpp @@ -0,0 +1,118 @@ +/******************************************************************************** +* This file is part of CinoLib * +* Copyright(C) 2019: Tommaso Sorgente +* * +* The MIT License * +* * +* Permission is hereby granted, free of charge, to any person obtaining a * +* copy of this software and associated documentation files (the "Software"), * +* to deal in the Software without restriction, including without limitation * +* the rights to use, copy, modify, merge, publish, distribute, sublicense, * +* and/or sell copies of the Software, and to permit persons to whom the * +* Software is furnished to do so, subject to the following conditions: * +* * +* The above copyright notice and this permission notice shall be included in * +* all copies or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * +* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE * +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * +* IN THE SOFTWARE. * +* * +* Author(s): * +* * +* Tommaso Sorgente (tommaso.sorgente@.cnr.it) * +* * +* Italian National Research Council (CNR) * +* Institute for Applied Mathematics and Information Technologies (IMATI) * +* Via de Marini, 6 * +* 16149 Genoa, * +* Italy * +*********************************************************************************/ + +#include +#include +#include + +namespace cinolib +{ + +CINO_INLINE +void read_RF(const char *filename, std::vector &verts, + std::vector> &faces, + std::vector> &polys, + std::vector> &polys_winding) { + setlocale(LC_NUMERIC, + "en_US.UTF-8"); // makes sure "." is the decimal separator + + size_t lastindex = std::string(filename).find_last_of("."); + std::string basename = std::string(filename).substr(0, lastindex); + std::string n_node = basename + ".node"; + FILE *f_node = fopen(n_node.c_str(), "r"); + FILE *f_ele = fopen((basename + ".ele").c_str(), "r"); + if (!f_node || !f_ele) { + std::cerr << "ERROR : " << __FILE__ << ", line " << __LINE__ + << " : load_RF() : couldn't open input file(s) " << basename + << std::endl; + exit(-1); + } + char buffer[100]; + fgets(buffer, 100, f_node); // ignore the initial line of both files + fgets(buffer, 100, f_ele); + + uint nv, dim, fv0, fv1; + fscanf(f_node, "%d %d %d %d", &nv, &dim, &fv0, &fv1); + for (uint i = 0; i < nv; ++i) { + double x, y, z; + uint vid; + fscanf(f_node, "%d %lf %lf %lf", &vid, &x, &y, &z); + verts.push_back(vec3d(x, y, z)); + } + fclose(f_node); + + std::map, uint> f_map; + uint fresh_id = 0; + uint np, fp0; + fscanf(f_ele, "%d %d", &np, &fp0); + for (uint i = 0; i < np; ++i) { + uint pid, nf; + fscanf(f_ele, "%d %d", &pid, &nf); + + std::vector p; + std::vector p_winding; + for (uint j = 0; j < nf; ++j) { + uint fid, nv; + fscanf(f_ele, "%d %d", &fid, &nv); + + std::vector f; + for (uint j = 0; j < nv; ++j) { + uint vid; + fscanf(f_ele, "%d", &vid); + f.push_back(vid); + } + + std::vector f2(f.size()); // f2 = face f with inverse winding + std::reverse_copy(f.begin(), f.end(), f2.begin()); + + auto it = f_map.find(f2); + if (it != f_map.end()) { // f exists with inverse winding + p.push_back(it->second); + p_winding.push_back(false); + } else { // f does not exist yet + faces.push_back(f); + f_map[f] = fresh_id; + ++fresh_id; + p.push_back(faces.size() - 1); + p_winding.push_back(true); + } + } + polys.push_back(p); + polys_winding.push_back(p_winding); + } + fclose(f_ele); +} + +} // namespace cinolib diff --git a/include/cinolib/io/read_RF.h b/include/cinolib/io/read_RF.h new file mode 100644 index 00000000..14d2fd2e --- /dev/null +++ b/include/cinolib/io/read_RF.h @@ -0,0 +1,60 @@ +/******************************************************************************** +* This file is part of CinoLib * +* Copyright(C) 2019: Tommaso Sorgente +* * +* The MIT License * +* * +* Permission is hereby granted, free of charge, to any person obtaining a * +* copy of this software and associated documentation files (the "Software"), * +* to deal in the Software without restriction, including without limitation * +* the rights to use, copy, modify, merge, publish, distribute, sublicense, * +* and/or sell copies of the Software, and to permit persons to whom the * +* Software is furnished to do so, subject to the following conditions: * +* * +* The above copyright notice and this permission notice shall be included in * +* all copies or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * +* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE * +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * +* IN THE SOFTWARE. * +* * +* Author(s): * +* * +* Tommaso Sorgente (tommaso.sorgente@.cnr.it) * +* * +* Italian National Research Council (CNR) * +* Institute for Applied Mathematics and Information Technologies (IMATI) * +* Via de Marini, 6 * +* 16149 Genoa, * +* Italy * +*********************************************************************************/ + +#ifndef CINO_READ_RF_H +#define CINO_READ_RF_H + +#include +#include +#include +#include + +namespace cinolib +{ + +CINO_INLINE +void read_RF(const char * basename, + std::vector & verts, + std::vector> & faces, + std::vector> & polys, + std::vector> & polys_winding); + +} + +#ifndef CINO_STATIC_LIB +#include "read_RF.cpp" +#endif + +#endif // CINO_READ_RF_H diff --git a/include/cinolib/io/write_RF.cpp b/include/cinolib/io/write_RF.cpp new file mode 100644 index 00000000..626524b1 --- /dev/null +++ b/include/cinolib/io/write_RF.cpp @@ -0,0 +1,87 @@ +/******************************************************************************** + * This file is part of CinoLib * Copyright(C) 2019: Tommaso Sorgente + * * + * The MIT License * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE + ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + ** IN THE SOFTWARE. * + * * + * Author(s): * + * * + * Tommaso Sorgente (tommaso.sorgente@.cnr.it) * + * * + * Italian National Research Council (CNR) * Institute for Applied + *Mathematics and Information Technologies (IMATI) * Via de Marini, 6 * 16149 + *Genoa, * Italy * + *********************************************************************************/ + +#include +#include + +namespace cinolib { + +CINO_INLINE +void write_RF(const char *basename, const Polyhedralmesh<> &m) +{ + // output file for nodes coordinates + std::string fnode = basename + std::string(".node"); + std::ofstream out_node(fnode.c_str()); + uint offset = 0; + out_node << "# *.node file of 3D-mesh in REGN_FACE format" << std::endl; + out_node << m.num_verts() << " 3 0 0 " << std::endl; + + // write the nodes coordinates + for (uint vid = 0; vid < m.num_verts(); ++vid) + { + out_node << (vid + offset) << " " << m.vert(vid).x() + << " " << m.vert(vid).y() << " " << m.vert(vid).z() << std::endl; + } + out_node.close(); + + // output file for elements structure + std::string fele = basename + std::string(".ele"); + std::ofstream out_ele(fele.c_str()); + out_ele << "# *.ele file of 3D-mesh in REGN_FACE format" << std::endl; + out_ele << m.num_polys() << " 0" << std::endl; + + // write the elements structure + for (uint pid = 0; pid < m.num_polys(); ++pid) + { + std::vector flist = m.adj_p2f(pid); + uint nf = m.faces_per_poly(pid); + out_ele << pid + offset << " " << nf << std::endl; // element summary + + for (uint f = 0; f < flist.size(); f++) + { + uint fid = flist.at(f); + std::vector vlist = m.adj_f2v(fid); + uint nv = vlist.size(); + out_ele << " " << f + offset << " " << nv << " "; // face summary + + for (uint vid = 0; vid < nv; ++vid) // list of nodes in CCW order + { + uint v = m.poly_face_is_CCW(pid, fid) ? vlist[vid] : vlist[nv - 1 - vid]; + out_ele << " " << v + offset; + } + out_ele << std::endl; + } + } + out_ele.close(); +} + +} // namespace cinolib diff --git a/include/cinolib/io/write_RF.h b/include/cinolib/io/write_RF.h new file mode 100644 index 00000000..6f779952 --- /dev/null +++ b/include/cinolib/io/write_RF.h @@ -0,0 +1,57 @@ +/******************************************************************************** +* This file is part of CinoLib * +* Copyright(C) 2019: Tommaso Sorgente +* * +* The MIT License * +* * +* Permission is hereby granted, free of charge, to any person obtaining a * +* copy of this software and associated documentation files (the "Software"), * +* to deal in the Software without restriction, including without limitation * +* the rights to use, copy, modify, merge, publish, distribute, sublicense, * +* and/or sell copies of the Software, and to permit persons to whom the * +* Software is furnished to do so, subject to the following conditions: * +* * +* The above copyright notice and this permission notice shall be included in * +* all copies or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * +* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE * +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * +* IN THE SOFTWARE. * +* * +* Author(s): * +* * +* Tommaso Sorgente (tommaso.sorgente@.cnr.it) * +* * +* Italian National Research Council (CNR) * +* Institute for Applied Mathematics and Information Technologies (IMATI) * +* Via de Marini, 6 * +* 16149 Genoa, * +* Italy * +*********************************************************************************/ + +#ifndef CINO_WRITE_RF_H +#define CINO_WRITE_RF_H + +#include +#include +#include +#include +#include + +namespace cinolib +{ + +CINO_INLINE +void write_RF(const char *basename, const Polyhedralmesh<> &m); + +} + +#ifndef CINO_STATIC_LIB +#include "write_RF.cpp" +#endif + +#endif // CINO_WRITE_RF_H