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>
27
stl_invalidate_shared_vertices(stl_file *stl)
29
if (stl->v_indices != NULL) {
31
stl->v_indices = NULL;
33
if (stl->v_shared != NULL) {
40
stl_generate_shared_vertices(stl_file *stl)
53
// make sure this function is idempotent and does not leak memory
54
stl_invalidate_shared_vertices(stl);
56
stl->v_indices = (v_indices_struct*)
57
calloc(stl->stats.number_of_facets, sizeof(v_indices_struct));
58
if(stl->v_indices == NULL) perror("stl_generate_shared_vertices");
59
stl->v_shared = (stl_vertex*)
60
calloc((stl->stats.number_of_facets / 2), sizeof(stl_vertex));
61
if(stl->v_shared == NULL) perror("stl_generate_shared_vertices");
62
stl->stats.shared_malloced = stl->stats.number_of_facets / 2;
63
stl->stats.shared_vertices = 0;
65
for(i = 0; i < stl->stats.number_of_facets; i++)
67
stl->v_indices[i].vertex[0] = -1;
68
stl->v_indices[i].vertex[1] = -1;
69
stl->v_indices[i].vertex[2] = -1;
73
for(i = 0; i < stl->stats.number_of_facets; i++)
76
for(j = 0; j < 3; j++)
78
if(stl->v_indices[i].vertex[j] != -1)
82
if(stl->stats.shared_vertices == stl->stats.shared_malloced)
84
stl->stats.shared_malloced += 1024;
85
stl->v_shared = (stl_vertex*)realloc(stl->v_shared,
86
stl->stats.shared_malloced * sizeof(stl_vertex));
87
if(stl->v_shared == NULL) perror("stl_generate_shared_vertices");
90
stl->v_shared[stl->stats.shared_vertices] =
91
stl->facet_start[i].vertex[j];
104
pivot_vertex = (vnot + 2) % 3;
105
next_edge = pivot_vertex;
110
pivot_vertex = (vnot + 1) % 3;
111
next_edge = vnot % 3;
119
pivot_vertex = (vnot + 1) % 3;
124
pivot_vertex = (vnot + 2) % 3;
125
next_edge = pivot_vertex;
128
stl->v_indices[facet_num].vertex[pivot_vertex] =
129
stl->stats.shared_vertices;
131
next_facet = stl->neighbors_start[facet_num].neighbor[next_edge];
143
facet_num = first_facet;
146
else if(next_facet != first_facet)
148
vnot = stl->neighbors_start[facet_num].
149
which_vertex_not[next_edge];
150
facet_num = next_facet;
157
stl->stats.shared_vertices += 1;
163
stl_write_off(stl_file *stl, char *file)
171
fp = fopen(file, "w");
175
malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
176
sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing",
183
fprintf(fp, "OFF\n");
184
fprintf(fp, "%d %d 0\n",
185
stl->stats.shared_vertices, stl->stats.number_of_facets);
187
for(i = 0; i < stl->stats.shared_vertices; i++)
189
fprintf(fp, "\t%f %f %f\n",
190
stl->v_shared[i].x, stl->v_shared[i].y, stl->v_shared[i].z);
192
for(i = 0; i < stl->stats.number_of_facets; i++)
194
fprintf(fp, "\t3 %d %d %d\n", stl->v_indices[i].vertex[0],
195
stl->v_indices[i].vertex[1], stl->v_indices[i].vertex[2]);
201
stl_write_vrml(stl_file *stl, char *file)
209
fp = fopen(file, "w");
213
malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
214
sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing",
221
fprintf(fp, "#VRML V1.0 ascii\n\n");
222
fprintf(fp, "Separator {\n");
223
fprintf(fp, "\tDEF STLShape ShapeHints {\n");
224
fprintf(fp, "\t\tvertexOrdering COUNTERCLOCKWISE\n");
225
fprintf(fp, "\t\tfaceType CONVEX\n");
226
fprintf(fp, "\t\tshapeType SOLID\n");
227
fprintf(fp, "\t\tcreaseAngle 0.0\n");
228
fprintf(fp, "\t}\n");
229
fprintf(fp, "\tDEF STLModel Separator {\n");
230
fprintf(fp, "\t\tDEF STLColor Material {\n");
231
fprintf(fp, "\t\t\temissiveColor 0.700000 0.700000 0.000000\n");
232
fprintf(fp, "\t\t}\n");
233
fprintf(fp, "\t\tDEF STLVertices Coordinate3 {\n");
234
fprintf(fp, "\t\t\tpoint [\n");
236
for(i = 0; i < (stl->stats.shared_vertices - 1); i++)
238
fprintf(fp, "\t\t\t\t%f %f %f,\n",
239
stl->v_shared[i].x, stl->v_shared[i].y, stl->v_shared[i].z);
241
fprintf(fp, "\t\t\t\t%f %f %f]\n",
242
stl->v_shared[i].x, stl->v_shared[i].y, stl->v_shared[i].z);
243
fprintf(fp, "\t\t}\n");
244
fprintf(fp, "\t\tDEF STLTriangles IndexedFaceSet {\n");
245
fprintf(fp, "\t\t\tcoordIndex [\n");
247
for(i = 0; i < (stl->stats.number_of_facets - 1); i++)
249
fprintf(fp, "\t\t\t\t%d, %d, %d, -1,\n", stl->v_indices[i].vertex[0],
250
stl->v_indices[i].vertex[1], stl->v_indices[i].vertex[2]);
252
fprintf(fp, "\t\t\t\t%d, %d, %d, -1]\n", stl->v_indices[i].vertex[0],
253
stl->v_indices[i].vertex[1], stl->v_indices[i].vertex[2]);
254
fprintf(fp, "\t\t}\n");
255
fprintf(fp, "\t}\n");
260
void stl_write_obj (stl_file *stl, char *file) {
264
FILE* fp = fopen(file, "w");
266
char* error_msg = (char*)malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
267
sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing", file);
273
for (i = 0; i < stl->stats.shared_vertices; i++) {
274
fprintf(fp, "v %f %f %f\n", stl->v_shared[i].x, stl->v_shared[i].y, stl->v_shared[i].z);
276
for (i = 0; i < stl->stats.number_of_facets; i++) {
277
fprintf(fp, "f %d %d %d\n", stl->v_indices[i].vertex[0]+1, stl->v_indices[i].vertex[1]+1, stl->v_indices[i].vertex[2]+1);