00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "mesh_loader.h"
00036
00037 #include "debug.h"
00038
00039 const char *elem_names[] = {
00040 "vertex", "face"
00041 };
00042
00043 PlyProperty vert_props[] = {
00044 {"x", Float32, Float32, offsetof(Vertex,x), 0, 0, 0, 0},
00045 {"y", Float32, Float32, offsetof(Vertex,y), 0, 0, 0, 0},
00046 {"z", Float32, Float32, offsetof(Vertex,z), 0, 0, 0, 0},
00047 };
00048
00049 PlyProperty face_props[] = {
00050 {"vertex_indices", Int32, Int32, offsetof(Face,verts),
00051 1, Uint8, Uint8, offsetof(Face,nverts)},
00052 };
00053
00054
00055 inline void PLYModelLoader::endian_swap(void* p)
00056 {
00057 unsigned int* x = (unsigned int*)p;
00058
00059 *x = (*x>>24) |
00060 ((*x<<8) & 0x00FF0000) |
00061 ((*x>>8) & 0x0000FF00) |
00062 (*x<<24);
00063 }
00064
00065
00066 int PLYModelLoader::readFromFile(const string& filename, std::vector<position> &vertices,
00067 std::vector<int> &triangles)
00068 {
00069 DBGA("Loading mesh file: " << filename);
00070 int i,j;
00071 int elem_count;
00072 char *elem_name;
00073
00074
00075 vertices.clear();
00076 triangles.clear();
00077
00078 FILE* fin = fopen(filename.c_str(), "rb");
00079
00080 if (fin==NULL) {
00081 DBGA("Cannot read file: " << filename);
00082 return -1;
00083 }
00084
00085 in_ply = read_ply (fin);
00086
00087 float version;
00088 int file_type;
00089 get_info_ply(in_ply, &version, &file_type);
00090
00091
00092
00093 for (i = 0; i < in_ply->num_elem_types; i++) {
00094
00095
00096 elem_name = setup_element_read_ply (in_ply, i, &elem_count);
00097
00098 if (equal_strings ((char*)"vertex", elem_name)) {
00099 nverts = elem_count;
00100 vertices.reserve(nverts);
00101
00102
00103
00104
00105 setup_property_ply (in_ply, &vert_props[0]);
00106 setup_property_ply (in_ply, &vert_props[1]);
00107 setup_property_ply (in_ply, &vert_props[2]);
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 vert_other = get_other_properties_ply (in_ply,
00131 offsetof(Vertex,other_props));
00132
00133
00134 for (j = 0; j < elem_count; j++) {
00135 get_element_ply (in_ply, (void *) &vertex);
00136 if (file_type==PLY_BINARY_BE) {
00137 endian_swap(&vertex.x);
00138 endian_swap(&vertex.y);
00139 endian_swap(&vertex.z);
00140 }
00141 vertices.push_back( position(vertex.x, vertex.y, vertex.z) );
00142 }
00143 }
00144 else if (equal_strings ((char*)"face", elem_name)) {
00145
00146 nfaces = elem_count;
00147 triangles.reserve(nfaces*3);
00148
00149
00150
00151
00152 setup_property_ply (in_ply, &face_props[0]);
00153 face_other = get_other_properties_ply (in_ply,
00154 offsetof(Face,other_props));
00155
00156
00157
00158 for (j = 0; j < elem_count; j++) {
00159 get_element_ply (in_ply, (void *) &face);
00160 if (file_type==PLY_BINARY_BE) {
00161 for (int k=0;k<face.nverts;++k) {
00162 endian_swap(&face.verts[k]);
00163 }
00164 }
00165
00166
00167 if (face.nverts!=3) {
00168 DBGA("Mesh contains non triangle faces!");
00169 }
00170 else {
00171 triangles.push_back(face.verts[0]);
00172 triangles.push_back(face.verts[1]);
00173 triangles.push_back(face.verts[2]);
00174 }
00175 }
00176 }
00177 else
00178 get_other_element_ply (in_ply);
00179 }
00180
00181 close_ply (in_ply);
00182
00183 DBGA("File contains " << nverts << " vertices and " << nfaces << " faces");
00184 return 0;
00185 }