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

RevLine 
[1926]1#define USE_THREADS 0
[1457]2
[1199]3#ifdef UNICODE
4#undef UNICODE
5#endif
6
[1151]7#include <windows.h>
8#include <stdio.h>
9#include <crtdbg.h>
10
[1486]11#include "PreprocessorFactory.h"
[162]12#include "Parser.h"
13#include "Environment.h"
[170]14#include "MeshKdTree.h"
[1486]15#include "Preprocessor.h"
[1634]16#include "common.h"
[1272]17
[1457]18#include "PreprocessorThread.h"
[1694]19#include "ObjExporter.h"
20#include "SceneGraph.h"
[1949]21#include "GlobalLinesRenderer.h"
[2116]22#include "PvsDefinitions.h"
[1579]23
[1926]24#include "ViewCellsManager.h"
[1694]25
[1926]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
[1272]34#endif
35
[1001]36#include "ResourceManager.h"
[492]37#include "GlRenderer.h"
38
[1272]39
[162]40#define USE_EXE_PATH false
41
[372]42
[863]43using namespace GtpVisibilityPreprocessor;
[492]44
[1888]45//Preprocessor *preprocessor = NULL;
[1151]46GlRendererWidget *rendererWidget = NULL;
[1940]47//GlobalLinesRenderer *globalLinesRenderer = NULL;
[1145]48
[1940]49
[1151]50// DLL function signature
[1153]51typedef GlRendererWidget *(*importFunction)(Preprocessor *);
[1145]52
[1241]53
[1199]54
[1145]55void Cleanup()
56{
[1151]57        DEL_PTR(rendererWidget);
[1145]58        DEL_PTR(preprocessor);
59
60        Environment::DelSingleton();
61        MeshManager::DelSingleton();
62        MaterialManager::DelSingleton();
63}
64
65
[1626]66static string ReplaceSuffix(string filename, string a, string b)
67{
68        string result = filename;
[1145]69
[1626]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}
[1145]76
[1626]77
[1634]78static int SplitFilenames(const string str, vector<string> &filenames)
[1272]79{
[1634]80        int pos = 0;
[1633]81
[1634]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;
[1633]89        }
[1634]90       
91        filenames.push_back(string(str, pos, str.size() - pos));
92        return (int)filenames.size();
93}
94
95
[1695]96static string GetInternFilename(const string &filename, const string newSuffix)
[1634]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)
[1633]106        {
[1634]107                string currentFile = *sit;
108                string strippedFilename;
[1633]109
[1634]110                if (i == 0)
111                {       
112                        strippedFilename = currentFile;
113                }
114                else
115                {
[1640]116                        char *str = StripPath(currentFile.c_str());
117                        strippedFilename = string(str);
118
[1695]119                        delete [] str;
[1634]120                }
121               
122                string suffix("_");
123
124                if (i == (int)filenames.size() - 1)
125                {
[1695]126                        suffix = newSuffix;
[1634]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                {
[1658]139                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
[1634]140                }
141                else
142                {
143                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
144                }
[1626]145        }
[1272]146
[1634]147        //cout << "kdfilename: " << kdFilename << endl;
148        return kdFilename;
[1272]149}
150
[1926]151
[162]152int
[492]153main(int argc, char **argv)
[997]154{
[1145]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() {
[1696]160#if 1
[1272]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
[997]165
[1145]166        int returnCode = 0;
[1926]167
[1145]168        InitTiming();
[2020]169        Debug.open("debug.log");
[997]170
[2023]171#if 0
[2020]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
[2023]186       
[1145]187        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
188        MeshKdTree::ParseEnvironment();
[1002]189
[1145]190        char buff[128];
191        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
192        string preprocessorType(buff);
193
[1486]194        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]195        {
[1486]196                Environment::DelSingleton();
197                cerr << "Unknown preprocessor type" << endl;
198                exit(1);
[1328]199        }
[997]200
[2023]201
[1145]202        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
203        string filename(buff);
[1695]204       
[2003]205        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
[2049]206        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
207                                                                                                  ".kdm" : ".kdt");
[1328]208
[2003]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
[2116]221        if (HASH_PVS)
222                Debug << "using hash pvs" << endl;
223        else
224                Debug << "using verbose pvs" << endl;
225
[2003]226        /////////////
227        //-- load scene
228
[1328]229        if (!preprocessor->LoadScene(filename))
230        {
231                cout << "loading file " << filename << " failed" << endl;
232                Cleanup();
233                exit(1);
234        }
[1926]235       
[1697]236        ////////////
237        //-- initialize external ray caster
[1999]238
[2003]239        if (preprocessor->LoadInternKdTree(internKdTree))
[1328]240        {
[2003]241                cout << "intern kd tree loaded!" << endl;
[1328]242        }
[1221]243        else
244        {
[2003]245                cout << "loading intern kd tree failed!" << endl;
[1328]246                Cleanup();
[1221]247                exit(1);
248        }
[1344]249
[1695]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;
[1701]256                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]257                }
258                else
259                {
[2049]260                 
[1695]261                        const string objname = GetInternFilename(filename, ".obj");
262
[1701]263                        cout << "exporting scene to " << objname << endl;
[1695]264                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
265
[2049]266                       
[1695]267                        if (success)
268                        {
269                                cout << "finished exporting obj" << endl;
270                        }
271                        else
272                        {
273                                cerr << "error exporting " << objname << endl;
274                        }
275                }
276        }
[1713]277       
[1145]278        // parse view cells related options
[1522]279        if (!preprocessor->PrepareViewCells())
280        {
[1926]281                cerr << "error: view cells could not be loaded" << endl;
282
[1522]283                Cleanup();
284                exit(1);
285        }
[1999]286
[2048]287        string viewCellPointsFile;
[2017]288
[2048]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");
[2023]295
[2048]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
[1968]307        bool useHwGlobalLines;
308        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
309                                                                                           useHwGlobalLines);
310
311        if (useHwGlobalLines)
312                preprocessor->PrepareHwGlobalLines();
313
[1239]314        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1926]315        PreprocessorThread *pt = NULL;
[1387]316
[1940]317#ifdef TRY_GLOBAL_LINES
[1968]318       
[1969]319        preprocessor->PrepareHwGlobalLines();
[1940]320        globalLinesRenderer->Run();
321
322#else
323
[1926]324#ifdef USE_QT
325       
[1715]326        // create a qt application first (must be created before any opengl widget ...)
327        QApplication *app = new QApplication(argc, NULL);
[1926]328
[1387]329        pt = new QtPreprocessorThread(preprocessor);
[1926]330
[1387]331#else
[1926]332
333        //#if USE_THREADS
334        //      pt = new BoostPreprocessorThread(preprocessor);
335        //#else
336                // use a dummy thread
337                pt = new DummyPreprocessorThread(preprocessor);
338        //#endif
[1272]339#endif
[1387]340
[1926]341        preprocessor->SetThread(pt);
342
[1248]343        bool guiSupported = false;
[2049]344        bool useRendererBuffer = true;
345
346        int frames;
347        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
[1926]348       
[2049]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,
[2053]364                                                                 preprocessor->mViewCellsManager,
[2049]365                                                                 preprocessor->mKdTree);
366          }
367#endif
368
[1145]369        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[2049]370          {
[1926]371#ifdef USE_QT
[1241]372                cout << "using gl widget" << endl;
[1153]373                // create and run the preprocessor application in a parallel thread
[1926]374               
[1387]375                pt->InitThread();
[1926]376                pt->RunThread();
377
[1248]378                // display the render widget
[1344]379                if (!rendererWidget)
380                {
[2049]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;
[1926]387                        }
[1613]388
[2049]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;
[1272]401                }
[1926]402               
[2049]403               
[2048]404                bool exportRandomViewCells;
405                Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
406                                                                                                  exportRandomViewCells);
[2017]407
[2048]408                if (exportRandomViewCells)
409                {
410                        cout << "exporting random view cells" << endl;
411                        preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
412                        cout << "finished" << endl;
413                }
[2017]414
[2048]415                /*bool evaluatePixelError;
416                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
[2023]417
[2048]418                if (evaluatePixelError)
419                {
420                        cout << "evaluating pixel error" << endl;
421                        preprocessor->ComputeRenderError();
422                }*/
[2049]423               
[1785]424                qApp->exec();
[1387]425#endif
[1926]426        }
[1785]427
[1926]428        // no gui available
429        if (!guiSupported)
430        {
[1940]431                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
432                        cout << "warning: gui not supported!" << endl;
[1926]433               
434                preprocessor->mUseGlRenderer = false;
435                preprocessor->mUseGlDebugger = false;
[1005]436        }
[1696]437
[1926]438        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
439        {
440                cout << "executing main thread" << endl;
[1696]441                // just call the mail method -> will be executed in the main thread
442                pt->Main();
443        }       
[1940]444#endif
[1999]445
[1415]446        // release memory
[1145]447        Cleanup();
[2017]448       
[1999]449        delete pt;
[1926]450
[1145]451        return returnCode;
[162]452}
453
Note: See TracBrowser for help on using the repository browser.