talpa/iomesh.cpp
// ---------------------------------------------------------
///
/// Read mesh in Wavefront OBJ format
///
// ---------------------------------------------------------
Read mesh in Wavefront OBJ format
Read mesh in Wavefront OBJ format
bool read_objfile(NonDestructiveTriMesh &mesh, std::vector<Vec3d> &x, const char *filename_format, ...)
{
#ifdef _MSC_VER
    va_list ap;
    va_start(ap, filename_format);
    int len=_vscprintf(filename_format, ap) 
        +1; 
    char *filename=new char[len];
    vsprintf(filename, filename_format, ap);
    std::ifstream input(filename, std::ifstream::binary);
    delete[] filename;
    va_end(ap);
#else
    va_list ap;
    va_start(ap, filename_format);
    char *filename;
    vasprintf(&filename, filename_format, ap);
    std::ifstream input(filename, std::ifstream::binary);
    std::free(filename);
    va_end(ap);
#endif
    if(!input.good()) return false;
    x.clear();
    mesh.clear();
    char line[LINESIZE];
    std::vector<int> vertex_list;
    while(input.good()){
        input.getline(line, LINESIZE);
        switch(line[0]){
            case 'v': 
                if(line[1]==' '){
                    Vec3d new_vertex;
                    std::sscanf(line+2, "%lf %lf %lf", &new_vertex[0], &new_vertex[1], &new_vertex[2]);
                    x.push_back(new_vertex);
                }
                break;
            case 'f': 
                if(line[1]==' '){
                    read_face_list(line+2, vertex_list);
                    for(int j=0; j<(int)vertex_list.size()-2; ++j)
                        mesh.add_triangle( Vec3st(vertex_list[0], vertex_list[j+1], vertex_list[j+2]) );
                }
                break;
        }
    }
    return true;
}
bool read_objfile ( NonDestructiveTriMesh & mesh , std :: vector < Vec3d > & x , const char * filename_format , ... ) { #ifdef _MSC_VER va_list ap ; va_start ( ap , filename_format ) ; int len = _vscprintf ( filename_format , ap ) + 1 ; char * filename = new char [ len ] ; vsprintf ( filename , filename_format , ap ) ; std :: ifstream input ( filename , std :: ifstream :: binary ) ; delete [ ] filename ; va_end ( ap ) ; #else va_list ap ; va_start ( ap , filename_format ) ; char * filename ; vasprintf ( & filename , filename_format , ap ) ; std :: ifstream input ( filename , std :: ifstream :: binary ) ; std :: free ( filename ) ; va_end ( ap ) ; #endif if ( ! input . good ( ) ) return false ; x . clear ( ) ; mesh . clear ( ) ; char line [ LINESIZE ] ; std :: vector < int > vertex_list ; while ( input . good ( ) ) { input . getline ( line , LINESIZE ) ; switch ( line [ 0 ] ) { case ' ' : if ( line [ 1 ] == ' ' ) { Vec3d new_vertex ; std :: sscanf ( line + 2 , " " , & new_vertex [ 0 ] , & new_vertex [ 1 ] , & new_vertex [ 2 ] ) ; x . push_back ( new_vertex ) ; } break ; case ' ' : if ( line [ 1 ] == ' ' ) { read_face_list ( line + 2 , vertex_list ) ; for ( int j = 0 ; j < ( int ) vertex_list . size ( ) - 2 ; ++ j ) mesh . add_triangle ( Vec3st ( vertex_list [ 0 ] , vertex_list [ j + 1 ] , vertex_list [ j + 2 ] ) ) ; } break ; } } return true ; }
Read mesh in Wavefront OBJ format
Read mesh in Wavefront OBJ format
// _vscprintf doesn't count // terminating '\0' // vertex data // face data