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

Revision 2667, 10.2 KB checked in by bittner, 16 years ago (diff)

merge on nemo

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