File: export_obj.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 45,124 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 29
file content (415 lines) | stat: -rw-r--r-- 14,805 bytes parent folder | download | duplicates (2)
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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program 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 General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/



#ifndef __VCGLIB_EXPORT_OBJ
#define __VCGLIB_EXPORT_OBJ

#include <wrap/callback.h>
#include <wrap/io_trimesh/io_mask.h>
#include <wrap/io_trimesh/io_material.h>
#include <iostream>
#include <fstream>
#include <map>

namespace vcg {
namespace tri {
namespace io {

template <class SaveMeshType>
class ExporterOBJ
{
public:
  typedef typename SaveMeshType::FaceIterator FaceIterator;
  typedef typename SaveMeshType::EdgeIterator EdgeIterator;
  typedef typename SaveMeshType::VertexIterator VertexIterator;
  typedef typename SaveMeshType::VertexType VertexType;
  typedef typename SaveMeshType::ScalarType ScalarType;
  typedef typename SaveMeshType::CoordType CoordType;
  typedef typename SaveMeshType::FaceType::TexCoordType TexCoordType;

  /*
            enum of all the types of error
        */
  enum SaveError
  {
    E_NOERROR,            // 0
    E_CANTOPENFILE,       // 1
    E_CANTCLOSEFILE,      // 2
    E_UNESPECTEDEOF,      // 3
    E_ABORTED,            // 4
    E_NOTDEFINITION,      // 5
    E_NO_VERTICES,        // 6
    E_NOTFACESVALID,      // 7
    E_NO_VALID_MATERIAL,  // 8
	E_STREAMERROR         // 9
  };

  /*
            this function takes an index and the relative error message gets back
        */
  static const char* ErrorMsg(int error)
  {
    static const char* obj_error_msg[] =
    {
      "No errors",  // 0
      "Can't open file",  // 1
      "can't close file",  // 2
      "Premature End of file",  // 3
      "File saving aborted",  // 4
      "Function not defined",  // 5
      "Vertices not valid",  // 6
      "Faces not valid",  // 7
      "The mesh has not a attribute containing the vector of materials",  // 8
	  "Output Stream Error" //9
    };

    if(error>9 || error<0) return "Unknown error";
    else return obj_error_msg[error];
  };

  /*
            returns mask of capability one define with what are the saveable information of the format.
        */
  static int GetExportMaskCapability()
  {
    int capability = 0;

    //vert
    capability |= vcg::tri::io::Mask::IOM_VERTNORMAL;
    capability |= vcg::tri::io::Mask::IOM_VERTTEXCOORD;
    capability |= vcg::tri::io::Mask::IOM_VERTCOLOR;

    //face
    capability |= vcg::tri::io::Mask::IOM_FACECOLOR;

    capability |= vcg::tri::io::Mask::IOM_EDGEINDEX;
    //wedg
    capability |= vcg::tri::io::Mask::IOM_WEDGTEXCOORD;
    capability |= vcg::tri::io::Mask::IOM_WEDGNORMAL;

    return capability;
  }
  
  static int Save(SaveMeshType &m, const char * filename, int mask, CallBackPos *cb=0)
  {
    return Save(m,filename,mask,false,cb);
  }
  
