Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
stl_io.cpp File Reference
#include <stdlib.h>
#include <string.h>
#include <boost/log/trivial.hpp>
#include <boost/nowide/cstdio.hpp>
#include <boost/predef/other/endian.h>
#include "stl.h"
+ Include dependency graph for stl_io.cpp:

Go to the source code of this file.

Macros

#define VERSION   "unknown"
 
#define SEEK_SET   0
 

Functions

void stl_stats_out (stl_file *stl, FILE *file, char *input_file)
 
bool stl_write_ascii (stl_file *stl, const char *file, const char *label)
 
bool stl_print_neighbors (stl_file *stl, char *file)
 
bool stl_write_binary (stl_file *stl, const char *file, const char *label)
 
void stl_write_vertex (stl_file *stl, int facet, int vertex)
 
void stl_write_facet (stl_file *stl, char *label, int facet)
 
void stl_write_neighbor (stl_file *stl, int facet)
 
bool stl_write_quad_object (stl_file *stl, char *file)
 
bool stl_write_dxf (stl_file *stl, const char *file, char *label)
 

Macro Definition Documentation

◆ SEEK_SET

#define SEEK_SET   0

◆ VERSION

#define VERSION   "unknown"

Function Documentation

◆ stl_print_neighbors()

bool stl_print_neighbors ( stl_file stl,
char *  file 
)
96{
97 FILE *fp = boost::nowide::fopen(file, "w");
98 if (fp == nullptr) {
99 BOOST_LOG_TRIVIAL(error) << "stl_print_neighbors: Couldn't open " << file << " for writing";
100 return false;
101 }
102
103 for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) {
104 fprintf(fp, "%d, %d,%d, %d,%d, %d,%d\n",
105 i,
106 stl->neighbors_start[i].neighbor[0],
107 (int)stl->neighbors_start[i].which_vertex_not[0],
108 stl->neighbors_start[i].neighbor[1],
109 (int)stl->neighbors_start[i].which_vertex_not[1],
110 stl->neighbors_start[i].neighbor[2],
111 (int)stl->neighbors_start[i].which_vertex_not[2]);
112 }
113 fclose(fp);
114 return true;
115}
stl_stats stats
Definition stl.h:153
std::vector< stl_neighbors > neighbors_start
Definition stl.h:151
uint32_t number_of_facets
Definition stl.h:95
static char error[256]
Definition tga.cpp:50
unsigned __int32 uint32_t
Definition unistd.h:79

References error, stl_file::neighbors_start, stl_stats::number_of_facets, and stl_file::stats.

◆ stl_stats_out()

