Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
plyimporter.hpp
Go to the documentation of this file.
1 
23 #ifndef __PLYIMPORTER__
24 #define __PLYIMPORTER__
25 
26 #include <tucano/mesh.hpp>
27 #include <tucano/utils/rply.hpp>
28 
29 
30 namespace Tucano
31 {
32 namespace MeshImporter
33 {
34 #if _WIN32 //define something for Windows (32-bit and 64-bit, this part is common)
35  #pragma warning(disable:4996)
36 #else
37  // avoid warnings of unused function
38  static bool loadPlyFile (Mesh* mesh, string filename) __attribute__ ((unused));
39 #endif
40 
41 
42 
43  static int normal_cb( p_ply_argument argument )
44  {
45  static Eigen::Vector3f v;
46 
47  void* data;
48  long coord;
49 
50  ply_get_argument_user_data( argument, &data, &coord );
51 
52  vector<Eigen::Vector3f>* vec = static_cast< vector<Eigen::Vector3f>* >( data );
53 
54  switch( coord )
55  {
56  case 0:
57  case 1:
58  v[coord] = ply_get_argument_value( argument );
59  break;
60 
61  case 2:
62  v[2] = ply_get_argument_value( argument );
63  vec->push_back( v );
64  break;
65  }
66 
67  return 1;
68  }
69 
70  static int color_cb( p_ply_argument argument )
71  {
72  static Eigen::Vector4f c;
73 
74  void* data;
75  long coord;
76 
77  ply_get_argument_user_data( argument, &data, &coord );
78 
79  float channel = ply_get_argument_value( argument );
80  if (channel > 1.0)
81  channel /= 255.0;
82 
83  switch( coord )
84  {
85  case 0:
86  case 1:
87  c[coord] = channel;
88  break;
89 
90  case 2:
91  c[2] = channel;
92  c[3] = 1.0;
93  (static_cast< vector<Eigen::Vector4f>* >( data ))->push_back( c );
94  break;
95  }
96 
97  return 1;
98  }
99 
100  static int vertex_cb( p_ply_argument argument )
101  {
102  static Eigen::Vector4f v;
103  void* data;
104  long coord;
105 
106  ply_get_argument_user_data( argument, &data, &coord );
107 
108  switch( coord )
109  {
110  case 0:
111  case 1:
112  v[coord] = ply_get_argument_value( argument );
113  break;
114 
115  case 2:
116  v[2] = ply_get_argument_value( argument );
117  v[3] = 1.0;
118  (static_cast< vector<Eigen::Vector4f>* >( data ))->push_back( v );
119  break;
120  }
121 
122  return 1;
123  }
124 
125 
126  static int face_cb( p_ply_argument argument )
127  {
128  long value_index;
129  void* data;
130 
131  // 3rd parameter is the list length, which is always 3 for triangle meshes
132  // if necessary it can be retrieved passing a reference to a long var
133  ply_get_argument_property( argument, NULL, NULL, &value_index);
134  ply_get_argument_user_data( argument, &data, NULL );
135 
136  if (value_index >= 0 && value_index < 3)
137  {
138  (static_cast< vector<unsigned int>* >(data))->push_back(ply_get_argument_value(argument));
139  }
140 
141  return 1;
142  }
143 
150  static bool loadPlyFile (Mesh *mesh, string filename)
151  {
152  p_ply ply = ply_open( filename.c_str(), NULL, 0, NULL );
153  if( !ply || !ply_read_header( ply ) )
154  {
155  std::cerr << "Cannot open " << filename.c_str() << std::endl;
156  return false;
157  }
158 
159  #ifdef TUCANODEBUG
160  std::cout << "Opening Stanford ply file " << filename.c_str() << std::endl << std::endl;
161  #endif
162 
163 
164  vector<Eigen::Vector4f> vertices;
165  vector<Eigen::Vector3f> norm;
166  vector<Eigen::Vector2f> texCoord;
167  vector<Eigen::Vector4f> color;
168  std::vector<unsigned int> indices;
169 
170  // long nvertices, ncolors, nnormals, ntriangles;
171 
172  // nvertices = ply_set_read_cb( ply, "vertex", "x", vertex_cb, ( void* )&vertices, 0 );
173  ply_set_read_cb( ply, "vertex", "x", vertex_cb, ( void* )&vertices, 0 );
174  ply_set_read_cb( ply, "vertex", "y", vertex_cb, ( void* )&vertices, 1 );
175  ply_set_read_cb( ply, "vertex", "z", vertex_cb, ( void* )&vertices, 2 );
176 
177  // ncolors = ply_set_read_cb( ply, "vertex", "red", vertex_cb, ( void* )&color, 0 );
178  ply_set_read_cb( ply, "vertex", "red", color_cb, ( void* )&color, 0 );
179  ply_set_read_cb( ply, "vertex", "green", color_cb, ( void* )&color, 1 );
180  ply_set_read_cb( ply, "vertex", "blue", color_cb, ( void* )&color, 2 );
181 
182  // nnormals = ply_set_read_cb( ply, "vertex", "nx", normal_cb, ( void* )&norm, 0 );
183  ply_set_read_cb( ply, "vertex", "ny", normal_cb, ( void* )&norm, 1 );
184  ply_set_read_cb( ply, "vertex", "nx", normal_cb, ( void* )&norm, 0 );
185  ply_set_read_cb( ply, "vertex", "nz", normal_cb, ( void* )&norm, 2 );
186 
187  // ntriangles = ply_set_read_cb(ply, "face", "vertex_indices", face_cb, &indices, 0);
188  ply_set_read_cb(ply, "face", "vertex_indices", face_cb, &indices, 0);
189 
190  if( !ply_read( ply ) )
191  {
192  return false;
193  }
194 
195  ply_close( ply );
196 
197  // load attributes found in file
198  if (vertices.size() > 0)
199  mesh->loadVertices(vertices);
200  if (norm.size() > 0)
201  mesh->loadNormals(norm);
202  if (texCoord.size() > 0)
203  mesh->loadTexCoords(texCoord);
204  if (color.size() > 0)
205  mesh->loadColors(color);
206  if (indices.size() > 0)
207  mesh->loadIndices(indices);
208 
209  // sets the default locations for accesing attributes in shaders
211 
212 
213  #ifdef TUCANODEBUG
214  Tucano::Misc::errorCheckFunc(__FILE__, __LINE__);
215  #endif
216 
217  return true;
218  }
219 
220 }
221 }
222 #endif
Definition: rply.hpp:566
int ply_get_argument_user_data(p_ply_argument argument, void **pdata, long *idata)
Definition: rply.hpp:1410
void loadNormals(vector< Eigen::Vector3f > &norm)
Load normals (x,y,z) as a vertex attribute.
Definition: mesh.hpp:384
void loadColors(vector< Eigen::Vector4f > &clrs)
Load colors (r,g,b,a) as a vertex attribute.
Definition: mesh.hpp:438
Definition: bufferobject.hpp:34
static int face_cb(p_ply_argument argument)
Definition: plyimporter.hpp:126
static int color_cb(p_ply_argument argument)
Definition: plyimporter.hpp:70
int ply_read(p_ply ply)
Definition: rply.hpp:1067
void errorCheckFunc(std::string file, int line, std::string message="")
GL error check method.
Definition: misc.hpp:53
double ply_get_argument_value(p_ply_argument argument)
Definition: rply.hpp:1419
void loadIndices(vector< GLuint > &ind)
Load indices into indices array.
Definition: mesh.hpp:448
static int vertex_cb(p_ply_argument argument)
Definition: plyimporter.hpp:100
int ply_close(p_ply ply)
Definition: rply.hpp:1306
p_ply ply_open(const char *name, p_ply_error_cb error_cb, long idata, void *pdata)
Definition: rply.hpp:993
int ply_get_argument_property(p_ply_argument argument, p_ply_property *property, long *length, long *value_index)
Definition: rply.hpp:1400
void setDefaultAttribLocations(void)
Sets default attribute locations. vertex coords -> location 0 normals -> location 1 colors -> locatio...
Definition: mesh.hpp:474
static bool loadPlyFile(Mesh *mesh, string filename) __attribute__((unused))
Loads a mesh from an PLY file.
Definition: plyimporter.hpp:150
Definition: rply.hpp:471
int ply_read_header(p_ply ply)
Definition: rply.hpp:1022
long ply_set_read_cb(p_ply ply, const char *element_name, const char *property_name, p_ply_read_cb read_cb, void *pdata, long idata)
Definition: rply.hpp:1051
void loadVertices(vector< Eigen::Vector4f > &vert)
Load vertices (x,y,z,w) and creates appropriate vertex attribute. The default attribute name is "in_P...
Definition: mesh.hpp:312
void loadTexCoords(vector< Eigen::Vector2f > &tex, bool normalize=false)
Load tex coords (u,v) as a vertex attribute. Optionally normalizes coords in range [0...
Definition: mesh.hpp:397
A common Mesh, usually containing triagles or points.
Definition: mesh.hpp:194
static int normal_cb(p_ply_argument argument)
Definition: plyimporter.hpp:43