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

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

worked on vsp osp methodsd

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                {
[1640]114                        char *str = StripPath(currentFile.c_str());
115                        strippedFilename = string(str);
116
117                        delete []str;
[1634]118                }
119               
120                string suffix("_");
121
122                if (i == (int)filenames.size() - 1)
123                {
124                        if (preprocessor->mLoadMeshes)
125                        {
126                                suffix = ".kdm";       
127                        }
128                        else
129                        {
130                                suffix = ".kdt";
131                        }
132                }
133
134                if (strstr(strippedFilename.c_str(), ".x3d"))
135                {
136                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
137                }
138        else if (strstr(strippedFilename.c_str(), ".dat"))
139                {
140                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
141                }
142                else if (strstr(strippedFilename.c_str(), ".obj"))
143                {
144                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
145                }
146                else
147                {
148                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
149                }
[1626]150        }
[1272]151
[1634]152        //cout << "kdfilename: " << kdFilename << endl;
153        return kdFilename;
[1272]154}
155
[1626]156
[162]157int
[492]158main(int argc, char **argv)
[997]159{
[991]160
[1145]161        //Now just call this function at the start of your program and if you're
162        //compiling in debug mode (F5), any leaks will be displayed in the Output
163        //window when the program shuts down. If you're not in debug mode this will
164        //be ignored. Use it as you will!
165        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
[1272]166#if 0
167  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
168  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
169  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
170#endif
[997]171
[1145]172        int returnCode = 0;
[1153]173       
[1145]174        InitTiming();
[997]175
[1145]176        Debug.open("debug.log");
[1199]177 
[1145]178        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
179        MeshKdTree::ParseEnvironment();
[1002]180
[1292]181       
[1145]182        char buff[128];
183        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
184        string preprocessorType(buff);
185
[1486]186        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]187        {
[1486]188                Environment::DelSingleton();
189                cerr << "Unknown preprocessor type" << endl;
190                exit(1);
[1328]191        }
[997]192
[1415]193
194        /////////////
[1145]195        //-- load scene
[492]196
[1145]197        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
198        string filename(buff);
[1634]199        const string dummyname = GetInternKdTreeName(filename);
[1328]200
201        if (!preprocessor->LoadScene(filename))
202        {
203                cout << "loading file " << filename << " failed" << endl;
204                Cleanup();
205                exit(1);
206        }
207
[1626]208        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
209        const string internKdTree = GetInternKdTreeName(filename);
[1418]210
[1221]211        //-- initialize external ray casters
[1418]212        if (preprocessor->InitRayCast(externKdTree, internKdTree))
[1328]213        {
[1221]214                cout << "ray casting initialized!" << endl;
[1328]215        }
[1221]216        else
217        {
218                cout << "ray casting initialization failed" << endl;
[1328]219                Cleanup();
[1221]220                exit(1);
221        }
[1344]222
[1145]223        // parse view cells related options
[1522]224        if (!preprocessor->PrepareViewCells())
225        {
226                Cleanup();
227                exit(1);
228        }
[1145]229
[1196]230        if (0)
231        {
[1545]232                preprocessor->Export(filename + "-out.x3d", true, false);
233                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
[746]234        }
[1344]235
[1239]236        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1272]237        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
[1387]238        PreprocessorThread *pt;
239
240#if USE_QT
241        pt = new QtPreprocessorThread(preprocessor);
242#else
[1579]243#if USE_THREADS
[1387]244        pt = new BoostPreprocessorThread(preprocessor);
[1457]245#else
246        pt = new PreprocessorThread(preprocessor);
[1272]247#endif
[1579]248#endif
[1387]249
[1248]250        bool guiSupported = false;
[1454]251
[1145]252        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[1387]253          {
[1241]254                cout << "using gl widget" << endl;
[1153]255                // create and run the preprocessor application in a parallel thread
[1457]256#if USE_QT             
[1387]257#if USE_THREADS
258                pt->InitThread();
[1613]259                if (!preprocessor->mDelayVisibilityComputation)
260                  pt->RunThread();
[1272]261#endif
[1387]262               
[1248]263                // display the render widget
[1344]264                if (!rendererWidget)
265                {
[1387]266                  // create a qt application first (must be created before any opengl widget ...)
267                  QApplication *app = new QApplication(argc, NULL);
268                 
269                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
270                        QMessageBox::information(0, "OpenGL pbuffers",
271                                                                         "This system does not support OpenGL/pbuffers.",
272                                                                         QMessageBox::Ok);
273                        return NULL;
274                  }
275                 
276                 
277                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
278                                                                                                  preprocessor->mViewCellsManager,
279                                                                                                  preprocessor->mKdTree);
[1613]280
281                  ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
[1387]282                  rendererWidget->Show();
[1404]283                  guiSupported = true;
[1272]284                }
[1344]285
[1404]286                qApp->exec();   
[1387]287#endif
288          }
[1221]289       
[1248]290        if (!guiSupported) {
291          preprocessor->mUseGlRenderer = false;
292          preprocessor->mUseGlDebugger = false;
[1005]293        }
[1181]294       
[1248]295        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
296          // just call the mail method -> will be executed in the main thread
[1454]297          pt->Main();
[1248]298        }
299       
[1415]300        // release memory
[1145]301        Cleanup();
[1248]302       
[1145]303        return returnCode;
[162]304}
305
Note: See TracBrowser for help on using the repository browser.