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

Revision 2115, 10.3 KB checked in by mattausch, 17 years ago (diff)

changed pvs loading: loading objects in a first pass

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