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

Revision 2667, 10.2 KB checked in by bittner, 16 years ago (diff)

merge on nemo

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