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

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