void stl_stats_out ( stl_file stl,
FILE *  file,
char *  input_file 
)
33{
34 // This is here for Slic3r, without our config.h it won't use this part of the code anyway.
35#ifndef VERSION
36#define VERSION "unknown"
37#endif
38 fprintf(file, "\n================= Results produced by ADMesh version " VERSION " ================\n");
39 fprintf(file, "Input file : %s\n", input_file);
40 if (stl->stats.type == binary)
41 fprintf(file, "File type : Binary STL file\n");
42 else
43 fprintf(file, "File type : ASCII STL file\n");
44 fprintf(file, "Header : %s\n", stl->stats.header);
45 fprintf(file, "============== Size ==============\n");
46 fprintf(file, "Min X = % f, Max X = % f\n", stl->stats.min(0), stl->stats.max(0));
47 fprintf(file, "Min Y = % f, Max Y = % f\n", stl->stats.min(1), stl->stats.max(1));
48 fprintf(file, "Min Z = % f, Max Z = % f\n", stl->stats.min(2), stl->stats.max(2));
49 fprintf(file, "========= Facet Status ========== Original ============ Final ====\n");
50 fprintf(file, "Number of facets : %5d %5d\n", stl->stats.original_num_facets, stl->stats.number_of_facets);
51 fprintf(file, "Facets with 1 disconnected edge : %5d %5d\n",
53 fprintf(file, "Facets with 2 disconnected edges : %5d %5d\n",
55 fprintf(file, "Facets with 3 disconnected edges : %5d %5d\n",
57 fprintf(file, "Total disconnected facets : %5d %5d\n",
59 fprintf(file, "=== Processing Statistics === ===== Other Statistics =====\n");
60 fprintf(file, "Number of parts : %5d Volume : %f\n", stl->stats.number_of_parts, stl->stats.volume);
61 fprintf(file, "Degenerate facets : %5d\n", stl->stats.degenerate_facets);
62 fprintf(file, "Edges fixed : %5d\n", stl->stats.edges_fixed);
63 fprintf(file, "Facets removed : %5d\n", stl->stats.facets_removed);
64 fprintf(file, "Facets added : %5d\n", stl->stats.facets_added);
65 fprintf(file, "Facets reversed : %5d\n", stl->stats.facets_reversed);
66 fprintf(file, "Backwards edges : %5d\n", stl->stats.backwards_edges);
67 fprintf(file, "Normals fixed : %5d\n", stl->stats.normals_fixed);
68}
@ binary
Definition stl.h:70
#define VERSION
int backwards_edges
Definition stl.h:128
stl_type type
Definition stl.h:93
int facets_reversed
Definition stl.h:126
int connected_facets_1_edge
Definition stl.h:108
int facets_w_3_bad_edge
Definition stl.h:114
int facets_w_2_bad_edge
Definition stl.h:113
char header[81]
Definition stl.h:92
int degenerate_facets
Definition stl.h:120
stl_vertex max
Definition stl.h:97
int number_of_parts
Definition stl.h:132
int facets_added
Definition stl.h:124
stl_vertex min
Definition stl.h:98
int facets_removed
Definition stl.h:122
int facets_w_1_bad_edge
Definition stl.h:112
int normals_fixed
Definition stl.h:130
int edges_fixed
Definition stl.h:118
int connected_facets_2_edge
Definition stl.h:109
int original_num_facets
Definition stl.h:116
float volume
Definition stl.h:103
int connected_facets_3_edge
Definition stl.h:110

References stl_stats::backwards_edges, binary, stl_stats::connected_facets_1_edge, stl_stats::connected_facets_2_edge, stl_stats::connected_facets_3_edge, stl_stats::degenerate_facets, stl_stats::edges_fixed, stl_stats::facets_added, stl_stats::facets_removed, stl_stats::facets_reversed, stl_stats::facets_w_1_bad_edge, stl_stats::facets_w_2_bad_edge, stl_stats::facets_w_3_bad_edge, stl_stats::header, stl_stats::max, stl_stats::min, stl_stats::normals_fixed, stl_stats::number_of_facets, stl_stats::number_of_parts, stl_stats::original_num_facets, stl_file::stats, stl_stats::type, VERSION, and stl_stats::volume.

◆ stl_write_ascii()

bool stl_write_ascii ( stl_file stl,
const char *  file,
const char *  label 
)
71{
72 FILE *fp = boost::nowide::fopen(file, "w");
73 if (fp == nullptr) {
74 BOOST_LOG_TRIVIAL(error) << "stl_write_ascii: Couldn't open " << file << " for writing";
75 return false;
76 }
77
78 fprintf(fp, "solid %s\n", label);
79
80 for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) {
81 fprintf(fp, " facet normal % .8E % .8E % .8E\n", stl->facet_start[i].normal(0), stl->facet_start[i].normal(1), stl->facet_start[i].normal(2));
82 fprintf(fp, " outer loop\n");
83 fprintf(fp, " vertex % .8E % .8E % .8E\n", stl->facet_start[i].vertex[0](0), stl->facet_start[i].vertex[0](1), stl->facet_start[i].vertex[0](2));
84 fprintf(fp, " vertex % .8E % .8E % .8E\n", stl->facet_start[i].vertex[1](0), stl->facet_start[i].vertex[1](1), stl->facet_start[i].vertex[1](2));
85 fprintf(fp, " vertex % .8E % .8E % .8E\n", stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), stl->facet_start[i].vertex[2](2));
86 fprintf(fp, " endloop\n");
87 fprintf(fp, " endfacet\n");
88 }
89
90 fprintf(fp, "endsolid %s\n", label);
91 fclose(fp);
92 return true;
93}
std::vector< stl_facet > facet_start
Definition stl.h:150

References error, stl_file::facet_start, stl_stats::number_of_facets, and stl_file::stats.

◆ stl_write_binary()

bool stl_write_binary ( stl_file stl,
const char *  file,
const char *  label 
)
129{
130 FILE *fp = boost::nowide::fopen(file, "wb");
131 if (fp == nullptr) {
132 BOOST_LOG_TRIVIAL(error) << "stl_write_binary: Couldn't open " << file << " for writing";
133 return false;
134 }
135
136 fprintf(fp, "%s", label);
137 for (size_t i = strlen(label); i < LABEL_SIZE; ++ i)
138 putc(0, fp);
139
140#if !defined(SEEK_SET)
141 #define SEEK_SET 0
142#endif
143 fseek(fp, LABEL_SIZE, SEEK_SET);
144#if BOOST_ENDIAN_LITTLE_BYTE
145 fwrite(&stl->stats.number_of_facets, 4, 1, fp);
146 for (const stl_facet &facet : stl->facet_start)
147 fwrite(&facet, SIZEOF_STL_FACET, 1, fp);
148#else /* BOOST_ENDIAN_LITTLE_BYTE */
149 char buffer[50];
150 // Convert the number of facets to little endian.
151 memcpy(buffer, &stl->stats.number_of_facets, 4);
152 stl_internal_reverse_quads(buffer, 4);
153 fwrite(buffer, 4, 1, fp);
154 for (const stl_facet &facet : stl->facet_start) {
155 memcpy(buffer, &facet, 50);
156 // Convert to little endian.
157 stl_internal_reverse_quads(buffer, 48);
158 fwrite(buffer, SIZEOF_STL_FACET, 1, fp);
159 }
160#endif /* BOOST_ENDIAN_LITTLE_BYTE */
161 fclose(fp);
162 return true;
163}
#define SIZEOF_STL_FACET
Definition stl.h:63
#define LABEL_SIZE
Definition stl.h:34
#define SEEK_SET
Definition stl.h:48

References error, stl_file::facet_start, LABEL_SIZE, stl_stats::number_of_facets, SEEK_SET, SIZEOF_STL_FACET, and stl_file::stats.

◆ stl_write_dxf()

bool stl_write_dxf ( stl_file stl,
const char *  file,
char *  label 
)
225{
226 FILE *fp = boost::nowide::fopen(file, "w");
227 if (fp == nullptr) {
228 BOOST_LOG_TRIVIAL(error) << "stl_write_quad_object: Couldn't open " << file << " for writing";
229 return false;
230 }
231
232 fprintf(fp, "999\n%s\n", label);
233 fprintf(fp, "0\nSECTION\n2\nHEADER\n0\nENDSEC\n");
234 fprintf(fp, "0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLAYER\n70\n1\n\
235 0\nLAYER\n2\n0\n70\n0\n62\n7\n6\nCONTINUOUS\n0\nENDTAB\n0\nENDSEC\n");
236 fprintf(fp, "0\nSECTION\n2\nBLOCKS\n0\nENDSEC\n");
237
238 fprintf(fp, "0\nSECTION\n2\nENTITIES\n");
239
240 for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) {
241 fprintf(fp, "0\n3DFACE\n8\n0\n");
242 fprintf(fp, "10\n%f\n20\n%f\n30\n%f\n", stl->facet_start[i].vertex[0](0), stl->facet_start[i].vertex[0](1), stl->facet_start[i].vertex[0](2));
243 fprintf(fp, "11\n%f\n21\n%f\n31\n%f\n", stl->facet_start[i].vertex[1](0), stl->facet_start[i].vertex[1](1), stl->facet_start[i].vertex[1](2));
244 fprintf(fp, "12\n%f\n22\n%f\n32\n%f\n", stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), stl->facet_start[i].vertex[2](2));
245 fprintf(fp, "13\n%f\n23\n%f\n33\n%f\n", stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), stl->facet_start[i].vertex[2](2));
246 }
247
248 fprintf(fp, "0\nENDSEC\n0\nEOF\n");
249 fclose(fp);
250 return true;
251}

