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

Revision 2543, 10.2 KB checked in by mattausch, 17 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
[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
[2452]66static string ReplaceSuffix(const string &filename, const string &a, const string &b)
[1626]67{
68        string result = filename;
[1145]69
[1626]70        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
[2452]71        if (pos == filename.size() - a.size())
72        {
[1626]73                result.replace(pos, a.size(), b);
74        }
[2452]75
[1626]76        return result;
77}
[1145]78
[1626]79
[2452]80static int SplitFilenames(const string &str, vector<string> &filenames)
[1272]81{
[1634]82        int pos = 0;
[1633]83
[1634]84        while(1) {
85                int npos = (int)str.find(';', pos);
86               
87                if (npos < 0 || npos - pos < 1)
88                        break;
89                filenames.push_back(string(str, pos, npos - pos));
90                pos = npos + 1;
[1633]91        }
[1634]92       
93        filenames.push_back(string(str, pos, str.size() - pos));
94        return (int)filenames.size();
95}
96
97
[1695]98static string GetInternFilename(const string &filename, const string newSuffix)
[1634]99{
100        vector<string> filenames;
101        const int files = SplitFilenames(filename, filenames);
102
103        vector<string>::const_iterator sit, sit_end = filenames.end();
104        string kdFilename;
105
106        int i = 0;
107        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
[1633]108        {
[1634]109                string currentFile = *sit;
110                string strippedFilename;
[1633]111
[1634]112                if (i == 0)
113                {       
114                        strippedFilename = currentFile;
115                }
116                else
117                {
[1640]118                        char *str = StripPath(currentFile.c_str());
119                        strippedFilename = string(str);
120
[1695]121                        delete [] str;
[1634]122                }
123               
124                string suffix("_");
125
126                if (i == (int)filenames.size() - 1)
127                {
[1695]128                        suffix = newSuffix;
[1634]129                }
130
131                if (strstr(strippedFilename.c_str(), ".x3d"))
132                {
133                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
134                }
135        else if (strstr(strippedFilename.c_str(), ".dat"))
136                {
137                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
138                }
139                else if (strstr(strippedFilename.c_str(), ".obj"))
140                {
[1658]141                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
[1634]142                }
143                else
144                {
145                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
146                }
[1626]147        }
[1272]148
[1634]149        //cout << "kdfilename: " << kdFilename << endl;
150        return kdFilename;
[1272]151}
152
[1926]153
[162]154int
[492]155main(int argc, char **argv)
[997]156{
[1145]157        //Now just call this function at the start of your program and if you're
158        //compiling in debug mode (F5), any leaks will be displayed in the Output
159        //window when the program shuts down. If you're not in debug mode this will
160        //be ignored. Use it as you will!
161        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
[1696]162#if 1
[1272]163  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
164  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
165  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
166#endif
[997]167
[1145]168        int returnCode = 0;
[1926]169
[1145]170        InitTiming();
[2020]171        Debug.open("debug.log");
[997]172
[1145]173        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
174        MeshKdTree::ParseEnvironment();
[1002]175
[1145]176        char buff[128];
177        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
178        string preprocessorType(buff);
179
[1486]180        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]181        {
[1486]182                Environment::DelSingleton();
183                cerr << "Unknown preprocessor type" << endl;
184                exit(1);
[1328]185        }
[997]186
[2023]187
[1145]188        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
189        string filename(buff);
[1695]190       
[2003]191        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
[2049]192        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
193                                                                                                  ".kdm" : ".kdt");
[1328]194
[2003]195        if (preprocessor->InitRayCast(externKdTree, internKdTree))
196        {
197                cout << "ray casting initialized!" << endl;
198        }
199        else
200        {
201                cout << "ray casting initialization failed!" << endl;
202                Cleanup();
203                exit(1);
204        }
205
[2176]206        //Debug << "using pvs type " << PVS_TYPE << endl;
[2003]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        }
[1926]217       
[1697]218        ////////////
219        //-- initialize external ray caster
[1999]220
[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        }
[1344]231
[1695]232        // export objects as obj
233        if (preprocessor->mExportObj)
234        {
235                if (strstr(filename.c_str(), ".obj"))
236                {
237                        cerr << "already in obj format" << endl;
[1701]238                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]239                }
240                else
241                {
[2049]242                 
[1695]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
[2049]248                       
[1695]249                        if (success)
250                                cout << "finished exporting obj" << endl;
251                        else
252                                cerr << "error exporting " << objname << endl;
253                }
254        }
[1713]255       
[1145]256        // parse view cells related options
[1522]257        if (!preprocessor->PrepareViewCells())
258        {
[1926]259                cerr << "error: view cells could not be loaded" << endl;
260
[1522]261                Cleanup();
262                exit(1);
263        }
[1999]264
[2048]265        string viewCellPointsFile;
[2017]266
[2048]267        if (strstr(filename.c_str(), ".obj"))
268                viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
269        else if (strstr(filename.c_str(), ".dat"))
270                viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
271        else if (strstr(filename.c_str(), ".x3d"))
272                viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
[2023]273
[2048]274        bool importRandomViewCells;
275        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
276                                                                                           importRandomViewCells);
277
278        if (importRandomViewCells)
279        {
280                cout << "importing random view cells" << endl;
281                preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellPointsFile);
282                cout << "finished" << endl;
283        }
284
[1968]285        bool useHwGlobalLines;
286        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
287                                                                                           useHwGlobalLines);
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;
[1387]294
[1940]295#ifdef TRY_GLOBAL_LINES
[1968]296       
[1969]297        preprocessor->PrepareHwGlobalLines();
[1940]298        globalLinesRenderer->Run();
299
300#else
301
[1926]302#ifdef USE_QT
303       
[1715]304        // create a qt application first (must be created before any opengl widget ...)
305        QApplication *app = new QApplication(argc, NULL);
[1926]306
[1387]307        pt = new QtPreprocessorThread(preprocessor);
[1926]308
[1387]309#else
[1926]310
311        //#if USE_THREADS
312        //      pt = new BoostPreprocessorThread(preprocessor);
313        //#else
314                // use a dummy thread
315                pt = new DummyPreprocessorThread(preprocessor);
316        //#endif
[1272]317#endif
[1387]318
[1926]319        preprocessor->SetThread(pt);
320
[1248]321        bool guiSupported = false;
[2049]322        bool useRendererBuffer = true;
323
324        int frames;
325        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
[1926]326       
[2049]327       
328#ifdef USE_QT
329
[2543]330        if (0 && preprocessor->mUseGlRenderer && (importRandomViewCells || frames))
331        {
[2049]332                QGLFormat f;
333                f.setStencil(true);
334                QGLFormat::setDefaultFormat(f);
335
336                // NOTE: render texture should be power of 2 and square
337                // renderer must be initialised
338                // $$matt
339                preprocessor->renderer =
[2543]340                        new QtGlRendererBuffer(1024, 1024,
341                                               preprocessor->mSceneGraph,
342                                                                   preprocessor->mViewCellsManager,
343                                                                   preprocessor->mKdTree);
344        }
[2049]345#endif
346
[2538]347          if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[2049]348          {
[1926]349#ifdef USE_QT
350
[2538]351                  ////////
352                  // create and run the preprocessor application in a parallel thread
[2049]353
[2538]354                  cout << "using gl widget" << endl;
[1613]355
[2538]356                  pt->InitThread();
357                  pt->RunThread();
[2017]358
[2538]359                  // display the render widget
360                  if (!rendererWidget)
361                  {
[2017]362
[2538]363                          if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
364                                  QMessageBox::information(0, "OpenGL pbuffers",
365                                          "This system does not support OpenGL/pbuffers.",
366                                          QMessageBox::Ok);
367                                  return NULL;
368                          }
[2023]369
[2538]370                          rendererWidget =
371                                  new QtGlRendererWidget(preprocessor->mSceneGraph,
[2543]372                                                         preprocessor->mViewCellsManager,
373                                                         preprocessor->mKdTree);
[2538]374
375                          rendererWidget->Show();
376
377                          if (0)
378                          {
379                                  QtGlViewer *viewer =
380                                          new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
381                                  viewer->show();
382                          }
383
384                          guiSupported = true;
385                  }
386
387
388                  bool exportRandomViewCells;
389                  Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
390                          exportRandomViewCells);
391
392                  if (exportRandomViewCells)
393                  {
394                          cout << "exporting random view cells" << endl;
395                          preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
396                          cout << "finished" << endl;
397                  }
398
399                  /*bool evaluatePixelError;
400                  Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
401
402                  if (evaluatePixelError)
403                  {
404                  cout << "evaluating pixel error" << endl;
405                  preprocessor->ComputeRenderError();
406                  }*/
407
408                  qApp->exec();
[1387]409#endif
[2538]410          }
[1785]411
[2538]412          // no gui available
413          if (!guiSupported)
414          {
415                  if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
416                          cout << "warning: gui not supported!" << endl;
[1696]417
[2538]418                  preprocessor->mUseGlRenderer = false;
419                  preprocessor->mUseGlDebugger = false;
420          }
421
422          if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
423          {
424                  cout << "executing main thread" << endl;
425                  // just call the mail method -> will be executed in the main thread
426                  pt->Main();
427          }     
[1940]428#endif
[1999]429
[2538]430          // release memory
431          Cleanup();
432          delete pt;
433
434          return returnCode;
[162]435}
436
Note: See TracBrowser for help on using the repository browser.