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

Revision 2575, 10.8 KB checked in by bittner, 17 years ago (diff)

big merge: preparation for havran ray caster, check if everything works

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