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

Revision 2677, 9.9 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
237                Cleanup();
238                exit(1);
239        }
240        else
241                cout << "view cells successfully loaded" << endl;
242
243        string viewCellPointsFile;
244
245        if (strstr(filename.c_str(), ".obj"))
246                viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
247        else if (strstr(filename.c_str(), ".dat"))
248                viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
249        else if (strstr(filename.c_str(), ".x3d"))
250                viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
251
252        bool importRandomViewCells;
253        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
254                importRandomViewCells);
255
256        if (importRandomViewCells)
257        {
258                cout << "importing random view cells" << endl;
259                if (preprocessor->mViewCellsManager->ImportViewCellsList(viewCellPointsFile))
260                        cout << "successfully loaded " << viewCellPointsFile << endl;
261                else
262                        cout << "error: file << " << viewCellPointsFile << " not found, generating random view points!" << endl;
263        }
264
265
266        bool useHwGlobalLines;
267        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
268                useHwGlobalLines);
269
270        if (useHwGlobalLines)
271                preprocessor->PrepareHwGlobalLines();
272
273        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
274        PreprocessorThread *pt = NULL;
275
276        //preprocessor->PrepareHwGlobalLines();
277        //globalLinesRenderer->Run();
278
279
280        bool guiSupported = false;
281        const bool useRendererBuffer = true;
282
283        int frames;
284        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
285
286        bool evalPixelError;
287        Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", evalPixelError);
288
289
290#ifdef USE_QT
291
292        // create a qt application first (must be created before any opengl widget ...)
293        QApplication *app = new QApplication(argc, NULL);
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                preprocessor->renderer =
317                        new QtGlRendererBuffer(1024, 1024,
318                        preprocessor->mSceneGraph,
319                        preprocessor->mViewCellsManager,
320                        preprocessor->mKdTree);
321
322        }
323
324        bool exportRandomViewCells;
325        Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
326                exportRandomViewCells);
327
328        if (exportRandomViewCells)
329        {
330                cout << "exporting random view cells" << endl;
331                preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
332                cout << "finished" << endl;
333        }
334
335        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
336        {
337                ////////
338                //-- create and run the preprocessor application in a parallel thread
339
340                cout << "using gl widget" << endl;
341
342                pt->InitThread();
343                pt->RunThread();
344
345                // display the render widget
346                if (!rendererWidget)
347                {
348                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers())
349                        {
350                                cerr << "this system does not support OpenGL/pbuffers" << endl;
351
352                                QMessageBox::information(0, "OpenGL pbuffers",
353                                        "This system does not support OpenGL/pbuffers.",
354                                        QMessageBox::Ok);
355
356                                return NULL;
357                        }
358
359                        rendererWidget =
360                                new QtGlRendererWidget(preprocessor->mSceneGraph,
361                                preprocessor->mViewCellsManager,
362                                preprocessor->mKdTree);
363
364                        rendererWidget->Show();
365
366                        // hack matt
367                        preprocessor->mRendererWidget = rendererWidget;
368                        //rendererWidget->SetWindowTitle("Global Visualization");
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                qApp->exec();
382        }
383#else // USE_QT
384
385        /*#if USE_THREADS
386        pt = new BoostPreprocessorThread(preprocessor);
387        #else
388        // use a dummy thread
389        pt = new DummyPreprocessorThread(preprocessor);
390        #endif
391        */
392#endif
393
394        preprocessor->SetThread(pt);
395
396        // no gui available
397        if (!guiSupported)
398        {
399                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
400                        cout << "warning: gui not supported!" << endl;
401
402                preprocessor->mUseGlRenderer = false;
403                preprocessor->mUseGlDebugger = false;
404        }
405
406        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
407        {
408                cout << "executing main thread" << endl;
409                // just call the mail method -> will be executed in the main thread
410                pt->Main();
411        }       
412
413        // release memory
414        Cleanup();
415        delete pt;
416
417        return returnCode;
418}
419
Note: See TracBrowser for help on using the repository browser.