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

Revision 2610, 9.7 KB checked in by bittner, 16 years ago (diff)

pixel error computation revival

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