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

Revision 2053, 10.3 KB checked in by mattausch, 17 years ago (diff)
RevLine 
[1926]1/*#ifdef GTP_INTERNAL
2#ifndef USE_QT
[1579]3#define USE_QT 1
[1501]4#else
[1579]5#define USE_QT 0
[1272]6#endif
[1766]7#endif
[1926]8*/
[1272]9
[1926]10#define USE_THREADS 0
[1457]11
[1199]12#ifdef UNICODE
13#undef UNICODE
14#endif
15
[1151]16#include <windows.h>
17#include <stdio.h>
18#include <crtdbg.h>
19
[1486]20#include "PreprocessorFactory.h"
[162]21#include "Parser.h"
22#include "Environment.h"
[170]23#include "MeshKdTree.h"
[1486]24#include "Preprocessor.h"
[1634]25#include "common.h"
[1272]26
[1457]27#include "PreprocessorThread.h"
[1694]28#include "ObjExporter.h"
29#include "SceneGraph.h"
[1949]30#include "GlobalLinesRenderer.h"
[1579]31
[1926]32#include "ViewCellsManager.h"
[1694]33
[1926]34#ifdef USE_QT 
35        #include "QtPreprocessorThread.h"
36        #include "QtGlViewer.h"
37        #include "QtGlRenderer.h"
38#else
39        #if USE_THREADS
40                #include "BoostPreprocessorThread.h"
41        #endif
[1272]42#endif
43
[1001]44#include "ResourceManager.h"
[492]45#include "GlRenderer.h"
46
[1272]47
[162]48#define USE_EXE_PATH false
49
[372]50
[863]51using namespace GtpVisibilityPreprocessor;
[492]52
[1888]53//Preprocessor *preprocessor = NULL;
[1151]54GlRendererWidget *rendererWidget = NULL;
[1940]55//GlobalLinesRenderer *globalLinesRenderer = NULL;
[1145]56
[1940]57
[1151]58// DLL function signature
[1153]59typedef GlRendererWidget *(*importFunction)(Preprocessor *);
[1145]60
[1241]61
[1199]62
[1145]63void Cleanup()
64{
[1151]65        DEL_PTR(rendererWidget);
[1145]66        DEL_PTR(preprocessor);
67
68        Environment::DelSingleton();
69        MeshManager::DelSingleton();
70        MaterialManager::DelSingleton();
71}
72
73
[1626]74static string ReplaceSuffix(string filename, string a, string b)
75{
76        string result = filename;
[1145]77
[1626]78        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
79        if (pos == filename.size() - a.size()) {
80                result.replace(pos, a.size(), b);
81        }
82        return result;
83}
[1145]84
[1626]85
[1634]86static int SplitFilenames(const string str, vector<string> &filenames)
[1272]87{
[1634]88        int pos = 0;
[1633]89
[1634]90        while(1) {
91                int npos = (int)str.find(';', pos);
92               
93                if (npos < 0 || npos - pos < 1)
94                        break;
95                filenames.push_back(string(str, pos, npos - pos));
96                pos = npos + 1;
[1633]97        }
[1634]98       
99        filenames.push_back(string(str, pos, str.size() - pos));
100        return (int)filenames.size();
101}
102
103
[1695]104static string GetInternFilename(const string &filename, const string newSuffix)
[1634]105{
106        vector<string> filenames;
107        const int files = SplitFilenames(filename, filenames);
108
109        vector<string>::const_iterator sit, sit_end = filenames.end();
110        string kdFilename;
111
112        int i = 0;
113        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
[1633]114        {
[1634]115                string currentFile = *sit;
116                string strippedFilename;
[1633]117
[1634]118                if (i == 0)
119                {       
120                        strippedFilename = currentFile;
121                }
122                else
123                {
[1640]124                        char *str = StripPath(currentFile.c_str());
125                        strippedFilename = string(str);
126
[1695]127                        delete [] str;
[1634]128                }
129               
130                string suffix("_");
131
132                if (i == (int)filenames.size() - 1)
133                {
[1695]134                        suffix = newSuffix;
[1634]135                }
136
137                if (strstr(strippedFilename.c_str(), ".x3d"))
138                {
139                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
140                }
141        else if (strstr(strippedFilename.c_str(), ".dat"))
142                {
143                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
144                }
145                else if (strstr(strippedFilename.c_str(), ".obj"))
146                {
[1658]147                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
[1634]148                }
149                else
150                {
151                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
152                }
[1626]153        }
[1272]154
[1634]155        //cout << "kdfilename: " << kdFilename << endl;
156        return kdFilename;
[1272]157}
158
[1926]159
[162]160int
[492]161main(int argc, char **argv)
[997]162{
[1145]163        //Now just call this function at the start of your program and if you're
164        //compiling in debug mode (F5), any leaks will be displayed in the Output
165        //window when the program shuts down. If you're not in debug mode this will
166        //be ignored. Use it as you will!
167        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
[1696]168#if 1
[1272]169  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
170  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
171  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
172#endif
[997]173
[1145]174        int returnCode = 0;
[1926]175
[1145]176        InitTiming();
[2020]177        Debug.open("debug.log");
[997]178
[2023]179#if 0
[2020]180        cout<<"Allocating 1.5GB..."<<endl;
181
182       
183        for (int i=0; i < 1000; i++) {
184          void *p = malloc(1250000000);
185          if (p != NULL) {
186                cout<<"success"<<(i+1)*1000<<"MB"<<endl;
187          }
188          else {
189                cout<<"fail"<<endl;
190                exit(1);
191          }
192        }
193#endif
[2023]194       
[1145]195        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
196        MeshKdTree::ParseEnvironment();
[1002]197
[1145]198        char buff[128];
199        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
200        string preprocessorType(buff);
201
[1486]202        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]203        {
[1486]204                Environment::DelSingleton();
205                cerr << "Unknown preprocessor type" << endl;
206                exit(1);
[1328]207        }
[997]208
[2023]209
[1145]210        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
211        string filename(buff);
[1695]212       
[2003]213        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
[2049]214        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
215                                                                                                  ".kdm" : ".kdt");
[1328]216
[2003]217        if (preprocessor->InitRayCast(externKdTree, internKdTree))
218        {
219                cout << "ray casting initialized!" << endl;
220        }
221        else
222        {
223                cout << "ray casting initialization failed!" << endl;
224                Cleanup();
225                exit(1);
226        }
227
228
229        /////////////
230        //-- load scene
231
[1328]232        if (!preprocessor->LoadScene(filename))
233        {
234                cout << "loading file " << filename << " failed" << endl;
235                Cleanup();
236                exit(1);
237        }
[1926]238       
[1697]239        ////////////
240        //-- initialize external ray caster
[1999]241
[2003]242        if (preprocessor->LoadInternKdTree(internKdTree))
[1328]243        {
[2003]244                cout << "intern kd tree loaded!" << endl;
[1328]245        }
[1221]246        else
247        {
[2003]248                cout << "loading intern kd tree failed!" << endl;
[1328]249                Cleanup();
[1221]250                exit(1);
251        }
[1344]252
[1695]253        // export objects as obj
254        if (preprocessor->mExportObj)
255        {
256                if (strstr(filename.c_str(), ".obj"))
257                {
258                        cerr << "already in obj format" << endl;
[1701]259                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]260                }
261                else
262                {
[2049]263                 
[1695]264                        const string objname = GetInternFilename(filename, ".obj");
265
[1701]266                        cout << "exporting scene to " << objname << endl;
[1695]267                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
268
[2049]269                       
[1695]270                        if (success)
271                        {
272                                cout << "finished exporting obj" << endl;
273                        }
274                        else
275                        {
276                                cerr << "error exporting " << objname << endl;
277                        }
278                }
279        }
[1713]280       
[1145]281        // parse view cells related options
[1522]282        if (!preprocessor->PrepareViewCells())
283        {
[1926]284                cerr << "error: view cells could not be loaded" << endl;
285
[1522]286                Cleanup();
287                exit(1);
288        }
[1999]289
[2048]290        string viewCellPointsFile;
[2017]291
[2048]292        if (strstr(filename.c_str(), ".obj"))
293                viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
294        else if (strstr(filename.c_str(), ".dat"))
295                viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
296        else if (strstr(filename.c_str(), ".x3d"))
297                viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
[2023]298
[2048]299        bool importRandomViewCells;
300        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
301                                                                                           importRandomViewCells);
302
303        if (importRandomViewCells)
304        {
305                cout << "importing random view cells" << endl;
306                preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellPointsFile);
307                cout << "finished" << endl;
308        }
309
[1968]310        bool useHwGlobalLines;
311        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
312                                                                                           useHwGlobalLines);
313
314        if (useHwGlobalLines)
315                preprocessor->PrepareHwGlobalLines();
316
[1239]317        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1926]318        PreprocessorThread *pt = NULL;
[1387]319
[1940]320#ifdef TRY_GLOBAL_LINES
[1968]321       
[1969]322        preprocessor->PrepareHwGlobalLines();
[1940]323        globalLinesRenderer->Run();
324
325#else
326
[1926]327#ifdef USE_QT
328       
[1715]329        // create a qt application first (must be created before any opengl widget ...)
330        QApplication *app = new QApplication(argc, NULL);
[1926]331
[1387]332        pt = new QtPreprocessorThread(preprocessor);
[1926]333
[1387]334#else
[1926]335
336        //#if USE_THREADS
337        //      pt = new BoostPreprocessorThread(preprocessor);
338        //#else
339                // use a dummy thread
340                pt = new DummyPreprocessorThread(preprocessor);
341        //#endif
[1272]342#endif
[1387]343
[1926]344        preprocessor->SetThread(pt);
345
[1248]346        bool guiSupported = false;
[2049]347
348        bool useRendererBuffer = true;
349
350        int frames;
351        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
[1926]352       
[2049]353       
354#ifdef USE_QT
355
356        if (importRandomViewCells || frames)
357          {
358                QGLFormat f;
359                f.setStencil(true);
360                QGLFormat::setDefaultFormat(f);
361
362                // NOTE: render texture should be power of 2 and square
363                // renderer must be initialised
364                // $$matt
365                preprocessor->renderer =
366                  new QtGlRendererBuffer(1024, 1024,
367                                                                 preprocessor->mSceneGraph,
[2053]368                                                                 preprocessor->mViewCellsManager,
[2049]369                                                                 preprocessor->mKdTree);
370          }
371#endif
372
[1145]373        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[2049]374          {
[1926]375#ifdef USE_QT
[1241]376                cout << "using gl widget" << endl;
[1153]377                // create and run the preprocessor application in a parallel thread
[1926]378               
[1387]379                pt->InitThread();
[1926]380                pt->RunThread();
381
[1248]382                // display the render widget
[1344]383                if (!rendererWidget)
384                {
[2049]385
386                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
387                        QMessageBox::information(0, "OpenGL pbuffers",
388                                                                           "This system does not support OpenGL/pbuffers.",
389                                                                           QMessageBox::Ok);
390                          return NULL;
[1926]391                        }
[1613]392
[2049]393                  rendererWidget =
394                        new QtGlRendererWidget(preprocessor->mSceneGraph,
395                                                                   preprocessor->mViewCellsManager,
396                                                                   preprocessor->mKdTree);
397                 
398                  rendererWidget->Show();
399                 
400                  QtGlViewer *viewer =
401                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
402                 
403                  viewer->show();
404                  guiSupported = true;
[1272]405                }
[1926]406               
[2049]407               
[2048]408                bool exportRandomViewCells;
409                Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
410                                                                                                  exportRandomViewCells);
[2017]411
[2048]412                if (exportRandomViewCells)
413                {
414                        cout << "exporting random view cells" << endl;
415                        preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
416                        cout << "finished" << endl;
417                }
[2017]418
[2048]419                /*bool evaluatePixelError;
420                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
[2023]421
[2048]422                if (evaluatePixelError)
423                {
424                        cout << "evaluating pixel error" << endl;
425                        preprocessor->ComputeRenderError();
426                }*/
[2049]427               
[1785]428                qApp->exec();
[1387]429#endif
[1926]430        }
[1785]431
[1926]432        // no gui available
433        if (!guiSupported)
434        {
[1940]435                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
436                        cout << "warning: gui not supported!" << endl;
[1926]437               
438                preprocessor->mUseGlRenderer = false;
439                preprocessor->mUseGlDebugger = false;
[1005]440        }
[1696]441
[1926]442        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
443        {
444                cout << "executing main thread" << endl;
[1696]445                // just call the mail method -> will be executed in the main thread
446                pt->Main();
447        }       
[1940]448#endif
[1999]449
[1415]450        // release memory
[1145]451        Cleanup();
[2017]452       
[1999]453        delete pt;
[1926]454
[1145]455        return returnCode;
[162]456}
457
Note: See TracBrowser for help on using the repository browser.