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

Revision 1888, 8.1 KB checked in by mattausch, 18 years ago (diff)

removed possible bug in mixtedstrategy

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
[1842]39#ifndef NO_QT
[1387]40#include "QtPreprocessorThread.h"
41#include "QtGlRenderer.h"
[1832]42#include "QtGlViewer.h"
[1842]43#endif
[1272]44
[162]45#define USE_EXE_PATH false
46
[372]47
[863]48using namespace GtpVisibilityPreprocessor;
[492]49
[1888]50//Preprocessor *preprocessor = NULL;
[1151]51GlRendererWidget *rendererWidget = NULL;
[1145]52
[1151]53// DLL function signature
[1153]54typedef GlRendererWidget *(*importFunction)(Preprocessor *);
[1145]55
[1241]56
[1199]57
[1145]58void Cleanup()
59{
[1151]60        DEL_PTR(rendererWidget);
[1145]61        DEL_PTR(preprocessor);
62
63        Environment::DelSingleton();
64        MeshManager::DelSingleton();
65        MaterialManager::DelSingleton();
66}
67
68
[1626]69static string ReplaceSuffix(string filename, string a, string b)
70{
71        string result = filename;
[1145]72
[1626]73        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
74        if (pos == filename.size() - a.size()) {
75                result.replace(pos, a.size(), b);
76        }
77        return result;
78}
[1145]79
[1626]80
[1634]81static int SplitFilenames(const string str, vector<string> &filenames)
[1272]82{
[1634]83        int pos = 0;
[1633]84
[1634]85        while(1) {
86                int npos = (int)str.find(';', pos);
87               
88                if (npos < 0 || npos - pos < 1)
89                        break;
90                filenames.push_back(string(str, pos, npos - pos));
91                pos = npos + 1;
[1633]92        }
[1634]93       
94        filenames.push_back(string(str, pos, str.size() - pos));
95        return (int)filenames.size();
96}
97
98
[1695]99static string GetInternFilename(const string &filename, const string newSuffix)
[1634]100{
101        vector<string> filenames;
102        const int files = SplitFilenames(filename, filenames);
103
104        vector<string>::const_iterator sit, sit_end = filenames.end();
105        string kdFilename;
106
107        int i = 0;
108        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
[1633]109        {
[1634]110                string currentFile = *sit;
111                string strippedFilename;
[1633]112
[1634]113                if (i == 0)
114                {       
115                        strippedFilename = currentFile;
116                }
117                else
118                {
[1640]119                        char *str = StripPath(currentFile.c_str());
120                        strippedFilename = string(str);
121
[1695]122                        delete [] str;
[1634]123                }
124               
125                string suffix("_");
126
127                if (i == (int)filenames.size() - 1)
128                {
[1695]129                        suffix = newSuffix;
[1634]130                }
131
132                if (strstr(strippedFilename.c_str(), ".x3d"))
133                {
134                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
135                }
136        else if (strstr(strippedFilename.c_str(), ".dat"))
137                {
138                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
139                }
140                else if (strstr(strippedFilename.c_str(), ".obj"))
141                {
[1658]142                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
[1634]143                }
144                else
145                {
146                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
147                }
[1626]148        }
[1272]149
[1634]150        //cout << "kdfilename: " << kdFilename << endl;
151        return kdFilename;
[1272]152}
153
[1723]154#include "ViewCellsManager.h"
[162]155int
[492]156main(int argc, char **argv)
[997]157{
[991]158
[1145]159        //Now just call this function at the start of your program and if you're
160        //compiling in debug mode (F5), any leaks will be displayed in the Output
161        //window when the program shuts down. If you're not in debug mode this will
162        //be ignored. Use it as you will!
163        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
[1696]164#if 1
[1272]165  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
166  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
167  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
168#endif
[997]169
[1145]170        int returnCode = 0;
[1153]171       
[1145]172        InitTiming();
[997]173
[1145]174        Debug.open("debug.log");
[1199]175 
[1145]176        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
177        MeshKdTree::ParseEnvironment();
[1002]178
[1292]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
192        /////////////
[1145]193        //-- load scene
[492]194
[1145]195        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
196        string filename(buff);
[1695]197       
[1328]198
199        if (!preprocessor->LoadScene(filename))
200        {
201                cout << "loading file " << filename << " failed" << endl;
202                Cleanup();
203                exit(1);
204        }
205
[1626]206        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
[1695]207        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
[1418]208
[1697]209        ////////////
210        //-- initialize external ray caster
[1695]211       
[1418]212        if (preprocessor->InitRayCast(externKdTree, internKdTree))
[1328]213        {
[1887]214                cout << "ray casting initialized! here6" << endl;
[1328]215        }
[1221]216        else
217        {
218                cout << "ray casting initialization failed" << endl;
[1328]219                Cleanup();
[1221]220                exit(1);
221        }
[1344]222
[1695]223       
224        // export objects as obj
225        if (preprocessor->mExportObj)
226        {
227                if (strstr(filename.c_str(), ".obj"))
228                {
229                        cerr << "already in obj format" << endl;
[1701]230                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]231                }
232                else
233                {
234                        const string objname = GetInternFilename(filename, ".obj");
235
[1701]236                        cout << "exporting scene to " << objname << endl;
[1695]237                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
238
239                        if (success)
240                        {
241                                cout << "finished exporting obj" << endl;
242                        }
243                        else
244                        {
245                                cerr << "error exporting " << objname << endl;
246                        }
247                }
248        }
[1713]249       
[1887]250        cout<<"here34"<<endl;
[1145]251        // parse view cells related options
[1522]252        if (!preprocessor->PrepareViewCells())
253        {
254                Cleanup();
255                exit(1);
256        }
[1723]257       
[1196]258        if (0)
259        {
[1545]260                preprocessor->Export(filename + "-out.x3d", true, false);
261                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
[746]262        }
[1344]263
[1239]264        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1272]265        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
[1387]266        PreprocessorThread *pt;
[1887]267cout << "here8"<<endl;
[1387]268
[1887]269#if 0//USE_QT
[1715]270        // create a qt application first (must be created before any opengl widget ...)
271        QApplication *app = new QApplication(argc, NULL);
272       
[1387]273        pt = new QtPreprocessorThread(preprocessor);
274#else
[1579]275#if USE_THREADS
[1387]276        pt = new BoostPreprocessorThread(preprocessor);
[1457]277#else
278        pt = new PreprocessorThread(preprocessor);
[1272]279#endif
[1579]280#endif
[1387]281
[1248]282        bool guiSupported = false;
[1454]283
[1145]284        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[1387]285          {
[1241]286                cout << "using gl widget" << endl;
[1153]287                // create and run the preprocessor application in a parallel thread
[1887]288#if 0//USE_QT           
[1387]289#if USE_THREADS
290                pt->InitThread();
[1613]291                if (!preprocessor->mDelayVisibilityComputation)
292                  pt->RunThread();
[1272]293#endif
[1387]294               
[1248]295                // display the render widget
[1344]296                if (!rendererWidget)
297                {
[1387]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                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
[1887]306                                                                                                  preprocessor->mViewCellsManager,
307                                                                                                  preprocessor->mKdTree);
[1832]308                       
309                  //((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
310                  rendererWidget->Show();
[1613]311
[1887]312                  QtGlViewer *viewer = new QtGlViewer(NULL,
313                                                                                        (QtGlRendererWidget *)
314                                                                                        rendererWidget);
315                  viewer->show();
316                  guiSupported = true;
[1272]317                }
[1344]318
[1785]319                int frames;
320                Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
321                if (frames) {
322                  // NOTE: render texture should be power of 2 and square
323                  // renderer must be initialised
324                  // $$matt
325                  preprocessor->renderer =
326                        new QtGlRendererBuffer(512, 512,
[1887]327                                                                 preprocessor->mSceneGraph,
328                                                                 preprocessor->mViewCellsManager,
329                                                                 preprocessor->mKdTree);
[1785]330                  //            preprocessor->Getrenderer->makeCurrent();
331                }
332                qApp->exec();
[1387]333#endif
334          }
[1785]335
[1248]336        if (!guiSupported) {
337          preprocessor->mUseGlRenderer = false;
338          preprocessor->mUseGlDebugger = false;
[1005]339        }
[1696]340
[1248]341        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
[1696]342                // just call the mail method -> will be executed in the main thread
343                pt->Main();
344        }       
345
[1415]346        // release memory
[1145]347        Cleanup();
[1248]348       
[1145]349        return returnCode;
[162]350}
351
Note: See TracBrowser for help on using the repository browser.