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

Revision 2161, 10.3 KB checked in by mattausch, 17 years ago (diff)
Line 
1#define USE_THREADS 0
2
3#ifdef UNICODE
4#undef UNICODE
5#endif
6
7#include <windows.h>
8#include <stdio.h>
9#include <crtdbg.h>
10
11#include "PreprocessorFactory.h"
12#include "Parser.h"
13#include "Environment.h"
14#include "MeshKdTree.h"
15#include "Preprocessor.h"
16#include "common.h"
17
18#include "PreprocessorThread.h"
19#include "ObjExporter.h"
20#include "SceneGraph.h"
21#include "GlobalLinesRenderer.h"
22//#include "PvsDefinitions.h"
23
24#include "ViewCellsManager.h"
25
26#ifdef USE_QT 
27        #include "QtPreprocessorThread.h"
28        #include "QtGlViewer.h"
29        #include "QtGlRenderer.h"
30#else
31        #if USE_THREADS
32                #include "BoostPreprocessorThread.h"
33        #endif
34#endif
35
36#include "ResourceManager.h"
37#include "GlRenderer.h"
38
39
40#define USE_EXE_PATH false
41
42
43using namespace GtpVisibilityPreprocessor;
44
45//Preprocessor *preprocessor = NULL;
46GlRendererWidget *rendererWidget = NULL;
47//GlobalLinesRenderer *globalLinesRenderer = NULL;
48
49
50// DLL function signature
51typedef GlRendererWidget *(*importFunction)(Preprocessor *);
52
53
54
55void Cleanup()
56{
57        DEL_PTR(rendererWidget);
58        DEL_PTR(preprocessor);
59
60        Environment::DelSingleton();
61        MeshManager::DelSingleton();
62        MaterialManager::DelSingleton();
63}
64
65
66static string ReplaceSuffix(string filename, string a, string b)
67{
68        string result = filename;
69
70        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
71        if (pos == filename.size() - a.size()) {
72                result.replace(pos, a.size(), b);
73        }
74        return result;
75}
76
77
78static int SplitFilenames(const string str, vector<string> &filenames)
79{
80        int pos = 0;
81
82        while(1) {
83                int npos = (int)str.find(';', pos);
84               
85                if (npos < 0 || npos - pos < 1)
86                        break;
87                filenames.push_back(string(str, pos, npos - pos));
88                pos = npos + 1;
89        }
90       
91        filenames.push_back(string(str, pos, str.size() - pos));
92        return (int)filenames.size();
93}
94
95
96static string GetInternFilename(const string &filename, const string newSuffix)
97{
98        vector<string> filenames;
99        const int files = SplitFilenames(filename, filenames);
100
101        vector<string>::const_iterator sit, sit_end = filenames.end();
102        string kdFilename;
103
104        int i = 0;
105        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
106        {
107                string currentFile = *sit;
108                string strippedFilename;
109
110                if (i == 0)
111                {       
112                        strippedFilename = currentFile;
113                }
114                else
115                {
116                        char *str = StripPath(currentFile.c_str());
117                        strippedFilename = string(str);
118
119                        delete [] str;
120                }
121               
122                string suffix("_");
123
124                if (i == (int)filenames.size() - 1)
125                {
126                        suffix = newSuffix;
127                }
128
129                if (strstr(strippedFilename.c_str(), ".x3d"))
130                {
131                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
132                }
133        else if (strstr(strippedFilename.c_str(), ".dat"))
134                {
135                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
136                }
137                else if (strstr(strippedFilename.c_str(), ".obj"))
138                {
139                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
140                }
141                else
142                {
143                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
144                }
145        }
146
147        //cout << "kdfilename: " << kdFilename << endl;
148        return kdFilename;
149}
150
151
152int
153main(int argc, char **argv)
154{
155        //Now just call this function at the start of your program and if you're
156        //compiling in debug mode (F5), any leaks will be displayed in the Output
157        //window when the program shuts down. If you're not in debug mode this will
158        //be ignored. Use it as you will!
159        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
160#if 1
161  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
162  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
163  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
164#endif
165
166        int returnCode = 0;
167
168        InitTiming();
169        Debug.open("debug.log");
170
171#if 0
172        cout<<"Allocating 1.5GB..."<<endl;
173
174       
175        for (int i=0; i < 1000; i++) {
176          void *p = malloc(1250000000);
177          if (p != NULL) {
178                cout<<"success"<<(i+1)*1000<<"MB"<<endl;
179          }
180          else {
181                cout<<"fail"<<endl;
182                exit(1);
183          }
184        }
185#endif
186       
187        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
188        MeshKdTree::ParseEnvironment();
189
190        char buff[128];
191        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
192        string preprocessorType(buff);
193
194        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
195        {
196                Environment::DelSingleton();
197                cerr << "Unknown preprocessor type" << endl;
198                exit(1);
199        }
200
201
202        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
203        string filename(buff);
204       
205        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
206        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
207                                                                                                  ".kdm" : ".kdt");
208
209        if (preprocessor->InitRayCast(externKdTree, internKdTree))
210        {
211                cout << "ray casting initialized!" << endl;
212        }
213        else
214        {
215                cout << "ray casting initialization failed!" << endl;
216                Cleanup();
217                exit(1);
218        }
219
220        Debug << "using pvs type " << PVS_TYPE << endl;
221
222        /////////////
223        //-- load scene
224
225        if (!preprocessor->LoadScene(filename))
226        {
227                cout << "loading file " << filename << " failed" << endl;
228                Cleanup();
229                exit(1);
230        }
231       
232        ////////////
233        //-- initialize external ray caster
234
235        if (preprocessor->LoadInternKdTree(internKdTree))
236        {
237                cout << "intern kd tree loaded!" << endl;
238        }
239        else
240        {
241                cout << "loading intern kd tree failed!" << endl;
242                Cleanup();
243                exit(1);
244        }
245
246        // export objects as obj
247        if (preprocessor->mExportObj)
248        {
249                if (strstr(filename.c_str(), ".obj"))
250                {
251                        cerr << "already in obj format" << endl;
252                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
253                }
254                else
255                {
256                 
257                        const string objname = GetInternFilename(filename, ".obj");
258
259                        cout << "exporting scene to " << objname << endl;
260                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
261
262                       
263                        if (success)
264                        {
265                                cout << "finished exporting obj" << endl;
266                        }
267                        else
268                        {
269                                cerr << "error exporting " << objname << endl;
270                        }
271                }
272        }
273       
274        // parse view cells related options
275        if (!preprocessor->PrepareViewCells())
276        {
277                cerr << "error: view cells could not be loaded" << endl;
278
279                Cleanup();
280                exit(1);
281        }
282
283        string viewCellPointsFile;
284
285        if (strstr(filename.c_str(), ".obj"))
286                viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
287        else if (strstr(filename.c_str(), ".dat"))
288                viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
289        else if (strstr(filename.c_str(), ".x3d"))
290                viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
291
292        bool importRandomViewCells;
293        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
294                                                                                           importRandomViewCells);
295
296        if (importRandomViewCells)
297        {
298                cout << "importing random view cells" << endl;
299                preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellPointsFile);
300                cout << "finished" << endl;
301        }
302
303        bool useHwGlobalLines;
304        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
305                                                                                           useHwGlobalLines);
306
307        if (useHwGlobalLines)
308                preprocessor->PrepareHwGlobalLines();
309
310        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
311        PreprocessorThread *pt = NULL;
312
313#ifdef TRY_GLOBAL_LINES
314       
315        preprocessor->PrepareHwGlobalLines();
316        globalLinesRenderer->Run();
317
318#else
319
320#ifdef USE_QT
321       
322        // create a qt application first (must be created before any opengl widget ...)
323        QApplication *app = new QApplication(argc, NULL);
324
325        pt = new QtPreprocessorThread(preprocessor);
326
327#else
328
329        //#if USE_THREADS
330        //      pt = new BoostPreprocessorThread(preprocessor);
331        //#else
332                // use a dummy thread
333                pt = new DummyPreprocessorThread(preprocessor);
334        //#endif
335#endif
336
337        preprocessor->SetThread(pt);
338
339        bool guiSupported = false;
340        bool useRendererBuffer = true;
341
342        int frames;
343        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
344       
345       
346#ifdef USE_QT
347
348        if (importRandomViewCells || frames)
349          {
350                QGLFormat f;
351                f.setStencil(true);
352                QGLFormat::setDefaultFormat(f);
353
354                // NOTE: render texture should be power of 2 and square
355                // renderer must be initialised
356                // $$matt
357                preprocessor->renderer =
358                  new QtGlRendererBuffer(1024, 1024,
359                                                                 preprocessor->mSceneGraph,
360                                                                 preprocessor->mViewCellsManager,
361                                                                 preprocessor->mKdTree);
362          }
363#endif
364
365        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
366          {
367#ifdef USE_QT
368                cout << "using gl widget" << endl;
369                // create and run the preprocessor application in a parallel thread
370               
371                pt->InitThread();
372                pt->RunThread();
373
374                // display the render widget
375                if (!rendererWidget)
376                {
377
378                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
379                        QMessageBox::information(0, "OpenGL pbuffers",
380                                                                           "This system does not support OpenGL/pbuffers.",
381                                                                           QMessageBox::Ok);
382                          return NULL;
383                        }
384
385                  rendererWidget =
386                        new QtGlRendererWidget(preprocessor->mSceneGraph,
387                                                                   preprocessor->mViewCellsManager,
388                                                                   preprocessor->mKdTree);
389                 
390                  rendererWidget->Show();
391                 
392                  QtGlViewer *viewer =
393                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
394                 
395                  viewer->show();
396                  guiSupported = true;
397                }
398               
399               
400                bool exportRandomViewCells;
401                Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
402                                                                                                  exportRandomViewCells);
403
404                if (exportRandomViewCells)
405                {
406                        cout << "exporting random view cells" << endl;
407                        preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
408                        cout << "finished" << endl;
409                }
410
411                /*bool evaluatePixelError;
412                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
413
414                if (evaluatePixelError)
415                {
416                        cout << "evaluating pixel error" << endl;
417                        preprocessor->ComputeRenderError();
418                }*/
419               
420                qApp->exec();
421#endif
422        }
423
424        // no gui available
425        if (!guiSupported)
426        {
427                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
428                        cout << "warning: gui not supported!" << endl;
429               
430                preprocessor->mUseGlRenderer = false;
431                preprocessor->mUseGlDebugger = false;
432        }
433
434        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
435        {
436                cout << "executing main thread" << endl;
437                // just call the mail method -> will be executed in the main thread
438                pt->Main();
439        }       
440#endif
441
442        // release memory
443        Cleanup();
444       
445        delete pt;
446
447        return returnCode;
448}
449
Note: See TracBrowser for help on using the repository browser.