1
/* ADMesh -- process triangulated solid meshes
2
* Copyright (C) 1995, 1996 Anthony D. Martin
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2, or (at your option)
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
* Questions, comments, suggestions, etc to <amartin@engr.csulb.edu>
25
#if !defined(SEEK_SET)
31
static void stl_put_little_int(FILE *fp, int value);
32
static void stl_put_little_float(FILE *fp, float value_in);
35
stl_print_edges(stl_file *stl, FILE *file)
40
edges_allocated = stl->stats.number_of_facets * 3;
41
for(i = 0; i < edges_allocated; i++)
43
fprintf(file, "%d, %f, %f, %f, %f, %f, %f\n",
44
stl->edge_start[i].facet_number,
45
stl->edge_start[i].p1.x, stl->edge_start[i].p1.y,
46
stl->edge_start[i].p1.z, stl->edge_start[i].p2.x,
47
stl->edge_start[i].p2.y, stl->edge_start[i].p2.z);
53
stl_stats_out(stl_file *stl, FILE *file, char *input_file)
56
================= Results produced by ADMesh version 0.95 ================\n");
58
Input file : %s\n", input_file);
59
if(stl->stats.type == binary)
62
File type : Binary STL file\n");
67
File type : ASCII STL file\n");
70
Header : %s\n", stl->stats.header);
71
fprintf(file, "============== Size ==============\n");
72
fprintf(file, "Min X = % f, Max X = % f\n",
73
stl->stats.min.x, stl->stats.max.x);
74
fprintf(file, "Min Y = % f, Max Y = % f\n",
75
stl->stats.min.y, stl->stats.max.y);
76
fprintf(file, "Min Z = % f, Max Z = % f\n",
77
stl->stats.min.z, stl->stats.max.z);
80
========= Facet Status ========== Original ============ Final ====\n");
82
Number of facets : %5d %5d\n",
83
stl->stats.original_num_facets, stl->stats.number_of_facets);
85
Facets with 1 disconnected edge : %5d %5d\n",
86
stl->stats.facets_w_1_bad_edge, stl->stats.connected_facets_2_edge -
87
stl->stats.connected_facets_3_edge);
89
Facets with 2 disconnected edges : %5d %5d\n",
90
stl->stats.facets_w_2_bad_edge, stl->stats.connected_facets_1_edge -
91
stl->stats.connected_facets_2_edge);
93
Facets with 3 disconnected edges : %5d %5d\n",
94
stl->stats.facets_w_3_bad_edge, stl->stats.number_of_facets -
95
stl->stats.connected_facets_1_edge);
97
Total disconnected facets : %5d %5d\n",
98
stl->stats.facets_w_1_bad_edge + stl->stats.facets_w_2_bad_edge +
99
stl->stats.facets_w_3_bad_edge, stl->stats.number_of_facets -
100
stl->stats.connected_facets_3_edge);
103
"=== Processing Statistics === ===== Other Statistics =====\n");
105
Number of parts : %5d Volume : % f\n",
106
stl->stats.number_of_parts, stl->stats.volume);
108
Degenerate facets : %5d\n", stl->stats.degenerate_facets);
110
Edges fixed : %5d\n", stl->stats.edges_fixed);
112
Facets removed : %5d\n", stl->stats.facets_removed);
114
Facets added : %5d\n", stl->stats.facets_added);
116
Facets reversed : %5d\n", stl->stats.facets_reversed);
118
Backwards edges : %5d\n", stl->stats.backwards_edges);
120
Normals fixed : %5d\n", stl->stats.normals_fixed);
124
stl_write_ascii(stl_file *stl, const char *file, const char *label)
132
fp = fopen(file, "w");
136
malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
137
sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing",
144
fprintf(fp, "solid %s\n", label);
146
for(i = 0; i < stl->stats.number_of_facets; i++)
148
fprintf(fp, " facet normal % .8E % .8E % .8E\n",
149
stl->facet_start[i].normal.x, stl->facet_start[i].normal.y,
150
stl->facet_start[i].normal.z);
151
fprintf(fp, " outer loop\n");
152
fprintf(fp, " vertex % .8E % .8E % .8E\n",
153
stl->facet_start[i].vertex[0].x, stl->facet_start[i].vertex[0].y,
154
stl->facet_start[i].vertex[0].z);
155
fprintf(fp, " vertex % .8E % .8E % .8E\n",
156
stl->facet_start[i].vertex[1].x, stl->facet_start[i].vertex[1].y,
157
stl->facet_start[i].vertex[1].z);
158
fprintf(fp, " vertex % .8E % .8E % .8E\n",
159
stl->facet_start[i].vertex[2].x, stl->facet_start[i].vertex[2].y,
160
stl->facet_start[i].vertex[2].z);
161
fprintf(fp, " endloop\n");
162
fprintf(fp, " endfacet\n");
165
fprintf(fp, "endsolid %s\n", label);
171
stl_print_neighbors(stl_file *stl, char *file)
178
fp = fopen(file, "w");
182
malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
183
sprintf(error_msg, "stl_print_neighbors: Couldn't open %s for writing",
190
for(i = 0; i < stl->stats.number_of_facets; i++)
192
fprintf(fp, "%d, %d,%d, %d,%d, %d,%d\n",
194
stl->neighbors_start[i].neighbor[0],
195
(int)stl->neighbors_start[i].which_vertex_not[0],
196
stl->neighbors_start[i].neighbor[1],
197
(int)stl->neighbors_start[i].which_vertex_not[1],
198
stl->neighbors_start[i].neighbor[2],
199
(int)stl->neighbors_start[i].which_vertex_not[2]);
205
stl_put_little_int(FILE *fp, int value_in)
214
value.int_value = value_in;
216
new_value = value.char_value[0] & 0xFF;
217
new_value |= (value.char_value[1] & 0xFF) << 0x08;
218
new_value |= (value.char_value[2] & 0xFF) << 0x10;
219
new_value |= (value.char_value[3] & 0xFF) << 0x18;
220
fwrite(&new_value, sizeof(int), 1, fp);
224
stl_put_little_float(FILE *fp, float value_in)
233
value.float_value = value_in;
235
new_value = value.char_value[0] & 0xFF;
236
new_value |= (value.char_value[1] & 0xFF) << 0x08;
237
new_value |= (value.char_value[2] & 0xFF) << 0x10;
238
new_value |= (value.char_value[3] & 0xFF) << 0x18;
239
fwrite(&new_value, sizeof(int), 1, fp);
244
stl_write_binary(stl_file *stl, const char *file, const char *label)
252
fp = fopen(file, "wb");
256
malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
257
sprintf(error_msg, "stl_write_binary: Couldn't open %s for writing",
264
fprintf(fp, "%s", label);
265
for(i = strlen(label); i < LABEL_SIZE; i++) putc(0, fp);
267
fseek(fp, LABEL_SIZE, SEEK_SET);
269
stl_put_little_int(fp, stl->stats.number_of_facets);
271
for(i = 0; i < stl->stats.number_of_facets; i++)
273
stl_put_little_float(fp, stl->facet_start[i].normal.x);
274
stl_put_little_float(fp, stl->facet_start[i].normal.y);
275
stl_put_little_float(fp, stl->facet_start[i].normal.z);
276
stl_put_little_float(fp, stl->facet_start[i].vertex[0].x);
277
stl_put_little_float(fp, stl->facet_start[i].vertex[0].y);
278
stl_put_little_float(fp, stl->facet_start[i].vertex[0].z);
279
stl_put_little_float(fp, stl->facet_start[i].vertex[1].x);
280
stl_put_little_float(fp, stl->facet_start[i].vertex[1].y);
281
stl_put_little_float(fp, stl->facet_start[i].vertex[1].z);
282
stl_put_little_float(fp, stl->facet_start[i].vertex[2].x);
283
stl_put_little_float(fp, stl->facet_start[i].vertex[2].y);
284
stl_put_little_float(fp, stl->facet_start[i].vertex[2].z);
285
fputc(stl->facet_start[i].extra[0], fp);
286
fputc(stl->facet_start[i].extra[1], fp);
293
stl_write_vertex(stl_file *stl, int facet, int vertex)
295
printf(" vertex %d/%d % .8E % .8E % .8E\n", vertex, facet,
296
stl->facet_start[facet].vertex[vertex].x,
297
stl->facet_start[facet].vertex[vertex].y,
298
stl->facet_start[facet].vertex[vertex].z);
302
stl_write_facet(stl_file *stl, char *label, int facet)
304
printf("facet (%d)/ %s\n", facet, label);
305
stl_write_vertex(stl, facet, 0);
306
stl_write_vertex(stl, facet, 1);
307
stl_write_vertex(stl, facet, 2);
311
stl_write_edge(stl_file *stl, char *label, stl_hash_edge edge)
313
printf("edge (%d)/(%d) %s\n", edge.facet_number, edge.which_edge, label);
314
if(edge.which_edge < 3)
316
stl_write_vertex(stl, edge.facet_number, edge.which_edge % 3);
317
stl_write_vertex(stl, edge.facet_number, (edge.which_edge + 1) % 3);
321
stl_write_vertex(stl, edge.facet_number, (edge.which_edge + 1) % 3);
322
stl_write_vertex(stl, edge.facet_number, edge.which_edge % 3);
327
stl_write_neighbor(stl_file *stl, int facet)
329
printf("Neighbors %d: %d, %d, %d ; %d, %d, %d\n", facet,
330
stl->neighbors_start[facet].neighbor[0],
331
stl->neighbors_start[facet].neighbor[1],
332
stl->neighbors_start[facet].neighbor[2],
333
stl->neighbors_start[facet].which_vertex_not[0],
334
stl->neighbors_start[facet].which_vertex_not[1],
335
stl->neighbors_start[facet].which_vertex_not[2]);
339
stl_write_quad_object(stl_file *stl, char *file)
345
stl_vertex connect_color;
346
stl_vertex uncon_1_color;
347
stl_vertex uncon_2_color;
348
stl_vertex uncon_3_color;
352
fp = fopen(file, "w");
356
malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
357
sprintf(error_msg, "stl_write_quad_object: Couldn't open %s for writing",
364
connect_color.x = 0.0;
365
connect_color.y = 0.0;
366
connect_color.z = 1.0;
367
uncon_1_color.x = 0.0;
368
uncon_1_color.y = 1.0;
369
uncon_1_color.z = 0.0;
370
uncon_2_color.x = 1.0;
371
uncon_2_color.y = 1.0;
372
uncon_2_color.z = 1.0;
373
uncon_3_color.x = 1.0;
374
uncon_3_color.y = 0.0;
375
uncon_3_color.z = 0.0;
377
fprintf(fp, "CQUAD\n");
378
for(i = 0; i < stl->stats.number_of_facets; i++)
380
j = ((stl->neighbors_start[i].neighbor[0] == -1) +
381
(stl->neighbors_start[i].neighbor[1] == -1) +
382
(stl->neighbors_start[i].neighbor[2] == -1));
385
color = connect_color;
389
color = uncon_1_color;
393
color = uncon_2_color;
397
color = uncon_3_color;
399
fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n",
400
stl->facet_start[i].vertex[0].x,
401
stl->facet_start[i].vertex[0].y,
402
stl->facet_start[i].vertex[0].z, color.x, color.y, color.z);
403
fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n",
404
stl->facet_start[i].vertex[1].x,
405
stl->facet_start[i].vertex[1].y,
406
stl->facet_start[i].vertex[1].z, color.x, color.y, color.z);
407
fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n",
408
stl->facet_start[i].vertex[2].x,
409
stl->facet_start[i].vertex[2].y,
410
stl->facet_start[i].vertex[2].z, color.x, color.y, color.z);
411
fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n",
412
stl->facet_start[i].vertex[2].x,
413
stl->facet_start[i].vertex[2].y,
414
stl->facet_start[i].vertex[2].z, color.x, color.y, color.z);
420
stl_write_dxf(stl_file *stl, char *file, char *label)
428
fp = fopen(file, "w");
432
malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
433
sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing",
440
fprintf(fp, "999\n%s\n", label);
441
fprintf(fp, "0\nSECTION\n2\nHEADER\n0\nENDSEC\n");
442
fprintf(fp, "0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLAYER\n70\n1\n\
443
0\nLAYER\n2\n0\n70\n0\n62\n7\n6\nCONTINUOUS\n0\nENDTAB\n0\nENDSEC\n");
444
fprintf(fp, "0\nSECTION\n2\nBLOCKS\n0\nENDSEC\n");
446
fprintf(fp, "0\nSECTION\n2\nENTITIES\n");
448
for(i = 0; i < stl->stats.number_of_facets; i++)
450
fprintf(fp, "0\n3DFACE\n8\n0\n");
451
fprintf(fp, "10\n%f\n20\n%f\n30\n%f\n",
452
stl->facet_start[i].vertex[0].x, stl->facet_start[i].vertex[0].y,
453
stl->facet_start[i].vertex[0].z);
454
fprintf(fp, "11\n%f\n21\n%f\n31\n%f\n",
455
stl->facet_start[i].vertex[1].x, stl->facet_start[i].vertex[1].y,
456
stl->facet_start[i].vertex[1].z);
457
fprintf(fp, "12\n%f\n22\n%f\n32\n%f\n",
458
stl->facet_start[i].vertex[2].x, stl->facet_start[i].vertex[2].y,
459
stl->facet_start[i].vertex[2].z);
460
fprintf(fp, "13\n%f\n23\n%f\n33\n%f\n",
461
stl->facet_start[i].vertex[2].x, stl->facet_start[i].vertex[2].y,
462
stl->facet_start[i].vertex[2].z);
465
fprintf(fp, "0\nENDSEC\n0\nEOF\n");