source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/vrippack-0.31/src/march/mctoply.cc @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 5.4 KB
Line 
1/*
2
3Converts from Paul Ning's polygon file format to text polygon description.
4
5Greg Turk - November 1992
6
7*/
8
9#include <stdio.h>
10#include <malloc.h>
11#include "mcfile.h"
12#include <math.h>
13#include <string.h>
14#include <unistd.h>
15
16#include "Linear.h"
17
18#include "ply.h"
19
20struct PlyVertex {
21    float x, y, z;
22    float nx, ny, nz;
23};
24
25struct PlyFace {
26    unsigned char nverts;
27    int *verts;
28};
29
30static PlyVertex *vert_dummy;
31#define voffset(field) ((char *) (&vert_dummy->field) - (char *) vert_dummy)
32
33static PlyFace *face_dummy;
34#define foffset(field) ((char *) (&face_dummy->field) - (char *) face_dummy)
35
36static char *elem_names[] = { 
37  "vertex", "face",
38};
39
40static PlyProperty vert_props[] =  {
41    {"x", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0},
42    {"y", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0},
43    {"z", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0},
44    {"nx", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0},
45    {"ny", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0},
46    {"nz", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}
47};
48
49static PlyProperty face_props[] = { 
50  {"vertex_indices", PLY_INT, PLY_INT, 0, 1, PLY_UCHAR, PLY_UCHAR, 0},
51};
52
53
54TriangleVertex *verts;
55int npolys;
56
57
58void write_polys_ply(char *filename);
59
60
61/******************************************************************************
62Main routine.
63******************************************************************************/
64
65void
66main(int, char **argv)
67{
68  int i,j;
69  FILE *fp;
70  char infile[80],outfile[80];
71  mcfile header;
72  int index;
73
74  strcpy (infile, argv[1]);
75  strcpy (outfile, argv[2]);
76
77  if (strlen (infile) < 3 ||
78      strcmp (infile + strlen (infile) - 3, ".mc") != 0)
79      strcat (infile, ".mc");
80
81  if (strlen (outfile) < 5 ||
82      strcmp (outfile + strlen (outfile) - 5, ".ply") != 0)
83      strcat (outfile, ".ply");
84
85  printf ("reading polygons from '%s'\n", infile);
86
87  /* open the polygon input file */
88
89  if ((fp = fopen(infile, "r")) == NULL) {
90    fprintf (stderr, "bad open\n");
91    exit (-1);
92  }
93
94  /* open the polygon output file */
95
96/*
97  if ((fp_out = fopen(outfile, "w+b")) == NULL) {
98    fprintf (stderr, "bad open\n");
99    exit (-1);
100  }
101*/
102
103  /* read header info from the polygon file */
104  header = MC_ReadHeader (fp);
105
106  /*
107  npolys = header.mc_length / (3 * sizeof (sizeof(TriangleVertex)));
108  */
109  npolys = header.mc_length / 42;
110  printf ("%d polygons\n", npolys);
111  verts = (TriangleVertex *) malloc (sizeof (TriangleVertex) * 3 * npolys);
112  if (verts == 0) {
113    fprintf (stderr, "can't allocate enough space\n");
114    exit (-1);
115  }
116
117  /* read in the polygons */
118  printf ("reading polygons...\n");
119  for (i = 0; i < npolys; i++) {
120    for (j = 0; j < 3; j++) {
121      index = i * 3 + j;
122      verts[index] = MC_ReadTriangleVertex (fp);
123    }
124  }
125
126  /* write polygons to output file */
127  printf ("writing polygons...\n");
128  write_polys_ply (outfile);
129
130  printf ("done.\n");
131}
132
133
134/******************************************************************************
135Write out polygons in Inventor format.
136******************************************************************************/
137
138void
139write_polys_ply(char *filename)
140{
141    int i,j;
142    int index;
143    float len;
144    float version;
145
146    PlyFile *ply = ply_open_for_writing(filename, 2, elem_names, 
147                                        PLY_BINARY_BE, &version);
148
149
150    // Shouldn't this be a tri-strip-set???
151    int numVerts = 3*npolys;  // Hugely inefficient!!
152    int numFaces = npolys;   
153
154    int nvp = 0;
155    vert_props[nvp].offset = voffset(x); nvp++;
156    vert_props[nvp].offset = voffset(y); nvp++;
157    vert_props[nvp].offset = voffset(z); nvp++;
158
159    vert_props[nvp].offset = voffset(nx); nvp++;
160    vert_props[nvp].offset = voffset(ny); nvp++;
161    vert_props[nvp].offset = voffset(nz); nvp++;
162
163
164    face_props[0].offset = foffset(verts);
165    face_props[0].count_offset = foffset(nverts);  /* count offset */
166   
167    ply_describe_element (ply, "vertex", numVerts, 
168                          nvp, vert_props);
169
170    ply_describe_element (ply, "face", npolys, 1, face_props);
171
172    ply_header_complete (ply);
173   
174    /* set up and write the vertex elements */
175    PlyVertex vert;
176    ply_put_element_setup (ply, "vertex");
177    for (i = 0; i < npolys; i++) {
178        for (j = 0; j < 3; j++) {
179            index = i * 3 + j;
180            vert.x = verts[index].x / 128.0;
181            vert.y = verts[index].y / 128.0;
182            vert. z = verts[index].z / 128.0;
183            vert.nx = -verts[index].nx;
184            vert.ny = -verts[index].ny;
185            vert.nz = -verts[index].nz;
186            len = sqrt (vert.nx*vert.nx + vert.ny*vert.ny + vert.nz*vert.nz);
187            vert.nx /= len;
188            vert.ny /= len;
189            vert.nz /= len;
190            ply_put_element (ply, (void *) &vert);
191        }
192    }   
193
194    /* set up and write the face elements */
195    PlyFace face;
196    int v[3];
197    face.nverts = 3;
198    face.verts = (int *)(v);
199    ply_put_element_setup (ply, "face");
200    for (i = 0; i < npolys; i++) {
201        Vec3f v1(verts[i*3].x, verts[i*3].y, verts[i*3].z);
202        Vec3f v2(verts[i*3+1].x, verts[i*3+1].y, verts[i*3+1].z);
203        Vec3f v3(verts[i*3+2].x, verts[i*3+2].y, verts[i*3+2].z);
204
205        Vec3f a = v2 - v1;
206        Vec3f b = v3 - v1;
207        Vec3f c = a.cross(b);
208        Vec3f norm(-verts[i*3].nx, -verts[i*3].ny, -verts[i*3].nz);
209        if (c.dot(norm) > 0) {
210            face.verts[0] = i*3;
211            face.verts[1] = i*3+1;
212            face.verts[2] = i*3+2;
213        }
214        else {
215            face.verts[0] = i*3+1;
216            face.verts[1] = i*3;
217            face.verts[2] = i*3+2;
218        }
219        ply_put_element (ply, (void *) &face);
220    }   
221   
222    /* close the PLY file */
223    ply_close (ply);   
224}
225
Note: See TracBrowser for help on using the repository browser.