Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read/write RF #174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions include/cinolib/io/read_RF.cpp
Original file line number Diff line number Diff line change
@@ -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 ([email protected]) *
* *
* Italian National Research Council (CNR) *
* Institute for Applied Mathematics and Information Technologies (IMATI) *
* Via de Marini, 6 *
* 16149 Genoa, *
* Italy *
*********************************************************************************/

#include <cinolib/io/read_RF.h>
#include <iostream>
#include <map>

namespace cinolib
{

CINO_INLINE
void read_RF(const char *filename, std::vector<vec3d> &verts,
std::vector<std::vector<uint>> &faces,
std::vector<std::vector<uint>> &polys,
std::vector<std::vector<bool>> &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<std::vector<uint>, 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<uint> p;
std::vector<bool> p_winding;
for (uint j = 0; j < nf; ++j) {
uint fid, nv;
fscanf(f_ele, "%d %d", &fid, &nv);

std::vector<uint> f;
for (uint j = 0; j < nv; ++j) {
uint vid;
fscanf(f_ele, "%d", &vid);
f.push_back(vid);
}

std::vector<uint> 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
60 changes: 60 additions & 0 deletions include/cinolib/io/read_RF.h
Original file line number Diff line number Diff line change
@@ -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 ([email protected]) *
* *
* 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 <cinolib/cino_inline.h>
#include <cinolib/geometry/vec_mat.h>
#include <sys/types.h>
#include <vector>

namespace cinolib
{

CINO_INLINE
void read_RF(const char * basename,
std::vector<vec3d> & verts,
std::vector<std::vector<uint>> & faces,
std::vector<std::vector<uint>> & polys,
std::vector<std::vector<bool>> & polys_winding);

}

#ifndef CINO_STATIC_LIB
#include "read_RF.cpp"
#endif

#endif // CINO_READ_RF_H
87 changes: 87 additions & 0 deletions include/cinolib/io/write_RF.cpp
Original file line number Diff line number Diff line change
@@ -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 ([email protected]) *
* *
* Italian National Research Council (CNR) * Institute for Applied
*Mathematics and Information Technologies (IMATI) * Via de Marini, 6 * 16149
*Genoa, * Italy *
*********************************************************************************/

#include <cinolib/io/write_RF.h>
#include <iostream>

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<uint> 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<uint> 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
57 changes: 57 additions & 0 deletions include/cinolib/io/write_RF.h
Original file line number Diff line number Diff line change
@@ -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 ([email protected]) *
* *
* 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 <cinolib/cino_inline.h>
#include <cinolib/geometry/vec_mat.h>
#include <cinolib/meshes/polyhedralmesh.h>
#include <sys/types.h>
#include <vector>

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