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

Revision 2743, 10.7 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[1926]1#define USE_THREADS 0
[1457]2
[1199]3#ifdef UNICODE
4#undef UNICODE
5#endif
6
[2176]7#define NOMINMAX
[2634]8#ifdef _WIN32
[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"
[1926]28#include "ViewCellsManager.h"
[2707]29#include "SepPlanesBox3.h"
[1694]30
[2629]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
[2686]62static string GetViewPointsListName(const string &filename)
63{
64        char buff[200];
65        Environment::GetSingleton()->GetStringValue("ViewCells.randomViewPointsList", buff);
[2706]66        string viewCellPointsFile = buff;
[2686]67
[2706]68        if (viewCellPointsFile == "")
[2686]69        {
70                // if not specified, take file that has same prefix as scene file
71                if (strstr(filename.c_str(), ".obj"))
72                        viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
73                else if (strstr(filename.c_str(), ".dat"))
74                        viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
75                else if (strstr(filename.c_str(), ".x3d"))
76                        viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
77        }
78
79        return viewCellPointsFile;
80}
81
82
[1145]83void Cleanup()
84{
[1151]85        DEL_PTR(rendererWidget);
[1145]86        DEL_PTR(preprocessor);
87
88        Environment::DelSingleton();
89        MeshManager::DelSingleton();
90        MaterialManager::DelSingleton();
91}
92
93
94
[2452]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
[162]151int
[492]152main(int argc, char **argv)
[997]153{
[2676]154        int returnCode = 0;
[2659]155
[2676]156#ifdef _CRT_SET
[2575]157
[2676]158        //Now just call this function at the start of your program and if you're
159        //compiling in debug mode (F5), any leaks will be displayed in the Output
160        //window when the program shuts down. If you're not in debug mode this will
161        //be ignored. Use it as you will!
162        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
[2582]163
[2676]164        _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
165        _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
166        _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
[2575]167#endif
168
[2676]169        InitTiming();
170        Debug.open("debug.log");
[997]171
[2707]172       
[2709]173        //TestSepAxis();
[2707]174       
[2676]175        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
176        MeshKdTree::ParseEnvironment();
177
178        char buff[128];
179        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
180        string preprocessorType(buff);
181
182        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]183        {
[1486]184                Environment::DelSingleton();
185                cerr << "Unknown preprocessor type" << endl;
186                exit(1);
[1328]187        }
[997]188
[2676]189
[1145]190        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
191        string filename(buff);
[2676]192
[2003]193        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
[2049]194        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
[2676]195                ".kdm" : ".kdt");
[1328]196
[2003]197        if (preprocessor->InitRayCast(externKdTree, internKdTree))
198        {
199                cout << "ray casting initialized!" << endl;
200        }
201        else
202        {
203                cout << "ray casting initialization failed!" << endl;
204                Cleanup();
205                exit(1);
206        }
207
208        /////////////
209        //-- load scene
210
[1328]211        if (!preprocessor->LoadScene(filename))
212        {
213                cout << "loading file " << filename << " failed" << endl;
214                Cleanup();
215                exit(1);
216        }
[2560]217
[2676]218
[1697]219        ////////////
220        //-- initialize external ray caster
[2003]221        if (preprocessor->LoadInternKdTree(internKdTree))
[1328]222        {
[2003]223                cout << "intern kd tree loaded!" << endl;
[1328]224        }
[1221]225        else
226        {
[2003]227                cout << "loading intern kd tree failed!" << endl;
[1328]228                Cleanup();
[1221]229                exit(1);
230        }
[2676]231
[2699]232       
[1695]233        // export objects as obj
234        if (preprocessor->mExportObj)
235        {
236                if (strstr(filename.c_str(), ".obj"))
237                {
238                        cerr << "already in obj format" << endl;
[1701]239                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]240                }
241                else
242                {
243                        const string objname = GetInternFilename(filename, ".obj");
244
[1701]245                        cout << "exporting scene to " << objname << endl;
[1695]246                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
247
248                        if (success)
249                                cout << "finished exporting obj" << endl;
250                        else
251                                cerr << "error exporting " << objname << endl;
252                }
253        }
[2660]254
[1145]255        // parse view cells related options
[1522]256        if (!preprocessor->PrepareViewCells())
257        {
[1926]258                cerr << "error: view cells could not be loaded" << endl;
[1522]259                Cleanup();
260                exit(1);
261        }
[2659]262        else
263                cout << "view cells successfully loaded" << endl;
[1999]264
[2017]265
[2686]266        const string viewCellPointsFile = GetViewPointsListName(filename);
[2023]267
[2048]268        bool importRandomViewCells;
[2686]269        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells", importRandomViewCells);
[2048]270
271        if (importRandomViewCells)
272        {
[2686]273                cout << "importing random view points" << endl;
274
[2677]275                if (preprocessor->mViewCellsManager->ImportViewCellsList(viewCellPointsFile))
276                        cout << "successfully loaded " << viewCellPointsFile << endl;
277                else
[2706]278                        cerr << "error: file " << viewCellPointsFile << " not found" << endl;
[2048]279        }
280
[2695]281        // create meshes of view cells
282        preprocessor->mViewCellsManager->FinalizeViewCells(true);
[2660]283
[2695]284        cout << "view space box: " << preprocessor->mViewCellsManager->GetViewSpaceBox() << endl;
[1968]285        bool useHwGlobalLines;
286        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
[2676]287                useHwGlobalLines);
[1968]288
289        if (useHwGlobalLines)
290                preprocessor->PrepareHwGlobalLines();
291
[1239]292        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1926]293        PreprocessorThread *pt = NULL;
[2676]294
[2560]295        //preprocessor->PrepareHwGlobalLines();
296        //globalLinesRenderer->Run();
[1940]297
298
[1248]299        bool guiSupported = false;
[2560]300        const bool useRendererBuffer = true;
[2049]301
302        int frames;
303        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
[2560]304
[2611]305        bool evalPixelError;
306        Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", evalPixelError);
[2660]307
308
[2049]309#ifdef USE_QT
310
[2560]311        // create a qt application first (must be created before any opengl widget ...)
312        QApplication *app = new QApplication(argc, NULL);
[2679]313
[2560]314        pt = new QtPreprocessorThread(preprocessor);   
[2676]315
[2611]316        if (evalPixelError && (importRandomViewCells || frames))
[2625]317        {
[2664]318                if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers())
319                {
320                        cerr << "this system does not support OpenGL/pbuffers" << endl;
321
322                        QMessageBox::information(0, "OpenGL pbuffers",
323                                "This system does not support OpenGL/pbuffers.",
324                                QMessageBox::Ok);
325
326                        return NULL;
327                }
328
[2049]329                QGLFormat f;
330                f.setStencil(true);
331                QGLFormat::setDefaultFormat(f);
[2625]332
[2049]333                // NOTE: render texture should be power of 2 and square
334                // renderer must be initialised
[2743]335                const float w = 2048;//1024;
336                const float h = 2048;//1024;
[2709]337
[2679]338                QtGlRendererBuffer *glbuf =
[2709]339                        new QtGlRendererBuffer(w, h,
[2679]340                                                                   preprocessor->mSceneGraph,
341                                                                   preprocessor->mViewCellsManager,
342                                                                   preprocessor->mKdTree);
[2676]343
[2679]344                preprocessor->renderer = glbuf;
[2647]345        }
346
347        bool exportRandomViewCells;
348        Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
[2676]349                exportRandomViewCells);
350
[2647]351        if (exportRandomViewCells)
[2664]352        {
[2647]353                cout << "exporting random view cells" << endl;
354                preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
355                cout << "finished" << endl;
[2664]356        }
[2647]357
[2560]358        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
359        {
360                ////////
361                //-- create and run the preprocessor application in a parallel thread
[1926]362
[2560]363                cout << "using gl widget" << endl;
[2049]364
[2560]365                pt->InitThread();
366                pt->RunThread();
[1613]367
[2560]368                // display the render widget
369                if (!rendererWidget)
370                {
[2664]371                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers())
372                        {
373                                cerr << "this system does not support OpenGL/pbuffers" << endl;
374
[2560]375                                QMessageBox::information(0, "OpenGL pbuffers",
376                                        "This system does not support OpenGL/pbuffers.",
377                                        QMessageBox::Ok);
[2664]378
[2560]379                                return NULL;
380                        }
[2017]381
[2560]382                        rendererWidget =
383                                new QtGlRendererWidget(preprocessor->mSceneGraph,
[2676]384                                preprocessor->mViewCellsManager,
385                                preprocessor->mKdTree);
[2017]386
[2560]387                        rendererWidget->Show();
[2023]388
[2625]389                        // hack matt
390                        preprocessor->mRendererWidget = rendererWidget;
[2615]391                        //rendererWidget->SetWindowTitle("Global Visualization");
392
[2689]393                        if (1 && !rendererWidget->GetUseVbos()) // viewer not working with vbo
[2560]394                        {
[2569]395                                cout << "starting the qt viewer" << endl;
[2560]396                                QtGlViewer *viewer =
397                                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
398                                viewer->show();
399                        }
[2538]400
[2560]401                        guiSupported = true;
402                }
[2538]403
[2560]404                qApp->exec();
405        }
[2664]406#else // USE_QT
[2538]407
[2664]408        /*#if USE_THREADS
[2676]409        pt = new BoostPreprocessorThread(preprocessor);
[2664]410        #else
[2676]411        // use a dummy thread
412        pt = new DummyPreprocessorThread(preprocessor);
[2664]413        #endif
414        */
[1387]415#endif
[1785]416
[2560]417        preprocessor->SetThread(pt);
[1696]418
[2560]419        // no gui available
420        if (!guiSupported)
421        {
422                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
423                        cout << "warning: gui not supported!" << endl;
[2538]424
[2560]425                preprocessor->mUseGlRenderer = false;
426                preprocessor->mUseGlDebugger = false;
427        }
[1999]428
[2560]429        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
430        {
431                cout << "executing main thread" << endl;
432                // just call the mail method -> will be executed in the main thread
433                pt->Main();
434        }       
[2538]435
[2560]436        // release memory
437        Cleanup();
438        delete pt;
439
440        return returnCode;
[162]441}
442
Note: See TracBrowser for help on using the repository browser.