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

Revision 2116, 10.3 KB checked in by mattausch, 17 years ago (diff)

implemented hashpvs

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
221        if (HASH_PVS)
222                Debug << "using hash pvs" << endl;
223        else
224                Debug << "using verbose pvs" << endl;
225
226        /////////////
227        //-- load scene
228
229        if (!preprocessor->LoadScene(filename))
230        {
231                cout << "loading file " << filename << " failed" << endl;
232                Cleanup();
233                exit(1);
234        }
235       
236        ////////////
237        //-- initialize external ray caster
238
239        if (preprocessor->LoadInternKdTree(internKdTree))
240        {
241                cout << "intern kd tree loaded!" << endl;
242        }
243        else
244        {
245                cout << "loading intern kd tree failed!" << endl;
246                Cleanup();
247                exit(1);
248        }
249
250        // export objects as obj
251        if (preprocessor->mExportObj)
252        {
253                if (strstr(filename.c_str(), ".obj"))
254                {
255                        cerr << "already in obj format" << endl;
256                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
257                }
258                else
259                {
260                 
261                        const string objname = GetInternFilename(filename, ".obj");
262
263                        cout << "exporting scene to " << objname << endl;
264                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
265
266                       
267                        if (success)
268                        {
269                                cout << "finished exporting obj" << endl;
270                        }
271                        else
272                        {
273                                cerr << "error exporting " << objname << endl;
274                        }
275                }
276        }
277       
278        // parse view cells related options
279        if (!preprocessor->PrepareViewCells())
280        {
281                cerr << "error: view cells could not be loaded" << endl;
282
283                Cleanup();
284                exit(1);
285        }
286
287        string viewCellPointsFile;
288
289        if (strstr(filename.c_str(), ".obj"))
290                viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
291        else if (strstr(filename.c_str(), ".dat"))
292                viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
293        else if (strstr(filename.c_str(), ".x3d"))
294                viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
295
296        bool importRandomViewCells;
297        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
298                                                                                           importRandomViewCells);
299
300        if (importRandomViewCells)
301        {
302                cout << "importing random view cells" << endl;
303                preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellPointsFile);
304                cout << "finished" << endl;
305        }
306
307        bool useHwGlobalLines;
308        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
309                                                                                           useHwGlobalLines);
310
311        if (useHwGlobalLines)
312                preprocessor->PrepareHwGlobalLines();
313
314        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
315        PreprocessorThread *pt = NULL;
316
317#ifdef TRY_GLOBAL_LINES
318       
319        preprocessor->PrepareHwGlobalLines();
320        globalLinesRenderer->Run();
321
322#else
323
324#ifdef USE_QT
325       
326        // create a qt application first (must be created before any opengl widget ...)
327        QApplication *app = new QApplication(argc, NULL);
328
329        pt = new QtPreprocessorThread(preprocessor);
330
331#else
332
333        //#if USE_THREADS
334        //      pt = new BoostPreprocessorThread(preprocessor);
335        //#else
336                // use a dummy thread
337                pt = new DummyPreprocessorThread(preprocessor);
338        //#endif
339#endif
340
341        preprocessor->SetThread(pt);
342
343        bool guiSupported = false;
344        bool useRendererBuffer = true;
345
346        int frames;
347        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
348       
349       
350#ifdef USE_QT
351
352        if (importRandomViewCells || frames)
353          {
354                QGLFormat f;
355                f.setStencil(true);
356                QGLFormat::setDefaultFormat(f);
357
358                // NOTE: render texture should be power of 2 and square
359                // renderer must be initialised
360                // $$matt
361                preprocessor->renderer =
362                  new QtGlRendererBuffer(1024, 1024,
363                                                                 preprocessor->mSceneGraph,
364                                                                 preprocessor->mViewCellsManager,
365                                                                 preprocessor->mKdTree);
366          }
367#endif
368
369        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
370          {
371#ifdef USE_QT
372                cout << "using gl widget" << endl;
373                // create and run the preprocessor application in a parallel thread
374               
375                pt->InitThread();
376                pt->RunThread();
377
378                // display the render widget
379                if (!rendererWidget)
380                {
381
382                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
383                        QMessageBox::information(0, "OpenGL pbuffers",
384                                                                           "This system does not support OpenGL/pbuffers.",
385                                                                           QMessageBox::Ok);
386                          return NULL;
387                        }
388
389                  rendererWidget =
390                        new QtGlRendererWidget(preprocessor->mSceneGraph,
391                                                                   preprocessor->mViewCellsManager,
392                                                                   preprocessor->mKdTree);
393                 
394                  rendererWidget->Show();
395                 
396                  QtGlViewer *viewer =
397                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
398                 
399                  viewer->show();
400                  guiSupported = true;
401                }
402               
403               
404                bool exportRandomViewCells;
405                Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
406                                                                                                  exportRandomViewCells);
407
408                if (exportRandomViewCells)
409                {
410                        cout << "exporting random view cells" << endl;
411                        preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
412                        cout << "finished" << endl;
413                }
414
415                /*bool evaluatePixelError;
416                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
417
418                if (evaluatePixelError)
419                {
420                        cout << "evaluating pixel error" << endl;
421                        preprocessor->ComputeRenderError();
422                }*/
423               
424                qApp->exec();
425#endif
426        }
427
428        // no gui available
429        if (!guiSupported)
430        {
431                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
432                        cout << "warning: gui not supported!" << endl;
433               
434                preprocessor->mUseGlRenderer = false;
435                preprocessor->mUseGlDebugger = false;
436        }
437
438        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
439        {
440                cout << "executing main thread" << endl;
441                // just call the mail method -> will be executed in the main thread
442                pt->Main();
443        }       
444#endif
445
446        // release memory
447        Cleanup();
448       
449        delete pt;
450
451        return returnCode;
452}
453
Note: See TracBrowser for help on using the repository browser.