This commit is contained in:
张壹 2025-02-18 22:02:35 +08:00
parent 6c7ec61fca
commit 5a00846fa8
6 changed files with 955646 additions and 2 deletions

View File

@ -23,7 +23,7 @@ add_example(windowfunc_ex OFF)
add_example(legendre_ex OFF)
add_example(refellipsoid_ex OFF)
add_example(kde_ex OFF)
add_example(meshio_ex ON)
add_example(meshio_ex OFF)
add_example(autodiff_ex OFF)
add_example(multinary_ex OFF)
add_example(text_io_ex OFF)
@ -31,4 +31,5 @@ add_example(getoption_ex OFF)
add_example(process_ex OFF)
add_example(array_ex OFF)
add_example(gmt_ex OFF)
add_example(gnuplot_ex OFF)
add_example(gnuplot_ex OFF)
add_example(stl_io_ex OFF)

50
example/stl_io_ex.cpp Normal file
View File

@ -0,0 +1,50 @@
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#include "../lib/core.h"
#include "../lib/io.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
array<vertex3dc> nodes;
array<triangle> tris;
read_stl_ascii("tmp/Stanford_Bunny", nodes, tris);
mesh_io mshio;
mshio.import_from(tris, nodes);
mshio.save_gmsh_v2_ascii("tmp");
mshio.info();
save_stl_ascii("tmp", nodes, tris, "Stanford_Bunny");
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

View File

@ -38,5 +38,6 @@
#include "io/tetgen_io.h"
#include "io/triangle_io.h"
#include "io/off_io.h"
#include "io/stl_io.h"
#endif // _GCTL_IO_H

158
lib/io/stl_io.h Normal file
View File

@ -0,0 +1,158 @@
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#ifndef _GCTL_STL_IO_H
#define _GCTL_STL_IO_H
// library's head file
#include "../core.h"
#include "../utility.h"
#include "../geometry.h"
namespace gctl
{
template <typename E, typename A>
void read_stl_ascii(std::string filename, array<vertex<point3dc, E>> &nodes,
array<type_triangle<A>> &facets, std::string solid = "null", double eps = 1e-8)
{
std::ifstream infile;
open_infile(infile, filename, ".stl");
vertex3dc tmp_node;
std::vector<vertex3dc> tmp_nodes;
_1i_vector one_tri(3);
_2i_vector tmp_tris;
bool inside_solid, new_node;
point3dc nor, vt[3];
char solid_name[256], solid_endname[256];
std::string line;
while (std::getline(infile, line))
{
if (sscanf(line.c_str(), "solid %s", solid_name) == 1 && (!strcmp(solid_name, solid.c_str()) || solid == "null"))
{
inside_solid = true;
while (inside_solid)
{
std::getline(infile, line);
if (sscanf(line.c_str(), "endsolid %s", solid_endname) == 1)
{
if (strcmp(solid_endname, solid_name))
throw std::runtime_error("[gctl::read_stl_ascii] Invalid solid names.");
inside_solid = false;
}
else
{
sscanf(line.c_str(), "facet normal %lf %lf %lf", &nor.x, &nor.y, &nor.z);
std::getline(infile, line); // outer loop
for (int i = 0; i < 3; i++)
{
std::getline(infile, line);
line.erase(0, line.find_first_not_of(" \t"));
line.erase(line.find_last_not_of(" \t") + 1);
sscanf(line.c_str(), "vertex %lf %lf %lf", &vt[i].x, &vt[i].y, &vt[i].z);
}
std::getline(infile, line); // endloop
std::getline(infile, line); // endfacet
for (size_t i = 0; i < 3; i++)
{
tmp_node.id = tmp_nodes.size();
tmp_node.x = vt[i].x;
tmp_node.y = vt[i].y;
tmp_node.z = vt[i].z;
new_node = true;
for (size_t v = 0; v < tmp_nodes.size(); v++)
{
if (isequal(tmp_nodes[v], tmp_node, eps))
{
tmp_node.id = tmp_nodes[v].id;
new_node = false;
break;
}
}
if (new_node) tmp_nodes.push_back(tmp_node);
one_tri[i] = tmp_node.id;
}
tmp_tris.push_back(one_tri);
}
}
}
}
infile.close();
nodes.resize(tmp_nodes.size());
for (size_t i = 0; i < tmp_nodes.size(); i++)
{
nodes[i].id = tmp_nodes[i].id;
nodes[i].x = tmp_nodes[i].x;
nodes[i].y = tmp_nodes[i].y;
nodes[i].z = tmp_nodes[i].z;
}
facets.resize(tmp_tris.size());
for (size_t i = 0; i < tmp_tris.size(); i++)
{
facets[i].set(nodes[tmp_tris[i][0]], nodes[tmp_tris[i][1]], nodes[tmp_tris[i][2]], i);
}
destroy_vector(tmp_nodes);
destroy_vector(tmp_tris);
destroy_vector(one_tri);
return;
}
template <typename E, typename A>
void save_stl_ascii(std::string filename, const array<vertex<point3dc, E>> &nodes,
const array<type_triangle<A>> &facets, std::string solid)
{
std::ofstream outfile;
open_outfile(outfile, filename, ".stl");
point3dc nor;
outfile << "solid " << solid << "\n";
for (size_t i = 0; i < facets.size(); i++)
{
nor = cross(*facets[i].vert[1] - *facets[i].vert[0], *facets[i].vert[2] - *facets[i].vert[0]).normal();
outfile << "facet normal " << nor.x << " " << nor.y << " " << nor.z << "\n";
outfile << " outer loop\n";
outfile << " vertex " << facets[i].vert[0]->x << " " << facets[i].vert[0]->y << " " << facets[i].vert[0]->z << "\n";
outfile << " vertex " << facets[i].vert[1]->x << " " << facets[i].vert[1]->y << " " << facets[i].vert[1]->z << "\n";
outfile << " vertex " << facets[i].vert[2]->x << " " << facets[i].vert[2]->y << " " << facets[i].vert[2]->z << "\n";
outfile << " endloop\n";
outfile << "endfacet\n";
}
outfile << "endsolid " << solid << "\n";
outfile.close();
return;
}
};
#endif // _GCTL_STL_IO_H

168618
tmp.msh Normal file

File diff suppressed because it is too large Load Diff

786816
tmp.stl Normal file

File diff suppressed because it is too large Load Diff