| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
#ifndef __VCGLIB_EXPORT_OBJ |
| 103 |
#define __VCGLIB_EXPORT_OBJ |
| 104 |
|
| 105 |
#include <wrap/callback.h> |
| 106 |
#include <vcg/complex/trimesh/allocate.h> |
| 107 |
#include <vcg/space/texcoord2.h> |
| 108 |
#include <wrap/io_trimesh/io_mask.h> |
| 109 |
#include "io_material.h" |
| 110 |
#include <iostream> |
| 111 |
#include <fstream> |
| 112 |
#include <map> |
| 113 |
|
| 114 |
namespace vcg { |
| 115 |
namespace tri { |
| 116 |
namespace io { |
| 117 |
|
| 118 |
template <class SaveMeshType> |
| 119 |
class ExporterOBJ |
| 120 |
{ |
| 121 |
public: |
| 122 |
typedef typename SaveMeshType::FaceIterator FaceIterator; |
| 123 |
typedef typename SaveMeshType::VertexIterator VertexIterator; |
| 124 |
typedef typename SaveMeshType::VertexType VertexType; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
enum SaveError |
| 130 |
{ |
| 131 |
E_NOERROR, |
| 132 |
E_CANTOPENFILE, |
| 133 |
E_CANTCLOSEFILE, |
| 134 |
E_UNESPECTEDEOF, |
| 135 |
E_ABORTED, |
| 136 |
E_NOTDEFINITION, |
| 137 |
E_NOTVEXTEXVALID, |
| 138 |
E_NOTFACESVALID |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
static const char* ErrorMsg(int error) |
| 145 |
{ |
| 146 |
static const char* obj_error_msg[] = |
| 147 |
{ |
| 148 |
"No errors", |
| 149 |
"Can't open file", |
| 150 |
"can't close file", |
| 151 |
"Premature End of file", |
| 152 |
"File saving aborted", |
| 153 |
"Function not defined", |
| 154 |
"Vertices not valid", |
| 155 |
"Faces not valid" |
| 156 |
}; |
| 157 |
|
| 158 |
if(error>7 || error<0) return "Unknown error"; |
| 159 |
else return obj_error_msg[error]; |
| 160 |
}; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
static int GetExportMaskCapability() |
| 166 |
{ |
| 167 |
int capability = 0; |
| 168 |
|
| 169 |
|
| 170 |
capability |= vcg::tri::io::Mask::IOM_VERTNORMAL; |
| 171 |
|
| 172 |
|
| 173 |
capability |= vcg::tri::io::Mask::IOM_FACECOLOR; |
| 174 |
|
| 175 |
|
| 176 |
capability |= vcg::tri::io::Mask::IOM_WEDGTEXCOORD; |
| 177 |
capability |= vcg::tri::io::Mask::IOM_WEDGNORMAL; |
| 178 |
|
| 179 |
return capability; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
static int Save(SaveMeshType &m, const char * filename, int mask, CallBackPos *cb=0) |
| 186 |
{ |
| 187 |
if(m.vn == 0) return E_NOTVEXTEXVALID; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
int current = 0; |
| 192 |
int totalPrimitives = m.vn+m.fn; |
| 193 |
|
| 194 |
std::vector<Material> materialVec; |
| 195 |
|
| 196 |
std::string fn(filename); |
| 197 |
int LastSlash=fn.size()-1; |
| 198 |
while(LastSlash>=0 && fn[LastSlash]!='/') |
| 199 |
--LastSlash; |
| 200 |
|
| 201 |
FILE *fp; |
| 202 |
fp = fopen(filename,"w"); |
| 203 |
if(fp == NULL) return E_CANTOPENFILE; |
| 204 |
|
| 205 |
fprintf(fp,"####\n#\n# OBJ File Generated by Meshlab\n#\n####\n"); |
| 206 |
fprintf(fp,"# Object %s\n#\n# Vertices: %d\n# Faces: %d\n#\n####\n",fn.substr(LastSlash+1).c_str(),m.vn,m.fn); |
| 207 |
|
| 208 |
|
| 209 |
if( (mask & vcg::tri::io::Mask::IOM_FACECOLOR) || (mask & Mask::IOM_WEDGTEXCOORD) ) |
| 210 |
fprintf(fp,"mtllib ./%s.mtl\n\n",fn.substr(LastSlash+1).c_str()); |
| 211 |
|
| 212 |
|
| 213 |
VertexIterator vi; |
| 214 |
std::map<Point3f,int> NormalVertex; |
| 215 |
std::vector<int> VertexId(m.vert.size()); |
| 216 |
int numvert = 0; |
| 217 |
int curNormalIndex = 1; |
| 218 |
for(vi=m.vert.begin(); vi!=m.vert.end(); ++vi) if( !(*vi).IsD() ) |
| 219 |
{ |
| 220 |
VertexId[vi-m.vert.begin()]=numvert; |
| 221 |
|
| 222 |
if((mask & Mask::IOM_VERTNORMAL) || (mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD) ) |
| 223 |
{ |
| 224 |
if(AddNewNormalVertex(NormalVertex,(*vi).N(),curNormalIndex)) |
| 225 |
{ |
| 226 |
fprintf(fp,"vn %f %f %f\n",(*vi).N()[0],(*vi).N()[1],(*vi).N()[2]); |
| 227 |
curNormalIndex++; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
fprintf(fp,"v %f %f %f\n",(*vi).P()[0],(*vi).P()[1],(*vi).P()[2]); |
| 233 |
|
| 234 |
if (cb !=NULL) { |
| 235 |
if(!(*cb)((100*++current)/totalPrimitives, "writing vertices ")) |
| 236 |
{ fclose(fp); return E_ABORTED;} } |
| 237 |
numvert++; |
| 238 |
} |
| 239 |
assert(numvert == m.vn); |
| 240 |
|
| 241 |
fprintf(fp,"# %d vertices, %d vertices normals\n\n",m.vn,NormalVertex.size()); |
| 242 |
|
| 243 |
|
| 244 |
FaceIterator fi; |
| 245 |
std::map<vcg::TexCoord2<float>,int> CoordIndexTexture; |
| 246 |
unsigned int material_num = 0; |
| 247 |
int mem_index = 0; |
| 248 |
int curTexCoordIndex = 1; |
| 249 |
for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) |
| 250 |
{ |
| 251 |
if((mask & Mask::IOM_FACECOLOR) || (mask & Mask::IOM_WEDGTEXCOORD) ) |
| 252 |
{ |
| 253 |
int index = Materials<SaveMeshType>::CreateNewMaterial(m,materialVec,material_num,fi); |
| 254 |
|
| 255 |
if(index == materialVec.size()) |
| 256 |
{ |
| 257 |
material_num++; |
| 258 |
fprintf(fp,"\nusemtl material_%d\n",materialVec[index-1].index); |
| 259 |
mem_index = index-1; |
| 260 |
} |
| 261 |
else |
| 262 |
{ |
| 263 |
if(index != mem_index) |
| 264 |
{ |
| 265 |
fprintf(fp,"\nusemtl material_%d\n",materialVec[index].index); |
| 266 |
mem_index=index; |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
for(unsigned int k=0;k<3;k++) |
| 273 |
{ |
| 274 |
if(HasPerWedgeTexCoord(m) && (mask & Mask::IOM_WEDGTEXCOORD)) |
| 275 |
{ |
| 276 |
if(AddNewTextureCoord(CoordIndexTexture,(*fi).WT(k),curTexCoordIndex)) |
| 277 |
{ |
| 278 |
fprintf(fp,"vt %f %f\n",(*fi).WT(k).u(),(*fi).WT(k).v()); |
| 279 |
curTexCoordIndex++; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
fprintf(fp,"f "); |
| 285 |
for(unsigned int k=0;k<3;k++) |
| 286 |
{ |
| 287 |
if(k!=0) fprintf(fp," "); |
| 288 |
int vInd = -1; |
| 289 |
|
| 290 |
vInd = VertexId[GetIndexVertex(m, (*fi).V(k))] + 1; |
| 291 |
|
| 292 |
int vt = -1; |
| 293 |
if(mask & Mask::IOM_WEDGTEXCOORD) |
| 294 |
vt = GetIndexVertexTexture(CoordIndexTexture,(*fi).WT(k)); |
| 295 |
|
| 296 |
int vn = -1; |
| 297 |
if((mask & Mask::IOM_VERTNORMAL) || (mask & Mask::IOM_WEDGNORMAL) ) |
| 298 |
vn = GetIndexVertexNormal(m, NormalVertex, (*fi).V(k)->cN()); |
| 299 |
|
| 300 |
|
| 301 |
WriteFacesElement(fp,vInd,vt,vn); |
| 302 |
} |
| 303 |
fprintf(fp,"\n"); |
| 304 |
if (cb !=NULL) { |
| 305 |
if(!(*cb)((100*++current)/totalPrimitives, "writing vertices ")) |
| 306 |
{ fclose(fp); return E_ABORTED;} |
| 307 |
} |
| 308 |
|
| 309 |
} |
| 310 |
fprintf(fp,"# %d faces, %d coords texture\n\n",m.face.size(),CoordIndexTexture.size()); |
| 311 |
|
| 312 |
fprintf(fp,"# End of File"); |
| 313 |
fclose(fp); |
| 314 |
|
| 315 |
int r = 0; |
| 316 |
if((mask & Mask::IOM_WEDGTEXCOORD) || (mask & Mask::IOM_FACECOLOR) ) |
| 317 |
r = WriteMaterials(materialVec, filename,cb); |
| 318 |
|
| 319 |
if(r!= E_NOERROR) |
| 320 |
return r; |
| 321 |
return E_NOERROR; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
inline static int GetIndexVertex(SaveMeshType &m, VertexType *p) |
| 328 |
{ |
| 329 |
return p-&*(m.vert.begin()); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
inline static int GetIndexVertexTexture(std::map<TexCoord2<float>,int> &mapTexToInt, const vcg::TexCoord2<float> &wt) |
| 336 |
{ |
| 337 |
std::map<vcg::TexCoord2<float>,int>::iterator iter= mapTexToInt.find(wt); |
| 338 |
if(iter != mapTexToInt.end()) return (*iter).second; |
| 339 |
else return -1; |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
inline static int GetIndexVertexNormal(SaveMeshType &m, std::map<Point3f,int> &mapNormToInt, const Point3f &norm ) |
| 349 |
{ |
| 350 |
std::map<Point3f,int>::iterator iter= mapNormToInt.find(norm); |
| 351 |
if(iter != mapNormToInt.end()) return (*iter).second; |
| 352 |
else return -1; |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
inline static void WriteFacesElement(FILE *fp,int v,int vt, int vn) |
| 368 |
{ |
| 369 |
fprintf(fp,"%d",v); |
| 370 |
if(vt!=-1) |
| 371 |
{ |
| 372 |
fprintf(fp,"/%d",vt); |
| 373 |
if(vn!=-1) |
| 374 |
fprintf(fp,"/%d",vn); |
| 375 |
} |
| 376 |
else if(vn!=-1) |
| 377 |
fprintf(fp,"//%d",vn); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
inline static bool AddNewTextureCoord(std::map<vcg::TexCoord2<float>,int> &m, const vcg::TexCoord2<float> &wt,int value) |
| 385 |
{ |
| 386 |
int index = m[wt]; |
| 387 |
if(index==0){m[wt]=value;return true;} |
| 388 |
return false; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
inline static bool AddNewNormalVertex(std::map<Point3f,int> &m, Point3f &n ,int value) |
| 396 |
{ |
| 397 |
int index = m[n]; |
| 398 |
if(index==0){m[n]=value;return true;} |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
inline static int WriteMaterials(std::vector<Material> &materialVec, const char * filename, CallBackPos *cb=0) |
| 406 |
{ |
| 407 |
std::string fileName = std::string(filename); |
| 408 |
fileName+=".mtl"; |
| 409 |
|
| 410 |
if(materialVec.size() > 0) |
| 411 |
{ |
| 412 |
FILE *fp; |
| 413 |
fp = fopen(fileName.c_str(),"w"); |
| 414 |
if(fp==NULL)return E_ABORTED; |
| 415 |
|
| 416 |
fprintf(fp,"#\n# Wavefront material file\n# Converted by Meshlab Group\n#\n\n"); |
| 417 |
|
| 418 |
int current = 0; |
| 419 |
|
| 420 |
for(unsigned int i=0;i<materialVec.size();i++) |
| 421 |
{ |
| 422 |
if (cb !=NULL) |
| 423 |
(*cb)((100 * ++current)/materialVec.size(), "saving material file "); |
| 424 |
else |
| 425 |
{ } |
| 426 |
|
| 427 |
fprintf(fp,"newmtl material_%d\n",materialVec[i].index); |
| 428 |
fprintf(fp,"Ka %f %f %f\n",materialVec[i].Ka[0],materialVec[i].Ka[1],materialVec[i].Ka[2]); |
| 429 |
fprintf(fp,"Kd %f %f %f\n",materialVec[i].Kd[0],materialVec[i].Kd[1],materialVec[i].Kd[2]); |
| 430 |
fprintf(fp,"Ks %f %f %f\n",materialVec[i].Ks[0],materialVec[i].Ks[1],materialVec[i].Ks[2]); |
| 431 |
fprintf(fp,"Tr %f\n",materialVec[i].Tr); |
| 432 |
fprintf(fp,"illum %d\n",materialVec[i].illum); |
| 433 |
fprintf(fp,"Ns %f\n",materialVec[i].Ns); |
| 434 |
|
| 435 |
if(materialVec[i].map_Kd.size()>0) |
| 436 |
fprintf(fp,"map_Kd %s\n",materialVec[i].map_Kd.c_str()); |
| 437 |
fprintf(fp,"\n"); |
| 438 |
} |
| 439 |
fclose(fp); |
| 440 |
} |
| 441 |
return E_NOERROR; |
| 442 |
} |
| 443 |
|
| 444 |
}; |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
#endif |