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

Revision 2560, 10.0 KB checked in by mattausch, 17 years ago (diff)

added functionality for visualization

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