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

Revision 2575, 10.8 KB checked in by bittner, 17 years ago (diff)

big merge: preparation for havran ray caster, check if everything works

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        //Now just call this function at the start of your program and if you're
186        //compiling in debug mode (F5), any leaks will be displayed in the Output
187        //window when the program shuts down. If you're not in debug mode this will
188        //be ignored. Use it as you will!
189        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
190        _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
191        _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
192        _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
193
194        InitTiming();
195        Debug.open("debug.log");
196
197        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
198        MeshKdTree::ParseEnvironment();
199
200        char buff[128];
201        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
202        string preprocessorType(buff);
203
204        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
205        {
206                Environment::DelSingleton();
207                cerr << "Unknown preprocessor type" << endl;
208                exit(1);
209        }
210
211        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
212        string filename(buff);
213       
214        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
215        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
216                                                                                                  ".kdm" : ".kdt");
217
218        if (preprocessor->InitRayCast(externKdTree, internKdTree))
219        {
220                cout << "ray casting initialized!" << endl;
221        }
222        else
223        {
224                cout << "ray casting initialization failed!" << endl;
225                Cleanup();
226                exit(1);
227        }
228
229        //Debug << "using pvs type " << PVS_TYPE << endl;
230
231        /////////////
232        //-- load scene
233
234        if (!preprocessor->LoadScene(filename))
235        {
236                cout << "loading file " << filename << " failed" << endl;
237                Cleanup();
238                exit(1);
239        }
240       
241
242        ////////////
243        //-- initialize external ray caster
244
245        if (preprocessor->LoadInternKdTree(internKdTree))
246        {
247                cout << "intern kd tree loaded!" << endl;
248        }
249        else
250        {
251                cout << "loading intern kd tree failed!" << endl;
252                Cleanup();
253                exit(1);
254        }
255
256        // export objects as obj
257        if (preprocessor->mExportObj)
258        {
259                if (strstr(filename.c_str(), ".obj"))
260                {
261                        cerr << "already in obj format" << endl;
262                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
263                }
264                else
265                {
266                 
267                        const string objname = GetInternFilename(filename, ".obj");
268
269                        cout << "exporting scene to " << objname << endl;
270                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
271
272                       
273                        if (success)
274                                cout << "finished exporting obj" << endl;
275                        else
276                                cerr << "error exporting " << objname << endl;
277                }
278        }
279       
280        // parse view cells related options
281        if (!preprocessor->PrepareViewCells())
282        {
283                cerr << "error: view cells could not be loaded" << endl;
284
285                Cleanup();
286                exit(1);
287        }
288
289        string viewCellPointsFile;
290
291        if (strstr(filename.c_str(), ".obj"))
292                viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
293        else if (strstr(filename.c_str(), ".dat"))
294                viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
295        else if (strstr(filename.c_str(), ".x3d"))
296                viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
297
298        bool importRandomViewCells;
299        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
300                                                                                           importRandomViewCells);
301
302        if (importRandomViewCells)
303        {
304                cout << "importing random view cells" << endl;
305                preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellPointsFile);
306                cout << "finished" << endl;
307        }
308
309        bool useHwGlobalLines;
310        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
311                                                                                           useHwGlobalLines);
312
313        if (useHwGlobalLines)
314                preprocessor->PrepareHwGlobalLines();
315
316        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
317        PreprocessorThread *pt = NULL;
318       
319        //preprocessor->PrepareHwGlobalLines();
320        //globalLinesRenderer->Run();
321
322
323        bool guiSupported = false;
324        const bool useRendererBuffer = true;
325
326        int frames;
327        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
328
329#ifdef USE_QT
330
331        // create a qt application first (must be created before any opengl widget ...)
332        QApplication *app = new QApplication(argc, NULL);
333        pt = new QtPreprocessorThread(preprocessor);   
334       
335        if (0 && preprocessor->mUseGlRenderer && (importRandomViewCells || frames))
336        {
337                QGLFormat f;
338                f.setStencil(true);
339                QGLFormat::setDefaultFormat(f);
340
341                // NOTE: render texture should be power of 2 and square
342                // renderer must be initialised
343                // $$matt
344                preprocessor->renderer =
345                        new QtGlRendererBuffer(1024, 1024,
346                        preprocessor->mSceneGraph,
347                        preprocessor->mViewCellsManager,
348                        preprocessor->mKdTree);
349        }
350
351        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
352        {
353                ////////
354                //-- create and run the preprocessor application in a parallel thread
355
356                cout << "using gl widget" << endl;
357
358                pt->InitThread();
359                pt->RunThread();
360
361                // display the render widget
362                if (!rendererWidget)
363                {
364                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
365                                cout << "damn" << endl;
366                                QMessageBox::information(0, "OpenGL pbuffers",
367                                        "This system does not support OpenGL/pbuffers.",
368                                        QMessageBox::Ok);
369                                return NULL;
370                        }
371
372                        rendererWidget =
373                                new QtGlRendererWidget(preprocessor->mSceneGraph,
374                                preprocessor->mViewCellsManager,
375                                preprocessor->mKdTree);
376
377                        rendererWidget->Show();
378
379                        if (1) // not working with vbo
380                        {
381                                cout << "starting the qt viewer" << endl;
382                                QtGlViewer *viewer =
383                                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
384                                viewer->show();
385                        }
386
387                        guiSupported = true;
388                }
389
390                bool exportRandomViewCells;
391                Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
392                        exportRandomViewCells);
393
394                if (exportRandomViewCells)
395                {
396                        cout << "exporting random view cells" << endl;
397                        preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
398                        cout << "finished" << endl;
399                }
400
401                /*bool evaluatePixelError;
402                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
403
404                if (evaluatePixelError)
405                {
406                cout << "evaluating pixel error" << endl;
407                preprocessor->ComputeRenderError();
408                }*/
409
410                qApp->exec();
411        }
412#else // USE_QUT
413
414        //#if USE_THREADS
415        //      pt = new BoostPreprocessorThread(preprocessor);
416        //#else
417                // use a dummy thread
418                pt = new DummyPreprocessorThread(preprocessor);
419        //#endif
420#endif
421
422        preprocessor->SetThread(pt);
423
424        // no gui available
425        if (!guiSupported)
426        {
427                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
428                        cout << "warning: gui not supported!" << endl;
429
430                preprocessor->mUseGlRenderer = false;
431                preprocessor->mUseGlDebugger = false;
432        }
433
434        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
435        {
436                cout << "executing main thread" << endl;
437                // just call the mail method -> will be executed in the main thread
438                pt->Main();
439        }       
440
441        // release memory
442        Cleanup();
443        delete pt;
444
445        return returnCode;
446}
447
Note: See TracBrowser for help on using the repository browser.