source: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp @ 1658

Revision 1658, 6.8 KB checked in by mattausch, 18 years ago (diff)

implemented obj dump for fast loading

RevLine 
[1272]1#ifdef GTP_INTERNAL
[1579]2#define USE_QT 1
[1501]3#else
[1579]4#define USE_QT 0
[1272]5#endif
6
[1579]7#define USE_THREADS 1
[1457]8
[1199]9#ifdef UNICODE
10#undef UNICODE
11#endif
12
[1151]13#include <windows.h>
14#include <stdio.h>
15#include <crtdbg.h>
16
[1486]17#include "PreprocessorFactory.h"
[162]18#include "Parser.h"
19#include "Environment.h"
[170]20#include "MeshKdTree.h"
[1486]21#include "Preprocessor.h"
[1634]22#include "common.h"
[1272]23
[1457]24#include "PreprocessorThread.h"
[1579]25
26#if !USE_QT && USE_THREADS
[1146]27#include "BoostPreprocessorThread.h"
[1272]28#endif
29
[1001]30#include "ResourceManager.h"
[492]31#include "GlRenderer.h"
32
[1272]33#if USE_QT
[1387]34#include "QtPreprocessorThread.h"
35#include "QtGlRenderer.h"
[1272]36#endif
37
[162]38#define USE_EXE_PATH false
39
[372]40
[863]41using namespace GtpVisibilityPreprocessor;
[492]42
[1145]43Preprocessor *preprocessor = NULL;
[1151]44GlRendererWidget *rendererWidget = NULL;
[1145]45
[1151]46// DLL function signature
[1153]47typedef GlRendererWidget *(*importFunction)(Preprocessor *);
[1145]48
[1241]49
[1199]50
[1145]51void Cleanup()
52{
[1151]53        DEL_PTR(rendererWidget);
[1145]54        DEL_PTR(preprocessor);
55
56        Environment::DelSingleton();
57        MeshManager::DelSingleton();
58        MaterialManager::DelSingleton();
59}
60
61
[1626]62static string ReplaceSuffix(string filename, string a, string b)
63{
64        string result = filename;
[1145]65
[1626]66        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
67        if (pos == filename.size() - a.size()) {
68                result.replace(pos, a.size(), b);
69        }
70        return result;
71}
[1145]72
[1626]73
[1634]74static int SplitFilenames(const string str, vector<string> &filenames)
[1272]75{
[1634]76        int pos = 0;
[1633]77
[1634]78        while(1) {
79                int npos = (int)str.find(';', pos);
80               
81                if (npos < 0 || npos - pos < 1)
82                        break;
83                filenames.push_back(string(str, pos, npos - pos));
84                pos = npos + 1;
[1633]85        }
[1634]86       
87        filenames.push_back(string(str, pos, str.size() - pos));
88        return (int)filenames.size();
89}
90
91
92static string GetInternKdTreeName(const string &filename)
93{
94        vector<string> filenames;
95        const int files = SplitFilenames(filename, filenames);
96
97        vector<string>::const_iterator sit, sit_end = filenames.end();
98        string kdFilename;
99
100        int i = 0;
101        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
[1633]102        {
[1634]103                string currentFile = *sit;
104                //cout << "here6 " << currentFile << endl;
105                string strippedFilename;
[1633]106
[1634]107                if (i == 0)
108                {       
109                        strippedFilename = currentFile;
110                }
111                else
112                {
[1640]113                        char *str = StripPath(currentFile.c_str());
114                        strippedFilename = string(str);
115
116                        delete []str;
[1634]117                }
118               
119                string suffix("_");
120
121                if (i == (int)filenames.size() - 1)
122                {
123                        if (preprocessor->mLoadMeshes)
124                        {
125                                suffix = ".kdm";       
126                        }
127                        else
128                        {
129                                suffix = ".kdt";
130                        }
131                }
132
133                if (strstr(strippedFilename.c_str(), ".x3d"))
134                {
135                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
136                }
137        else if (strstr(strippedFilename.c_str(), ".dat"))
138                {
139                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
140                }
141                else if (strstr(strippedFilename.c_str(), ".obj"))
142                {
[1658]143                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
[1634]144                }
145                else
146                {
147                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
148                }
[1626]149        }
[1272]150
[1634]151        //cout << "kdfilename: " << kdFilename << endl;
152        return kdFilename;
[1272]153}
154
[1626]155
[162]156int
[492]157main(int argc, char **argv)
[997]158{
[991]159
[1145]160        //Now just call this function at the start of your program and if you're
161        //compiling in debug mode (F5), any leaks will be displayed in the Output
162        //window when the program shuts down. If you're not in debug mode this will
163        //be ignored. Use it as you will!
164        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
[1272]165#if 0
166  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
167  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
168  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
169#endif
[997]170
[1145]171        int returnCode = 0;
[1153]172       
[1145]173        InitTiming();
[997]174
[1145]175        Debug.open("debug.log");
[1199]176 
[1145]177        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
178        MeshKdTree::ParseEnvironment();
[1002]179
[1292]180       
[1145]181        char buff[128];
182        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
183        string preprocessorType(buff);
184
[1486]185        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]186        {
[1486]187                Environment::DelSingleton();
188                cerr << "Unknown preprocessor type" << endl;
189                exit(1);
[1328]190        }
[997]191
[1415]192
193        /////////////
[1145]194        //-- load scene
[492]195
[1145]196        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
197        string filename(buff);
[1634]198        const string dummyname = GetInternKdTreeName(filename);
[1328]199
200        if (!preprocessor->LoadScene(filename))
201        {
202                cout << "loading file " << filename << " failed" << endl;
203                Cleanup();
204                exit(1);
205        }
206
[1626]207        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
208        const string internKdTree = GetInternKdTreeName(filename);
[1418]209
[1221]210        //-- initialize external ray casters
[1418]211        if (preprocessor->InitRayCast(externKdTree, internKdTree))
[1328]212        {
[1221]213                cout << "ray casting initialized!" << endl;
[1328]214        }
[1221]215        else
216        {
217                cout << "ray casting initialization failed" << endl;
[1328]218                Cleanup();
[1221]219                exit(1);
220        }
[1344]221
[1145]222        // parse view cells related options
[1522]223        if (!preprocessor->PrepareViewCells())
224        {
225                Cleanup();
226                exit(1);
227        }
[1145]228
[1196]229        if (0)
230        {
[1545]231                preprocessor->Export(filename + "-out.x3d", true, false);
232                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
[746]233        }
[1344]234
[1239]235        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1272]236        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
[1387]237        PreprocessorThread *pt;
238
239#if USE_QT
240        pt = new QtPreprocessorThread(preprocessor);
241#else
[1579]242#if USE_THREADS
[1387]243        pt = new BoostPreprocessorThread(preprocessor);
[1457]244#else
245        pt = new PreprocessorThread(preprocessor);
[1272]246#endif
[1579]247#endif
[1387]248
[1248]249        bool guiSupported = false;
[1454]250
[1145]251        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[1387]252          {
[1241]253                cout << "using gl widget" << endl;
[1153]254                // create and run the preprocessor application in a parallel thread
[1457]255#if USE_QT             
[1387]256#if USE_THREADS
257                pt->InitThread();
[1613]258                if (!preprocessor->mDelayVisibilityComputation)
259                  pt->RunThread();
[1272]260#endif
[1387]261               
[1248]262                // display the render widget
[1344]263                if (!rendererWidget)
264                {
[1387]265                  // create a qt application first (must be created before any opengl widget ...)
266                  QApplication *app = new QApplication(argc, NULL);
267                 
268                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
269                        QMessageBox::information(0, "OpenGL pbuffers",
270                                                                         "This system does not support OpenGL/pbuffers.",
271                                                                         QMessageBox::Ok);
272                        return NULL;
273                  }
274                 
275                 
276                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
277                                                                                                  preprocessor->mViewCellsManager,
278                                                                                                  preprocessor->mKdTree);
[1613]279
280                  ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
[1387]281                  rendererWidget->Show();
[1404]282                  guiSupported = true;
[1272]283                }
[1344]284
[1404]285                qApp->exec();   
[1387]286#endif
287          }
[1221]288       
[1248]289        if (!guiSupported) {
290          preprocessor->mUseGlRenderer = false;
291          preprocessor->mUseGlDebugger = false;
[1005]292        }
[1181]293       
[1248]294        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
295          // just call the mail method -> will be executed in the main thread
[1454]296          pt->Main();
[1248]297        }
298       
[1415]299        // release memory
[1145]300        Cleanup();
[1248]301       
[1145]302        return returnCode;
[162]303}
304
Note: See TracBrowser for help on using the repository browser.