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

Revision 2538, 10.4 KB checked in by mattausch, 17 years ago (diff)
Line 
1#define USE_THREADS 0
2
3#ifdef UNICODE
4#undef UNICODE
5#endif
6
7#define NOMINMAX
8#include <windows.h>
9#include <stdio.h>
10#include <crtdbg.h>
11
12#include "PreprocessorFactory.h"
13#include "Parser.h"
14#include "Environment.h"
15#include "MeshKdTree.h"
16#include "Preprocessor.h"
17#include "common.h"
18#include "PreprocessorThread.h"
19#include "ObjExporter.h"
20#include "SceneGraph.h"
21#include "GlobalLinesRenderer.h"
22
23
24#include "ViewCellsManager.h"
25
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
34#endif
35
36#include "ResourceManager.h"
37#include "GlRenderer.h"
38
39
40#define USE_EXE_PATH false
41
42
43using namespace GtpVisibilityPreprocessor;
44
45//Preprocessor *preprocessor = NULL;
46GlRendererWidget *rendererWidget = NULL;
47//GlobalLinesRenderer *globalLinesRenderer = NULL;
48
49
50// DLL function signature
51typedef GlRendererWidget *(*importFunction)(Preprocessor *);
52
53
54
55void Cleanup()
56{
57        DEL_PTR(rendererWidget);
58        DEL_PTR(preprocessor);
59
60        Environment::DelSingleton();
61        MeshManager::DelSingleton();
62        MaterialManager::DelSingleton();
63}
64
65
66static string ReplaceSuffix(const string &filename, const string &a, const string &b)
67{
68        string result = filename;
69
70        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
71        if (pos == filename.size() - a.size())
72        {
73                result.replace(pos, a.size(), b);
74        }
75
76        return result;
77}
78
79
80static int SplitFilenames(const string &str, vector<string> &filenames)
81{
82        int pos = 0;
83
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;
91        }
92       
93        filenames.push_back(string(str, pos, str.size() - pos));
94        return (int)filenames.size();
95}
96
97
98static string GetInternFilename(const string &filename, const string newSuffix)
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)
108        {
109                string currentFile = *sit;
110                string strippedFilename;
111
112                if (i == 0)
113                {       
114                        strippedFilename = currentFile;
115                }
116                else
117                {
118                        char *str = StripPath(currentFile.c_str());
119                        strippedFilename = string(str);
120
121                        delete [] str;
122                }
123               
124                string suffix("_");
125
126                if (i == (int)filenames.size() - 1)
127                {
128                        suffix = newSuffix;
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                {
141                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
142                }
143                else
144                {
145                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
146                }
147        }
148
149        //cout << "kdfilename: " << kdFilename << endl;
150        return kdFilename;
151}
152
153
154int
155main(int argc, char **argv)
156{
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() {
162#if 1
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
167
168        int returnCode = 0;
169
170        InitTiming();
171        Debug.open("debug.log");
172
173#if 0
174        cout<<"Allocating 1.5GB..."<<endl;
175
176       
177        for (int i=0; i < 1000; i++) {
178          void *p = malloc(1250000000);
179          if (p != NULL) {
180                cout<<"success"<<(i+1)*1000<<"MB"<<endl;
181          }
182          else {
183                cout<<"fail"<<endl;
184                exit(1);
185          }
186        }
187#endif
188
189        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
190        MeshKdTree::ParseEnvironment();
191
192        char buff[128];
193        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
194        string preprocessorType(buff);
195
196        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
197        {
198                Environment::DelSingleton();
199                cerr << "Unknown preprocessor type" << endl;
200                exit(1);
201        }
202
203
204        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
205        string filename(buff);
206       
207        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
208        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
209                                                                                                  ".kdm" : ".kdt");
210
211        if (preprocessor->InitRayCast(externKdTree, internKdTree))
212        {
213                cout << "ray casting initialized!" << endl;
214        }
215        else
216        {
217                cout << "ray casting initialization failed!" << endl;
218                Cleanup();
219                exit(1);
220        }
221
222        //Debug << "using pvs type " << PVS_TYPE << endl;
223
224        /////////////
225        //-- load scene
226
227        if (!preprocessor->LoadScene(filename))
228        {
229                cout << "loading file " << filename << " failed" << endl;
230                Cleanup();
231                exit(1);
232        }
233       
234        ////////////
235        //-- initialize external ray caster
236
237        if (preprocessor->LoadInternKdTree(internKdTree))
238        {
239                cout << "intern kd tree loaded!" << endl;
240        }
241        else
242        {
243                cout << "loading intern kd tree failed!" << endl;
244                Cleanup();
245                exit(1);
246        }
247
248        // export objects as obj
249        if (preprocessor->mExportObj)
250        {
251                if (strstr(filename.c_str(), ".obj"))
252                {
253                        cerr << "already in obj format" << endl;
254                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
255                }
256                else
257                {
258                 
259                        const string objname = GetInternFilename(filename, ".obj");
260
261                        cout << "exporting scene to " << objname << endl;
262                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
263
264                       
265                        if (success)
266                        {
267                                cout << "finished exporting obj" << endl;
268                        }
269                        else
270                        {
271                                cerr << "error exporting " << objname << endl;
272                        }
273                }
274        }
275       
276        // parse view cells related options
277        if (!preprocessor->PrepareViewCells())
278        {
279                cerr << "error: view cells could not be loaded" << endl;
280
281                Cleanup();
282                exit(1);
283        }
284
285        string viewCellPointsFile;
286
287        if (strstr(filename.c_str(), ".obj"))
288                viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
289        else if (strstr(filename.c_str(), ".dat"))
290                viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
291        else if (strstr(filename.c_str(), ".x3d"))
292                viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
293
294        bool importRandomViewCells;
295        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
296                                                                                           importRandomViewCells);
297
298        if (importRandomViewCells)
299        {
300                cout << "importing random view cells" << endl;
301                preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellPointsFile);
302                cout << "finished" << endl;
303        }
304
305        bool useHwGlobalLines;
306        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
307                                                                                           useHwGlobalLines);
308
309        if (useHwGlobalLines)
310                preprocessor->PrepareHwGlobalLines();
311
312        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
313        PreprocessorThread *pt = NULL;
314
315#ifdef TRY_GLOBAL_LINES
316       
317        preprocessor->PrepareHwGlobalLines();
318        globalLinesRenderer->Run();
319
320#else
321
322#ifdef USE_QT
323       
324        // create a qt application first (must be created before any opengl widget ...)
325        QApplication *app = new QApplication(argc, NULL);
326
327        pt = new QtPreprocessorThread(preprocessor);
328
329#else
330
331        //#if USE_THREADS
332        //      pt = new BoostPreprocessorThread(preprocessor);
333        //#else
334                // use a dummy thread
335                pt = new DummyPreprocessorThread(preprocessor);
336        //#endif
337#endif
338
339        preprocessor->SetThread(pt);
340
341        bool guiSupported = false;
342        bool useRendererBuffer = true;
343
344        int frames;
345        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
346       
347       
348#ifdef USE_QT
349
350        if (importRandomViewCells || frames)
351          {
352                QGLFormat f;
353                f.setStencil(true);
354                QGLFormat::setDefaultFormat(f);
355
356                // NOTE: render texture should be power of 2 and square
357                // renderer must be initialised
358                // $$matt
359                preprocessor->renderer =
360                  new QtGlRendererBuffer(1024, 1024,
361                                                                 preprocessor->mSceneGraph,
362                                                                 preprocessor->mViewCellsManager,
363                                                                 preprocessor->mKdTree);
364          }
365#endif
366
367          if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
368          {
369#ifdef USE_QT
370
371                  ////////
372                  // create and run the preprocessor application in a parallel thread
373
374                  cout << "using gl widget" << endl;
375
376                  pt->InitThread();
377                  pt->RunThread();
378
379                  // display the render widget
380                  if (!rendererWidget)
381                  {
382
383                          if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
384                                  QMessageBox::information(0, "OpenGL pbuffers",
385                                          "This system does not support OpenGL/pbuffers.",
386                                          QMessageBox::Ok);
387                                  return NULL;
388                          }
389
390                          rendererWidget =
391                                  new QtGlRendererWidget(preprocessor->mSceneGraph,
392                                  preprocessor->mViewCellsManager,
393                                  preprocessor->mKdTree);
394
395                          rendererWidget->Show();
396
397                          if (0)
398                          {
399                                  QtGlViewer *viewer =
400                                          new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
401                                  viewer->show();
402                          }
403
404                          guiSupported = true;
405                  }
406
407
408                  bool exportRandomViewCells;
409                  Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
410                          exportRandomViewCells);
411
412                  if (exportRandomViewCells)
413                  {
414                          cout << "exporting random view cells" << endl;
415                          preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
416                          cout << "finished" << endl;
417                  }
418
419                  /*bool evaluatePixelError;
420                  Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
421
422                  if (evaluatePixelError)
423                  {
424                  cout << "evaluating pixel error" << endl;
425                  preprocessor->ComputeRenderError();
426                  }*/
427
428                  qApp->exec();
429#endif
430          }
431
432          // no gui available
433          if (!guiSupported)
434          {
435                  if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
436                          cout << "warning: gui not supported!" << endl;
437
438                  preprocessor->mUseGlRenderer = false;
439                  preprocessor->mUseGlDebugger = false;
440          }
441
442          if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
443          {
444                  cout << "executing main thread" << endl;
445                  // just call the mail method -> will be executed in the main thread
446                  pt->Main();
447          }     
448#endif
449
450          // release memory
451          Cleanup();
452
453          delete pt;
454
455          return returnCode;
456}
457
Note: See TracBrowser for help on using the repository browser.