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

Revision 1633, 5.8 KB checked in by mattausch, 18 years ago (diff)

worked on gradient method for vsposp

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