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

Revision 1694, 7.1 KB checked in by bittner, 18 years ago (diff)

obj exporter, vienna.obj + kdf added

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