~ubuntu-branches/ubuntu/utopic/slic3r/utopic

« back to all changes in this revision

Viewing changes to xs/src/admesh/shared.c

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2014-06-17 01:27:26 UTC
  • Revision ID: package-import@ubuntu.com-20140617012726-2wrs4zdo251nr4vg
Tags: upstream-1.1.4+dfsg
ImportĀ upstreamĀ versionĀ 1.1.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  ADMesh -- process triangulated solid meshes
 
2
 *  Copyright (C) 1995, 1996  Anthony D. Martin
 
3
 *
 
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)
 
7
 *  any later version.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 *  
 
18
 *  Questions, comments, suggestions, etc to <amartin@engr.csulb.edu>
 
19
 */
 
20
 
 
21
#include <cstring>
 
22
#include <stdlib.h>
 
23
 
 
24
#include "stl.h"
 
25
 
 
26
void
 
27
stl_invalidate_shared_vertices(stl_file *stl)
 
28
{
 
29
    if (stl->v_indices != NULL) {
 
30
        free(stl->v_indices);
 
31
        stl->v_indices = NULL;
 
32
    }
 
33
    if (stl->v_shared != NULL) {
 
34
        free(stl->v_shared);
 
35
        stl->v_shared = NULL;
 
36
    }
 
37
}
 
38
 
 
39
void
 
40
stl_generate_shared_vertices(stl_file *stl)
 
41
{
 
42
  int i;
 
43
  int j;
 
44
  int first_facet;
 
45
  int direction;
 
46
  int facet_num;
 
47
  int vnot;
 
48
  int next_edge;
 
49
  int pivot_vertex;
 
50
  int next_facet;
 
51
  int reversed;
 
52
  
 
53
  // make sure this function is idempotent and does not leak memory
 
54
  stl_invalidate_shared_vertices(stl);
 
55
  
 
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;
 
64
  
 
65
  for(i = 0; i < stl->stats.number_of_facets; i++)
 
66
    {
 
67
      stl->v_indices[i].vertex[0] = -1;
 
68
      stl->v_indices[i].vertex[1] = -1;
 
69
      stl->v_indices[i].vertex[2] = -1;
 
70
    }
 
71
  
 
72
 
 
73
  for(i = 0; i < stl->stats.number_of_facets; i++)
 
74
    {
 
75
      first_facet = i;
 
76
      for(j = 0; j < 3; j++)
 
77
        {
 
78
          if(stl->v_indices[i].vertex[j] != -1)
 
79
            {
 
80
              continue;
 
81
            }
 
82
          if(stl->stats.shared_vertices == stl->stats.shared_malloced)
 
83
            {
 
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");
 
88
            }
 
89
              
 
90
          stl->v_shared[stl->stats.shared_vertices] = 
 
91
            stl->facet_start[i].vertex[j];
 
92
 
 
93
          direction = 0;
 
94
          reversed = 0;
 
95
          facet_num = i;
 
96
          vnot = (j + 2) % 3;
 
97
 
 
98
          for(;;)
 
99
            {
 
100
              if(vnot > 2)
 
101
                {
 
102
                  if(direction == 0)
 
103
                    {
 
104
                      pivot_vertex = (vnot + 2) % 3;
 
105
                      next_edge = pivot_vertex;
 
106
                      direction = 1;
 
107
                    }
 
108
                  else
 
109
                    {
 
110
                      pivot_vertex = (vnot + 1) % 3;
 
111
                      next_edge = vnot % 3;
 
112
                      direction = 0;
 
113
                    }
 
114
                }
 
115
              else
 
116
                {
 
117
                  if(direction == 0)
 
118
                    {
 
119
                      pivot_vertex = (vnot + 1) % 3;
 
120
                      next_edge = vnot;
 
121
                    }
 
122
                  else
 
123
                    {
 
124
                      pivot_vertex = (vnot + 2) % 3;
 
125
                      next_edge = pivot_vertex;
 
126
                    }
 
127
                }
 
128
              stl->v_indices[facet_num].vertex[pivot_vertex] =
 
129
                stl->stats.shared_vertices;
 
130
              
 
131
              next_facet = stl->neighbors_start[facet_num].neighbor[next_edge];
 
132
              if(next_facet == -1)
 
133
                {
 
134
                  if(reversed)
 
135
                    {
 
136
                      break;
 
137
                    }
 
138
                  else
 
139
                    {
 
140
                      direction = 1;
 
141
                      vnot = (j + 1) % 3;
 
142
                      reversed = 1;
 
143
                      facet_num = first_facet;
 
144
                    }
 
145
                }
 
146
              else if(next_facet != first_facet)
 
147
                {
 
148
                  vnot = stl->neighbors_start[facet_num].
 
149
                    which_vertex_not[next_edge];
 
150
                  facet_num = next_facet;
 
151
                }
 
152
              else
 
153
                {
 
154
                  break;
 
155
                }
 
156
            }
 
157
          stl->stats.shared_vertices += 1;
 
158
        }
 
159
    }
 
160
}
 
