Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
337 jab 1
/*
2
 *  ply_load.cpp
3
 *  GEL
4
 *
5
 *  Created by J. Andreas Bærentzen on 08/08/07.
6
 *  Copyright 2007 __MyCompanyName__. All rights reserved.
7
 *
8
 */
9
 
10
 
11
#include <stdio.h>
12
#include <math.h>
13
#include <strings.h>
14
#include "ply_load.h"
15
 
16
using namespace std;
17
using namespace Geometry;
18
using namespace CGLA;
19
 
20
typedef struct Vertex {
21
  float x,y,z;
22
  float r,g,b;
23
  float nx,ny,nz;
24
  void *other_props;       /* other properties */
25
} Vertex;
26
 
27
typedef struct Face {
28
  unsigned char nverts;    /* number of vertex indices in list */
29
  int *verts;              /* vertex index list */
30
  void *other_props;       /* other properties */
31
} Face;
32
 
33
char *elem_names[] = { /* list of the elements in the object */
34
  "vertex", "face"
35
};
36
 
37
PlyProperty vert_props[] = { /* list of property information for a vertex */
38
  {"x", Float32, Float32, offsetof(Vertex,x), 0, 0, 0, 0},
39
  {"y", Float32, Float32, offsetof(Vertex,y), 0, 0, 0, 0},
40
  {"z", Float32, Float32, offsetof(Vertex,z), 0, 0, 0, 0},
41
  {"r", Float32, Float32, offsetof(Vertex,r), 0, 0, 0, 0},
42
  {"g", Float32, Float32, offsetof(Vertex,g), 0, 0, 0, 0},
43
  {"b", Float32, Float32, offsetof(Vertex,b), 0, 0, 0, 0},
44
  {"nx", Float32, Float32, offsetof(Vertex,nx), 0, 0, 0, 0},
45
  {"ny", Float32, Float32, offsetof(Vertex,ny), 0, 0, 0, 0},
46
  {"nz", Float32, Float32, offsetof(Vertex,nz), 0, 0, 0, 0},
47
};
48
 
49
PlyProperty face_props[] = { /* list of property information for a face */
50
  {"vertex_indices", Int32, Int32, offsetof(Face,verts),
51
   1, Uint8, Uint8, offsetof(Face,nverts)},
52
};
53
 
54
 
55
/*** the PLY object ***/
56
 
57
static int nverts,nfaces;
58
static Vertex **vlist;
59
static Face **flist;
60
 
61
static PlyOtherProp *vert_other,*face_other;
62
 
63
static int per_vertex_color = 0;
64
static int has_normals = 0;
65
 
66
void read_file(FILE* f)
67
{
68
  int i,j;
69
  int elem_count;
70
  char *elem_name;
71
  PlyFile *in_ply;
72
 
73
  /*** Read in the original PLY object ***/
74
 
75
  in_ply  = read_ply (f);
76
 
77
  for (i = 0; i < in_ply->num_elem_types; i++) {
78
 
79
    /* prepare to read the i'th list of elements */
80
    elem_name = setup_element_read_ply (in_ply, i, &elem_count);
81
 
82
    if (equal_strings ("vertex", elem_name)) {
83
 
84
      /* create a vertex list to hold all the vertices */
85
      vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count);
86
      nverts = elem_count;
87
 
88
      /* set up for getting vertex elements */
89
 
90
      setup_property_ply (in_ply, &vert_props[0]);
91
      setup_property_ply (in_ply, &vert_props[1]);
92
      setup_property_ply (in_ply, &vert_props[2]);
93
 
94
      for (j = 0; j < in_ply->elems[i]->nprops; j++) {
95
	PlyProperty *prop;
96
	prop = in_ply->elems[i]->props[j];
97
	if (equal_strings ("r", prop->name)) {
98
	  setup_property_ply (in_ply, &vert_props[3]);
99
	  per_vertex_color = 1;
100
	}
101
	if (equal_strings ("g", prop->name)) {
102
	  setup_property_ply (in_ply, &vert_props[4]);
103
	  per_vertex_color = 1;
104
	}
105
	if (equal_strings ("b", prop->name)) {
106
	  setup_property_ply (in_ply, &vert_props[5]);
107
	  per_vertex_color = 1;
108
	}
109
	if (equal_strings ("nx", prop->name)) {
110
	  setup_property_ply (in_ply, &vert_props[6]);
111
	  has_normals = 1;
112
	}
113
	if (equal_strings ("ny", prop->name)) {
114
	  setup_property_ply (in_ply, &vert_props[7]);
115
	  has_normals = 1;
116
	}
117
	if (equal_strings ("nz", prop->name)) {
118
	  setup_property_ply (in_ply, &vert_props[8]);
119
	  has_normals = 1;
120
	}
121
      }
122
 
123
      vert_other = get_other_properties_ply (in_ply, 
124
					     offsetof(Vertex,other_props));
125
 
126
      /* grab all the vertex elements */
127
      for (j = 0; j < elem_count; j++) {
128
        vlist[j] = (Vertex *) malloc (sizeof (Vertex));
129
	vlist[j]->r = 1;
130
	vlist[j]->g = 1;
131
	vlist[j]->b = 1;
132
        get_element_ply (in_ply, (void *) vlist[j]);
133
      }
134
    }
135
    else if (equal_strings ("face", elem_name)) {
136
 
137
      /* create a list to hold all the face elements */
138
      flist = (Face **) malloc (sizeof (Face *) * elem_count);
139
      nfaces = elem_count;
140
 
141
      /* set up for getting face elements */
142
 
143
      setup_property_ply (in_ply, &face_props[0]);
144
      face_other = get_other_properties_ply (in_ply, 
145
					     offsetof(Face,other_props));
146
 
147
      /* grab all the face elements */
148
      for (j = 0; j < elem_count; j++) {
149
        flist[j] = (Face *) malloc (sizeof (Face));
150
        get_element_ply (in_ply, (void *) flist[j]);
151
      }
152
    }
153
    else
154
      get_other_element_ply (in_ply);
155
  }
156
 
157
  close_ply (in_ply);
158
  free_ply (in_ply);
159
}
160
 
161
 
162
 
163
void ply_load(const std::string& fn, Geometry::TriMesh& mesh)
164
{
165
	Vec3f v;
166
	FILE* f = fopen(fn.c_str(),"rb");
167
	read_file(f);
168
 
169
	mesh.materials.resize(1);
170
 
171
	int i;
172
	for (i = 0; i < nverts; i++)
173
		mesh.geometry.add_vertex(Vec3f(vlist[i]->x, vlist[i]->y, vlist[i]->z));
174
 
175
	for (i = 0; i < nfaces; i++) {
176
		 for(int j=2;j<flist[i]->nverts;++j)
177
			{
178
				mesh.mat_idx.push_back(0);
179
				Vec3i f_geo(Vec3i(flist[i]->verts[0], flist[i]->verts[1], flist[i]->verts[j]));
180
				mesh.geometry.add_face(f_geo);
181
			}
182
  }
183
  cout << "LOADED " << nverts << " verts " << nfaces << " faces " << endl;
184
 
185
}
186