iCub-main
xloader.cpp
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4 * Copyright (C) 2010 RobotCub Consortium, European Commission FP6 Project IST-004370
5 * Author: Vadim Tikhanoff
6 * This code was written starting from tutorial code by Kosei Demura
7 * email: vadim.tikhanoff@iit.it
8 * website: www.robotcub.org
9 * Permission is granted to copy, distribute, and/or modify this program
10 * under the terms of the GNU General Public License, version 2 or any
11 * later version published by the Free Software Foundation.
12 *
13 * A copy of the license can be found at
14 * http://www.robotcub.org/icub/license/gpl.txt
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details
20 */
21 
29 #include "xloader.h"
30 #include <yarp/os/LogStream.h>
31 
32 #if _MSC_VER
33 #pragma warning(disable:4996 4244 4305)
34 #endif
35 
36 const float ModelScale = 1.01;
37 //---------------------------------------------------------------
38 //---------------------------------------------------------------
39 dxTriMeshX *dLoadMeshFromX(const char* FileName)
40 {
41  dxTriMeshX *tmpTriMesh = new dxTriMeshX;
42  char buff[256];
43  char* word = buff;
44  char* symbol;
45  int indexCount;
46  char *p;
47  int i, j=0;
48  int ret;
49  FILE *in;
50  double dblval;
51  int intval;
52 
53  if ((in = fopen(FileName, "r")) == NULL) {
54  yError ("Can't open the file '%s'\n", FileName);
55  return 0;
56  }
57  else {
58  yDebug("Loading mesh data from '%s' ", FileName);
59 
60  while((symbol = fgets(buff, 256, in)) != (char*) NULL) { // Read till the end of file
61  word = buff;
62  while (word[0]==' ' || word[0]=='\t')
63  word=word+1;
64  if (strcmp(word, "template") == 0) {
65  ret=fscanf(in, "%s", word);
66  ret=fscanf(in, "%s", word);
67  }else
68  if (strncmp(word, "Mesh ", 5) == 0){ // All you need is Mesh !!
69  if (strcmp(word, "{") != 0 ) // If the mesh has a name
70  //fscanf(in, "%s", word); // then skip it.
71  ret=fscanf(in, "%d", &(tmpTriMesh->VertexCount)); // Get vertex count
72  tmpTriMesh->Vertices = (float *)malloc(tmpTriMesh->VertexCount * 3 * sizeof(float));
73  ret=fscanf(in, "%s", word);
74  yDebug("...");
75  if (fgets(word, 256, in)==0)
76  return 0;
77 
78  for (i = 0; i < tmpTriMesh->VertexCount; i++) {
79  //fscanf(in, "%s", word);
80  if (fgets(word, 256, in)==0)
81  return 0;
82 
83  //yDebug("Read(%d): '%s'\n", i, word);
84  p = strtok(word, ",;");
85  while (p != NULL) {
86  //yDebug("p = '%s'\n", p);
87  ret = sscanf(p, "%lf", &dblval);
88  if(ret > 0) { // only process if double was read
89  tmpTriMesh->Vertices[j] = dblval * ModelScale;
90  j++;
91  }
92  p = strtok(NULL, ",;");
93  //yDebug("j = %d\n", j);
94  }
95  }
96  yDebug("...");
97  ret=fscanf(in, "%d", &indexCount); // Get index count
98  tmpTriMesh->IndexCount = indexCount * 3;
99  tmpTriMesh->Indices = (int *)malloc(tmpTriMesh->IndexCount * sizeof(int));
100 
101  if (fgets(word, 256, in)==0)
102  return 0;
103 
104  for (i = 0; i < indexCount; i++) {
105  if (fgets(word, 256, in)==0)
106  return 0;
107 
108  p = strtok(word, ",;");
109  ret = sscanf(p, "%d", &intval);
110  if(intval != 3) {
111  yError("Only triangular polygons supported! Convert your model!\n");
112  return 0;
113  }
114  for(j = 0; j < 3; j++) {//hardcoded 3
115  p = strtok(NULL, ",;");
116  ret = sscanf(p, "%d", &intval);
117  tmpTriMesh->Indices[i*3 + j] = intval;
118  }
119  }
120  yDebug("... OK!\n");
121  }
122  }
123  }
124  fclose(in);
125 
126  if ((in = fopen(FileName, "r")) == NULL) {
127  yError ("Can't open the file '%s'\n", FileName);
128  return 0;
129  }
130  else {
131  yDebug("Loading texture coordinate from '%s' ", FileName);
132  j=0;
133  while((symbol = fgets(buff, 256, in)) != (char*) NULL) {
134  word = buff;
135  while (word[0]==' ' || word[0]=='\t')
136  word=word+1;
137  if (strncmp(word, "MeshTextureCoords", 17) == 0){
138  ret=fscanf(in, "%d", &(tmpTriMesh->MeshCoordCount));
139  if (fgets(word, 256, in)==NULL)
140  return 0; // consume newline
141 
142  tmpTriMesh->MeshCoord = (float *)malloc(tmpTriMesh->MeshCoordCount*2 * sizeof(float));
143  yDebug("...");
144  for (i = 0; i < tmpTriMesh->MeshCoordCount; i++) {
145  if (fgets(word, 256, in)==0)
146  return 0;
147 
148  p = strtok(word, ",;");
149  while (p != NULL) {
150  ret = sscanf(p, "%lf", &dblval);
151  if(ret > 0) {
152  tmpTriMesh->MeshCoord[j] = dblval ;//* ModelScale;
153  j++;
154  }
155  p = strtok(NULL, ",;");
156  }
157  }
158  yDebug("...");
159  }
160  }yDebug("... OK!\n");
161  }
162 
163  fclose(in);
164  //now load normals
165  if ((in = fopen(FileName, "r")) == NULL) {
166  yError ("Can't open the file '%s'\n", FileName);
167  return 0;
168  }
169  else {
170  yDebug("Loading normals from '%s' ", FileName);
171  j=0;
172  while((symbol = fgets(buff, 256, in)) != (char*) NULL) {
173  word = buff;
174  while (word[0]==' ' || word[0]=='\t')
175  word=word+1;
176  if (strncmp(word, "MeshNormals", 11) == 0){
177  ret=fscanf(in, "%d", &(tmpTriMesh->NormCount));
178  if (fgets(word, 256, in)==NULL)
179  return 0; // consume newline
180 
181  tmpTriMesh->NormCoord = (float *)malloc(tmpTriMesh->NormCount*3 * sizeof(float));
182  yDebug("...");
183  for (i = 0; i < tmpTriMesh->NormCount; i++) {
184  if (fgets(word, 256, in)==0)
185  return 0;
186 
187  p = strtok(word, ",;");
188  while (p != NULL) {
189  ret = sscanf(p, "%lf", &dblval);
190  if(ret > 0) {
191  tmpTriMesh->NormCoord[j] = dblval ;//* ModelScale;
192  j++;
193  }
194  p = strtok(NULL, ",;");
195  }
196  }
197  yDebug("...");
198  }
199  }yDebug("... OK!\n");
200  }
201 
202  fclose(in);
203  return tmpTriMesh;
204 }
206 {
207  free (TriMesh->Vertices);
208  free (TriMesh->Indices);
209  free (TriMesh->MeshCoord);
210  free (TriMesh->NormCoord);
211  free (TriMesh);
212 }
dxTriMeshX::Vertices
float * Vertices
Definition: xloader.h:45
ms3d::word
unsigned short word
Definition: MS3D.h:75
dLoadMeshFromX
dxTriMeshX * dLoadMeshFromX(const char *FileName)
Definition: xloader.cpp:39
dxTriMeshX::Indices
int * Indices
Definition: xloader.h:47
dxTriMeshX::IndexCount
int IndexCount
Definition: xloader.h:48
xloader.h
Header for the 3D x loader.
ModelScale
const float ModelScale
Definition: xloader.cpp:36
p
p
Definition: show_eyes_axes.m:23
dxTriMeshX::MeshCoord
float * MeshCoord
Definition: xloader.h:49
dTriMeshXDestroy
void dTriMeshXDestroy(dTriMeshX TriMesh)
Definition: xloader.cpp:205
dxTriMeshX::NormCoord
float * NormCoord
Definition: xloader.h:51
dxTriMeshX::MeshCoordCount
int MeshCoordCount
Definition: xloader.h:50
dxTriMeshX
Definition: xloader.h:44
dxTriMeshX::VertexCount
int VertexCount
Definition: xloader.h:46
dxTriMeshX::NormCount
int NormCount
Definition: xloader.h:52
fclose
fclose(fileID)