importPlyData.cpp 6.99 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Utils/os_spec.h"
#include "Algo/Import/importPlyData.h"
#include <stdlib.h>

namespace CGoGN
{

char* PlyImportData::elem_names[] = { /* list of the elements in the object */
	(char*) "vertex", (char*) "face"
	};
	
PlyProperty PlyImportData::vert_props[] = { /* list of property information for a vertex */
CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
37
38
39
	{(char*) "x", PLY_Float32, PLY_Float32, offsetof(Vertex,x), 0, 0, 0, 0},
	{(char*) "y", PLY_Float32, PLY_Float32, offsetof(Vertex,y), 0, 0, 0, 0},
	{(char*) "z", PLY_Float32, PLY_Float32, offsetof(Vertex,z), 0, 0, 0, 0},
40
41
42
	{(char*) "red", PLY_Uint8, PLY_Uint8, offsetof(Vertex,red), 0, 0, 0, 0},
	{(char*) "green", PLY_Uint8, PLY_Uint8, offsetof(Vertex,green), 0, 0, 0, 0},
	{(char*) "blue", PLY_Uint8, PLY_Uint8, offsetof(Vertex,blue), 0, 0, 0, 0},
CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
43
44
45
46
47
48
	{(char*) "r", PLY_Float32, PLY_Float32, offsetof(Vertex,r), 0, 0, 0, 0},
	{(char*) "g", PLY_Float32, PLY_Float32, offsetof(Vertex,g), 0, 0, 0, 0},
	{(char*) "b", PLY_Float32, PLY_Float32, offsetof(Vertex,b), 0, 0, 0, 0},
	{(char*) "nx", PLY_Float32, PLY_Float32, offsetof(Vertex,nx), 0, 0, 0, 0},
	{(char*) "ny", PLY_Float32, PLY_Float32, offsetof(Vertex,ny), 0, 0, 0, 0},
	{(char*) "nz", PLY_Float32, PLY_Float32, offsetof(Vertex,nz), 0, 0, 0, 0},
Pierre Kraemer's avatar
Pierre Kraemer committed
49
50
51
	};
	
PlyProperty PlyImportData::face_props[] = { /* list of property information for a face */
CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
52
53
	{(char*) "vertex_indices", PLY_Int32, PLY_Int32, offsetof(Face,verts),
	1, PLY_Uint8, PLY_Uint8, offsetof(Face,nverts)},
Pierre Kraemer's avatar
Pierre Kraemer committed
54
55
56
57
58
59
60
61
62
	};


PlyImportData::PlyImportData(): 
	nverts(0),nfaces(0),
	vlist(NULL),
	flist(NULL),
	vert_other(NULL),
	face_other(NULL),
63
	per_vertex_color_float32(0),
64
	per_vertex_color_uint8(0),
Pierre Kraemer's avatar
Pierre Kraemer committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
	has_normals(0)	
{
}

PlyImportData::~PlyImportData()
{
// 	if (vlist!= NULL)
// 	{
// 		for (int i=0; i<nverts; ++i)
// 		{
// 			if (vlist[i]!=NULL)
// 				free(vlist[i]);
// 		}
// 		free(vlist);
// 	}
// 	
// 	if (flist!= NULL)
// 	{
// 		for (int i=0; i<nfaces; ++i)
// 		{
// 			if (flist[i]!=NULL)
// 				free(flist[i]);
// 		}
// 		free(flist);
// 	}
		
// need to free *vert_other,*face_other ????
}
	
	
	
bool PlyImportData::read_file(const std::string& filename)
{

  /*** Read in the original PLY object ***/

  FILE* fp = fopen(filename.c_str(),"r");

  if (fp==NULL) return false;
  
  PlyFile *in_ply  = read_ply (fp);


  if (in_ply==NULL) return false;


  for (int i = 0; i < in_ply->num_elem_types; i++) 
  {
	  int elem_count;
    /* prepare to read the i'th list of elements */
    char *elem_name = setup_element_read_ply (in_ply, i, &elem_count);

    if (equal_strings ((char*) "vertex", elem_name)) {

      /* create a vertex list to hold all the vertices */
      vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count);
      nverts = elem_count;

      /* set up for getting vertex elements */

      setup_property_ply (in_ply, &vert_props[0]);
      setup_property_ply (in_ply, &vert_props[1]);
      setup_property_ply (in_ply, &vert_props[2]);

      for (int j = 0; j < in_ply->elems[i]->nprops; j++) 
	  {
		PlyProperty *prop;
		prop = in_ply->elems[i]->props[j];
133
		if (equal_strings ((char*) "red", prop->name)) {
Pierre Kraemer's avatar
Pierre Kraemer committed
134
		setup_property_ply (in_ply, &vert_props[3]);
135
		per_vertex_color_uint8 = 1;
Pierre Kraemer's avatar
Pierre Kraemer committed
136
		}
137
		if (equal_strings ((char*) "green", prop->name)) {
Pierre Kraemer's avatar
Pierre Kraemer committed
138
		setup_property_ply (in_ply, &vert_props[4]);
139
		per_vertex_color_uint8 = 1;
Pierre Kraemer's avatar
Pierre Kraemer committed
140
		}
141
		if (equal_strings ((char*) "blue", prop->name)) {
Pierre Kraemer's avatar
Pierre Kraemer committed
142
		setup_property_ply (in_ply, &vert_props[5]);
143
		per_vertex_color_uint8 = 1;
Pierre Kraemer's avatar
Pierre Kraemer committed
144
		}
145
		if (equal_strings ((char*) "r", prop->name)) {
Pierre Kraemer's avatar
Pierre Kraemer committed
146
		setup_property_ply (in_ply, &vert_props[6]);
147
148
149
150
151
152
153
154
155
156
157
158
		per_vertex_color_float32 = 1;
		}
		if (equal_strings ((char*) "g", prop->name)) {
		setup_property_ply (in_ply, &vert_props[7]);
		per_vertex_color_float32 = 1;
		}
		if (equal_strings ((char*) "b", prop->name)) {
		setup_property_ply (in_ply, &vert_props[8]);
		per_vertex_color_float32 = 1;
		}
		if (equal_strings ((char*) "nx", prop->name)) {
		setup_property_ply (in_ply, &vert_props[9]);
Pierre Kraemer's avatar
Pierre Kraemer committed
159
160
161
		has_normals = 1;
		}
		if (equal_strings ((char*) "ny", prop->name)) {
162
		setup_property_ply (in_ply, &vert_props[10]);
Pierre Kraemer's avatar
Pierre Kraemer committed
163
164
165
		has_normals = 1;
		}
		if (equal_strings ((char*) "nz", prop->name)) {
166
		setup_property_ply (in_ply, &vert_props[11]);
Pierre Kraemer's avatar
Pierre Kraemer committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
		has_normals = 1;
		}
      }

      vert_other = get_other_properties_ply (in_ply, 
					     offsetof(Vertex,other_props));

      /* grab all the vertex elements */
      for (int j = 0; j < elem_count; j++) {
        vlist[j] = (Vertex *) malloc (sizeof (Vertex));
		vlist[j]->r = 1;
		vlist[j]->g = 1;
		vlist[j]->b = 1;
        get_element_ply (in_ply, (void *) vlist[j]);
      }
    }
    else if (equal_strings ((char*) "face", elem_name)) {

      /* create a list to hold all the face elements */
      flist = (Face **) malloc (sizeof (Face *) * elem_count);
      nfaces = elem_count;

      /* set up for getting face elements */

      setup_property_ply (in_ply, &face_props[0]);
      face_other = get_other_properties_ply (in_ply, 
					     offsetof(Face,other_props));

      /* grab all the face elements */
      for (int j = 0; j < elem_count; j++) {
        flist[j] = (Face *) malloc (sizeof (Face));
        get_element_ply (in_ply, (void *) flist[j]);
      }
    }
    else
      get_other_element_ply (in_ply);
  }
  
  close_ply (in_ply);
  
  free_ply (in_ply);

  return true;
}

} // namespace CGoGN