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

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

multiple path support for kd

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                        strippedFilename = string(StripPath(currentFile.c_str()));
115                }
116               
117                string suffix("_");
118
119                if (i == (int)filenames.size() - 1)
120                {
121                        if (preprocessor->mLoadMeshes)
122                        {
123                                suffix = ".kdm";       
124                        }
125                        else
126                        {
127                                suffix = ".kdt";
128                        }
129                }
130
131                if (strstr(strippedFilename.c_str(), ".x3d"))
132                {
133                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
134                }
135        else if (strstr(strippedFilename.c_str(), ".dat"))
136                {
137                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
138                }
139                else if (strstr(strippedFilename.c_str(), ".obj"))
140                {
141                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
142                }
143                else
144                {
145                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
146                }
147        }
148
149        //cout << "kdfilename: " << kdFilename << endl;
150        return kdFilename;
151}
152
153
154int
155main(int argc, char **argv)
156{
157
158        //Now just call this function at the start of your program and if you're
159        //compiling in debug mode (F5), any leaks will be displayed in the Output
160        //window when the program shuts down. If you're not in debug mode this will
161        //be ignored. Use it as you will!
162        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
163#if 0
164  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
165  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
166  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
167#endif
168
169        int returnCode = 0;
170       
171        InitTiming();
172
173        Debug.open("debug.log");
174 
175        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
176        MeshKdTree::ParseEnvironment();
177
178       
179        char buff[128];
180        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
181        string preprocessorType(buff);
182
183        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
184        {
185                Environment::DelSingleton();
186                cerr << "Unknown preprocessor type" << endl;
187                exit(1);
188        }
189
190
191        /////////////
192        //-- load scene
193
194        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
195        string filename(buff);
196        const string dummyname = GetInternKdTreeName(filename);
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 = GetInternKdTreeName(filename);
207
208        //-- initialize external ray casters
209        if (preprocessor->InitRayCast(externKdTree, internKdTree))
210        {
211                cout << "ray casting initialized!" << endl;
212        }
213        else
214        {
215                cout << "ray casting initialization failed" << endl;
216                Cleanup();
217                exit(1);
218        }
219
220        // parse view cells related options
221        if (!preprocessor->PrepareViewCells())
222        {
223                Cleanup();
224                exit(1);
225        }
226
227        if (0)
228        {
229                preprocessor->Export(filename + "-out.x3d", true, false);
230                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
231        }
232
233        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
234        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
235        PreprocessorThread *pt;
236
237#if USE_QT
238        pt = new QtPreprocessorThread(preprocessor);
239#else
240#if USE_THREADS
241        pt = new BoostPreprocessorThread(preprocessor);
242#else
243        pt = new PreprocessorThread(preprocessor);
244#endif
245#endif
246
247        bool guiSupported = false;
248
249        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
250          {
251                cout << "using gl widget" << endl;
252                // create and run the preprocessor application in a parallel thread
253#if USE_QT             
254#if USE_THREADS
255                pt->InitThread();
256                if (!preprocessor->mDelayVisibilityComputation)
257                  pt->RunThread();
258#endif
259               
260                // display the render widget
261                if (!rendererWidget)
262                {
263                  // create a qt application first (must be created before any opengl widget ...)
264                  QApplication *app = new QApplication(argc, NULL);
265                 
266                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
267                        QMessageBox::information(0, "OpenGL pbuffers",
268                                                                         "This system does not support OpenGL/pbuffers.",
269                                                                         QMessageBox::Ok);
270                        return NULL;
271                  }
272                 
273                 
274                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
275                                                                                                  preprocessor->mViewCellsManager,
276                                                                                                  preprocessor->mKdTree);
277
278                  ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
279                  rendererWidget->Show();
280                  guiSupported = true;
281                }
282
283                qApp->exec();   
284#endif
285          }
286       
287        if (!guiSupported) {
288          preprocessor->mUseGlRenderer = false;
289          preprocessor->mUseGlDebugger = false;
290        }
291       
292        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
293          // just call the mail method -> will be executed in the main thread
294          pt->Main();
295        }
296       
297        // release memory
298        Cleanup();
299       
300        return returnCode;
301}
302
Note: See TracBrowser for help on using the repository browser.