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

Revision 1785, 7.9 KB checked in by bittner, 18 years ago (diff)

merge, filter update, renderebuffer made functional

RevLine 
[1272]1#ifdef GTP_INTERNAL
[1766]2#ifndef NO_QT
[1579]3#define USE_QT 1
[1501]4#else
[1579]5#define USE_QT 0
[1272]6#endif
[1766]7#endif
[1272]8
[1579]9#define USE_THREADS 1
[1457]10
[1199]11#ifdef UNICODE
12#undef UNICODE
13#endif
14
[1151]15#include <windows.h>
16#include <stdio.h>
17#include <crtdbg.h>
18
[1486]19#include "PreprocessorFactory.h"
[162]20#include "Parser.h"
21#include "Environment.h"
[170]22#include "MeshKdTree.h"
[1486]23#include "Preprocessor.h"
[1634]24#include "common.h"
[1272]25
[1457]26#include "PreprocessorThread.h"
[1694]27#include "ObjExporter.h"
28#include "SceneGraph.h"
[1579]29
[1694]30
31
[1579]32#if !USE_QT && USE_THREADS
[1146]33#include "BoostPreprocessorThread.h"
[1272]34#endif
35
[1001]36#include "ResourceManager.h"
[492]37#include "GlRenderer.h"
38
[1272]39#if USE_QT
[1387]40#include "QtPreprocessorThread.h"
41#include "QtGlRenderer.h"
[1272]42#endif
43
[162]44#define USE_EXE_PATH false
45
[372]46
[863]47using namespace GtpVisibilityPreprocessor;
[492]48
[1145]49Preprocessor *preprocessor = NULL;
[1151]50GlRendererWidget *rendererWidget = NULL;
[1145]51
[1151]52// DLL function signature
[1153]53typedef GlRendererWidget *(*importFunction)(Preprocessor *);
[1145]54
[1241]55
[1199]56
[1145]57void Cleanup()
58{
[1151]59        DEL_PTR(rendererWidget);
[1145]60        DEL_PTR(preprocessor);
61
62        Environment::DelSingleton();
63        MeshManager::DelSingleton();
64        MaterialManager::DelSingleton();
65}
66
67
[1626]68static string ReplaceSuffix(string filename, string a, string b)
69{
70        string result = filename;
[1145]71
[1626]72        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
73        if (pos == filename.size() - a.size()) {
74                result.replace(pos, a.size(), b);
75        }
76        return result;
77}
[1145]78
[1626]79
[1634]80static int SplitFilenames(const string str, vector<string> &filenames)
[1272]81{
[1634]82        int pos = 0;
[1633]83
[1634]84        while(1) {
85                int npos = (int)str.find(';', pos);
86               
87                if (npos < 0 || npos - pos < 1)
88                        break;
89                filenames.push_back(string(str, pos, npos - pos));
90                pos = npos + 1;
[1633]91        }
[1634]92       
93        filenames.push_back(string(str, pos, str.size() - pos));
94        return (int)filenames.size();
95}
96
97
[1695]98static string GetInternFilename(const string &filename, const string newSuffix)
[1634]99{
100        vector<string> filenames;
101        const int files = SplitFilenames(filename, filenames);
102
103        vector<string>::const_iterator sit, sit_end = filenames.end();
104        string kdFilename;
105
106        int i = 0;
107        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
[1633]108        {
[1634]109                string currentFile = *sit;
110                string strippedFilename;
[1633]111
[1634]112                if (i == 0)
113                {       
114                        strippedFilename = currentFile;
115                }
116                else
117                {
[1640]118                        char *str = StripPath(currentFile.c_str());
119                        strippedFilename = string(str);
120
[1695]121                        delete [] str;
[1634]122                }
123               
124                string suffix("_");
125
126                if (i == (int)filenames.size() - 1)
127                {
[1695]128                        suffix = newSuffix;
[1634]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                {
[1658]141                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
[1634]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
[1723]153#include "ViewCellsManager.h"
[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() {
[1696]163#if 1
[1272]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);
[1695]196       
[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");
[1695]206        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
[1418]207
[1697]208        ////////////
209        //-- initialize external ray caster
[1695]210       
[1418]211        if (preprocessor->InitRayCast(externKdTree, internKdTree))
[1328]212        {
[1221]213                cout << "ray casting initialized!" << endl;
[1328]214        }
[1221]215        else
216        {
217                cout << "ray casting initialization failed" << endl;
[1328]218                Cleanup();
[1221]219                exit(1);
220        }
[1344]221
[1695]222       
223        // export objects as obj
224        if (preprocessor->mExportObj)
225        {
226                if (strstr(filename.c_str(), ".obj"))
227                {
228                        cerr << "already in obj format" << endl;
[1701]229                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]230                }
231                else
232                {
233                        const string objname = GetInternFilename(filename, ".obj");
234
[1701]235                        cout << "exporting scene to " << objname << endl;
[1695]236                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
237
238                        if (success)
239                        {
240                                cout << "finished exporting obj" << endl;
241                        }
242                        else
243                        {
244                                cerr << "error exporting " << objname << endl;
245                        }
246                }
247        }
[1713]248       
249       
[1145]250        // parse view cells related options
[1522]251        if (!preprocessor->PrepareViewCells())
252        {
253                Cleanup();
254                exit(1);
255        }
[1723]256       
[1196]257        if (0)
258        {
[1545]259                preprocessor->Export(filename + "-out.x3d", true, false);
260                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
[746]261        }
[1344]262
[1239]263        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1272]264        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
[1387]265        PreprocessorThread *pt;
266
[1715]267
[1387]268#if USE_QT
[1715]269        // create a qt application first (must be created before any opengl widget ...)
270        QApplication *app = new QApplication(argc, NULL);
271       
[1387]272        pt = new QtPreprocessorThread(preprocessor);
273#else
[1579]274#if USE_THREADS
[1387]275        pt = new BoostPreprocessorThread(preprocessor);
[1457]276#else
277        pt = new PreprocessorThread(preprocessor);
[1272]278#endif
[1579]279#endif
[1387]280
[1248]281        bool guiSupported = false;
[1454]282
[1145]283        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[1387]284          {
[1241]285                cout << "using gl widget" << endl;
[1153]286                // create and run the preprocessor application in a parallel thread
[1457]287#if USE_QT             
[1387]288#if USE_THREADS
289                pt->InitThread();
[1613]290                if (!preprocessor->mDelayVisibilityComputation)
291                  pt->RunThread();
[1272]292#endif
[1387]293               
[1248]294                // display the render widget
[1344]295                if (!rendererWidget)
296                {
[1387]297                 
298                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
299                        QMessageBox::information(0, "OpenGL pbuffers",
300                                                                         "This system does not support OpenGL/pbuffers.",
301                                                                         QMessageBox::Ok);
302                        return NULL;
303                  }
304                 
305                 
306                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
307                                                                                                  preprocessor->mViewCellsManager,
308                                                                                                  preprocessor->mKdTree);
[1613]309
310                  ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
[1387]311                  rendererWidget->Show();
[1404]312                  guiSupported = true;
[1272]313                }
[1344]314
[1785]315                int frames;
316                Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
317                if (frames) {
318                  // NOTE: render texture should be power of 2 and square
319                  // renderer must be initialised
320                  // $$matt
321                  preprocessor->renderer =
322                        new QtGlRendererBuffer(512, 512,
323                                                                   preprocessor->mSceneGraph,
324                                                                   preprocessor->mViewCellsManager,
325                                                                   preprocessor->mKdTree);
326                  //            preprocessor->Getrenderer->makeCurrent();
327                }
328                qApp->exec();
[1387]329#endif
330          }
[1785]331
[1248]332        if (!guiSupported) {
333          preprocessor->mUseGlRenderer = false;
334          preprocessor->mUseGlDebugger = false;
[1005]335        }
[1696]336
[1248]337        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
[1696]338                // just call the mail method -> will be executed in the main thread
339                pt->Main();
340        }       
341
[1415]342        // release memory
[1145]343        Cleanup();
[1248]344       
[1145]345        return returnCode;
[162]346}
347
Note: See TracBrowser for help on using the repository browser.