Blender V5.3
stl_import_ascii_reader.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <system_error>
10
11#include "BLI_fileops.hh"
13#include "BLI_memory_utils.hh"
14
15#include "DNA_mesh_types.h"
16
17/* NOTE: we could use C++17 <charconv> from_chars to parse
18 * floats, but even if some compilers claim full support,
19 * their standard libraries are not quite there yet.
20 * LLVM/libc++ only has a float parser since LLVM 14,
21 * and gcc/libstdc++ since 11.1. So until at least these are
22 * the minimum spec, use an external library. */
23#include "fast_float.h"
24
25#include "stl_data.hh"
27#include "stl_import_mesh.hh"
28
29#include "CLG_log.h"
30
31namespace blender {
32
33static CLG_LogRef LOG = {"io.stl"};
34
35namespace io::stl {
36
38 private:
39 char *start;
40 const char *end;
41
42 public:
43 StringBuffer(char *buf, size_t len)
44 {
45 start = buf;
46 end = start + len;
47 }
48
49 bool is_empty() const
50 {
51 return start == end;
52 }
53
55 {
56 while ((start < end) && (*start) <= ' ') {
57 start++;
58 }
59 }
60
62 {
63 while ((start < end) && (*start) > ' ') {
64 start++;
65 }
66 }
67
68 void drop_line()
69 {
70 while (start < end && *start != '\n') {
71 start++;
72 }
73 }
74
75 bool parse_token(const char *token, size_t token_length)
76 {
78 if (end - start < token_length + 1) {
79 return false;
80 }
81 if (memcmp(start, token, token_length) != 0) {
82 return false;
83 }
84 if (start[token_length] > ' ') {
85 return false;
86 }
87 start += token_length + 1;
88 return true;
89 }
90
96
97 void parse_float(float &out)
98 {
100 /* Skip '+' */
101 if (start < end && *start == '+') {
102 start++;
103 }
104 fast_float::from_chars_result res = fast_float::from_chars(start, end, out);
105 if (ELEM(res.ec, std::errc::invalid_argument, std::errc::result_out_of_range)) {
106 out = 0.0f;
107 }
108 start = const_cast<char *>(res.ptr);
109 }
110};
111
112static inline void parse_float3(StringBuffer &buf, float3 &out)
113{
114 for (int i = 0; i < 3; i++) {
115 buf.parse_float(out[i]);
116 }
117}
118
119Mesh *read_stl_ascii(const char *filepath, const bool use_custom_normals)
120{
121 size_t buffer_len;
122 char *buffer = BLI_file_read_text_as_mem(filepath, 0, &buffer_len);
123 if (buffer == nullptr) {
124 CLOG_ERROR(&LOG, "STL Importer: cannot read from ASCII STL file: '%s'", filepath);
125 return nullptr;
126 }
127 BLI_SCOPED_DEFER([&]() { MEM_delete(buffer); });
128
129 constexpr int num_reserved_tris = 1024;
130
131 StringBuffer str_buf(buffer, buffer_len);
132 STLMeshHelper stl_mesh(num_reserved_tris, use_custom_normals);
133
135 str_buf.drop_line(); /* Skip header line */
136 while (!str_buf.is_empty()) {
137 if (str_buf.parse_token("vertex", 6)) {
138 parse_float3(str_buf, data.vertices[0]);
139 if (str_buf.parse_token("vertex", 6)) {
140 parse_float3(str_buf, data.vertices[1]);
141 }
142 if (str_buf.parse_token("vertex", 6)) {
143 parse_float3(str_buf, data.vertices[2]);
144 }
145
146 stl_mesh.add_triangle(data);
147 }
148 else if (str_buf.parse_token("facet", 5)) {
149 str_buf.drop_token(); /* Expecting "normal" */
150 parse_float3(str_buf, data.normal);
151 }
152 else {
153 str_buf.drop_token();
154 }
155 }
156
157 return stl_mesh.to_mesh();
158}
159
160} // namespace io::stl
161} // namespace blender
File and directory operations.
#define BLI_SCOPED_DEFER(function_to_defer)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define ELEM(...)
bool add_triangle(const PackedTriangle &data)
bool parse_token(const char *token, size_t token_length)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
static void parse_float3(StringBuffer &buf, float3 &out)
Mesh * read_stl_ascii(const char *filepath, const bool use_custom_normals)
BMesh Iterators.
char * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:539
BMesh const char void * data
static CLG_LogRef LOG
Definition logging.cpp:12
VecBase< float, 3 > float3