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

Revision 1613, 5.6 KB checked in by bittner, 18 years ago (diff)

kd-tree hack active

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