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

Revision 2676, 9.8 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                preprocessor->mViewCellsManager->ImportViewCellsList(viewCellPointsFile);
260                cout << "finished" << endl;
261        }
262
263
264        bool useHwGlobalLines;
265        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
266                useHwGlobalLines);
267
268        if (useHwGlobalLines)
269                preprocessor->PrepareHwGlobalLines();
270
271        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
272        PreprocessorThread *pt = NULL;
273
274        //preprocessor->PrepareHwGlobalLines();
275        //globalLinesRenderer->Run();
276
277
278        bool guiSupported = false;
279        const bool useRendererBuffer = true;
280
281        int frames;
282        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
283
284        bool evalPixelError;
285        Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", evalPixelError);
286
287
288#ifdef USE_QT
289
290        // create a qt application first (must be created before any opengl widget ...)
291        QApplication *app = new QApplication(argc, NULL);
292        pt = new QtPreprocessorThread(preprocessor);   
293
294        if (evalPixelError && (importRandomViewCells || frames))
295        {
296                if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers())
297                {
298                        cerr << "this system does not support OpenGL/pbuffers" << endl;
299
300                        QMessageBox::information(0, "OpenGL pbuffers",
301                                "This system does not support OpenGL/pbuffers.",
302                                QMessageBox::Ok);
303
304                        return NULL;
305                }
306
307                QGLFormat f;
308                f.setStencil(true);
309                QGLFormat::setDefaultFormat(f);
310
311                // NOTE: render texture should be power of 2 and square
312                // renderer must be initialised
313                // $$matt
314                preprocessor->renderer =
315                        new QtGlRendererBuffer(1024, 1024,
316                        preprocessor->mSceneGraph,
317                        preprocessor->mViewCellsManager,
318                        preprocessor->mKdTree);
319
320        }
321
322        bool exportRandomViewCells;
323        Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
324                exportRandomViewCells);
325
326        if (exportRandomViewCells)
327        {
328                cout << "exporting random view cells" << endl;
329                preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
330                cout << "finished" << endl;
331        }
332
333        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
334        {
335                ////////
336                //-- create and run the preprocessor application in a parallel thread
337
338                cout << "using gl widget" << endl;
339
340                pt->InitThread();
341                pt->RunThread();
342
343                // display the render widget
344                if (!rendererWidget)
345                {
346                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers())
347                        {
348                                cerr << "this system does not support OpenGL/pbuffers" << endl;
349
350                                QMessageBox::information(0, "OpenGL pbuffers",
351                                        "This system does not support OpenGL/pbuffers.",
352                                        QMessageBox::Ok);
353
354                                return NULL;
355                        }
356
357                        rendererWidget =
358                                new QtGlRendererWidget(preprocessor->mSceneGraph,
359                                preprocessor->mViewCellsManager,
360                                preprocessor->mKdTree);
361
362                        rendererWidget->Show();
363
364                        // hack matt
365                        preprocessor->mRendererWidget = rendererWidget;
366                        //rendererWidget->SetWindowTitle("Global Visualization");
367
368                        if (1) // not working with vbo
369                        {
370                                cout << "starting the qt viewer" << endl;
371                                QtGlViewer *viewer =
372                                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
373                                viewer->show();
374                        }
375
376                        guiSupported = true;
377                }
378
379                qApp->exec();
380        }
381#else // USE_QT
382
383        /*#if USE_THREADS
384        pt = new BoostPreprocessorThread(preprocessor);
385        #else
386        // use a dummy thread
387        pt = new DummyPreprocessorThread(preprocessor);
388        #endif
389        */
390#endif
391
392        preprocessor->SetThread(pt);
393
394        // no gui available
395        if (!guiSupported)
396        {
397                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
398                        cout << "warning: gui not supported!" << endl;
399
400                preprocessor->mUseGlRenderer = false;
401                preprocessor->mUseGlDebugger = false;
402        }
403
404        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
405        {
406                cout << "executing main thread" << endl;
407                // just call the mail method -> will be executed in the main thread
408                pt->Main();
409        }       
410
411        // release memory
412        Cleanup();
413        delete pt;
414
415        return returnCode;
416}
417
Note: See TracBrowser for help on using the repository browser.