Ignore:
Timestamp:
10/20/06 13:21:15 (18 years ago)
Author:
mattausch
Message:

implemented obj dump for fast loading

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp

    r1655 r1658  
    226226bool Preprocessor::LoadBinaryObj(const string filename, 
    227227                                                                 SceneGraphNode *root, 
    228                                                                  const bool loadMeshes, 
    229228                                                                 vector<FaceParentInfo> *parents) 
    230229{ 
    231         ifstream samplesIn(fileName, ios::binary); 
     230        //ifstream samplesIn(filename, ios::binary); 
     231        igzstream samplesIn(filename.c_str()); 
     232         
    232233        if (!samplesIn.is_open()) 
    233234                return false; 
    234235 
    235  
     236        cout << "binary obj dump available, loading " << filename.c_str() << endl; 
    236237        // table associating indices with vectors 
    237238        map<int, Vector3> hashTable; 
     239 
    238240        // table for vertices 
    239241        VertexContainer vertices;  
    240242        FaceContainer faces; 
    241243 
    242         char str[100]; 
    243         int meshGrouping; 
    244         Environment::GetSingleton()->GetIntValue("ObjParser.meshGrouping", meshGrouping); 
    245  
    246         int nMaxFaces = meshGrouping; 
    247  
    248244        while (1) 
    249245        { 
     
    271267         
    272268        return true; 
     269} 
     270 
     271 
     272bool Preprocessor::ExportBinaryObj(const string filename, SceneGraphNode *root) 
     273{ 
     274        //ifstream samplesIn(filename, ios::binary); 
     275        ogzstream samplesOut(filename.c_str()); 
     276        if (!samplesOut.is_open()) 
     277                return false; 
     278 
     279        ObjectContainer::const_iterator oit, oit_end = root->mGeometry.end(); 
     280 
     281        for (oit = root->mGeometry.begin(); oit != oit_end; ++ oit) 
     282        { 
     283                Intersectable *obj = *oit; 
     284 
     285                if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE) 
     286                { 
     287                        Triangle3 tri = dynamic_cast<TriangleIntersectable *>(obj)->GetItem(); 
     288 
     289                        samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 0), sizeof(Vector3)); 
     290                        samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 1), sizeof(Vector3)); 
     291                        samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 2), sizeof(Vector3)); 
     292                } 
     293                else 
     294                { 
     295                        cout << "not implemented intersectable type " << obj->Type() << endl; 
     296                } 
     297        } 
     298 
     299        return true; 
     300} 
     301 
     302static string ReplaceSuffix(string filename, string a, string b) 
     303{ 
     304        string result = filename; 
     305 
     306        int pos = (int)filename.rfind(a, (int)filename.size() - 1); 
     307        if (pos == filename.size() - a.size()) { 
     308                result.replace(pos, a.size(), b); 
     309        } 
     310        return result; 
    273311} 
    274312 
     
    295333                &mFaceParents : NULL; 
    296334 
    297         if (files == 1) { 
    298                  
     335        if (files == 1)  
     336        { 
    299337                if (strstr(filename.c_str(), ".x3d")) 
    300338                { 
    301339                        parser = new X3dParser; 
    302                 } 
    303                 else 
    304                 { 
    305                   if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb")) 
    306                   { 
    307                           parser = new PlyParser; 
    308                   } 
    309                   else if (strstr(filename.c_str(), ".obj")) 
    310                   { 
    311                           // hack: load binary dump 
    312                           string binFile = ReplaceSuffix(filename, ".obj", ".bin"); 
    313340                         
    314                           const bool loaded =  
    315                                   LoadBinaryObj(binFile, mSceneGraph->GetRoot(), mLoadMeshes, fi); 
    316                                  
    317                           if (loaded) 
    318                                 return true; 
    319  
    320               parser = new ObjParser; 
    321                   } 
    322                   else  
    323                   { 
    324                           parser = new UnigraphicsParser; 
    325                   } 
    326                 } 
    327  
    328                 cout << filename << endl; 
    329                  
    330                 result = parser->ParseFile(filename,  
     341                        result = parser->ParseFile(filename,  
     342                                                                           mSceneGraph->GetRoot(), 
     343                                                                           mLoadMeshes, 
     344                                                                           fi); 
     345                        delete parser; 
     346                } 
     347                else if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb")) 
     348                { 
     349                        parser = new PlyParser; 
     350 
     351                        result = parser->ParseFile(filename,  
     352                                                                           mSceneGraph->GetRoot(), 
     353                                                                           mLoadMeshes, 
     354                                                                           fi); 
     355                        delete parser; 
     356                } 
     357                else if (strstr(filename.c_str(), ".obj")) 
     358                { 
     359                        // hack: load binary dump 
     360                        string binFile = ReplaceSuffix(filename, ".obj", ".bin"); 
     361 
     362                        if (!mLoadMeshes) 
     363                        { 
     364                                result = LoadBinaryObj(binFile, mSceneGraph->GetRoot(), fi); 
     365                        } 
     366 
     367                        if (!result) 
     368                        { 
     369                                cout << "no binary dump available or loading full meshes, parsing file" << endl; 
     370                                parser = new ObjParser; 
     371                 
     372                                result = parser->ParseFile(filename,  
    331373                                                                   mSceneGraph->GetRoot(), 
    332374                                                                   mLoadMeshes, 
    333375                                                                   fi); 
    334  
    335                 // only works for triangles 
    336                 ExportBinaryObj(binFile): 
    337  
    338                 delete parser; 
    339  
     376                                                 
     377                                // only works for triangles 
     378                                if (!mLoadMeshes) 
     379                                { 
     380                                        cout << "exporting binary obj to " << binFile << "... " << endl; 
     381                                        ExportBinaryObj(binFile, mSceneGraph->GetRoot()); 
     382                                        cout << "finished" << endl; 
     383                                } 
     384 
     385                                delete parser; 
     386                        } 
     387                        else if (1) 
     388                        { 
     389                                ExportBinaryObj("../data/test.bin", mSceneGraph->GetRoot()); 
     390                                //Exporter *exporter = Exporter::GetExporter("dummy.wrl"); 
     391                                //exporter->ExportGeometry(mSceneGraph->GetRoot()->mGeometry); 
     392                        } 
     393                } 
     394                else  
     395                { 
     396                        parser = new UnigraphicsParser; 
     397                        result = parser->ParseFile(filename,  
     398                                                                           mSceneGraph->GetRoot(), 
     399                                                                           mLoadMeshes,                                                            
     400                                                                           fi); 
     401                        delete parser; 
     402                } 
     403                 
     404                cout << filename << endl; 
    340405        }  
    341         else { 
     406        else  
     407        { 
    342408                vector<string>::const_iterator fit, fit_end = filenames.end(); 
    343409                 
Note: See TracChangeset for help on using the changeset viewer.