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

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