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

Revision 2020, 9.3 KB checked in by bittner, 17 years ago (diff)

power plant updates

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#if 0
178        Debug.open("debug.log");
179
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        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
195        MeshKdTree::ParseEnvironment();
196
197        char buff[128];
198        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
199        string preprocessorType(buff);
200
201        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
202        {
203                Environment::DelSingleton();
204                cerr << "Unknown preprocessor type" << endl;
205                exit(1);
206        }
207
208        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
209        string filename(buff);
210       
211        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
212        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
213
214        if (preprocessor->InitRayCast(externKdTree, internKdTree))
215        {
216                cout << "ray casting initialized!" << endl;
217        }
218        else
219        {
220                cout << "ray casting initialization failed!" << endl;
221                Cleanup();
222                exit(1);
223        }
224
225
226        /////////////
227        //-- load scene
228
229        if (!preprocessor->LoadScene(filename))
230        {
231                cout << "loading file " << filename << " failed" << endl;
232                Cleanup();
233                exit(1);
234        }
235       
236        ////////////
237        //-- initialize external ray caster
238
239        if (preprocessor->LoadInternKdTree(internKdTree))
240        {
241                cout << "intern kd tree loaded!" << endl;
242        }
243        else
244        {
245                cout << "loading intern kd tree failed!" << endl;
246                Cleanup();
247                exit(1);
248        }
249
250        // export objects as obj
251        if (preprocessor->mExportObj)
252        {
253                if (strstr(filename.c_str(), ".obj"))
254                {
255                        cerr << "already in obj format" << endl;
256                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
257                }
258                else
259                {
260                        const string objname = GetInternFilename(filename, ".obj");
261
262                        cout << "exporting scene to " << objname << endl;
263                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
264
265                        if (success)
266                        {
267                                cout << "finished exporting obj" << endl;
268                        }
269                        else
270                        {
271                                cerr << "error exporting " << objname << endl;
272                        }
273                }
274        }
275       
276        // parse view cells related options
277        if (!preprocessor->PrepareViewCells())
278        {
279                cerr << "error: view cells could not be loaded" << endl;
280
281                Cleanup();
282                exit(1);
283        }
284
285        const string viewCellsFile = ReplaceSuffix(filename, ".obj", ".vc");
286        const int numViewCells = 2;
287        const int numViewPoints = 2;
288
289        bool useHwGlobalLines;
290        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
291                                                                                           useHwGlobalLines);
292
293        if (useHwGlobalLines)
294                preprocessor->PrepareHwGlobalLines();
295
296        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
297        PreprocessorThread *pt = NULL;
298
299#ifdef TRY_GLOBAL_LINES
300       
301        preprocessor->PrepareHwGlobalLines();
302        globalLinesRenderer->Run();
303
304#else
305
306#ifdef USE_QT
307       
308        // create a qt application first (must be created before any opengl widget ...)
309        QApplication *app = new QApplication(argc, NULL);
310
311        pt = new QtPreprocessorThread(preprocessor);
312
313#else
314
315        //#if USE_THREADS
316        //      pt = new BoostPreprocessorThread(preprocessor);
317        //#else
318                // use a dummy thread
319                pt = new DummyPreprocessorThread(preprocessor);
320        //#endif
321#endif
322
323        preprocessor->SetThread(pt);
324
325        bool guiSupported = false;
326       
327        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
328        {
329#ifdef USE_QT
330                cout << "using gl widget" << endl;
331                // create and run the preprocessor application in a parallel thread
332               
333                pt->InitThread();
334                pt->RunThread();
335
336                // display the render widget
337                if (!rendererWidget)
338                {
339                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
340                                QMessageBox::information(0, "OpenGL pbuffers",
341                                        "This system does not support OpenGL/pbuffers.",
342                                        QMessageBox::Ok);
343                                return NULL;
344                        }
345
346                        rendererWidget =
347                                new QtGlRendererWidget(preprocessor->mSceneGraph,
348                                                                           preprocessor->mViewCellsManager,
349                                                                           preprocessor->mKdTree);
350
351                        rendererWidget->Show();
352
353                        QtGlViewer *viewer =
354                                new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
355
356                        viewer->show();
357                        guiSupported = true;
358                }
359
360                int frames;
361                Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
362               
363                if (frames)
364                {
365                        // NOTE: render texture should be power of 2 and square
366                        // renderer must be initialised
367                        // $$matt
368                        preprocessor->renderer =
369                                new QtGlRendererBuffer(1024, 1024,
370                                                                           preprocessor->mSceneGraph,
371                                                                           preprocessor->mViewCellsManager,
372                                                                           preprocessor->mKdTree);
373                }
374
375        /*      vector<ViewCellPoints *> myViewCells;
376
377                preprocessor->mViewCellsManager->GenerateRandomViewCells(myViewCells, numViewCells, numViewPoints);
378                cout << "exporting random view cells" << endl;
379                preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellsFile, myViewCells);
380
381                //preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellsFile, myViewCells);
382                CLEAR_CONTAINER(myViewCells);
383*/
384                qApp->exec();
385#endif
386        }
387
388        // no gui available
389        if (!guiSupported)
390        {
391                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
392                        cout << "warning: gui not supported!" << endl;
393               
394                preprocessor->mUseGlRenderer = false;
395                preprocessor->mUseGlDebugger = false;
396        }
397
398        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
399        {
400                cout << "executing main thread" << endl;
401                // just call the mail method -> will be executed in the main thread
402                pt->Main();
403        }       
404#endif
405
406        // release memory
407        Cleanup();
408       
409        delete pt;
410
411        return returnCode;
412}
413
Note: See TracBrowser for help on using the repository browser.