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

Revision 1926, 7.9 KB checked in by mattausch, 18 years ago (diff)

worked on preprocessor with and without qt and threads

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
31#include "ViewCellsManager.h"
32
33#ifdef USE_QT 
34        #include "QtPreprocessorThread.h"
35        #include "QtGlViewer.h"
36        #include "QtGlRenderer.h"
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
55// DLL function signature
56typedef GlRendererWidget *(*importFunction)(Preprocessor *);
57
58
59
60void Cleanup()
61{
62        DEL_PTR(rendererWidget);
63        DEL_PTR(preprocessor);
64
65        Environment::DelSingleton();
66        MeshManager::DelSingleton();
67        MaterialManager::DelSingleton();
68}
69
70
71static string ReplaceSuffix(string filename, string a, string b)
72{
73        string result = filename;
74
75        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
76        if (pos == filename.size() - a.size()) {
77                result.replace(pos, a.size(), b);
78        }
79        return result;
80}
81
82
83static int SplitFilenames(const string str, vector<string> &filenames)
84{
85        int pos = 0;
86
87        while(1) {
88                int npos = (int)str.find(';', pos);
89               
90                if (npos < 0 || npos - pos < 1)
91                        break;
92                filenames.push_back(string(str, pos, npos - pos));
93                pos = npos + 1;
94        }
95       
96        filenames.push_back(string(str, pos, str.size() - pos));
97        return (int)filenames.size();
98}
99
100
101static string GetInternFilename(const string &filename, const string newSuffix)
102{
103        vector<string> filenames;
104        const int files = SplitFilenames(filename, filenames);
105
106        vector<string>::const_iterator sit, sit_end = filenames.end();
107        string kdFilename;
108
109        int i = 0;
110        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
111        {
112                string currentFile = *sit;
113                string strippedFilename;
114
115                if (i == 0)
116                {       
117                        strippedFilename = currentFile;
118                }
119                else
120                {
121                        char *str = StripPath(currentFile.c_str());
122                        strippedFilename = string(str);
123
124                        delete [] str;
125                }
126               
127                string suffix("_");
128
129                if (i == (int)filenames.size() - 1)
130                {
131                        suffix = newSuffix;
132                }
133
134                if (strstr(strippedFilename.c_str(), ".x3d"))
135                {
136                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
137                }
138        else if (strstr(strippedFilename.c_str(), ".dat"))
139                {
140                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
141                }
142                else if (strstr(strippedFilename.c_str(), ".obj"))
143                {
144                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
145                }
146                else
147                {
148                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
149                }
150        }
151
152        //cout << "kdfilename: " << kdFilename << endl;
153        return kdFilename;
154}
155
156
157int
158main(int argc, char **argv)
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#if 1
166  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
167  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
168  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
169#endif
170
171        int returnCode = 0;
172
173        InitTiming();
174
175        Debug.open("debug.log");
176 
177        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
178        MeshKdTree::ParseEnvironment();
179
180        char buff[128];
181        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
182        string preprocessorType(buff);
183
184        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
185        {
186                Environment::DelSingleton();
187                cerr << "Unknown preprocessor type" << endl;
188                exit(1);
189        }
190
191        /////////////
192        //-- load scene
193
194        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
195        string filename(buff);
196       
197
198        if (!preprocessor->LoadScene(filename))
199        {
200                cout << "loading file " << filename << " failed" << endl;
201                Cleanup();
202                exit(1);
203        }
204       
205        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
206        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
207
208        ////////////
209        //-- initialize external ray caster
210       
211        if (preprocessor->InitRayCast(externKdTree, internKdTree))
212        {
213                cout << "ray casting initialized!" << endl;
214        }
215        else
216        {
217                cout << "ray casting initialization 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                        const string objname = GetInternFilename(filename, ".obj");
233
234                        cout << "exporting scene to " << objname << endl;
235                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
236
237                        if (success)
238                        {
239                                cout << "finished exporting obj" << endl;
240                        }
241                        else
242                        {
243                                cerr << "error exporting " << objname << endl;
244                        }
245                }
246        }
247       
248        // parse view cells related options
249        if (!preprocessor->PrepareViewCells())
250        {
251                cerr << "error: view cells could not be loaded" << endl;
252
253                Cleanup();
254                exit(1);
255        }
256       
257        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
258        PreprocessorThread *pt = NULL;
259
260#ifdef USE_QT
261       
262        // create a qt application first (must be created before any opengl widget ...)
263        QApplication *app = new QApplication(argc, NULL);
264
265        pt = new QtPreprocessorThread(preprocessor);
266
267#else
268
269        //#if USE_THREADS
270        //      pt = new BoostPreprocessorThread(preprocessor);
271        //#else
272                // use a dummy thread
273                pt = new DummyPreprocessorThread(preprocessor);
274        //#endif
275#endif
276
277        preprocessor->SetThread(pt);
278
279        bool guiSupported = false;
280       
281        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
282        {
283#ifdef USE_QT
284                cout << "using gl widget" << endl;
285                // create and run the preprocessor application in a parallel thread
286               
287                pt->InitThread();
288                pt->RunThread();
289
290                // display the render widget
291                if (!rendererWidget)
292                {
293                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
294                                QMessageBox::information(0, "OpenGL pbuffers",
295                                        "This system does not support OpenGL/pbuffers.",
296                                        QMessageBox::Ok);
297                                return NULL;
298                        }
299
300                        rendererWidget =
301                                new QtGlRendererWidget(preprocessor->mSceneGraph,
302                                                                           preprocessor->mViewCellsManager,
303                                                                           preprocessor->mKdTree);
304
305                        rendererWidget->Show();
306
307                        QtGlViewer *viewer =
308                                new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
309
310                        viewer->show();
311                        guiSupported = true;
312                }
313
314                int frames;
315                Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
316               
317                if (frames)
318                {
319                        // NOTE: render texture should be power of 2 and square
320                        // renderer must be initialised
321                        // $$matt
322                        preprocessor->renderer =
323                                new QtGlRendererBuffer(512, 512,
324                                                                           preprocessor->mSceneGraph,
325                                                                           preprocessor->mViewCellsManager,
326                                                                           preprocessor->mKdTree);
327                }
328
329                qApp->exec();
330#endif
331        }
332
333        // no gui available
334        if (!guiSupported)
335        {
336                cout << "warning: gui not supported!" << endl;
337               
338                preprocessor->mUseGlRenderer = false;
339                preprocessor->mUseGlDebugger = false;
340        }
341
342        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
343        {
344                cout << "executing main thread" << endl;
345
346                // just call the mail method -> will be executed in the main thread
347                pt->Main();
348        }       
349
350        // release memory
351        Cleanup();
352
353        return returnCode;
354}
355
Note: See TracBrowser for help on using the repository browser.