References error, stl_file::facet_start, stl_stats::number_of_facets, and stl_file::stats.

◆ stl_write_facet()

void stl_write_facet ( stl_file stl,
char *  label,
int  facet 
)
174{
175 printf("facet (%d)/ %s\n", facet, label);
176 stl_write_vertex(stl, facet, 0);
177 stl_write_vertex(stl, facet, 1);
178 stl_write_vertex(stl, facet, 2);
179}
void stl_write_vertex(stl_file *stl, int facet, int vertex)
Definition stl_io.cpp:165

References stl_write_vertex().

Referenced by stl_verify_neighbors().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ stl_write_neighbor()

void stl_write_neighbor ( stl_file stl,
int  facet 
)
182{
183 printf("Neighbors %d: %d, %d, %d ; %d, %d, %d\n", facet,
184 stl->neighbors_start[facet].neighbor[0],
185 stl->neighbors_start[facet].neighbor[1],
186 stl->neighbors_start[facet].neighbor[2],
187 stl->neighbors_start[facet].which_vertex_not[0],
188 stl->neighbors_start[facet].which_vertex_not[1],
189 stl->neighbors_start[facet].which_vertex_not[2]);
190}

References stl_file::neighbors_start.

◆ stl_write_quad_object()

bool stl_write_quad_object ( stl_file stl,
char *  file 
)
193{
194 stl_vertex connect_color = stl_vertex::Zero();
195 stl_vertex uncon_1_color = stl_vertex::Zero();
196 stl_vertex uncon_2_color = stl_vertex::Zero();
197 stl_vertex uncon_3_color = stl_vertex::Zero();
198 stl_vertex color;
199
200 FILE *fp = boost::nowide::fopen(file, "w");
201 if (fp == nullptr) {
202 BOOST_LOG_TRIVIAL(error) << "stl_write_quad_object: Couldn't open " << file << " for writing";
203 return false;
204 }
205
206 fprintf(fp, "CQUAD\n");
207 for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) {
208 switch (stl->neighbors_start[i].num_neighbors()) {
209 case 0:
210 default: color = uncon_3_color; break;
211 case 1: color = uncon_2_color; break;
212 case 2: color = uncon_1_color; break;
213 case 3: color = connect_color; break;
214 }
215 fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", stl->facet_start[i].vertex[0](0), stl->facet_start[i].vertex[0](1), stl->facet_start[i].vertex[0](2), color(0), color(1), color(2));
216 fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", stl->facet_start[i].vertex[1](0), stl->facet_start[i].vertex[1](1), stl->facet_start[i].vertex[1](2), color(0), color(1), color(2));
217 fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), stl->facet_start[i].vertex[2](2), color(0), color(1), color(2));
218 fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), stl->facet_start[i].vertex[2](2), color(0), color(1), color(2));
219 }
220 fclose(fp);
221 return true;
222}

References error, stl_file::facet_start, stl_file::neighbors_start, stl_stats::number_of_facets, and stl_file::stats.

◆ stl_write_vertex()

void stl_write_vertex ( stl_file stl,
int  facet,
int  vertex 
)
166{
167 printf(" vertex %d/%d % .8E % .8E % .8E\n", vertex, facet,
168 stl->facet_start[facet].vertex[vertex](0),
169 stl->facet_start[facet].vertex[vertex](1),
170 stl->facet_start[facet].vertex[vertex](2));
171}

References stl_file::facet_start.

Referenced by stl_write_facet().

+ Here is the caller graph for this function: