importPlyData.cpp 6.44 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Utils/os_spec.h"
#include "Algo/Import/importPlyData.h"
#include <stdlib.h>
28
#include <locale.h>
Pierre Kraemer's avatar
Pierre Kraemer committed
29
30
31
32
33
34
35
36
37

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
38
39
40
41
42
43
44
45
46
	{(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},
	{(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
47
48
49
	};
	
PlyProperty PlyImportData::face_props[] = { /* list of property information for a face */
CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
50
51
	{(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
52
53
54
55
56
57
58
59
60
61
62
63
	};


PlyImportData::PlyImportData(): 
	nverts(0),nfaces(0),
	vlist(NULL),
	flist(NULL),
	vert_other(NULL),
	face_other(NULL),
	per_vertex_color(0),
	has_normals(0)	
{
64
65
	old_locale = setlocale(LC_NUMERIC, NULL);
	setlocale(LC_NUMERIC, "C");
Pierre Kraemer's avatar
Pierre Kraemer committed
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
}

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 ????
91
	setlocale(LC_NUMERIC, old_locale);
Pierre Kraemer's avatar
Pierre Kraemer committed
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
}
	
	
	
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];
		if (equal_strings ((char*) "r", prop->name)) {
		setup_property_ply (in_ply, &vert_props[3]);
		per_vertex_color = 1;
		}
		if (equal_strings ((char*) "g", prop->name)) {
		setup_property_ply (in_ply, &vert_props[4]);
		per_vertex_color = 1;
		}
		if (equal_strings ((char*) "b", prop->name)) {
		setup_property_ply (in_ply, &vert_props[5]);
		per_vertex_color = 1;
		}
		if (equal_strings ((char*) "nx", prop->name)) {
		setup_property_ply (in_ply, &vert_props[6]);
		has_normals = 1;
		}
		if (equal_strings ((char*) "ny", prop->name)) {
		setup_property_ply (in_ply, &vert_props[7]);
		has_normals = 1;
		}
		if (equal_strings ((char*) "nz", prop->name)) {
		setup_property_ply (in_ply, &vert_props[8]);
		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