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

Revision 2176, 10.3 KB checked in by mattausch, 17 years ago (diff)

removed using namespace std from .h

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
[2176]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
[2176]220        //Debug << "using pvs type " << PVS_TYPE << endl;
[2003]221
222        /////////////
223        //-- load scene
224
[1328]225        if (!preprocessor->LoadScene(filename))
226        {
227                cout << "loading file " << filename << " failed" << endl;
228                Cleanup();
229                exit(1);
230        }
[1926]231       
[1697]232        ////////////
233        //-- initialize external ray caster
[1999]234
[2003]235        if (preprocessor->LoadInternKdTree(internKdTree))
[1328]236        {
[2003]237                cout << "intern kd tree loaded!" << endl;
[1328]238        }
[1221]239        else
240        {
[2003]241                cout << "loading intern kd tree failed!" << endl;
[1328]242                Cleanup();
[1221]243                exit(1);
244        }
[1344]245
[1695]246        // export objects as obj
247        if (preprocessor->mExportObj)
248        {
249                if (strstr(filename.c_str(), ".obj"))
250                {
251                        cerr << "already in obj format" << endl;
[1701]252                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]253                }
254                else
255                {
[2049]256                 
[1695]257                        const string objname = GetInternFilename(filename, ".obj");
258
[1701]259                        cout << "exporting scene to " << objname << endl;
[1695]260                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
261
[2049]262                       
[1695]263                        if (success)
264                        {
265                                cout << "finished exporting obj" << endl;
266                        }
267                        else
268                        {
269                                cerr << "error exporting " << objname << endl;
270                        }
271                }
272        }
[1713]273       
[1145]274        // parse view cells related options
[1522]275        if (!preprocessor->PrepareViewCells())
276        {
[1926]277                cerr << "error: view cells could not be loaded" << endl;
278
[1522]279                Cleanup();
280                exit(1);
281        }
[1999]282
[2048]283        string viewCellPointsFile;
[2017]284
[2048]285        if (strstr(filename.c_str(), ".obj"))
286                viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
287        else if (strstr(filename.c_str(), ".dat"))
288                viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
289        else if (strstr(filename.c_str(), ".x3d"))
290                viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
[2023]291
[2048]292        bool importRandomViewCells;
293        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
294                                                                                           importRandomViewCells);
295
296        if (importRandomViewCells)
297        {
298                cout << "importing random view cells" << endl;
299                preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellPointsFile);
300                cout << "finished" << endl;
301        }
302
[1968]303        bool useHwGlobalLines;
304        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
305                                                                                           useHwGlobalLines);
306
307        if (useHwGlobalLines)
308                preprocessor->PrepareHwGlobalLines();
309
[1239]310        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1926]311        PreprocessorThread *pt = NULL;
[1387]312
[1940]313#ifdef TRY_GLOBAL_LINES
[1968]314       
[1969]315        preprocessor->PrepareHwGlobalLines();
[1940]316        globalLinesRenderer->Run();
317
318#else
319
[1926]320#ifdef USE_QT
321       
[1715]322        // create a qt application first (must be created before any opengl widget ...)
323        QApplication *app = new QApplication(argc, NULL);
[1926]324
[1387]325        pt = new QtPreprocessorThread(preprocessor);
[1926]326
[1387]327#else
[1926]328
329        //#if USE_THREADS
330        //      pt = new BoostPreprocessorThread(preprocessor);
331        //#else
332                // use a dummy thread
333                pt = new DummyPreprocessorThread(preprocessor);
334        //#endif
[1272]335#endif
[1387]336
[1926]337        preprocessor->SetThread(pt);
338
[1248]339        bool guiSupported = false;
[2049]340        bool useRendererBuffer = true;
341
342        int frames;
343        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
[1926]344       
[2049]345       
346#ifdef USE_QT
347
348        if (importRandomViewCells || frames)
349          {
350                QGLFormat f;
351                f.setStencil(true);
352                QGLFormat::setDefaultFormat(f);
353
354                // NOTE: render texture should be power of 2 and square
355                // renderer must be initialised
356                // $$matt
357                preprocessor->renderer =
358                  new QtGlRendererBuffer(1024, 1024,
359                                                                 preprocessor->mSceneGraph,
[2053]360                                                                 preprocessor->mViewCellsManager,
[2049]361                                                                 preprocessor->mKdTree);
362          }
363#endif
364
[1145]365        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[2049]366          {
[1926]367#ifdef USE_QT
[1241]368                cout << "using gl widget" << endl;
[1153]369                // create and run the preprocessor application in a parallel thread
[1926]370               
[1387]371                pt->InitThread();
[1926]372                pt->RunThread();
373
[1248]374                // display the render widget
[1344]375                if (!rendererWidget)
376                {
[2049]377
378                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
379                        QMessageBox::information(0, "OpenGL pbuffers",
380                                                                           "This system does not support OpenGL/pbuffers.",
381                                                                           QMessageBox::Ok);
382                          return NULL;
[1926]383                        }
[1613]384
[2049]385                  rendererWidget =
386                        new QtGlRendererWidget(preprocessor->mSceneGraph,
387                                                                   preprocessor->mViewCellsManager,
388                                                                   preprocessor->mKdTree);
389                 
390                  rendererWidget->Show();
391                 
392                  QtGlViewer *viewer =
393                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
394                 
395                  viewer->show();
396                  guiSupported = true;
[1272]397                }
[1926]398               
[2049]399               
[2048]400                bool exportRandomViewCells;
401                Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
402                                                                                                  exportRandomViewCells);
[2017]403
[2048]404                if (exportRandomViewCells)
405                {
406                        cout << "exporting random view cells" << endl;
407                        preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
408                        cout << "finished" << endl;
409                }
[2017]410
[2048]411                /*bool evaluatePixelError;
412                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
[2023]413
[2048]414                if (evaluatePixelError)
415                {
416                        cout << "evaluating pixel error" << endl;
417                        preprocessor->ComputeRenderError();
418                }*/
[2049]419               
[1785]420                qApp->exec();
[1387]421#endif
[1926]422        }
[1785]423
[1926]424        // no gui available
425        if (!guiSupported)
426        {
[1940]427                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
428                        cout << "warning: gui not supported!" << endl;
[1926]429               
430                preprocessor->mUseGlRenderer = false;
431                preprocessor->mUseGlDebugger = false;
[1005]432        }
[1696]433
[1926]434        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
435        {
436                cout << "executing main thread" << endl;
[1696]437                // just call the mail method -> will be executed in the main thread
438                pt->Main();
439        }       
[1940]440#endif
[1999]441
[1415]442        // release memory
[1145]443        Cleanup();
[2017]444       
[1999]445        delete pt;
[1926]446
[1145]447        return returnCode;
[162]448}
449
Note: See TracBrowser for help on using the repository browser.