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

Revision 1579, 5.4 KB checked in by bittner, 18 years ago (diff)

merge

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
132        //-- initialize external ray casters
133        if (preprocessor->InitRayCast(externKdTree, internKdTree))
134        {
135                cout << "ray casting initialized!" << endl;
136        }
137        else
138        {
139                cout << "ray casting initialization failed" << endl;
140                Cleanup();
141                exit(1);
142        }
143
144        // export kd tree?
145        bool exportKdTree;
146        Environment::GetSingleton()->GetBoolValue("Preprocessor.exportKdTree", exportKdTree);
147        if (exportKdTree)
148        {
149                const long startTime = GetTime();
150                cout << "exporting kd tree ... ";
151
152                if (!preprocessor->ExportKdTree(internKdTree))
153                {
154                        cout << " error exporting kd tree with filename " << internKdTree << endl;
155                }
156                else
157                {
158                        cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
159                }
160        }
161
162        // parse view cells related options
163        if (!preprocessor->PrepareViewCells())
164        {
165                Cleanup();
166                exit(1);
167        }
168
169        if (0)
170        {
171                preprocessor->Export(filename + "-out.x3d", true, false);
172                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
173        }
174
175        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
176        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
177        PreprocessorThread *pt;
178
179#if USE_QT
180        pt = new QtPreprocessorThread(preprocessor);
181#else
182#if USE_THREADS
183        pt = new BoostPreprocessorThread(preprocessor);
184#else
185        pt = new PreprocessorThread(preprocessor);
186#endif
187#endif
188
189        bool guiSupported = false;
190
191        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
192          {
193                cout << "using gl widget" << endl;
194                // create and run the preprocessor application in a parallel thread
195#if USE_QT             
196#if USE_THREADS
197                pt->InitThread();
198                pt->RunThread();
199#endif
200               
201                // display the render widget
202                if (!rendererWidget)
203                {
204                  // create a qt application first (must be created before any opengl widget ...)
205                  QApplication *app = new QApplication(argc, NULL);
206                 
207                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
208                        QMessageBox::information(0, "OpenGL pbuffers",
209                                                                         "This system does not support OpenGL/pbuffers.",
210                                                                         QMessageBox::Ok);
211                        return NULL;
212                  }
213                 
214                 
215                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
216                                                                                                  preprocessor->mViewCellsManager,
217                                                                                                  preprocessor->mKdTree);
218
219                  rendererWidget->Show();
220                  guiSupported = true;
221                }
222
223                qApp->exec();   
224#endif
225          }
226       
227        if (!guiSupported) {
228          preprocessor->mUseGlRenderer = false;
229          preprocessor->mUseGlDebugger = false;
230        }
231       
232        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
233          // just call the mail method -> will be executed in the main thread
234          pt->Main();
235        }
236       
237        // release memory
238        Cleanup();
239       
240        return returnCode;
241}
242
Note: See TracBrowser for help on using the repository browser.