source: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp @ 574

Revision 574, 11.4 KB checked in by mattausch, 18 years ago (diff)

finished function for view cell construction
removed bsp rays from vspbspmanager

Line 
1#include "SceneGraph.h"
2#include "Exporter.h"
3#include "UnigraphicsParser.h"
4#include "X3dParser.h"
5#include "Preprocessor.h"
6#include "ViewCell.h"
7#include "Environment.h"
8#include "ViewCellsManager.h"
9#include "ViewCellBsp.h"
10#include "VspBspTree.h"
11#include "VspKdTree.h"
12#include "RenderSimulator.h"
13#include "GlRenderer.h"
14
15Preprocessor *preprocessor;
16
17Preprocessor::Preprocessor():
18mKdTree(NULL),
19mBspTree(NULL),
20mVspKdTree(NULL),
21mVspBspTree(NULL),
22mViewCellsManager(NULL)
23{
24  environment->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
25 
26  // renderer will be constructed when the scene graph and viewcell manager will be known
27  renderer = NULL;
28 
29  environment->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
30}
31
32
33Preprocessor::~Preprocessor()
34{
35  Debug<<"Deleting view cells manager...\n";
36  DEL_PTR(mViewCellsManager);
37  Debug<<"done.\n";
38  Debug<<"Deleting bsp tree...\n";
39  DEL_PTR(mBspTree);
40  Debug<<"done.\n";
41  Debug<<"Deleting kd tree...\n";
42  DEL_PTR(mKdTree);
43  Debug<<"done.\n";
44  Debug<<"Deleting vspkd tree...\n";
45  DEL_PTR(mVspKdTree);
46  Debug<<"done.\n";
47  Debug<<"Deleting vspbsp tree...\n";
48  DEL_PTR(mVspBspTree);
49  Debug<<"done.\n";
50}
51
52int
53SplitFilenames(const string str, vector<string> &filenames)
54{
55        int pos = 0;
56
57        while(1) {
58                int npos = (int)str.find(';', pos);
59               
60                if (npos < 0 || npos - pos < 1)
61                        break;
62                filenames.push_back(string(str, pos, npos - pos));
63                pos = npos + 1;
64        }
65       
66        filenames.push_back(string(str, pos, str.size() - pos));
67        return (int)filenames.size();
68}
69
70bool
71Preprocessor::LoadScene(const string filename)
72{
73        // use leaf nodes of the original spatial hiearrchy as occludees
74        mSceneGraph = new SceneGraph;
75 
76        Parser *parser;
77        vector<string> filenames;
78        int files = SplitFilenames(filename, filenames);
79        cout<<files<<endl;
80        bool result = false;
81        if (files == 1) {
82               
83                if (strstr(filename.c_str(), ".x3d"))
84                        parser = new X3dParser;
85                else
86                        parser = new UnigraphicsParser;
87
88                cout<<filename<<endl;
89                result = parser->ParseFile(filename, &mSceneGraph->mRoot);
90
91                delete parser;
92
93        } else {
94                // root for different files
95                mSceneGraph->mRoot = new SceneGraphNode;
96                for (int i= 0; i < filenames.size(); i++) {
97                        if (strstr(filenames[i].c_str(), ".x3d"))
98                                parser = new X3dParser;
99                        else
100                                parser = new UnigraphicsParser;
101                       
102                        SceneGraphNode *node;
103                        if (parser->ParseFile(filenames[i], &node)) {
104                                mSceneGraph->mRoot->mChildren.push_back(node);
105                                // at least one file parsed
106                                result = true;
107                        }
108                        delete parser;
109                }
110        }
111       
112
113        if (result) {
114         
115          mSceneGraph->AssignObjectIds();
116          int intersectables, faces;
117          mSceneGraph->GetStatistics(intersectables, faces);
118          cout<<filename<<" parsed successfully."<<endl;
119          cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
120          cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
121          mSceneGraph->CollectObjects(&mObjects);
122          mSceneGraph->mRoot->UpdateBox();
123        }
124       
125       
126        return result;
127}
128
129bool
130Preprocessor::ExportPreprocessedData(const string filename)
131{
132  return false;
133}
134
135bool
136Preprocessor::BuildKdTree()
137{
138  mKdTree = new KdTree;
139  // add mesh instances of the scene graph to the root of the tree
140  KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
141  mSceneGraph->CollectObjects(&root->mObjects);
142 
143  mKdTree->Construct();
144  return true;
145}
146
147void
148Preprocessor::KdTreeStatistics(ostream &s)
149{
150  s<<mKdTree->GetStatistics();
151}
152
153void
154Preprocessor::BspTreeStatistics(ostream &s)
155{
156        s << mBspTree->GetStatistics();
157}
158
159bool
160Preprocessor::Export( const string filename,
161                                          const bool scene,
162                                          const bool kdtree,
163                                          const bool bsptree
164                                          )
165{
166  Exporter *exporter = Exporter::GetExporter(filename);
167       
168  if (exporter) {
169    if (scene)
170      exporter->ExportScene(mSceneGraph->mRoot);
171
172    if (kdtree) {
173      exporter->SetWireframe();
174      exporter->ExportKdTree(*mKdTree);
175    }
176
177        if (bsptree) {
178                //exporter->SetWireframe();
179                exporter->ExportBspTree(*mBspTree);
180        }
181
182    delete exporter;
183    return true;
184  }
185
186  return false;
187}
188
189
190bool Preprocessor::PrepareViewCells()
191{
192        //-- parse type of view cell container
193        char viewCellsStr[64];
194        environment->GetStringValue("ViewCells.type", viewCellsStr);
195
196        int initialSamples = 0;
197       
198        if (strcmp(viewCellsStr, "kdTree") == 0)
199        {
200                mViewCellsManager = new KdViewCellsManager(mKdTree);
201        }
202        else if (strcmp(viewCellsStr, "bspTree") == 0)
203        {
204                mBspTree = new BspTree();
205
206                Debug << "view cell type: Bsp" << endl;
207
208                mViewCellsManager = new BspViewCellsManager(mBspTree);
209        }
210        else if (strcmp(viewCellsStr, "vspBspTree") == 0)
211        {
212                mVspBspTree = new VspBspTree();
213
214                Debug << "view cell type: VspBsp" << endl;
215
216                mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
217        }
218        else if (strcmp(viewCellsStr, "vspKdTree") == 0)
219        {
220                mVspKdTree = new VspKdTree();           
221       
222                mViewCellsManager = new VspKdViewCellsManager(mVspKdTree);
223        }
224        else if (strcmp(viewCellsStr, "sceneDependent") == 0)
225        {
226                //TODO
227                mBspTree = new BspTree();
228
229                Debug << "view cell type: Bsp" << endl;
230               
231                mViewCellsManager = new BspViewCellsManager(mBspTree);
232        }
233        else
234        {
235                cerr<<"Wrong view cells type" << viewCellsStr << endl;
236                exit(1);
237        }
238
239        float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
240
241        environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
242        environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
243        environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
244
245        mRenderSimulator =
246                new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
247
248        mViewCellsManager->SetRenderer(mRenderSimulator);
249
250        //-- parse view cells construction method
251        environment->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
252        char buf[100];
253
254        if (mLoadViewCells)
255        {
256                environment->GetStringValue("ViewCells.filename", buf);
257                mViewCellsFilename = buf;
258        }
259
260        if (mUseGlRenderer || mUseGlDebugger)
261        {
262                // NOTE: render texture should be power of 2 and square
263                // renderer must be initialised
264                renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
265                //              renderer->makeCurrent();
266        }
267
268
269        environment->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
270       
271
272
273        return true;
274}
275
276
277// use ascii format to store rays
278#define USE_ASCII 0
279
280
281inline bool ilt(Intersectable *obj1, Intersectable *obj2)
282{
283        return obj1->mId < obj2->mId;
284}
285
286
287bool Preprocessor::LoadSamples(VssRayContainer &samples,
288                                                           ObjectContainer &objects) const
289{
290        std::stable_sort(objects.begin(), objects.end(), ilt);
291        char fileName[100];
292        environment->GetStringValue("Preprocessor.samplesFilename", fileName);
293       
294    Vector3 origin, termination;
295        // HACK: needed only for lower_bound algorithm to find the
296        // intersected objects
297        MeshInstance sObj(NULL);
298        MeshInstance tObj(NULL);
299
300#if USE_ASCII
301        ifstream samplesIn(fileName, ios::binary);
302        if (!samplesIn.is_open())
303                return false;
304
305        string buf;
306        while (!(getline(samplesIn, buf)).eof())
307        {
308                sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
309                           &origin.x, &origin.y, &origin.z,
310                           &termination.x, &termination.y, &termination.z,
311                           &(sObj.mId), &(tObj.mId));
312               
313                Intersectable *sourceObj = NULL;
314                Intersectable *termObj = NULL;
315               
316                if (sObj.mId >= 0)
317                {
318                        ObjectContainer::iterator oit =
319                                lower_bound(objects.begin(), objects.end(), &sObj, ilt);
320                        sourceObj = *oit;
321                }
322               
323                if (tObj.mId >= 0)
324                {
325                        ObjectContainer::iterator oit =
326                                lower_bound(objects.begin(), objects.end(), &tObj, ilt);
327                        termObj = *oit;
328                }
329
330                samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
331        }
332#else
333        ifstream samplesIn(fileName, ios::binary);
334        if (!samplesIn.is_open())
335                return false;
336
337        while (1)
338        {
339                 samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
340                 samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
341                 samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
342                 samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
343               
344                 if (samplesIn.eof())
345                        break;
346
347                Intersectable *sourceObj = NULL;
348                Intersectable *termObj = NULL;
349               
350                if (sObj.mId >= 0)
351                {
352                        ObjectContainer::iterator oit =
353                                lower_bound(objects.begin(), objects.end(), &sObj, ilt);
354                        sourceObj = *oit;
355                }
356               
357                if (tObj.mId >= 0)
358                {
359                        ObjectContainer::iterator oit =
360                                lower_bound(objects.begin(), objects.end(), &tObj, ilt);
361                        termObj = *oit;
362                }
363
364                samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
365        }
366
367#endif
368        samplesIn.close();
369
370        return true;
371}
372
373
374bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
375{
376        char fileName[100];
377        environment->GetStringValue("Preprocessor.samplesFilename", fileName);
378       
379
380        VssRayContainer::const_iterator it, it_end = samples.end();
381       
382#if USE_ASCII
383        ofstream samplesOut(fileName);
384        if (!samplesOut.is_open())
385                return false;
386
387        for (it = samples.begin(); it != it_end; ++ it)
388        {
389                VssRay *ray = *it;
390                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
391                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;       
392
393                samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
394                                   << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
395                                   << sourceid << " " << termid << "\n";
396        }
397#else
398        ofstream samplesOut(fileName, ios::binary);
399        if (!samplesOut.is_open())
400                return false;
401
402        for (it = samples.begin(); it != it_end; ++ it)
403        {       
404                VssRay *ray = *it;
405                Vector3 origin(ray->GetOrigin());
406                Vector3 termination(ray->GetTermination());
407               
408                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
409                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;               
410
411                samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
412                samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
413                samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
414                samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
415    }
416#endif
417        samplesOut.close();
418        return true;
419}
420
421
422
423bool
424Preprocessor::GenerateRays(
425                                                   const int number,
426                                                   const int sampleType,
427                                                   SimpleRayContainer &rays
428                                                   )
429{
430  Vector3 origin, direction;
431
432  for (int i=0; rays.size() < number; i++) {
433        mViewCellsManager->GetViewPoint(origin);
434       
435        // now get the direction
436        switch (sampleType) {
437        case OBJECT_BASED_DISTRIBUTION: {
438          Vector3 point;
439          Vector3 normal;
440          int i = RandomValue(0, mObjects.size() - 1);
441          Intersectable *object = mObjects[i];
442          object->GetRandomSurfacePoint(point, normal);
443          direction = point - origin;
444        }
445          break;
446        case DIRECTION_BASED_DISTRIBUTION:
447          direction = UniformRandomVector();
448          break;
449        case DIRECTION_BOX_BASED_DISTRIBUTION: {
450          float alpha = RandomValue(0.0f, 2*M_PI);
451          float beta = RandomValue(-M_PI/2, M_PI/2);
452          direction = VssRay::GetDirection(alpha, beta);
453          break;
454        }
455        case SPATIAL_BOX_BASED_DISTRIBUTION:
456          direction = mKdTree->GetBox().GetRandomPoint() - origin;
457          break;
458        default:
459          // unsuported distribution type
460          return false;
461        }
462        // $$ jb the pdf is yet not correct for all sampling methods!
463        float pdf = 1.0f;
464        float c = Magnitude(direction);
465        if (c > Limits::Small) {
466          direction*=1.0f/c;
467          rays.AddRay(SimpleRay(origin, direction, pdf));
468        }
469  }
470  return true;
471}
Note: See TracBrowser for help on using the repository browser.