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

Revision 2032, 9.4 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");
214        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
[1328]215
[2003]216        if (preprocessor->InitRayCast(externKdTree, internKdTree))
217        {
218                cout << "ray casting initialized!" << endl;
219        }
220        else
221        {
222                cout << "ray casting initialization failed!" << endl;
223                Cleanup();
224                exit(1);
225        }
226
227
228        /////////////
229        //-- load scene
230
[1328]231        if (!preprocessor->LoadScene(filename))
232        {
233                cout << "loading file " << filename << " failed" << endl;
234                Cleanup();
235                exit(1);
236        }
[1926]237       
[1697]238        ////////////
239        //-- initialize external ray caster
[1999]240
[2003]241        if (preprocessor->LoadInternKdTree(internKdTree))
[1328]242        {
[2003]243                cout << "intern kd tree loaded!" << endl;
[1328]244        }
[1221]245        else
246        {
[2003]247                cout << "loading intern kd tree failed!" << endl;
[1328]248                Cleanup();
[1221]249                exit(1);
250        }
[1344]251
[1695]252        // export objects as obj
253        if (preprocessor->mExportObj)
254        {
255                if (strstr(filename.c_str(), ".obj"))
256                {
257                        cerr << "already in obj format" << endl;
[1701]258                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]259                }
260                else
261                {
262                        const string objname = GetInternFilename(filename, ".obj");
263
[1701]264                        cout << "exporting scene to " << objname << endl;
[1695]265                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
266
267                        if (success)
268                        {
269                                cout << "finished exporting obj" << endl;
270                        }
271                        else
272                        {
273                                cerr << "error exporting " << objname << endl;
274                        }
275                }
276        }
[1713]277       
[1145]278        // parse view cells related options
[1522]279        if (!preprocessor->PrepareViewCells())
280        {
[1926]281                cerr << "error: view cells could not be loaded" << endl;
282
[1522]283                Cleanup();
284                exit(1);
285        }
[1999]286
[2023]287        string viewCellsFile = ReplaceSuffix(filename, ".obj", ".vc");
[2032]288        //viewCellsFile = ReplaceSuffix(filename, ".dat", ".vc");
[2017]289
[2023]290        const int numViewCells = 5;
291        const int numViewPoints = 20;
292
[1968]293        bool useHwGlobalLines;
294        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
295                                                                                           useHwGlobalLines);
296
297        if (useHwGlobalLines)
298                preprocessor->PrepareHwGlobalLines();
299
[1239]300        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1926]301        PreprocessorThread *pt = NULL;
[1387]302
[1940]303#ifdef TRY_GLOBAL_LINES
[1968]304       
[1969]305        preprocessor->PrepareHwGlobalLines();
[1940]306        globalLinesRenderer->Run();
307
308#else
309
[1926]310#ifdef USE_QT
311       
[1715]312        // create a qt application first (must be created before any opengl widget ...)
313        QApplication *app = new QApplication(argc, NULL);
[1926]314
[1387]315        pt = new QtPreprocessorThread(preprocessor);
[1926]316
[1387]317#else
[1926]318
319        //#if USE_THREADS
320        //      pt = new BoostPreprocessorThread(preprocessor);
321        //#else
322                // use a dummy thread
323                pt = new DummyPreprocessorThread(preprocessor);
324        //#endif
[1272]325#endif
[1387]326
[1926]327        preprocessor->SetThread(pt);
328
[1248]329        bool guiSupported = false;
[1926]330       
[1145]331        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[1926]332        {
333#ifdef USE_QT
[1241]334                cout << "using gl widget" << endl;
[1153]335                // create and run the preprocessor application in a parallel thread
[1926]336               
[1387]337                pt->InitThread();
[1926]338                pt->RunThread();
339
[1248]340                // display the render widget
[1344]341                if (!rendererWidget)
342                {
[1926]343                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
344                                QMessageBox::information(0, "OpenGL pbuffers",
345                                        "This system does not support OpenGL/pbuffers.",
346                                        QMessageBox::Ok);
347                                return NULL;
348                        }
[1613]349
[1926]350                        rendererWidget =
351                                new QtGlRendererWidget(preprocessor->mSceneGraph,
352                                                                           preprocessor->mViewCellsManager,
353                                                                           preprocessor->mKdTree);
354
355                        rendererWidget->Show();
356
357                        QtGlViewer *viewer =
358                                new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
359
360                        viewer->show();
361                        guiSupported = true;
[1272]362                }
[1344]363
[1785]364                int frames;
365                Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
[1926]366               
[2023]367                if (1)//frames)
[1926]368                {
369                        // NOTE: render texture should be power of 2 and square
370                        // renderer must be initialised
371                        // $$matt
372                        preprocessor->renderer =
[2008]373                                new QtGlRendererBuffer(1024, 1024,
[1926]374                                                                           preprocessor->mSceneGraph,
375                                                                           preprocessor->mViewCellsManager,
376                                                                           preprocessor->mKdTree);
[1785]377                }
[1926]378
[2023]379                vector<ViewCellPoints *> myViewCells;
[2017]380
381                preprocessor->mViewCellsManager->GenerateRandomViewCells(myViewCells, numViewCells, numViewPoints);
382                cout << "exporting random view cells" << endl;
383                preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellsFile, myViewCells);
384
385                //preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellsFile, myViewCells);
386                CLEAR_CONTAINER(myViewCells);
[2023]387
388                //preprocessor->renderer->EvalPvsStat();
389
[1785]390                qApp->exec();
[1387]391#endif
[1926]392        }
[1785]393
[1926]394        // no gui available
395        if (!guiSupported)
396        {
[1940]397                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
398                        cout << "warning: gui not supported!" << endl;
[1926]399               
400                preprocessor->mUseGlRenderer = false;
401                preprocessor->mUseGlDebugger = false;
[1005]402        }
[1696]403
[1926]404        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
405        {
406                cout << "executing main thread" << endl;
[1696]407                // just call the mail method -> will be executed in the main thread
408                pt->Main();
409        }       
[1940]410#endif
[1999]411
[1415]412        // release memory
[1145]413        Cleanup();
[2017]414       
[1999]415        delete pt;
[1926]416
[1145]417        return returnCode;
[162]418}
419
Note: See TracBrowser for help on using the repository browser.