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

Revision 1626, 6.1 KB checked in by mattausch, 18 years ago (diff)

added kd tree loading

RevLine 
[1272]1#ifdef GTP_INTERNAL
[1579]2#define USE_QT 1
[1501]3#else
[1579]4#define USE_QT 0
[1272]5#endif
6
[1579]7#define USE_THREADS 1
[1457]8
[1199]9#ifdef UNICODE
10#undef UNICODE
11#endif
12
[1151]13#include <windows.h>
14#include <stdio.h>
15#include <crtdbg.h>
16
[1486]17#include "PreprocessorFactory.h"
[162]18#include "Parser.h"
19#include "Environment.h"
[170]20#include "MeshKdTree.h"
[1486]21#include "Preprocessor.h"
[1272]22
[1486]23
[1457]24#include "PreprocessorThread.h"
[1579]25
26#if !USE_QT && USE_THREADS
[1146]27#include "BoostPreprocessorThread.h"
[1272]28#endif
29
[1001]30#include "ResourceManager.h"
[492]31#include "GlRenderer.h"
32
[1272]33#if USE_QT
[1387]34#include "QtPreprocessorThread.h"
35#include "QtGlRenderer.h"
[1272]36#endif
37
[162]38#define USE_EXE_PATH false
39
[372]40
[863]41using namespace GtpVisibilityPreprocessor;
[492]42
[1145]43Preprocessor *preprocessor = NULL;
[1151]44GlRendererWidget *rendererWidget = NULL;
[1145]45
[1151]46// DLL function signature
[1153]47typedef GlRendererWidget *(*importFunction)(Preprocessor *);
[1145]48
[1241]49
[1199]50
[1241]51
[1145]52void Cleanup()
53{
[1151]54        DEL_PTR(rendererWidget);
[1145]55        DEL_PTR(preprocessor);
56
57        Environment::DelSingleton();
58        MeshManager::DelSingleton();
59        MaterialManager::DelSingleton();
60}
61
62
[1626]63static string ReplaceSuffix(string filename, string a, string b)
64{
65        string result = filename;
[1145]66
[1626]67        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
68        if (pos == filename.size() - a.size()) {
69                result.replace(pos, a.size(), b);
70        }
71        return result;
72}
[1145]73
[1626]74
75static string GetInternKdTreeName(string &filename)
[1272]76{
[1626]77        //Environment::GetSingleton()->GetStringValue("Preprocessor.kdTreeFilename", internKdTree);
78       
79        // hack! should take any extension
80    if (strstr(filename.c_str(), ".x3d"))
81        {
82                return ReplaceSuffix(filename, ".x3d", ".kd");
83        }
84        else if (strstr(filename.c_str(), ".dat"))
85        {
86                return ReplaceSuffix(filename, ".dat", ".kd");
87        }
88        else if (strstr(filename.c_str(), ".obj"))
89        {
90                return ReplaceSuffix(filename, ".dat", ".kd");
91        }
[1272]92
[1626]93        cerr << "Error: Currently unsupported format for kd, filename " << filename << endl;
94
95        // return empty string
96        return string();
[1272]97}
98
[1626]99
[162]100int
[492]101main(int argc, char **argv)
[997]102{
[991]103
[1145]104        //Now just call this function at the start of your program and if you're
105        //compiling in debug mode (F5), any leaks will be displayed in the Output
106        //window when the program shuts down. If you're not in debug mode this will
107        //be ignored. Use it as you will!
108        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
[1272]109#if 0
110  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
111  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
112  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
113#endif
[997]114
[1145]115        int returnCode = 0;
[1153]116       
[1145]117        InitTiming();
[997]118
[1145]119        Debug.open("debug.log");
[1199]120 
[1145]121        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
122        MeshKdTree::ParseEnvironment();
[1002]123
[1292]124       
[1145]125        char buff[128];
126        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
127        string preprocessorType(buff);
128
[1486]129        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]130        {
[1486]131                Environment::DelSingleton();
132                cerr << "Unknown preprocessor type" << endl;
133                exit(1);
[1328]134        }
[997]135
[1415]136
137        /////////////
[1145]138        //-- load scene
[492]139
[1145]140        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
141        string filename(buff);
[1328]142
143        if (!preprocessor->LoadScene(filename))
144        {
145                cout << "loading file " << filename << " failed" << endl;
146                Cleanup();
147                exit(1);
148        }
149
[1626]150        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
151        const string internKdTree = GetInternKdTreeName(filename);
[1418]152
[1221]153        //-- initialize external ray casters
[1418]154        if (preprocessor->InitRayCast(externKdTree, internKdTree))
[1328]155        {
[1221]156                cout << "ray casting initialized!" << endl;
[1328]157        }
[1221]158        else
159        {
160                cout << "ray casting initialization failed" << endl;
[1328]161                Cleanup();
[1221]162                exit(1);
163        }
[1344]164
[1418]165        // export kd tree?
166        bool exportKdTree;
167        Environment::GetSingleton()->GetBoolValue("Preprocessor.exportKdTree", exportKdTree);
168        if (exportKdTree)
169        {
170                const long startTime = GetTime();
171                cout << "exporting kd tree ... ";
172
173                if (!preprocessor->ExportKdTree(internKdTree))
174                {
175                        cout << " error exporting kd tree with filename " << internKdTree << endl;
176                }
177                else
178                {
179                        cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
180                }
181        }
182
[1145]183        // parse view cells related options
[1522]184        if (!preprocessor->PrepareViewCells())
185        {
186                Cleanup();
187                exit(1);
188        }
[1145]189
[1196]190        if (0)
191        {
[1545]192                preprocessor->Export(filename + "-out.x3d", true, false);
193                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
[746]194        }
[1344]195
[1239]196        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1272]197        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
[1387]198        PreprocessorThread *pt;
199
200#if USE_QT
201        pt = new QtPreprocessorThread(preprocessor);
202#else
[1579]203#if USE_THREADS
[1387]204        pt = new BoostPreprocessorThread(preprocessor);
[1457]205#else
206        pt = new PreprocessorThread(preprocessor);
[1272]207#endif
[1579]208#endif
[1387]209
[1248]210        bool guiSupported = false;
[1454]211
[1145]212        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
[1387]213          {
[1241]214                cout << "using gl widget" << endl;
[1153]215                // create and run the preprocessor application in a parallel thread
[1457]216#if USE_QT             
[1387]217#if USE_THREADS
218                pt->InitThread();
[1613]219                if (!preprocessor->mDelayVisibilityComputation)
220                  pt->RunThread();
[1272]221#endif
[1387]222               
[1248]223                // display the render widget
[1344]224                if (!rendererWidget)
225                {
[1387]226                  // create a qt application first (must be created before any opengl widget ...)
227                  QApplication *app = new QApplication(argc, NULL);
228                 
229                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
230                        QMessageBox::information(0, "OpenGL pbuffers",
231                                                                         "This system does not support OpenGL/pbuffers.",
232                                                                         QMessageBox::Ok);
233                        return NULL;
234                  }
235                 
236                 
237                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
238                                                                                                  preprocessor->mViewCellsManager,
239                                                                                                  preprocessor->mKdTree);
[1613]240
241                  ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
[1387]242                  rendererWidget->Show();
[1404]243                  guiSupported = true;
[1272]244                }
[1344]245
[1404]246                qApp->exec();   
[1387]247#endif
248          }
[1221]249       
[1248]250        if (!guiSupported) {
251          preprocessor->mUseGlRenderer = false;
252          preprocessor->mUseGlDebugger = false;
[1005]253        }
[1181]254       
[1248]255        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
256          // just call the mail method -> will be executed in the main thread
[1454]257          pt->Main();
[1248]258        }
259       
[1415]260        // release memory
[1145]261        Cleanup();
[1248]262       
[1145]263        return returnCode;
[162]264}
265
Note: See TracBrowser for help on using the repository browser.