19 #include "libmesh/stl_io.h" 21 #include "libmesh/distributed_mesh.h" 22 #include "libmesh/enum_elem_type.h" 23 #include "libmesh/face_tri3.h" 24 #include "libmesh/libmesh_config.h" 25 #include "libmesh/libmesh_logging.h" 26 #include "libmesh/mesh_base.h" 27 #include "libmesh/point.h" 28 #include "libmesh/utility.h" 31 #ifdef LIBMESH_HAVE_GZSTREAM 32 # include "gzstream.h" 41 #ifdef LIBMESH_HAVE_CXX11_REGEX 47 void test_and_add(std::unique_ptr<libMesh::Elem> e,
53 (
"Warning: STL file contained sliver element with volume " <<
55 mesh.add_elem(std::move(e));
66 _subdivide_second_order (true)
75 _subdivide_second_order (true)
83 LOG_SCOPE(
"write()",
"STLIO");
88 libmesh_parallel_only(mesh_ptr->
comm());
91 std::unique_ptr<DistributedMesh> mesh_copy;
92 std::unique_ptr<MeshSerializer> serializer;
95 mesh_copy = std::make_unique<DistributedMesh>(*mesh_ptr);
96 mesh_ptr = mesh_copy.get();
97 serializer = std::make_unique<MeshSerializer>(*mesh_copy,
true,
true);
106 std::ofstream out_stream (fname.c_str());
111 if (!out_stream.good())
112 libmesh_file_error(fname.c_str());
115 out_stream <<
"solid " << this->
_name <<
'\n';
119 for (
auto elem :
mesh.element_ptr_range())
122 if (elem->type() ==
TRI6 || elem->type() ==
TRI7)
125 libmesh_not_implemented();
127 libmesh_error_msg(
"Tried to write a non-linear triangle to an STL file");
130 libmesh_error_msg_if(elem->type() !=
TRI3,
131 "Tried to write a non-triangle to an STL file");
133 auto n = (elem->point(1)-elem->point(0)).cross(elem->point(2)-elem->point(0));
136 if (
auto length = n.norm())
139 out_stream <<
"facet normal " <<
145 elem->point(0)(0) <<
' ' <<
146 elem->point(0)(1) <<
' ' <<
147 elem->point(0)(2) <<
"\n" 149 elem->point(1)(0) <<
' ' <<
150 elem->point(1)(1) <<
' ' <<
151 elem->point(1)(2) <<
"\n" 153 elem->point(2)(0) <<
' ' <<
154 elem->point(2)(1) <<
' ' <<
155 elem->point(2)(2) <<
"\n" 160 out_stream <<
"endsolid" << std::endl;
167 LOG_SCOPE(
"read()",
"STLIO");
179 std::unique_ptr<std::istream> fstream =
183 const char * expected_header =
"solid!";
184 bool is_ascii_stl =
false,
185 is_binary_stl =
false;
186 while (fstream->get(c))
190 if (c == *expected_header)
193 if (*expected_header ==
'!')
201 is_binary_stl =
true;
211 else if (is_binary_stl)
213 fstream->seekg(0, std::ios_base::end);
214 std::size_t length = fstream->tellg();
219 libmesh_error_msg(
"Failed to read an STL header in " << filename);
227 std::unique_ptr<std::istream> file;
231 #ifdef LIBMESH_HAVE_GZSTREAM 232 auto inf = std::make_unique<igzstream>();
234 inf->open(filename.c_str(), std::ios::in);
235 file = std::move(inf);
237 libmesh_error_msg(
"ERROR: need gzstream to handle .gz files!!!");
242 auto inf = std::make_unique<std::ifstream>();
247 inf->open(new_name.c_str(), std::ios::in);
248 file = std::move(inf);
258 LOG_SCOPE(
"read_ascii()",
"STLIO");
260 #ifndef LIBMESH_HAVE_CXX11_REGEX 261 libmesh_not_implemented();
267 const std::regex all_expected_chars
268 (
"^[\\w\\d\\-\\.\\^\\$]*$");
270 const std::regex start_regex
272 const std::regex start_with_name_regex
273 (
"^\\s*solid\\s+(\\w+)");
275 const std::regex start_facet_regex
287 const std::regex start_loop_regex
288 (
"^\\s*outer\\s+loop");
290 const std::regex vertex_regex
292 "\\s+([+-]?(\\d+([.]\\d*)?([eE][+-]?\\d+)?|[.]\\d+([eE][+-]?\\d+)?)" 293 "\\s+[+-]?(\\d+([.]\\d*)?([eE][+-]?\\d+)?|[.]\\d+([eE][+-]?\\d+)?)" 294 "\\s+[+-]?(\\d+([.]\\d*)?([eE][+-]?\\d+)?|[.]\\d+([eE][+-]?\\d+)?))");
296 const std::regex end_facet_regex
297 (
"^\\s*end\\s*facet");
299 const std::regex end_loop_regex
300 (
"^\\s*end\\s*loop");
302 bool have_started =
false;
303 bool in_facet =
false;
304 bool in_vertex_loop =
false;
305 std::unique_ptr<Tri3> triangle;
309 std::unordered_map<Point, Node *> mesh_points;
311 for (std::string line; std::getline(file, line);)
316 for (
char & c : line)
319 (c !=
'\n' && c !=
'\r' &&
320 (c < ' ' || c >
'~'),
321 "Found non-ASCII character " <<
int(c) <<
" on line " <<
322 line_num <<
"\nSTLIO does not yet support binary files.");
325 if (std::regex_search(line, sm, start_regex))
327 if (std::regex_search(line, sm, start_with_name_regex))
331 "Found two 'solid' lines starting the same STL file?");
335 else if (std::regex_search(line, sm, start_facet_regex))
339 "Found a repeated 'facet' line with no 'endfacet' between them.");
357 else if (std::regex_search(line, end_facet_regex))
361 "Found an 'endfacet' line with no matching 'facet'.");
365 else if (std::regex_search(line, start_loop_regex))
369 "Found an 'outer loop' line with no matching 'facet'.");
372 "Found a repeated 'outer loop' line with no 'endloop' between them.");
373 in_vertex_loop =
true;
374 triangle = std::make_unique<Tri3>();
378 else if (std::regex_search(line, sm, vertex_regex))
380 const std::string normalvec = sm[1];
384 std::stringstream ss(normalvec);
391 if (
auto it = mesh_points.find(p); it != mesh_points.end())
398 mesh_points[p] = node;
403 "Found more than 3 vertices in a loop; STLIO only supports Tri3.");
404 triangle->set_node(next_vertex++, node);
407 else if (std::regex_search(line, end_loop_regex))
411 "Found an 'endloop' line after only seeing " << next_vertex <<
" vertices in 'loop'.");
414 "Found an 'endloop' line with no matching 'loop'.");
415 in_vertex_loop =
false;
417 test_and_add(std::move(triangle),
mesh);
423 "File ended without ending a facet first.");
427 "File ended without ending an outer loop first.");
428 #endif // LIBMESH_HAVE_CXX11_REGEX 434 std::size_t input_size)
436 LOG_SCOPE(
"read_binary()",
"STLIO");
440 char header_buffer[80];
443 file.read(header_buffer, 80);
448 uint32_t test_int = 0x87654321;
449 const bool big_endian = ((*
reinterpret_cast<char *
>(&test_int)) != 0x21);
455 if constexpr (
sizeof(
float) != 4)
456 libmesh_error_msg(
"Trying to read 4 byte floats without 4-byte float?");
460 file.read(reinterpret_cast<char*>(&
n_elem), 4);
465 (input_size < 84+
n_elem*50),
466 "Not enough data for " <<
n_elem <<
" STL triangles in " <<
467 input_size <<
" uncompressed bytes.");
469 std::unique_ptr<Tri3> triangle;
470 std::unordered_map<Point, Node *> mesh_points;
476 triangle = std::make_unique<Tri3>();
479 char ignored_buffer[12];
480 file.read(ignored_buffer, 12);
485 float point_buffer[3];
486 file.read(reinterpret_cast<char*>(point_buffer), 12);
487 endian_fix(point_buffer[0]);
488 endian_fix(point_buffer[1]);
489 endian_fix(point_buffer[2]);
490 const Point p {point_buffer[0],
495 if (
auto it = mesh_points.find(p); it != mesh_points.end())
502 mesh_points[p] = node;
505 triangle->set_node(i, node);
510 file.read(ignored_buffer, 2);
512 test_and_add(std::move(triangle),
mesh);
This Functor simply takes an object and reverses its byte representation.
std::unique_ptr< std::istream > open_file(const std::string &filename)
Helper to open possibly-zipped files.
A Node is like a Point, but with more information.
bool ends_with(std::string_view superstring, std::string_view suffix)
Look for a substring at the very end of a string.
static constexpr Real TOLERANCE
virtual void read(const std::string &mesh_file) override
This method implements reading a mesh from a specified file.
unsigned int & ascii_precision()
Return/set the precision to use when writing ASCII files.
const Parallel::Communicator & comm() const
This class defines an abstract interface for Mesh output.
The libMesh namespace provides an interface to certain functionality in the library.
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
Add a new Node at Point p to the end of the vertex array, with processor_id procid.
This is the MeshBase class.
virtual void read_ascii(std::istream &input)
This method implements reading a mesh from a specified ASCII input stream.
virtual bool is_serial() const
void libmesh_ignore(const Args &...)
virtual void write(const std::string &) override
This method implements writing a mesh to a specified file.
bool _subdivide_second_order
Flag to subdivide second order elements.
std::string unzip_file(std::string_view name)
Create an unzipped copy of a bz2 or xz file, returning the name of the now-unzipped file that can be ...
void set_mesh_dimension(unsigned char d)
Resets the logical dimension of the mesh.
virtual void read_binary(std::istream &input, std::size_t input_size=0)
This method implements reading a mesh from a specified binary input stream.
virtual void clear()
Deletes all the element and node data that is currently stored.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
STLIO(const MeshBase &)
Constructor.
processor_id_type processor_id() const
A Point defines a location in LIBMESH_DIM dimensional Real space.