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

Revision 1634, 6.8 KB checked in by mattausch, 18 years ago (diff)

multiple path support for kd

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