161
 
 
162
void
 
163
stl_write_off(stl_file *stl, char *file)
 
164
{
 
165
  int i;
 
166
  FILE      *fp;
 
167
  char      *error_msg;
 
168
  
 
169
  
 
170
  /* Open the file */
 
171
  fp = fopen(file, "w");
 
172
  if(fp == NULL)
 
173
    {
 
174
      error_msg = (char*)
 
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",
 
177
              file);
 
178
      perror(error_msg);
 
179
      free(error_msg);
 
180
      exit(1);
 
181
    }
 
182
  
 
183
  fprintf(fp, "OFF\n");
 
184
  fprintf(fp, "%d %d 0\n",
 
185
          stl->stats.shared_vertices, stl->stats.number_of_facets);
 
186
 
 
187
  for(i = 0; i < stl->stats.shared_vertices; i++)
 
188
    {
 
189
      fprintf(fp, "\t%f %f %f\n",
 
190
              stl->v_shared[i].x, stl->v_shared[i].y, stl->v_shared[i].z);
 
191
    }
 
192
  for(i = 0; i < stl->stats.number_of_facets; i++)
 
193
    {
 
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]);
 
196
    }
 
197
  fclose(fp);
 
198
}
 
199
 
 
200
void
 
201
stl_write_vrml(stl_file *stl, char *file)
 
202
{
 
203
  int i;
 
204
  FILE      *fp;
 
205
  char      *error_msg;
 
206
  
 
207
  
 
208
  /* Open the file */
 
209
  fp = fopen(file, "w");
 
210
  if(fp == NULL)
 
211
    {
 
212
      error_msg = (char*)
 
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",
 
215
              file);
 
216
      perror(error_msg);
 
217
      free(error_msg);
 
218
      exit(1);
 
219
    }
 
220
  
 
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");
 
235
 
 
236
  for(i = 0; i < (stl->stats.shared_vertices - 1); i++)
 
237
    {
 
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);
 
240
    }
 
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");
 
246
 
 
247
  for(i = 0; i < (stl->stats.number_of_facets - 1); i++)
 
248
    {
 
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]);
 
251
    }
 
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");
 
256
  fprintf(fp, "}\n");
 
257
  fclose(fp);
 
258
}
 
259
 
 
260
void stl_write_obj (stl_file *stl, char *file) {
 
261
    int i;
 
262
    
 
263
    /* Open the file */
 
264
    FILE* fp = fopen(file, "w");
 
265
    if (fp == NULL) {
 
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);
 
268
        perror(error_msg);
 
269
        free(error_msg);
 
270
        exit(1);
 
271
    }
 
272
    
 
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);
 
275
    }
 
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);
 
278
    }
 
279
    
 
280
    fclose(fp);
 
281
}