iCub-main
mesh.h
Go to the documentation of this file.
1 /*
2  * mesh.h
3  */
4 
5 /*
6  * Copyright (C) 2009 RobotCub Consortium
7  * Author: Alessandro Scalzo alessandro.scalzo@iit.it
8  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
9  *
10  * Based on:
11  *
12  * Qavimator
13  * Copyright (C) 2006 by Zi Ree *
14  * Zi Ree @ SecondLife *
15  * Released under the terms of the GNU GPL v2.0.
16  */
17 
18 #ifndef ICUBMESH_H
19 #define ICUBMESH_H
20 
21 #include <qstring.h>
22 #include <qmessagebox.h>
23 #include <qfile.h>
24 
25 #ifdef __APPLE__
26 #include <OpenGL/glu.h>
27 #else
28 #include <GL/glu.h>
29 #endif
30 
31 #include <math.h>
32 #include <stdlib.h>
33 
34 #include "visionobj.h"
35 
36 inline QStringList tokenize(QString in,char sep)
37 {
38  QStringList out;
39  while (in.length())
40  {
41  int pos=in.indexOf(sep);
42  if (pos==-1)
43  {
44  out.append(in);
45  return out;
46  }
47  if (pos) out.append(in.left(pos));
48  in.remove(0,pos+1);
49  }
50  return out;
51 }
52 
53 class iCubMesh
54 {
55 public:
56  iCubMesh(QString fileName,double rz=0.0,double ry=0.0,double rx=0.0,double tx=0.0,double ty=0.0,double tz=0.0)
57  {
58  nFaces=0;
59 
60  QFile objFile(fileName);
61  if(!objFile.open(QIODevice::ReadOnly))
62  {
63  QMessageBox::critical(0,QObject::tr("Missing Obj File"),
64  QObject::tr("<qt>File not found:<br>%1</qt>").arg(fileName));
65  return;
66  }
67 
68  int v=0,n=0;
69 
70  char buffer[4096];
71 
72  while(!objFile.atEnd())
73  {
74  objFile.readLine(buffer,4096);
75  QString line(buffer);
76  QStringList parameters=tokenize(line.simplified(),' ');
77 
78  if (parameters[0]=="v") ++v;
79  else if (parameters[0]=="vn") ++n;
80  else if (parameters[0]=="f") ++nFaces;
81  }
82  objFile.close();
83 
84  vx=new float[v]; vy=new float[v]; vz=new float[v];
85  nx=new float[n]; ny=new float[n]; nz=new float[n];
86 
87  av=new short[nFaces]; bv=new short[nFaces]; cv=new short[nFaces];
88  an=new short[nFaces]; bn=new short[nFaces]; cn=new short[nFaces];
89 
90  double grad2rad=M_PI/180.0;
91  rz*=grad2rad;
92  ry*=grad2rad;
93  rx*=grad2rad;
94  double cz=cos(rz),sz=sin(rz);
95  double cy=cos(ry),sy=sin(ry);
96  double cx=cos(rx),sx=sin(rx);
97 
98  nFaces=v=n=0;
99  QStringList num;
100  objFile.open(QIODevice::ReadOnly);
101 
102  double x1,y1,z1;
103  double x2,y2,z2;
104  double x3,y3,z3;
105 
106  double xmin=1E+20,xmax=-xmin;
107  double ymin=1E+20,ymax=-ymin;
108  double zmin=1E+20,zmax=-zmin;
109 
110  while(!objFile.atEnd())
111  {
112  objFile.readLine(buffer,4096);
113  QString line(buffer);
114  QStringList parameters=tokenize(line.simplified(),' ');
115 
116  if (parameters[0]=="v")
117  {
118  if (parameters[1].toDouble()<xmin) xmin=parameters[1].toDouble();
119  if (parameters[1].toDouble()>xmax) xmax=parameters[1].toDouble();
120  if (parameters[2].toDouble()<ymin) ymin=parameters[2].toDouble();
121  if (parameters[2].toDouble()>ymax) ymax=parameters[2].toDouble();
122  if (parameters[3].toDouble()<zmin) zmin=parameters[3].toDouble();
123  if (parameters[3].toDouble()>zmax) zmax=parameters[3].toDouble();
124 
125  x1=parameters[1].toDouble();
126  y1=parameters[2].toDouble();
127  z1=parameters[3].toDouble();
128 
129  x2=cz*x1-sz*y1;
130  y2=sz*x1+cz*y1;
131  z2=z1;
132 
133  x3= cy*x2+sy*z2;
134  y3= y2;
135  z3=-sy*x2+cy*z2;
136 
137  vx[v]=tx+x3;
138  vy[v]=ty+cx*y3-sx*z3;
139  vz[v]=tz+sx*y3+cx*z3;
140 
141  ++v;
142  }
143  else if (parameters[0]=="vn")
144  {
145  x1=(cz*parameters[1].toDouble()-sz*parameters[2].toDouble());
146  y1=(sz*parameters[1].toDouble()+cz*parameters[2].toDouble());
147  z1=parameters[3].toDouble();
148 
149  x2= cy*x1+sy*z1;
150  y2=y1;
151  z2=-sy*x1+cy*z1;
152 
153  nx[n]=x2;
154  ny[n]=cx*y2-sx*z2;
155  nz[n]=sx*y2+cx*z2;
156 
157  ++n;
158  }
159  else if (parameters[0]=="f")
160  {
161  num=tokenize(parameters[1].simplified(),'/');
162  av[nFaces]=num[0].toShort()-1;
163  an[nFaces]=num[1].toShort()-1;
164 
165  num=tokenize(parameters[2].simplified(),'/');
166  bv[nFaces]=num[0].toShort()-1;
167  bn[nFaces]=num[1].toShort()-1;
168 
169  num=tokenize(parameters[3].simplified(),'/');
170  cv[nFaces]=num[0].toShort()-1;
171  cn[nFaces]=num[1].toShort()-1;
172 
173  ++nFaces;
174  }
175  }
176  objFile.close();
177  }
178 
180  {
181  delete [] vx; delete [] vy; delete [] vz;
182  delete [] nx; delete [] ny; delete [] nz;
183  delete [] av; delete [] bv; delete [] cv;
184  delete [] an; delete [] bn; delete [] cn;
185  }
186 
187  void Draw()
188  {
189  short v,n;
190  glBegin(GL_TRIANGLES);
191  for(int f=0; f<nFaces; ++f)
192  {
193  v=av[f]; n=an[f];
194  glNormal3f(nx[n],ny[n],nz[n]);
195  glVertex3f(vx[v],vy[v],vz[v]);
196 
197  v=bv[f]; n=bn[f];
198  glNormal3f(nx[n],ny[n],nz[n]);
199  glVertex3f(vx[v],vy[v],vz[v]);
200 
201  v=cv[f]; n=cn[f];
202  glNormal3f(nx[n],ny[n],nz[n]);
203  glVertex3f(vx[v],vy[v],vz[v]);
204  }
205  glEnd();
206  }
207 
208 protected:
209  int nFaces;
210  float *vx,*vy,*vz;
211  float *nx,*ny,*nz;
212  short *av,*bv,*cv;
213  short *an,*bn,*cn;
214 };
215 
216 #endif
#define M_PI
Definition: XSensMTx.cpp:24
Definition: mesh.h:54
float * vz
Definition: mesh.h:210
iCubMesh(QString fileName, double rz=0.0, double ry=0.0, double rx=0.0, double tx=0.0, double ty=0.0, double tz=0.0)
Definition: mesh.h:56
float * nx
Definition: mesh.h:211
float * vy
Definition: mesh.h:210
float * vx
Definition: mesh.h:210
float * ny
Definition: mesh.h:211
short * cv
Definition: mesh.h:212
void Draw()
Definition: mesh.h:187
short * cn
Definition: mesh.h:213
short * bn
Definition: mesh.h:213
short * an
Definition: mesh.h:213
short * bv
Definition: mesh.h:212
short * av
Definition: mesh.h:212
float * nz
Definition: mesh.h:211
~iCubMesh()
Definition: mesh.h:179
int nFaces
Definition: mesh.h:209
int n
QStringList tokenize(QString in, char sep)
Definition: mesh.h:36
out
Definition: sine.m:8