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

Revision 1545, 5.5 KB checked in by mattausch, 18 years ago (diff)
Line 
1#ifdef GTP_INTERNAL
2        #define USE_QT 1
3#else
4        #define USE_QT 0
5#endif
6
7#if USE_QT
8#define USE_BOOST 0
9#else
10#define USE_BOOST 1
11#endif
12
13#ifdef UNICODE
14#undef UNICODE
15#endif
16
17#include <windows.h>
18#include <stdio.h>
19#include <crtdbg.h>
20
21#include "PreprocessorFactory.h"
22#include "Parser.h"
23#include "Environment.h"
24#include "MeshKdTree.h"
25#include "Preprocessor.h"
26
27
28#include "PreprocessorThread.h"
29#if USE_BOOST
30#include "BoostPreprocessorThread.h"
31#endif
32
33#include "ResourceManager.h"
34#include "GlRenderer.h"
35
36#if USE_QT
37#include "QtPreprocessorThread.h"
38#include "QtGlRenderer.h"
39#endif
40
41#define USE_EXE_PATH false
42
43
44using namespace GtpVisibilityPreprocessor;
45
46Preprocessor *preprocessor = NULL;
47GlRendererWidget *rendererWidget = NULL;
48
49// DLL function signature
50typedef GlRendererWidget *(*importFunction)(Preprocessor *);
51
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
66
67
68string ReplaceSuffix(string filename,
69                                         string a,
70                                         string b)
71{
72  string result = filename;
73
74  int pos = (int)filename.rfind(a, (int)filename.size() - 1);
75  if (pos == filename.size() - a.size()) {
76        result.replace(pos, a.size(), b);
77  }
78  return result;
79}
80
81int
82main(int argc, char **argv)
83{
84
85        //Now just call this function at the start of your program and if you're
86        //compiling in debug mode (F5), any leaks will be displayed in the Output
87        //window when the program shuts down. If you're not in debug mode this will
88        //be ignored. Use it as you will!
89        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
90#if 0
91  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
92  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
93  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
94#endif
95
96        int returnCode = 0;
97       
98        InitTiming();
99
100        Debug.open("debug.log");
101 
102        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
103        MeshKdTree::ParseEnvironment();
104
105       
106        char buff[128];
107        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
108        string preprocessorType(buff);
109
110        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
111        {
112                Environment::DelSingleton();
113                cerr << "Unknown preprocessor type" << endl;
114                exit(1);
115        }
116
117
118        /////////////
119        //-- load scene
120
121        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
122        string filename(buff);
123
124        if (!preprocessor->LoadScene(filename))
125        {
126                cout << "loading file " << filename << " failed" << endl;
127                Cleanup();
128                exit(1);
129        }
130
131        string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
132        char internKdTree[100];
133        Environment::GetSingleton()->GetStringValue("Preprocessor.kdTreeFilename", internKdTree);
134
135        //-- initialize external ray casters
136        if (preprocessor->InitRayCast(externKdTree, internKdTree))
137        {
138                cout << "ray casting initialized!" << endl;
139        }
140        else
141        {
142                cout << "ray casting initialization failed" << endl;
143                Cleanup();
144                exit(1);
145        }
146
147        // export kd tree?
148        bool exportKdTree;
149        Environment::GetSingleton()->GetBoolValue("Preprocessor.exportKdTree", exportKdTree);
150        if (exportKdTree)
151        {
152                const long startTime = GetTime();
153                cout << "exporting kd tree ... ";
154
155                if (!preprocessor->ExportKdTree(internKdTree))
156                {
157                        cout << " error exporting kd tree with filename " << internKdTree << endl;
158                }
159                else
160                {
161                        cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
162                }
163        }
164
165        // parse view cells related options
166        if (!preprocessor->PrepareViewCells())
167        {
168                Cleanup();
169                exit(1);
170        }
171
172        if (0)
173        {
174                preprocessor->Export(filename + "-out.x3d", true, false);
175                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
176        }
177
178        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
179        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
180        PreprocessorThread *pt;
181
182#if USE_QT
183        pt = new QtPreprocessorThread(preprocessor);
184#else
185#if USE_BOOST
186        pt = new BoostPreprocessorThread(preprocessor);
187#else
188        pt = new PreprocessorThread(preprocessor);
189#endif 
190#endif
191
192        bool guiSupported = false;
193
194        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
195          {
196                cout << "using gl widget" << endl;
197                // create and run the preprocessor application in a parallel thread
198#if USE_QT             
199#if USE_THREADS
200                pt->InitThread();
201                pt->RunThread();
202#endif
203               
204                // display the render widget
205                if (!rendererWidget)
206                {
207                  // create a qt application first (must be created before any opengl widget ...)
208                  QApplication *app = new QApplication(argc, NULL);
209                 
210                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
211                        QMessageBox::information(0, "OpenGL pbuffers",
212                                                                         "This system does not support OpenGL/pbuffers.",
213                                                                         QMessageBox::Ok);
214                        return NULL;
215                  }
216                 
217                 
218                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
219                                                                                                  preprocessor->mViewCellsManager,
220                                                                                                  preprocessor->mKdTree);
221
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.