  /** 
   * main function to export a mesh in OBJ file format
   * 
   * if you enable the useMaterialAttribute flag, the exporter will assume that the mesh has a consistent per mesh attribute and a per face attribute containing the index of the material
   */
  static int Save(SaveMeshType &m, const char * filename, int mask, bool useMaterialAttribute ,CallBackPos *cb=0)
  {
    // texture coord and color: in obj we cannot save BOTH per vertex and per wedge information. We default on wedge
    if (mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD &&
        mask & vcg::tri::io::Mask::IOM_VERTTEXCOORD ) {
      mask &= ~vcg::tri::io::Mask::IOM_VERTTEXCOORD;
    }
    if (mask & vcg::tri::io::Mask::IOM_WEDGCOLOR &&
        mask & vcg::tri::io::Mask::IOM_VERTCOLOR ) {
      mask &= ~vcg::tri::io::Mask::IOM_VERTCOLOR;
    }
    if(m.vn == 0)	return E_NO_VERTICES;
    
    typename SaveMeshType::template PerMeshAttributeHandle<std::vector<Material> > materialVecHandle =
        vcg::tri::Allocator<SaveMeshType>::template FindPerMeshAttribute<std::vector<Material> >(m, "materialVector");
    typename SaveMeshType::template PerFaceAttributeHandle<int> materialIndexHandle =
            vcg::tri::Allocator<SaveMeshType>::template FindPerFaceAttribute<int>(m, "materialIndex");
    
    if(useMaterialAttribute && (!Allocator<SaveMeshType>::IsValidHandle(m,materialVecHandle)) && 
       (!Allocator<SaveMeshType>::IsValidHandle(m,materialIndexHandle)) ) 
        return E_NO_VALID_MATERIAL;

    FILE *fp = fopen(filename,"w");
    if(fp == NULL) return E_CANTOPENFILE;
    std::string shortFilename(filename);
    int LastSlash=shortFilename.size()-1;
    while(LastSlash>=0 && shortFilename[LastSlash]!='/')
      --LastSlash;
    shortFilename = shortFilename.substr(LastSlash+1);
    
    fprintf(fp,"####\n#\n# OBJ File Generated by Meshlab\n#\n####\n");
    fprintf(fp,"# Object %s\n#\n# Vertices: %d\n# Faces: %d\n#\n####\n",shortFilename.c_str(),m.vn,m.fn);
    
    //library materialVec
    if( (mask & vcg::tri::io::Mask::IOM_FACECOLOR)  || (mask & Mask::IOM_WEDGTEXCOORD) || (mask & Mask::IOM_VERTTEXCOORD) )
      fprintf(fp,"mtllib ./%s.mtl\n\n",shortFilename.c_str());

    std::map<CoordType,int> NormalVertex;
    std::vector<int> VertexId(m.vert.size());
    int numvert = 0;
    int curNormalIndex = 1;
    int current = 0;
    const int totalPrimitives = m.vn+m.fn;    
    /*********************************** VERTICES *********************************/
    for(auto vi=m.vert.begin(); vi!=m.vert.end(); ++vi) if( !(*vi).IsD() )
    {
      VertexId[vi-m.vert.begin()]=numvert;
      //saves normal per vertex
      if (mask & Mask::IOM_WEDGNORMAL )
      {
        if(AddNewNormalVertex(NormalVertex,(*vi).N(),curNormalIndex))
        {
          fprintf(fp,"vn %f %f %f\n",(*vi).N()[0],(*vi).N()[1],(*vi).N()[2]);
          curNormalIndex++;
        }
      }
      if (mask & Mask::IOM_VERTNORMAL ) {
        fprintf(fp,"vn %f %f %f\n",(*vi).N()[0],(*vi).N()[1],(*vi).N()[2]);
      }
      
      if (mask & Mask::IOM_VERTTEXCOORD ) {
        fprintf(fp,"vt %f %f\n",(*vi).T().P()[0],(*vi).T().P()[1]);
      }

      fprintf(fp,"v %f %f %f",(*vi).P()[0],(*vi).P()[1],(*vi).P()[2]);
      
      if(mask & Mask::IOM_VERTCOLOR) // the socially accepted extension to the obj format. 
        fprintf(fp," %f %f %f",double((*vi).C()[0])/255.,double((*vi).C()[1])/255.,double((*vi).C()[2])/255.);
      fprintf(fp,"\n");

      if (cb !=NULL)
      {
        if(!(*cb)((100*++current)/totalPrimitives, "writing vertices "))
        {
          fclose(fp);
          return E_ABORTED;
        }
      }
      numvert++;
    }
    assert(numvert == m.vn);
    fprintf(fp,"# %d vertices, %d vertices normals\n\n",m.vn,int(NormalVertex.size()));

    /********************* FACES ************************/      
    //faces + texture coords
    std::map<TexCoordType,int> CoordIndexTexture;
    int curTexCoordIndex = 1;
    int curMatIndex = -1;
    std::vector<Material> materialVec; //used if we do not have material attributes 
    
    for(FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
    {
      if((mask & Mask::IOM_FACECOLOR) || (mask & Mask::IOM_WEDGTEXCOORD) || (mask & Mask::IOM_VERTTEXCOORD))
      {
        int index=-1;
        if(useMaterialAttribute) index = materialIndexHandle[fi];
        else                     index = Materials<SaveMeshType>::CreateNewMaterial(m,materialVec,fi);
                  
        if(index != curMatIndex) {
          fprintf(fp,"\nusemtl material_%d\n", index);
          curMatIndex = index;
        }
      }

      //saves texture coord x wedge
      if(HasPerWedgeTexCoord(m) && (mask & Mask::IOM_WEDGTEXCOORD))
        for(int k=0;k<(*fi).VN();k++)
        {
            if(AddNewTextureCoord(CoordIndexTexture,(*fi).WT(k),curTexCoordIndex))
            {
              fprintf(fp,"vt %f %f\n",(*fi).WT(k).u(),(*fi).WT(k).v());
              curTexCoordIndex++; //ncreases the value number to be associated to the Texture
            }
        }

      fprintf(fp,"f ");
      for(int k=0;k<(*fi).VN();k++)
      {
        if(k!=0) fprintf(fp," ");
        int vInd = -1;
        // +1 because Obj file format begins from index = 1 but not from index = 0.
        vInd = VertexId[tri::Index(m, (*fi).V(k))] + 1;//index of vertex per face

        int vt = -1;
        if(mask & Mask::IOM_WEDGTEXCOORD)
          vt = GetIndexVertexTexture(CoordIndexTexture,(*fi).WT(k));//index of vertex texture per face
        if (mask & Mask::IOM_VERTTEXCOORD)
          vt = vInd;

        int vn = -1;
        if(mask & Mask::IOM_WEDGNORMAL )
          vn = GetIndexVertexNormal(m, NormalVertex, (*fi).V(k)->cN());//index of vertex normal per face.
        if (mask & Mask::IOM_VERTNORMAL)
          vn = vInd;

        //writes elements on file obj
        WriteFacesElement(fp,vInd,vt,vn);
      }
      fprintf(fp,"\n");

      if (cb !=NULL) {
        if(!(*cb)((100*++current)/totalPrimitives, "writing vertices "))
        { fclose(fp); return E_ABORTED;}
      }

    } // end for faces

    for(EdgeIterator ei=m.edge.begin(); ei!=m.edge.end(); ++ei) if( !(*ei).IsD() )
    {
      fprintf(fp,"l %i %i\n",
              VertexId[tri::Index(m, (*ei).V(0))] + 1,
              VertexId[tri::Index(m, (*ei).V(1))] + 1);
    }

    fprintf(fp,"# %d faces, %d coords texture\n\n",m.fn,int(CoordIndexTexture.size()));

    fprintf(fp,"# End of File\n");

    int errCode = E_NOERROR;
    if((mask & Mask::IOM_WEDGTEXCOORD) || (mask & Mask::IOM_FACECOLOR) || (mask & Mask::IOM_VERTTEXCOORD) )
    {
      if(useMaterialAttribute) errCode = WriteMaterials(materialVecHandle(), filename,cb);
      else                     errCode = WriteMaterials(materialVec, filename,cb);
    }

	int result = E_NOERROR;
	if (errCode != E_NOERROR)
		result = errCode;
	else if (ferror(fp))
		result = E_STREAMERROR;
	fclose(fp);
	return result;
  }

   /*
            returns index of the texture coord
        */
  inline static int GetIndexVertexTexture(typename std::map<TexCoordType,int> &mapTexToInt, const TexCoordType &wt)
  {
    typename std::map<TexCoordType,int>::iterator iter= mapTexToInt.find(wt);
    if(iter != mapTexToInt.end()) return (*iter).second;
    else 		return -1;
    // Old wrong version.
    // int index = mapTexToInt[wt];
    // if(index!=0){return index;}
  }

  /*
            returns index of the vertex normal
        */
  inline static int GetIndexVertexNormal(SaveMeshType &/*m*/, std::map<CoordType,int> &mapNormToInt, const CoordType &norm )
  {
    typename std::map<CoordType,int>::iterator iter= mapNormToInt.find(norm);
    if(iter != mapNormToInt.end()) return (*iter).second;
    else 		return -1;
  }

  /*
            write elements on file

                f v/tc/vn v/tc/vn v/tc/vn ...
                f v/tc v/tc v/tc ...
                f v//vn v//vn v//vn ...
                f v v v ...

        */
  inline static void WriteFacesElement(FILE *fp,int v,int vt, int vn)
  {
    fprintf(fp,"%d",v);
    if(vt!=-1)
    {
      fprintf(fp,"/%d",vt);
      if(vn!=-1)
        fprintf(fp,"/%d",vn);
    }
    else if(vn!=-1)
      fprintf(fp,"//%d",vn);
  }

  /*
            adds a new index to the coordinate of Texture if it is the first time
            which is otherwise met not execute anything
        */
  template <class TexScalarType>
  inline static bool AddNewTextureCoord(std::map<typename vcg::TexCoord2<TexScalarType>,int> &m,
                                        const typename vcg::TexCoord2<TexScalarType> &wt,int value)
  {
    int index = m[wt];
    if(index==0){m[wt]=value;return true;}
    return false;
  }

  /*
            adds a new index to the normal per vertex if it is the first time
            which is otherwise met does not execute anything
        */
  inline static bool AddNewNormalVertex(typename std::map<CoordType,int> &m, CoordType &n ,int value)
  {
    int index = m[n];
    if(index==0){m[n]=value;return true;}
    return false;
  }

  /*
            writes material into file
        */
  inline static int WriteMaterials(std::vector<Material> &materialVec, const char * filename, CallBackPos *cb=0)
  {
    std::string fileName = std::string(filename);
    fileName+=".mtl";

    if(materialVec.size() > 0)
    {
      FILE *fp;
      fp = fopen(fileName.c_str(),"w");
      if(fp==NULL)return E_ABORTED;

      fprintf(fp,"#\n# Wavefront material file\n# Converted by Meshlab Group\n#\n\n");

      int current = 0;

      for(unsigned int i=0;i<materialVec.size();i++)
      {
        if (cb !=NULL)
          (*cb)((100 * ++current)/materialVec.size(), "saving material file ");
        else
        { /* fclose(fp); return E_ABORTED; */ }

        fprintf(fp,"newmtl material_%d\n",i);
        fprintf(fp,"Ka %f %f %f\n",materialVec[i].Ka[0],materialVec[i].Ka[1],materialVec[i].Ka[2]);
        fprintf(fp,"Kd %f %f %f\n",materialVec[i].Kd[0],materialVec[i].Kd[1],materialVec[i].Kd[2]);
        fprintf(fp,"Ks %f %f %f\n",materialVec[i].Ks[0],materialVec[i].Ks[1],materialVec[i].Ks[2]);
        fprintf(fp,"Tr %f\n",materialVec[i].Tr);
        fprintf(fp,"illum %d\n",materialVec[i].illum);
        fprintf(fp,"Ns %f\n",materialVec[i].Ns);

        if(materialVec[i].map_Kd.size()>0)
          fprintf(fp,"map_Kd %s\n",materialVec[i].map_Kd.c_str());
        fprintf(fp,"\n");
      }
      fclose(fp);
    }
    return E_NOERROR;
  }

}; // end class
} // end Namespace tri
} // end Namespace io
} // end Namespace vcg

#endif