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

Revision 1926, 7.9 KB checked in by mattausch, 18 years ago (diff)

worked on preprocessor with and without qt and threads

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"
[1579]30
[1926]31#include "ViewCellsManager.h"
[1694]32
[1926]33#ifdef USE_QT 
34        #include "QtPreprocessorThread.h"
35        #include "QtGlViewer.h"
36        #include "QtGlRenderer.h"
37#else
38        #if USE_THREADS
39                #include "BoostPreprocessorThread.h"
40        #endif
[1272]41#endif
42
[1001]43#include "ResourceManager.h"
[492]44#include "GlRenderer.h"
45
[1272]46
[162]47#define USE_EXE_PATH false
48
[372]49
[863]50using namespace GtpVisibilityPreprocessor;
[492]51
[1888]52//Preprocessor *preprocessor = NULL;
[1151]53GlRendererWidget *rendererWidget = NULL;
[1145]54
[1151]55// DLL function signature
[1153]56typedef GlRendererWidget *(*importFunction)(Preprocessor *);
[1145]57
[1241]58
[1199]59
[1145]60void Cleanup()
61{
[1151]62        DEL_PTR(rendererWidget);
[1145]63        DEL_PTR(preprocessor);
64
65        Environment::DelSingleton();
66        MeshManager::DelSingleton();
67        MaterialManager::DelSingleton();
68}
69
70
[1626]71static string ReplaceSuffix(string filename, string a, string b)
72{
73        string result = filename;
[1145]74
[1626]75        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
76        if (pos == filename.size() - a.size()) {
77                result.replace(pos, a.size(), b);
78        }
79        return result;
80}
[1145]81
[1626]82
[1634]83static int SplitFilenames(const string str, vector<string> &filenames)
[1272]84{
[1634]85        int pos = 0;
[1633]86
[1634]87        while(1) {
88                int npos = (int)str.find(';', pos);
89               
90                if (npos < 0 || npos - pos < 1)
91                        break;
92                filenames.push_back(string(str, pos, npos - pos));
93                pos = npos + 1;
[1633]94        }
[1634]95       
96        filenames.push_back(string(str, pos, str.size() - pos));
97        return (int)filenames.size();
98}
99
100
[1695]101static string GetInternFilename(const string &filename, const string newSuffix)
[1634]102{
103        vector<string> filenames;
104        const int files = SplitFilenames(filename, filenames);
105
106        vector<string>::const_iterator sit, sit_end = filenames.end();
107        string kdFilename;
108
109        int i = 0;
110        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
[1633]111        {
[1634]112                string currentFile = *sit;
113                string strippedFilename;
[1633]114
[1634]115                if (i == 0)
116                {       
117                        strippedFilename = currentFile;
118                }
119                else
120                {
[1640]121                        char *str = StripPath(currentFile.c_str());
122                        strippedFilename = string(str);
123
[1695]124                        delete [] str;
[1634]125                }
126               
127                string suffix("_");
128
129                if (i == (int)filenames.size() - 1)
130                {
[1695]131                        suffix = newSuffix;
[1634]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                {
[1658]144                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
[1634]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
[1926]156
[162]157int
[492]158main(int argc, char **argv)
[997]159{
[1145]160        //Now just call this function at the start of your program and if you're
161        //compiling in debug mode (F5), any leaks will be displayed in the Output
162        //window when the program shuts down. If you're not in debug mode this will
163        //be ignored. Use it as you will!
164        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
[1696]165#if 1
[1272]166  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
167  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
168  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
169#endif
[997]170
[1145]171        int returnCode = 0;
[1926]172
[1145]173        InitTiming();
[997]174
[1145]175        Debug.open("debug.log");
[1199]176 
[1145]177        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
178        MeshKdTree::ParseEnvironment();
[1002]179
[1145]180        char buff[128];
181        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
182        string preprocessorType(buff);
183
[1486]184        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]185        {
[1486]186                Environment::DelSingleton();
187                cerr << "Unknown preprocessor type" << endl;
188                exit(1);
[1328]189        }
[997]190
[1415]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        }
[1926]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        {
[1926]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        // export objects as obj
223        if (preprocessor->mExportObj)
224        {
225                if (strstr(filename.c_str(), ".obj"))
226                {
227                        cerr << "already in obj format" << endl;
[1701]228                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]229                }
230                else
231                {
232                        const string objname = GetInternFilename(filename, ".obj");
233
[1701]234                        cout << "exporting scene to " << objname << endl;
[1695]235                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
236
237                        if (success)
238                        {
239                                cout << "finished exporting obj" << endl;
240                        }
241                        else
242                        {
243                                cerr << "error exporting " << objname << endl;
244                        }
245                }
246        }
[1713]247       
[1145]248        // parse view cells related options
[1522]249        if (!preprocessor->PrepareViewCells())
250        {
[1926]251                cerr << "error: view cells could not be loaded" << endl;
252
[1522]253                Cleanup();
254                exit(1);
255        }
[1723]256       
[1239]257        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1926]258        PreprocessorThread *pt = NULL;
[1387]259
[1926]260#ifdef USE_QT
261       
[1715]262        // create a qt application first (must be created before any opengl widget ...)
263        QApplication *app = new QApplication(argc, NULL);
[1926]264
[1387]265        pt = new QtPreprocessorThread(preprocessor);
[1926]266
[1387]267#else
[1926]268
269        //#if USE_THREADS
270        //      pt = new BoostPreprocessorThread(preprocessor);
271        //#else
272                // use a dummy thread
273                pt = new DummyPreprocessorThread(preprocessor);
274        //#endif
[1272]275#endif
[1387]276
[1926]277        preprocessor->SetThread(pt);
278
[1248]279        bool guiSupported = false;
[1926]280       
[1145]281        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[1926]282        {
283#ifdef USE_QT
[1241]284                cout << "using gl widget" << endl;
[1153]285                // create and run the preprocessor application in a parallel thread
[1926]286               
[1387]287                pt->InitThread();
[1926]288                pt->RunThread();
289
[1248]290                // display the render widget
[1344]291                if (!rendererWidget)
292                {
[1926]293                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
294                                QMessageBox::information(0, "OpenGL pbuffers",
295                                        "This system does not support OpenGL/pbuffers.",
296                                        QMessageBox::Ok);
297                                return NULL;
298                        }
[1613]299
[1926]300                        rendererWidget =
301                                new QtGlRendererWidget(preprocessor->mSceneGraph,
302                                                                           preprocessor->mViewCellsManager,
303                                                                           preprocessor->mKdTree);
304
305                        rendererWidget->Show();
306
307                        QtGlViewer *viewer =
308                                new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
309
310                        viewer->show();
311                        guiSupported = true;
[1272]312                }
[1344]313
[1785]314                int frames;
315                Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
[1926]316               
317                if (frames)
318                {
319                        // NOTE: render texture should be power of 2 and square
320                        // renderer must be initialised
321                        // $$matt
322                        preprocessor->renderer =
323                                new QtGlRendererBuffer(512, 512,
324                                                                           preprocessor->mSceneGraph,
325                                                                           preprocessor->mViewCellsManager,
326                                                                           preprocessor->mKdTree);
[1785]327                }
[1926]328
[1785]329                qApp->exec();
[1387]330#endif
[1926]331        }
[1785]332
[1926]333        // no gui available
334        if (!guiSupported)
335        {
336                cout << "warning: gui not supported!" << endl;
337               
338                preprocessor->mUseGlRenderer = false;
339                preprocessor->mUseGlDebugger = false;
[1005]340        }
[1696]341
[1926]342        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
343        {
344                cout << "executing main thread" << endl;
345
[1696]346                // just call the mail method -> will be executed in the main thread
347                pt->Main();
348        }       
349
[1415]350        // release memory
[1145]351        Cleanup();
[1926]352
[1145]353        return returnCode;
[162]354}
355
Note: See TracBrowser for help on using the repository browser.