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

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