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

Revision 1626, 6.1 KB checked in by mattausch, 18 years ago (diff)

added kd tree loading

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
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 string GetInternKdTreeName(string &filename)
76{
77        //Environment::GetSingleton()->GetStringValue("Preprocessor.kdTreeFilename", internKdTree);
78       
79        // hack! should take any extension
80    if (strstr(filename.c_str(), ".x3d"))
81        {
82                return ReplaceSuffix(filename, ".x3d", ".kd");
83        }
84        else if (strstr(filename.c_str(), ".dat"))
85        {
86                return ReplaceSuffix(filename, ".dat", ".kd");
87        }
88        else if (strstr(filename.c_str(), ".obj"))
89        {
90                return ReplaceSuffix(filename, ".dat", ".kd");
91        }
92
93        cerr << "Error: Currently unsupported format for kd, filename " << filename << endl;
94
95        // return empty string
96        return string();
97}
98
99
100int
101main(int argc, char **argv)
102{
103
104        //Now just call this function at the start of your program and if you're
105        //compiling in debug mode (F5), any leaks will be displayed in the Output
106        //window when the program shuts down. If you're not in debug mode this will
107        //be ignored. Use it as you will!
108        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
109#if 0
110  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
111  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
112  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
113#endif
114
115        int returnCode = 0;
116       
117        InitTiming();
118
119        Debug.open("debug.log");
120 
121        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
122        MeshKdTree::ParseEnvironment();
123
124       
125        char buff[128];
126        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
127        string preprocessorType(buff);
128
129        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
130        {
131                Environment::DelSingleton();
132                cerr << "Unknown preprocessor type" << endl;
133                exit(1);
134        }
135
136
137        /////////////
138        //-- load scene
139
140        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
141        string filename(buff);
142
143        if (!preprocessor->LoadScene(filename))
144        {
145                cout << "loading file " << filename << " failed" << endl;
146                Cleanup();
147                exit(1);
148        }
149
150        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
151        const string internKdTree = GetInternKdTreeName(filename);
152
153        //-- initialize external ray casters
154        if (preprocessor->InitRayCast(externKdTree, internKdTree))
155        {
156                cout << "ray casting initialized!" << endl;
157        }
158        else
159        {
160                cout << "ray casting initialization failed" << endl;
161                Cleanup();
162                exit(1);
163        }
164
165        // export kd tree?
166        bool exportKdTree;
167        Environment::GetSingleton()->GetBoolValue("Preprocessor.exportKdTree", exportKdTree);
168        if (exportKdTree)
169        {
170                const long startTime = GetTime();
171                cout << "exporting kd tree ... ";
172
173                if (!preprocessor->ExportKdTree(internKdTree))
174                {
175                        cout << " error exporting kd tree with filename " << internKdTree << endl;
176                }
177                else
178                {
179                        cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
180                }
181        }
182
183        // parse view cells related options
184        if (!preprocessor->PrepareViewCells())
185        {
186                Cleanup();
187                exit(1);
188        }
189
190        if (0)
191        {
192                preprocessor->Export(filename + "-out.x3d", true, false);
193                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
194        }
195
196        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
197        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
198        PreprocessorThread *pt;
199
200#if USE_QT
201        pt = new QtPreprocessorThread(preprocessor);
202#else
203#if USE_THREADS
204        pt = new BoostPreprocessorThread(preprocessor);
205#else
206        pt = new PreprocessorThread(preprocessor);
207#endif
208#endif
209
210        bool guiSupported = false;
211
212        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
213          {
214                cout << "using gl widget" << endl;
215                // create and run the preprocessor application in a parallel thread
216#if USE_QT             
217#if USE_THREADS
218                pt->InitThread();
219                if (!preprocessor->mDelayVisibilityComputation)
220                  pt->RunThread();
221#endif
222               
223                // display the render widget
224                if (!rendererWidget)
225                {
226                  // create a qt application first (must be created before any opengl widget ...)
227                  QApplication *app = new QApplication(argc, NULL);
228                 
229                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
230                        QMessageBox::information(0, "OpenGL pbuffers",
231                                                                         "This system does not support OpenGL/pbuffers.",
232                                                                         QMessageBox::Ok);
233                        return NULL;
234                  }
235                 
236                 
237                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
238                                                                                                  preprocessor->mViewCellsManager,
239                                                                                                  preprocessor->mKdTree);
240
241                  ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
242                  rendererWidget->Show();
243                  guiSupported = true;
244                }
245
246                qApp->exec();   
247#endif
248          }
249       
250        if (!guiSupported) {
251          preprocessor->mUseGlRenderer = false;
252          preprocessor->mUseGlDebugger = false;
253        }
254       
255        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
256          // just call the mail method -> will be executed in the main thread
257          pt->Main();
258        }
259       
260        // release memory
261        Cleanup();
262       
263        return returnCode;
264}
265
Note: See TracBrowser for help on using the repository browser.