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

Revision 1695, 7.6 KB checked in by mattausch, 18 years ago (diff)
RevLine 
[1272]1#ifdef GTP_INTERNAL
[1579]2#define USE_QT 1
[1501]3#else
[1579]4#define USE_QT 0
[1272]5#endif
6
[1579]7#define USE_THREADS 1
[1457]8
[1199]9#ifdef UNICODE
10#undef UNICODE
11#endif
12
[1151]13#include <windows.h>
14#include <stdio.h>
15#include <crtdbg.h>
16
[1486]17#include "PreprocessorFactory.h"
[162]18#include "Parser.h"
19#include "Environment.h"
[170]20#include "MeshKdTree.h"
[1486]21#include "Preprocessor.h"
[1634]22#include "common.h"
[1272]23
[1457]24#include "PreprocessorThread.h"
[1694]25#include "ObjExporter.h"
26#include "SceneGraph.h"
[1579]27
[1694]28
29
[1579]30#if !USE_QT && USE_THREADS
[1146]31#include "BoostPreprocessorThread.h"
[1272]32#endif
33
[1001]34#include "ResourceManager.h"
[492]35#include "GlRenderer.h"
36
[1272]37#if USE_QT
[1387]38#include "QtPreprocessorThread.h"
39#include "QtGlRenderer.h"
[1272]40#endif
41
[162]42#define USE_EXE_PATH false
43
[372]44
[863]45using namespace GtpVisibilityPreprocessor;
[492]46
[1145]47Preprocessor *preprocessor = NULL;
[1151]48GlRendererWidget *rendererWidget = NULL;
[1145]49
[1151]50// DLL function signature
[1153]51typedef GlRendererWidget *(*importFunction)(Preprocessor *);
[1145]52
[1241]53
[1199]54
[1145]55void Cleanup()
56{
[1151]57        DEL_PTR(rendererWidget);
[1145]58        DEL_PTR(preprocessor);
59
60        Environment::DelSingleton();
61        MeshManager::DelSingleton();
62        MaterialManager::DelSingleton();
63}
64
65
[1626]66static string ReplaceSuffix(string filename, string a, string b)
67{
68        string result = filename;
[1145]69
[1626]70        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
71        if (pos == filename.size() - a.size()) {
72                result.replace(pos, a.size(), b);
73        }
74        return result;
75}
[1145]76
[1626]77
[1634]78static int SplitFilenames(const string str, vector<string> &filenames)
[1272]79{
[1634]80        int pos = 0;
[1633]81
[1634]82        while(1) {
83                int npos = (int)str.find(';', pos);
84               
85                if (npos < 0 || npos - pos < 1)
86                        break;
87                filenames.push_back(string(str, pos, npos - pos));
88                pos = npos + 1;
[1633]89        }
[1634]90       
91        filenames.push_back(string(str, pos, str.size() - pos));
92        return (int)filenames.size();
93}
94
95
[1695]96static string GetInternFilename(const string &filename, const string newSuffix)
[1634]97{
98        vector<string> filenames;
99        const int files = SplitFilenames(filename, filenames);
100
101        vector<string>::const_iterator sit, sit_end = filenames.end();
102        string kdFilename;
103
104        int i = 0;
105        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
[1633]106        {
[1634]107                string currentFile = *sit;
108                //cout << "here6 " << currentFile << endl;
109                string strippedFilename;
[1633]110
[1634]111                if (i == 0)
112                {       
113                        strippedFilename = currentFile;
114                }
115                else
116                {
[1640]117                        char *str = StripPath(currentFile.c_str());
118                        strippedFilename = string(str);
119
[1695]120                        delete [] str;
[1634]121                }
122               
123                string suffix("_");
124
125                if (i == (int)filenames.size() - 1)
126                {
[1695]127                        suffix = newSuffix;
[1634]128                }
129
130                if (strstr(strippedFilename.c_str(), ".x3d"))
131                {
132                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
133                }
134        else if (strstr(strippedFilename.c_str(), ".dat"))
135                {
136                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
137                }
138                else if (strstr(strippedFilename.c_str(), ".obj"))
139                {
[1658]140                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
[1634]141                }
142                else
143                {
144                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
145                }
[1626]146        }
[1272]147
[1634]148        //cout << "kdfilename: " << kdFilename << endl;
149        return kdFilename;
[1272]150}
151
[1626]152
[162]153int
[492]154main(int argc, char **argv)
[997]155{
[991]156
[1145]157        //Now just call this function at the start of your program and if you're
158        //compiling in debug mode (F5), any leaks will be displayed in the Output
159        //window when the program shuts down. If you're not in debug mode this will
160        //be ignored. Use it as you will!
161        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
[1272]162#if 0
163  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
164  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
165  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
166#endif
[997]167
[1145]168        int returnCode = 0;
[1153]169       
[1145]170        InitTiming();
[997]171
[1145]172        Debug.open("debug.log");
[1199]173 
[1145]174        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
175        MeshKdTree::ParseEnvironment();
[1002]176
[1292]177       
[1145]178        char buff[128];
179        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
180        string preprocessorType(buff);
181
[1486]182        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]183        {
[1486]184                Environment::DelSingleton();
185                cerr << "Unknown preprocessor type" << endl;
186                exit(1);
[1328]187        }
[997]188
[1415]189
190        /////////////
[1145]191        //-- load scene
[492]192
[1145]193        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
194        string filename(buff);
[1695]195       
[1328]196
197        if (!preprocessor->LoadScene(filename))
198        {
199                cout << "loading file " << filename << " failed" << endl;
200                Cleanup();
201                exit(1);
202        }
203
[1694]204        if (1) {
205          // export obj file
206          Exporter *exporter = new ObjExporter("scene.obj");
207          exporter->ExportScene(preprocessor->mSceneGraph->GetRoot());
208          delete exporter;
209        }
210       
211       
[1626]212        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
[1695]213        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
[1418]214
[1221]215        //-- initialize external ray casters
[1695]216       
[1418]217        if (preprocessor->InitRayCast(externKdTree, internKdTree))
[1328]218        {
[1221]219                cout << "ray casting initialized!" << endl;
[1328]220        }
[1221]221        else
222        {
223                cout << "ray casting initialization failed" << endl;
[1328]224                Cleanup();
[1221]225                exit(1);
226        }
[1344]227
[1695]228       
229        // export objects as obj
230        if (preprocessor->mExportObj)
231        {
232                if (strstr(filename.c_str(), ".obj"))
233                {
234                        cerr << "already in obj format" << endl;
235                        if (1)
236                        preprocessor->ExportObj("test.obj", preprocessor->mObjects);
237                }
238                else
239                {
240                        const string objname = GetInternFilename(filename, ".obj");
241
242                        //cout << "exporting scene to " << objname << endl;
243                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
244
245                        if (success)
246                        {
247                                cout << "finished exporting obj" << endl;
248                        }
249                        else
250                        {
251                                cerr << "error exporting " << objname << endl;
252                        }
253                }
254        }
255
[1145]256        // parse view cells related options
[1522]257        if (!preprocessor->PrepareViewCells())
258        {
259                Cleanup();
260                exit(1);
261        }
[1145]262
[1196]263        if (0)
264        {
[1545]265                preprocessor->Export(filename + "-out.x3d", true, false);
266                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
[746]267        }
[1344]268
[1239]269        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1272]270        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
[1387]271        PreprocessorThread *pt;
272
273#if USE_QT
274        pt = new QtPreprocessorThread(preprocessor);
275#else
[1579]276#if USE_THREADS
[1387]277        pt = new BoostPreprocessorThread(preprocessor);
[1457]278#else
279        pt = new PreprocessorThread(preprocessor);
[1272]280#endif
[1579]281#endif
[1387]282
[1248]283        bool guiSupported = false;
[1454]284
[1145]285        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[1387]286          {
[1241]287                cout << "using gl widget" << endl;
[1153]288                // create and run the preprocessor application in a parallel thread
[1457]289#if USE_QT             
[1387]290#if USE_THREADS
291                pt->InitThread();
[1613]292                if (!preprocessor->mDelayVisibilityComputation)
293                  pt->RunThread();
[1272]294#endif
[1387]295               
[1248]296                // display the render widget
[1344]297                if (!rendererWidget)
298                {
[1387]299                  // create a qt application first (must be created before any opengl widget ...)
300                  QApplication *app = new QApplication(argc, NULL);
301                 
302                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
303                        QMessageBox::information(0, "OpenGL pbuffers",
304                                                                         "This system does not support OpenGL/pbuffers.",
305                                                                         QMessageBox::Ok);
306                        return NULL;
307                  }
308                 
309                 
310                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
311                                                                                                  preprocessor->mViewCellsManager,
312                                                                                                  preprocessor->mKdTree);
[1613]313
314                  ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
[1387]315                  rendererWidget->Show();
[1404]316                  guiSupported = true;
[1272]317                }
[1344]318
[1404]319                qApp->exec();   
[1387]320#endif
321          }
[1221]322       
[1248]323        if (!guiSupported) {
324          preprocessor->mUseGlRenderer = false;
325          preprocessor->mUseGlDebugger = false;
[1005]326        }
[1181]327       
[1248]328        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
329          // just call the mail method -> will be executed in the main thread
[1454]330          pt->Main();
[1248]331        }
332       
[1415]333        // release memory
[1145]334        Cleanup();
[1248]335       
[1145]336        return returnCode;
[162]337}
338
Note: See TracBrowser for help on using the repository browser.