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

Revision 1640, 6.8 KB checked in by mattausch, 18 years ago (diff)

worked on vsp osp methodsd

Line 
1#ifdef GTP_INTERNAL
2#define USE_QT 1
3#else
4#define USE_QT 0
5#endif
6
7#define USE_THREADS 1
8
9#ifdef UNICODE
10#undef UNICODE
11#endif
12
13#include <windows.h>
14#include <stdio.h>
15#include <crtdbg.h>
16
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
24#include "PreprocessorThread.h"
25
26#if !USE_QT && USE_THREADS
27#include "BoostPreprocessorThread.h"
28#endif
29
30#include "ResourceManager.h"
31#include "GlRenderer.h"
32
33#if USE_QT
34#include "QtPreprocessorThread.h"
35#include "QtGlRenderer.h"
36#endif
37
38#define USE_EXE_PATH false
39
40
41using namespace GtpVisibilityPreprocessor;
42
43Preprocessor *preprocessor = NULL;
44GlRendererWidget *rendererWidget = NULL;
45
46// DLL function signature
47typedef GlRendererWidget *(*importFunction)(Preprocessor *);
48
49
50
51
52void Cleanup()
53{
54        DEL_PTR(rendererWidget);
55        DEL_PTR(preprocessor);
56
57        Environment::DelSingleton();
58        MeshManager::DelSingleton();
59        MaterialManager::DelSingleton();
60}
61
62
63static string ReplaceSuffix(string filename, string a, string b)
64{
65        string result = filename;
66
67        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
68        if (pos == filename.size() - a.size()) {
69                result.replace(pos, a.size(), b);
70        }
71        return result;
72}
73
74
75static int SplitFilenames(const string str, vector<string> &filenames)
76{
77        int pos = 0;
78
79        while(1) {
80                int npos = (int)str.find(';', pos);
81               
82                if (npos < 0 || npos - pos < 1)
83                        break;
84                filenames.push_back(string(str, pos, npos - pos));
85                pos = npos + 1;
86        }
87       
88        filenames.push_back(string(str, pos, str.size() - pos));
89        return (int)filenames.size();
90}
91
92
93static string GetInternKdTreeName(const string &filename)
94{
95        vector<string> filenames;
96        const int files = SplitFilenames(filename, filenames);
97
98        vector<string>::const_iterator sit, sit_end = filenames.end();
99        string kdFilename;
100
101        int i = 0;
102        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
103        {
104                string currentFile = *sit;
105                //cout << "here6 " << currentFile << endl;
106                string strippedFilename;
107
108                if (i == 0)
109                {       
110                        strippedFilename = currentFile;
111                }
112                else
113                {
114                        char *str = StripPath(currentFile.c_str());
115                        strippedFilename = string(str);
116
117                        delete []str;
118                }
119               
120                string suffix("_");
121
122                if (i == (int)filenames.size() - 1)
123                {
124                        if (preprocessor->mLoadMeshes)
125                        {
126                                suffix = ".kdm";       
127                        }
128                        else
129                        {
130                                suffix = ".kdt";
131                        }
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, ".dat", 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
161        //Now just call this function at the start of your program and if you're
162        //compiling in debug mode (F5), any leaks will be displayed in the Output
163        //window when the program shuts down. If you're not in debug mode this will
164        //be ignored. Use it as you will!
165        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
166#if 0
167  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
168  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
169  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
170#endif
171
172        int returnCode = 0;
173       
174        InitTiming();
175
176        Debug.open("debug.log");
177 
178        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
179        MeshKdTree::ParseEnvironment();
180
181       
182        char buff[128];
183        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
184        string preprocessorType(buff);
185
186        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
187        {
188                Environment::DelSingleton();
189                cerr << "Unknown preprocessor type" << endl;
190                exit(1);
191        }
192
193
194        /////////////
195        //-- load scene
196
197        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
198        string filename(buff);
199        const string dummyname = GetInternKdTreeName(filename);
200
201        if (!preprocessor->LoadScene(filename))
202        {
203                cout << "loading file " << filename << " failed" << endl;
204                Cleanup();
205                exit(1);
206        }
207
208        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
209        const string internKdTree = GetInternKdTreeName(filename);
210
211        //-- initialize external ray casters
212        if (preprocessor->InitRayCast(externKdTree, internKdTree))
213        {
214                cout << "ray casting initialized!" << endl;
215        }
216        else
217        {
218                cout << "ray casting initialization failed" << endl;
219                Cleanup();
220                exit(1);
221        }
222
223        // parse view cells related options
224        if (!preprocessor->PrepareViewCells())
225        {
226                Cleanup();
227                exit(1);
228        }
229
230        if (0)
231        {
232                preprocessor->Export(filename + "-out.x3d", true, false);
233                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
234        }
235
236        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
237        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
238        PreprocessorThread *pt;
239
240#if USE_QT
241        pt = new QtPreprocessorThread(preprocessor);
242#else
243#if USE_THREADS
244        pt = new BoostPreprocessorThread(preprocessor);
245#else
246        pt = new PreprocessorThread(preprocessor);
247#endif
248#endif
249
250        bool guiSupported = false;
251
252        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
253          {
254                cout << "using gl widget" << endl;
255                // create and run the preprocessor application in a parallel thread
256#if USE_QT             
257#if USE_THREADS
258                pt->InitThread();
259                if (!preprocessor->mDelayVisibilityComputation)
260                  pt->RunThread();
261#endif
262               
263                // display the render widget
264                if (!rendererWidget)
265                {
266                  // create a qt application first (must be created before any opengl widget ...)
267                  QApplication *app = new QApplication(argc, NULL);
268                 
269                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
270                        QMessageBox::information(0, "OpenGL pbuffers",
271                                                                         "This system does not support OpenGL/pbuffers.",
272                                                                         QMessageBox::Ok);
273                        return NULL;
274                  }
275                 
276                 
277                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
278                                                                                                  preprocessor->mViewCellsManager,
279                                                                                                  preprocessor->mKdTree);
280
281                  ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
282                  rendererWidget->Show();
283                  guiSupported = true;
284                }
285
286                qApp->exec();   
287#endif
288          }
289       
290        if (!guiSupported) {
291          preprocessor->mUseGlRenderer = false;
292          preprocessor->mUseGlDebugger = false;
293        }
294       
295        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
296          // just call the mail method -> will be executed in the main thread
297          pt->Main();
298        }
299       
300        // release memory
301        Cleanup();
302       
303        return returnCode;
304}
305
Note: See TracBrowser for help on using the repository browser.