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

Revision 2008, 8.5 KB checked in by bittner, 17 years ago (diff)

ray sort update

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();
[997]177
[1145]178        Debug.open("debug.log");
[1199]179 
[1145]180        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
181        MeshKdTree::ParseEnvironment();
[1002]182
[1145]183        char buff[128];
184        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
185        string preprocessorType(buff);
186
[1486]187        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]188        {
[1486]189                Environment::DelSingleton();
190                cerr << "Unknown preprocessor type" << endl;
191                exit(1);
[1328]192        }
[997]193
[1145]194        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
195        string filename(buff);
[1695]196       
[2003]197        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
198        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
[1328]199
[2003]200        if (preprocessor->InitRayCast(externKdTree, internKdTree))
201        {
202                cout << "ray casting initialized!" << endl;
203        }
204        else
205        {
206                cout << "ray casting initialization failed!" << endl;
207                Cleanup();
208                exit(1);
209        }
210
211
212        /////////////
213        //-- load scene
214
[1328]215        if (!preprocessor->LoadScene(filename))
216        {
217                cout << "loading file " << filename << " failed" << endl;
218                Cleanup();
219                exit(1);
220        }
[1926]221       
[1418]222
[1697]223        ////////////
224        //-- initialize external ray caster
[1999]225
[2003]226        if (preprocessor->LoadInternKdTree(internKdTree))
[1328]227        {
[2003]228                cout << "intern kd tree loaded!" << endl;
[1328]229        }
[1221]230        else
231        {
[2003]232                cout << "loading intern kd tree failed!" << endl;
[1328]233                Cleanup();
[1221]234                exit(1);
235        }
[1344]236
[1695]237        // export objects as obj
238        if (preprocessor->mExportObj)
239        {
240                if (strstr(filename.c_str(), ".obj"))
241                {
242                        cerr << "already in obj format" << endl;
[1701]243                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]244                }
245                else
246                {
247                        const string objname = GetInternFilename(filename, ".obj");
248
[1701]249                        cout << "exporting scene to " << objname << endl;
[1695]250                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
251
252                        if (success)
253                        {
254                                cout << "finished exporting obj" << endl;
255                        }
256                        else
257                        {
258                                cerr << "error exporting " << objname << endl;
259                        }
260                }
261        }
[1713]262       
[1145]263        // parse view cells related options
[1522]264        if (!preprocessor->PrepareViewCells())
265        {
[1926]266                cerr << "error: view cells could not be loaded" << endl;
267
[1522]268                Cleanup();
269                exit(1);
270        }
[1723]271       
[1999]272
[1968]273        bool useHwGlobalLines;
274        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
275                                                                                           useHwGlobalLines);
276
277        if (useHwGlobalLines)
278                preprocessor->PrepareHwGlobalLines();
279
[1239]280        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1926]281        PreprocessorThread *pt = NULL;
[1387]282
[1940]283#ifdef TRY_GLOBAL_LINES
[1968]284       
[1969]285        preprocessor->PrepareHwGlobalLines();
[1940]286        globalLinesRenderer->Run();
287
288#else
289
[1926]290#ifdef USE_QT
291       
[1715]292        // create a qt application first (must be created before any opengl widget ...)
293        QApplication *app = new QApplication(argc, NULL);
[1926]294
[1387]295        pt = new QtPreprocessorThread(preprocessor);
[1926]296
[1387]297#else
[1926]298
299        //#if USE_THREADS
300        //      pt = new BoostPreprocessorThread(preprocessor);
301        //#else
302                // use a dummy thread
303                pt = new DummyPreprocessorThread(preprocessor);
304        //#endif
[1272]305#endif
[1387]306
[1926]307        preprocessor->SetThread(pt);
308
[1248]309        bool guiSupported = false;
[1926]310       
[1145]311        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[1926]312        {
313#ifdef USE_QT
[1241]314                cout << "using gl widget" << endl;
[1153]315                // create and run the preprocessor application in a parallel thread
[1926]316               
[1387]317                pt->InitThread();
[1926]318                pt->RunThread();
319
[1248]320                // display the render widget
[1344]321                if (!rendererWidget)
322                {
[1926]323                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
324                                QMessageBox::information(0, "OpenGL pbuffers",
325                                        "This system does not support OpenGL/pbuffers.",
326                                        QMessageBox::Ok);
327                                return NULL;
328                        }
[1613]329
[1926]330                        rendererWidget =
331                                new QtGlRendererWidget(preprocessor->mSceneGraph,
332                                                                           preprocessor->mViewCellsManager,
333                                                                           preprocessor->mKdTree);
334
335                        rendererWidget->Show();
336
337                        QtGlViewer *viewer =
338                                new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
339
340                        viewer->show();
341                        guiSupported = true;
[1272]342                }
[1344]343
[1785]344                int frames;
345                Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
[1926]346               
347                if (frames)
348                {
349                        // NOTE: render texture should be power of 2 and square
350                        // renderer must be initialised
351                        // $$matt
352                        preprocessor->renderer =
[2008]353                                new QtGlRendererBuffer(1024, 1024,
[1926]354                                                                           preprocessor->mSceneGraph,
355                                                                           preprocessor->mViewCellsManager,
356                                                                           preprocessor->mKdTree);
[1785]357                }
[1926]358
[1785]359                qApp->exec();
[1387]360#endif
[1926]361        }
[1785]362
[1926]363        // no gui available
364        if (!guiSupported)
365        {
[1940]366                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
367                        cout << "warning: gui not supported!" << endl;
[1926]368               
369                preprocessor->mUseGlRenderer = false;
370                preprocessor->mUseGlDebugger = false;
[1005]371        }
[1696]372
[1926]373        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
374        {
375                cout << "executing main thread" << endl;
[1696]376                // just call the mail method -> will be executed in the main thread
377                pt->Main();
378        }       
[1940]379#endif
[1999]380
[1415]381        // release memory
[1145]382        Cleanup();
[1999]383        delete pt;
[1926]384
[1145]385        return returnCode;
[162]386}
387
Note: See TracBrowser for help on using the repository browser.