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

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