source: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp @ 1990

Revision 1990, 36.3 KB checked in by mattausch, 17 years ago (diff)
RevLine 
[372]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"
[439]8#include "ViewCellsManager.h"
[445]9#include "ViewCellBsp.h"
10#include "VspBspTree.h"
[469]11#include "RenderSimulator.h"
[496]12#include "GlRenderer.h"
[749]13#include "PlyParser.h"
[1020]14#include "SamplingStrategy.h"
[1233]15#include "VspTree.h"
16#include "OspTree.h"
[1221]17#include "ObjParser.h"
[1264]18#include "BvHierarchy.h"
[1279]19#include "HierarchyManager.h"
[1287]20#include "VssRay.h"
[1520]21#include "IntelRayCaster.h"
22#include "InternalRayCaster.h"
[1968]23#include "GlobalLinesRenderer.h"
[1264]24
[1287]25
[1292]26#define DEBUG_RAYCAST 0
[1584]27#define SHOW_RAYCAST_TIMING 1
[1292]28
29
[863]30namespace GtpVisibilityPreprocessor {
[860]31
[1867]32  const static bool ADDITIONAL_GEOMETRY_HACK = false;
[860]33
[1867]34  Preprocessor *preprocessor = NULL;
35 
[1001]36// HACK: Artificially modify scene to watch rendercost changes
[750]37static void AddGeometry(SceneGraph *scene)
38{
[1328]39        scene->GetRoot()->UpdateBox();
[752]40
[750]41        AxisAlignedBox3 sceneBox = scene->GetBox();
42
43        int n = 200;
44
[1291]45        if (0)
[750]46        {
[1291]47                // form grid of boxes
48                for (int i = 0; i < n; ++ i)
[750]49                {
[1291]50                        for (int j = 0; j < n; ++ j)
51                        {
52                                const Vector3 scale2((float)j * 0.8f / n + 0.1f,  0.05f, (float)i * 0.8f  / (float)n + 0.1f);
[750]53
[1291]54                                const Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
55
56                                const Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
57                                AxisAlignedBox3 box(pt2, pt2 + boxSize);
58                                Mesh *mesh = CreateMeshFromBox(box);
59
60                                mesh->Preprocess();
61
62                                MeshInstance *mi = new MeshInstance(mesh);
[1328]63                                scene->GetRoot()->mGeometry.push_back(mi);
[1291]64                        }
[750]65                }
66
[1291]67                for (int i = 0; i < n; ++ i)
[750]68                {
[1291]69                        for (int j = 0; j < n; ++ j)
70                        {
71                                const Vector3 scale2(0.15f, (float)j * 0.8f / n + 0.1f, (float)i * 0.8f  / (float)n + 0.1f);
72
73                                Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
74
75                                Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
76                                AxisAlignedBox3 box(pt2, pt2 + boxSize);
77                                Mesh *mesh = CreateMeshFromBox(box);
78
79                                mesh->Preprocess();
80
81                                MeshInstance *mi = new MeshInstance(mesh);
[1328]82                                scene->GetRoot()->mGeometry.push_back(mi);
[1291]83                        }
84                }
85
86                for (int i = 0; i < n; ++ i)
87                {
88                        const Vector3 scale2(2, 0.2f, (float)i * 0.8f  / (float)n + 0.1f);
89
[750]90                        Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
[1291]91                        Vector3 boxSize = sceneBox.Size() * Vector3(0.005f, 0.02f, 0.005f);
92
93                        AxisAlignedBox3 box(pt2 + 0.1f, pt2 + boxSize);
[991]94                        Mesh *mesh = CreateMeshFromBox(box);
[750]95
96                        mesh->Preprocess();
[1291]97
[750]98                        MeshInstance *mi = new MeshInstance(mesh);
[1328]99                        scene->GetRoot()->mGeometry.push_back(mi);
[750]100                }
[1291]101
[1328]102                scene->GetRoot()->UpdateBox();
[750]103        }
104
[840]105        if (1)
106        {
[1221]107                // plane separating view space regions
[1135]108                const Vector3 scale(1.0f, 0.0, 0);
[750]109
[840]110                Vector3 pt = sceneBox.Min() + scale * (sceneBox.Max() - sceneBox.Min());
[750]111
[840]112                Plane3 cuttingPlane(Vector3(1, 0, 0), pt);
113                Mesh *planeMesh = new Mesh();
[1291]114
[840]115                Polygon3 *poly = sceneBox.CrossSection(cuttingPlane);
116                IncludePolyInMesh(*poly, *planeMesh);
[1291]117
[840]118                planeMesh->Preprocess();
[1291]119
[840]120                MeshInstance *planeMi = new MeshInstance(planeMesh);
[1328]121                scene->GetRoot()->mGeometry.push_back(planeMi);
[840]122        }       
[750]123}
124
125
[372]126Preprocessor::Preprocessor():
127mKdTree(NULL),
[409]128mBspTree(NULL),
[445]129mVspBspTree(NULL),
[1002]130mViewCellsManager(NULL),
[1251]131mRenderSimulator(NULL),
[1279]132mPass(0),
[1520]133mSceneGraph(NULL),
[1613]134mRayCaster(NULL),
[1926]135mStopComputation(false),
[1968]136mThread(NULL),
137mGlobalLinesRenderer(NULL)
[308]138{
[1004]139        Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
[538]140 
[840]141        // renderer will be constructed when the scene graph and viewcell manager will be known
142        renderer = NULL;
[1785]143       
[1004]144        Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
[1379]145        Environment::GetSingleton()->GetBoolValue("Preprocessor.loadMeshes", mLoadMeshes);
[1004]146        Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
147        Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
148        Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
149        Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility );
[1520]150       
[871]151        char buffer[256];
[1004]152        Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile",  buffer);
[871]153        mVisibilityFileName = buffer;
[1771]154       
155        Environment::GetSingleton()->GetStringValue("Preprocessor.stats",  buffer);
156        mStats.open(buffer);
157       
158       
[1749]159        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter);
[1004]160        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
[1695]161                                                                                          mApplyVisibilitySpatialFilter );
[1004]162        Environment::GetSingleton()->GetFloatValue("Preprocessor.visibilityFilterWidth", mVisibilityFilterWidth);
[878]163
[1695]164        Environment::GetSingleton()->GetBoolValue("Preprocessor.exportObj", mExportObj);
165       
[1723]166        Environment::GetSingleton()->GetBoolValue("Preprocessor.useViewSpaceBox", mUseViewSpaceBox);
167
[1900]168        Environment::GetSingleton()->GetBoolValue("Preprocessor.Export.rays", mExportRays);
169        Environment::GetSingleton()->GetIntValue("Preprocessor.Export.numRays", mExportNumRays);
170
[1966]171
172        Environment::GetSingleton()->GetIntValue("Preprocessor.samplesPerPass", mSamplesPerPass);
173        Environment::GetSingleton()->GetIntValue("Preprocessor.totalSamples", mTotalSamples);
174        Environment::GetSingleton()->GetIntValue("Preprocessor.samplesPerEvaluation",
175                                                                                         mSamplesPerEvaluation);
176
[1695]177        Debug << "******* Preprocessor Options **********" << endl;
[840]178        Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
[1379]179        Debug << "load meshes: " << mLoadMeshes << endl;
[1695]180        Debug << "load meshes: " << mLoadMeshes << endl;
181        Debug << "export obj: " << mExportObj << endl;
[1723]182        Debug << "use view space box: " << mUseViewSpaceBox << endl;
[372]183}
184
185
186Preprocessor::~Preprocessor()
187{
[1291]188        cout << "cleaning up" << endl;
[752]189
[1291]190        cout << "Deleting view cells manager ... \n";
191        DEL_PTR(mViewCellsManager);
192        cout << "done.\n";
[752]193
[1291]194        cout << "Deleting bsp tree ... \n";
195        DEL_PTR(mBspTree);
[1924]196        cout << "done.\n";
[752]197
[1924]198        cout << "Deleting kd tree ...\n";
[1291]199        DEL_PTR(mKdTree);
200        cout << "done.\n";
[752]201
[1923]202        cout << "Deleting vspbsp tree ... \n";
[1291]203        DEL_PTR(mVspBspTree);
204        cout << "done.\n";
[1002]205
[1923]206        cout << "Deleting scene graph ... \n";
[1291]207        DEL_PTR(mSceneGraph);
208        cout << "done.\n";
209
210        DEL_PTR(mRenderSimulator);
211        DEL_PTR(renderer);
[1523]212        DEL_PTR(mRayCaster);
[1990]213#ifdef USE_CG
[1968]214        DEL_PTR(mGlobalLinesRenderer);
[1990]215#endif
[372]216}
217
[1521]218
219GlRendererBuffer *Preprocessor::GetRenderer()
220{
221        return renderer;
222}
223
224
225static int SplitFilenames(const string str, vector<string> &filenames)
[387]226{
227        int pos = 0;
228
229        while(1) {
[469]230                int npos = (int)str.find(';', pos);
[387]231               
232                if (npos < 0 || npos - pos < 1)
233                        break;
234                filenames.push_back(string(str, pos, npos - pos));
235                pos = npos + 1;
236        }
237       
238        filenames.push_back(string(str, pos, str.size() - pos));
[440]239        return (int)filenames.size();
[387]240}
241
[750]242
[1926]243void Preprocessor::SetThread(PreprocessorThread *t)
244{
245        mThread = t;
246}
247
248
249PreprocessorThread *Preprocessor::GetThread() const
250{
251        return mThread;
252}
253
254
[1655]255bool Preprocessor::LoadBinaryObj(const string filename,
256                                                                 SceneGraphNode *root,
257                                                                 vector<FaceParentInfo> *parents)
258{
[1658]259        //ifstream samplesIn(filename, ios::binary);
260        igzstream samplesIn(filename.c_str());
261       
[1655]262        if (!samplesIn.is_open())
263                return false;
264
[1658]265        cout << "binary obj dump available, loading " << filename.c_str() << endl;
[1655]266        // table associating indices with vectors
267        map<int, Vector3> hashTable;
[1658]268
[1655]269        // table for vertices
270        VertexContainer vertices;
271        FaceContainer faces;
272
[1786]273        if (parents)
274                cout << "using face parents" << endl;
275        else
276                cout << "not using face parents" << endl;
277
[1655]278        while (1)
279        {
280                Triangle3 tri;
281               
282                samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 0), sizeof(Vector3));
283                samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 1), sizeof(Vector3));
284                samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 2), sizeof(Vector3));
285               
286                // end of file reached
287                if (samplesIn.eof())
288                        break;
289
290                TriangleIntersectable *obj = new TriangleIntersectable(tri);
291                root->mGeometry.push_back(obj);
292
293                // matt: we don't really need to keep an additional data structure
294                // if working with triangles => remove this
295                if (parents)
296                {
297                        FaceParentInfo info(obj, 0);
298                        parents->push_back(info);
299                }       
300        }
301       
302        return true;
303}
304
305
[1658]306bool Preprocessor::ExportBinaryObj(const string filename, SceneGraphNode *root)
307{
308        //ifstream samplesIn(filename, ios::binary);
309        ogzstream samplesOut(filename.c_str());
310        if (!samplesOut.is_open())
311                return false;
312
313        ObjectContainer::const_iterator oit, oit_end = root->mGeometry.end();
314
315        for (oit = root->mGeometry.begin(); oit != oit_end; ++ oit)
316        {
317                Intersectable *obj = *oit;
318
319                if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
320                {
321                        Triangle3 tri = dynamic_cast<TriangleIntersectable *>(obj)->GetItem();
322
323                        samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 0), sizeof(Vector3));
324                        samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 1), sizeof(Vector3));
325                        samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 2), sizeof(Vector3));
326                }
327                else
328                {
329                        cout << "not implemented intersectable type " << obj->Type() << endl;
330                }
331        }
332
333        return true;
334}
335
[1695]336
337bool Preprocessor::ExportObj(const string filename, const ObjectContainer &objects)
338{
339        ofstream samplesOut(filename.c_str());
340
341        if (!samplesOut.is_open())
342                return false;
343
344        ObjectContainer::const_iterator oit, oit_end = objects.end();
345
[1713]346        AxisAlignedBox3 bbox = mSceneGraph->GetBox(); bbox.Enlarge(30.0);
[1695]347        for (oit = objects.begin(); oit != oit_end; ++ oit)
348        {
349                Intersectable *obj = *oit;
350
351                if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
352                {
353                        Triangle3 tri = dynamic_cast<TriangleIntersectable *>(obj)->GetItem();
[1701]354                        //if (bbox.IsInside(tri.mVertices[0]) && bbox.IsInside(tri.mVertices[1]) && bbox.IsInside(tri.mVertices[2]))
355                        //{
[1713]356                                samplesOut << "v " << tri.mVertices[0].x << " " << tri.mVertices[0].y << " " << tri.mVertices[0].z << endl;
357                                samplesOut << "v " << tri.mVertices[1].x << " " << tri.mVertices[1].y << " " << tri.mVertices[1].z << endl;
358                                samplesOut << "v " << tri.mVertices[2].x << " " << tri.mVertices[2].y << " " << tri.mVertices[2].z << endl;
[1701]359                        //}
[1695]360                }
361                else
362                {
363                        cout << "not implemented intersectable type " << obj->Type() << endl;
364                }
365        }
366
367        // write faces
368        int i = 1;
[1713]369        for (oit = objects.begin(); oit != oit_end; ++ oit)
[1695]370        {
371                Intersectable *obj = *oit;
372                if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
373                {
[1713]374                        //Triangle3 tri = dynamic_cast<TriangleIntersectable *>(obj)->GetItem();
375                        //if (bbox.IsInside(tri.mVertices[0]) && bbox.IsInside(tri.mVertices[1]) && bbox.IsInside(tri.mVertices[2]))
376                        //{
377                                Triangle3 tri = dynamic_cast<TriangleIntersectable *>(obj)->GetItem();
378                                samplesOut << "f " << i << " " << i + 1 << " " << i + 2 << endl;
379                                i += 3;
380                        //}
[1695]381                }
382                else
383                {
384                        cout << "not implemented intersectable type " << obj->Type() << endl;
385                }
386        }
387
388        return true;
389
390}
391
[1658]392static string ReplaceSuffix(string filename, string a, string b)
393{
394        string result = filename;
395
396        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
397        if (pos == filename.size() - a.size()) {
398                result.replace(pos, a.size(), b);
399        }
400        return result;
401}
402
403
[1786]404Intersectable *Preprocessor::GetParentObject(const int index) const
405{
406        if (index == -1)
[1984]407          return NULL;
[1786]408       
409        if (!mFaceParents.empty())
410        {
411                if (index >= (int)mFaceParents.size())
412                {
[1978]413                        cerr << "Warning: triangle index out of range! " << index << endl;
[1786]414                        return NULL;
415                }
416                else
417                {
418                        return mFaceParents[index].mObject;
419                }
420        }
421        else
[1984]422          {
423          if (index >= (int)mObjects.size()) {
424                cerr<<"Warning: triangle  index out of range! "<<index<<endl;
425                return NULL;
426          }
427          else
[1786]428                {
[1984]429                  return mObjects[index];
[1786]430                }
[1984]431          }
[1786]432}
433
434
435Vector3 Preprocessor::GetParentNormal(const int index) const
436{
437        if (!mFaceParents.empty())
438        {
439                return mFaceParents[index].mObject->GetNormal(mFaceParents[index].mFaceIndex);
440        }       
441        else
442        {
443                return mObjects[index]->GetNormal(0);
444        }
445}
446
447
[372]448bool
449Preprocessor::LoadScene(const string filename)
450{
[1655]451    // use leaf nodes of the original spatial hierarchy as occludees
[508]452        mSceneGraph = new SceneGraph;
[372]453 
[508]454        Parser *parser;
[387]455        vector<string> filenames;
[1404]456        const int files = SplitFilenames(filename, filenames);
[712]457        cout << "number of input files: " << files << endl;
[387]458        bool result = false;
[1695]459        bool isObj = false;
[1344]460
461        // root for different files
462        mSceneGraph->SetRoot(new SceneGraphNode());
463
[1404]464        // intel ray caster can only trace triangles
[1520]465        int rayCastMethod;
466        Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod", rayCastMethod);
[1827]467        vector<FaceParentInfo> *fi =
[1867]468          ((rayCastMethod == RayCaster::INTEL_RAYCASTER) && mLoadMeshes) ?
469          &mFaceParents : NULL;
470       
[1658]471        if (files == 1)
[1867]472          {
[387]473                if (strstr(filename.c_str(), ".x3d"))
[1867]474                  {
[1655]475                        parser = new X3dParser;
[1658]476                       
477                        result = parser->ParseFile(filename,
478                                                                           mSceneGraph->GetRoot(),
479                                                                           mLoadMeshes,
480                                                                           fi);
481                        delete parser;
[1655]482                }
[1658]483                else if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb"))
[1655]484                {
[1658]485                        parser = new PlyParser;
[1655]486
[1658]487                        result = parser->ParseFile(filename,
488                                                                           mSceneGraph->GetRoot(),
489                                                                           mLoadMeshes,
490                                                                           fi);
491                        delete parser;
[1655]492                }
[1658]493                else if (strstr(filename.c_str(), ".obj"))
494                {
[1695]495                        isObj = true;
496
[1658]497                        // hack: load binary dump
498                        string binFile = ReplaceSuffix(filename, ".obj", ".bin");
[372]499
[1658]500                        if (!mLoadMeshes)
501                        {
502                                result = LoadBinaryObj(binFile, mSceneGraph->GetRoot(), fi);
503                        }
504
505                        if (!result)
506                        {
507                                cout << "no binary dump available or loading full meshes, parsing file" << endl;
508                                parser = new ObjParser;
[1404]509               
[1658]510                                result = parser->ParseFile(filename,
[1655]511                                                                   mSceneGraph->GetRoot(),
512                                                                   mLoadMeshes,
513                                                                   fi);
[1658]514                                               
515                                // only works for triangles
516                                if (!mLoadMeshes)
517                                {
518                                        cout << "exporting binary obj to " << binFile << "... " << endl;
519                                        ExportBinaryObj(binFile, mSceneGraph->GetRoot());
520                                        cout << "finished" << endl;
521                                }
[1655]522
[1658]523                                delete parser;
524                        }
[1660]525                        else if (0)
[1658]526                        {
527                                ExportBinaryObj("../data/test.bin", mSceneGraph->GetRoot());
528                        }
529                }
530                else
531                {
532                        parser = new UnigraphicsParser;
533                        result = parser->ParseFile(filename,
534                                                                           mSceneGraph->GetRoot(),
535                                                                           mLoadMeshes,                                                           
536                                                                           fi);
537                        delete parser;
538                }
539               
540                cout << filename << endl;
[1404]541        }
[1658]542        else
543        {
[1404]544                vector<string>::const_iterator fit, fit_end = filenames.end();
[1344]545               
[1404]546                for (fit = filenames.begin(); fit != fit_end; ++ fit)
547                {
548                        const string filename = *fit;
[1328]549
[1404]550                        cout << "parsing file " << filename.c_str() << endl;
551                        if (strstr(filename.c_str(), ".x3d"))
552                                parser = new X3dParser;
553                        else
554                                parser = new UnigraphicsParser;
555
556                        SceneGraphNode *node = new SceneGraphNode();
[1695]557                        const bool success =
558                                parser->ParseFile(filename, node, mLoadMeshes, fi);
[1404]559
560                        if (success)
561                        {
562                                mSceneGraph->GetRoot()->mChildren.push_back(node);
563                                result = true; // at least one file parsed
564                        }
565
[1713]566                        // temporare hack
[1701]567                        //if (!strstr(filename.c_str(), "plane")) mSceneGraph->GetRoot()->UpdateBox();
568
[1404]569                        delete parser;
[387]570                }
571        }
[1344]572       
[752]573        if (result)
[1344]574        {
[752]575                // HACK
[1020]576                if (ADDITIONAL_GEOMETRY_HACK)
577                        AddGeometry(mSceneGraph);
[1328]578
[1020]579                mSceneGraph->AssignObjectIds();
[1344]580 
[1020]581                int intersectables, faces;
582                mSceneGraph->GetStatistics(intersectables, faces);
[1344]583 
[1020]584                cout<<filename<<" parsed successfully."<<endl;
585                cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
586                cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
[1701]587               
[1697]588                mObjects.reserve(intersectables);
[1020]589                mSceneGraph->CollectObjects(&mObjects);
[1713]590               
591                // temp hack
592                //ExportObj("cropped_vienna.obj", mObjects);
[1328]593                mSceneGraph->GetRoot()->UpdateBox();
[1713]594                               
595                cout << "finished loading" << endl;
[387]596        }
[1328]597
[492]598        return result;
[372]599}
600
601bool
602Preprocessor::ExportPreprocessedData(const string filename)
603{
[1486]604        mViewCellsManager->ExportViewCells(filename, true, mObjects);
605        return true;
[372]606}
607
[1486]608
[372]609bool
[871]610Preprocessor::PostProcessVisibility()
611{
612 
[904]613  if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
[997]614        cout<<"Applying visibility filter ...";
[1002]615        cout<<"filter width = " << mVisibilityFilterWidth << endl;
[904]616       
[1002]617        if (!mViewCellsManager)
[1199]618          return false;
619       
[871]620        mViewCellsManager->ApplyFilter(mKdTree,
[904]621                                                                   mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f,
622                                                                   mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f);
[997]623        cout << "done." << endl;
[871]624  }
625 
626  // export the preprocessed information to a file
627  if (mExportVisibility)
[1486]628  {
629          ExportPreprocessedData(mVisibilityFileName);
630  }
631
[871]632  return true;
633}
634
635
636bool
[372]637Preprocessor::BuildKdTree()
638{
639  mKdTree = new KdTree;
[1344]640
[372]641  // add mesh instances of the scene graph to the root of the tree
642  KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
[1344]643       
[372]644  mSceneGraph->CollectObjects(&root->mObjects);
[1344]645 
[1415]646  const long startTime = GetTime();
[1201]647  cout << "building kd tree ... " << endl;
[1344]648
[372]649  mKdTree->Construct();
[1344]650
[1415]651  cout << "finished kd tree construction in " << TimeDiff(startTime, GetTime()) * 1e-3
652           << " secs " << endl;
[1344]653
[372]654  return true;
655}
656
[1415]657
[372]658void
659Preprocessor::KdTreeStatistics(ostream &s)
660{
661  s<<mKdTree->GetStatistics();
662}
663
664void
665Preprocessor::BspTreeStatistics(ostream &s)
666{
667        s << mBspTree->GetStatistics();
668}
669
670bool
671Preprocessor::Export( const string filename,
[492]672                                          const bool scene,
[1545]673                                          const bool kdtree
[492]674                                          )
[372]675{
676  Exporter *exporter = Exporter::GetExporter(filename);
677       
678  if (exporter) {
[1545]679    if (2 && scene)
[1328]680      exporter->ExportScene(mSceneGraph->GetRoot());
[372]681
[1545]682    if (1 && kdtree) {
[372]683      exporter->SetWireframe();
684      exporter->ExportKdTree(*mKdTree);
685    }
686
687    delete exporter;
688    return true;
689  }
690
691  return false;
692}
[429]693
[508]694
[463]695bool Preprocessor::PrepareViewCells()
696{
[1523]697        ///////
[577]698        //-- parse view cells construction method
[1563]699
[1004]700        Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
[577]701        char buf[100];
[1585]702
[577]703        if (mLoadViewCells)
[997]704        {       
[1740]705                Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf);
706                cout << "loading view cells from " << buf << endl<<flush;
[1715]707
[1740]708                mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects, true, NULL);
709
710                cout << "view cells loaded." << endl<<flush;
711
712                if (!mViewCellsManager)
713                {
714                        return false;
715                }
[577]716        }
717        else
718        {
[1288]719                // parse type of view cell container
[1004]720                Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);             
[1740]721                mViewCellsManager = CreateViewCellsManager(buf);
[1112]722
723                // default view space is the extent of the scene
[1723]724                AxisAlignedBox3 viewSpaceBox;
[1740]725
[1723]726                if (mUseViewSpaceBox)
[1563]727                {
[1723]728                        viewSpaceBox = mSceneGraph->GetBox();
729
[1563]730                        // use a small box outside of the scene
[1723]731                        viewSpaceBox.Scale(Vector3(0.15f, 0.3f, 0.5f));
732                        //viewSpaceBox.Translate(Vector3(Magnitude(mSceneGraph->GetBox().Size()) * 0.5f, 0, 0));
733                        viewSpaceBox.Translate(Vector3(Magnitude(mSceneGraph->GetBox().Size()) * 0.3f, 0, 0));
734                        mViewCellsManager->SetViewSpaceBox(viewSpaceBox);
[1563]735                }
[1723]736                else
737                {
738                        viewSpaceBox = mSceneGraph->GetBox();
739                        mViewCellsManager->SetViewSpaceBox(viewSpaceBox);
740                }
[1545]741
742                bool loadVcGeometry;
743                Environment::GetSingleton()->GetBoolValue("ViewCells.loadGeometry", loadVcGeometry);
744
745                bool extrudeBaseTriangles;
746                Environment::GetSingleton()->GetBoolValue("ViewCells.useBaseTrianglesAsGeometry", extrudeBaseTriangles);
747
748                char vcGeomFilename[100];
749                Environment::GetSingleton()->GetStringValue("ViewCells.geometryFilename", vcGeomFilename);
[1740]750
[1940]751                // create view cells from specified geometry
[1627]752                if (loadVcGeometry)
[1545]753                {
[1627]754                        if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
[1545]755                        {
[1627]756                                if (!mViewCellsManager->LoadViewCellsGeometry(vcGeomFilename, extrudeBaseTriangles))
757                                {
758                                        cerr << "loading view cells geometry failed" << endl;
759                                }
[1545]760                        }
[1627]761                        else
762                        {
763                                cerr << "loading view cells geometry is not implemented for this manager" << endl;
764                        }
[1545]765                }
[577]766        }
[1740]767
[1486]768        ////////
[1523]769        //-- evaluation of render cost heuristics
[1768]770
[473]771        float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
772
[1004]773        Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost);
774        Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead);
775        Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed);
[1740]776
[473]777        mRenderSimulator =
778                new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
[440]779
[480]780        mViewCellsManager->SetRenderer(mRenderSimulator);
[1785]781       
[1581]782        mViewCellsManager->SetPreprocessor(this);
[1940]783
[463]784        return true;
[490]785}
786
[1292]787 
[1563]788bool Preprocessor::ConstructViewCells()
[1292]789{
[1627]790        // construct view cells using it's own set of samples
791        mViewCellsManager->Construct(this);
792
793        // visualizations and statistics
794        Debug << "finished view cells:" << endl;
795        mViewCellsManager->PrintStatistics(Debug);
796
797        return true;
[1292]798}
[490]799
[1294]800
[575]801ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
802{
[1264]803        ViewCellsTree *vcTree = new ViewCellsTree;
804
[575]805        if (strcmp(name, "kdTree") == 0)
806        {
[1264]807                mViewCellsManager = new KdViewCellsManager(vcTree, mKdTree);
[575]808        }
809        else if (strcmp(name, "bspTree") == 0)
810        {
811                Debug << "view cell type: Bsp" << endl;
812
[577]813                mBspTree = new BspTree();
[1264]814                mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
[575]815        }
816        else if (strcmp(name, "vspBspTree") == 0)
817        {
818                Debug << "view cell type: VspBsp" << endl;
819
[1004]820                mVspBspTree = new VspBspTree();
[1264]821                mViewCellsManager = new VspBspViewCellsManager(vcTree, mVspBspTree);
[575]822        }
[1006]823        else if (strcmp(name, "vspOspTree") == 0)
[575]824        {
[1421]825                Debug << "view cell type: VspOsp" << endl;
[1288]826                char buf[100];         
827                Environment::GetSingleton()->GetStringValue("Hierarchy.type", buf);     
[1022]828
[1740]829                mViewCellsManager = new VspOspViewCellsManager(vcTree, buf);
[575]830        }
[1421]831        else if (strcmp(name, "sceneDependent") == 0) //TODO
[575]832        {
[1143]833                Debug << "view cell type: Bsp" << endl;
[1421]834               
[575]835                mBspTree = new BspTree();
[1264]836                mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
[575]837        }
838        else
839        {
[664]840                cerr << "Wrong view cells type " << name << "!!!" << endl;
[575]841                exit(1);
842        }
843
844        return mViewCellsManager;
845}
846
847
[491]848// use ascii format to store rays
849#define USE_ASCII 0
850
851
[1145]852static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
[490]853{
854        return obj1->mId < obj2->mId;
855}
856
857
[1414]858bool Preprocessor::LoadKdTree(const string filename)
[1197]859{
[1414]860        mKdTree = new KdTree();
[1633]861
[1414]862        return mKdTree->LoadBinTree(filename.c_str(), mObjects);
[1197]863}
864
[1414]865
866bool Preprocessor::ExportKdTree(const string filename)
[1197]867{
[1414]868        return mKdTree->ExportBinTree(filename.c_str());
[1197]869}
870
871
[490]872bool Preprocessor::LoadSamples(VssRayContainer &samples,
873                                                           ObjectContainer &objects) const
874{
875        std::stable_sort(objects.begin(), objects.end(), ilt);
876        char fileName[100];
[1004]877        Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
[490]878       
[491]879    Vector3 origin, termination;
880        // HACK: needed only for lower_bound algorithm to find the
881        // intersected objects
882        MeshInstance sObj(NULL);
883        MeshInstance tObj(NULL);
[490]884
[491]885#if USE_ASCII
[656]886        ifstream samplesIn(fileName);
[490]887        if (!samplesIn.is_open())
888                return false;
889
890        string buf;
891        while (!(getline(samplesIn, buf)).eof())
892        {
[491]893                sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
[490]894                           &origin.x, &origin.y, &origin.z,
[491]895                           &termination.x, &termination.y, &termination.z,
896                           &(sObj.mId), &(tObj.mId));
[490]897               
[491]898                Intersectable *sourceObj = NULL;
899                Intersectable *termObj = NULL;
900               
901                if (sObj.mId >= 0)
[490]902                {
903                        ObjectContainer::iterator oit =
[491]904                                lower_bound(objects.begin(), objects.end(), &sObj, ilt);
905                        sourceObj = *oit;
906                }
907               
908                if (tObj.mId >= 0)
909                {
910                        ObjectContainer::iterator oit =
911                                lower_bound(objects.begin(), objects.end(), &tObj, ilt);
912                        termObj = *oit;
913                }
[490]914
[491]915                samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
916        }
917#else
918        ifstream samplesIn(fileName, ios::binary);
919        if (!samplesIn.is_open())
920                return false;
921
922        while (1)
923        {
924                 samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
925                 samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
926                 samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
927                 samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
928               
929                 if (samplesIn.eof())
930                        break;
931
932                Intersectable *sourceObj = NULL;
933                Intersectable *termObj = NULL;
934               
935                if (sObj.mId >= 0)
936                {
937                        ObjectContainer::iterator oit =
938                                lower_bound(objects.begin(), objects.end(), &sObj, ilt);
939                        sourceObj = *oit;
[490]940                }
[491]941               
942                if (tObj.mId >= 0)
[490]943                {
[491]944                        ObjectContainer::iterator oit =
945                                lower_bound(objects.begin(), objects.end(), &tObj, ilt);
946                        termObj = *oit;
[490]947                }
[491]948
949                samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
[490]950        }
[1779]951#endif
[491]952
[490]953        samplesIn.close();
954
955        return true;
956}
957
[508]958
959bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
[490]960{
[491]961        char fileName[100];
[1004]962        Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
[491]963       
[490]964
965        VssRayContainer::const_iterator it, it_end = samples.end();
966       
[491]967#if USE_ASCII
968        ofstream samplesOut(fileName);
[490]969        if (!samplesOut.is_open())
970                return false;
971
972        for (it = samples.begin(); it != it_end; ++ it)
973        {
974                VssRay *ray = *it;
[491]975                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
976                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;       
977
[490]978                samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
979                                   << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
[491]980                                   << sourceid << " " << termid << "\n";
[490]981        }
[491]982#else
983        ofstream samplesOut(fileName, ios::binary);
984        if (!samplesOut.is_open())
985                return false;
986
987        for (it = samples.begin(); it != it_end; ++ it)
988        {       
989                VssRay *ray = *it;
990                Vector3 origin(ray->GetOrigin());
991                Vector3 termination(ray->GetTermination());
992               
993                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
994                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;               
995
996                samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
997                samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
998                samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
999                samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
1000    }
1001#endif
[490]1002        samplesOut.close();
[1194]1003
[490]1004        return true;
1005}
[563]1006
[1771]1007
1008int
1009Preprocessor::GenerateRays(const int number,
[1867]1010                                                   SamplingStrategy &strategy,
1011                                                   SimpleRayContainer &rays)
[1020]1012{
[1867]1013  return strategy.GenerateSamples(number, rays);
[1771]1014}
[860]1015
[1990]1016
[1771]1017int
1018Preprocessor::GenerateRays(const int number,
[1898]1019                                                   const int sampleType,
1020                                                   SimpleRayContainer &rays)
[1771]1021{
[1772]1022        const int startSize = (int)rays.size();
1023        SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
1024        int castRays = 0;
1025
1026        if (!strategy)
[1381]1027        {
[1772]1028                return 0;
[1381]1029        }
[1772]1030
[1381]1031#if 1
[1772]1032        castRays = strategy->GenerateSamples(number, rays);
[1381]1033#else
[1772]1034        GenerateRayBundle(rays, newRay, 16, 0);
1035        castRays += 16;
[1381]1036#endif
[1772]1037
1038        delete strategy;
1039        return castRays;
[878]1040}
[1020]1041
1042
[1884]1043SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId)
[1020]1044{
1045        switch (strategyId)
1046        {
[1520]1047        case SamplingStrategy::OBJECT_BASED_DISTRIBUTION:
[1020]1048                return new ObjectBasedDistribution(*this);
[1520]1049        case SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION:
[1020]1050                return new ObjectDirectionBasedDistribution(*this);
[1520]1051        case SamplingStrategy::DIRECTION_BASED_DISTRIBUTION:
[1020]1052                return new DirectionBasedDistribution(*this);
[1520]1053        case SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION:
[1020]1054                return new DirectionBoxBasedDistribution(*this);
[1520]1055        case SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION:
[1020]1056                return new SpatialBoxBasedDistribution(*this);
[1695]1057        case SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION:
1058                return new ReverseObjectBasedDistribution(*this);
[1769]1059        case SamplingStrategy::VIEWCELL_BORDER_BASED_DISTRIBUTION:
1060                return new ViewCellBorderBasedDistribution(*this);
[1772]1061        case SamplingStrategy::VIEWSPACE_BORDER_BASED_DISTRIBUTION:
1062                return new ViewSpaceBorderBasedDistribution(*this);
1063        case SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION:
1064                return new ReverseViewSpaceBorderBasedDistribution(*this);
[1824]1065        case SamplingStrategy::GLOBAL_LINES_DISTRIBUTION:
1066                return new GlobalLinesDistribution(*this);
1067               
1068                //case OBJECTS_INTERIOR_DISTRIBUTION:
1069                //      return new ObjectsInteriorDistribution(*this);
[1020]1070        default: // no valid strategy
[1279]1071                Debug << "warning: no valid sampling strategy" << endl;
[1020]1072                return NULL;
1073        }
[1221]1074
[1570]1075        return NULL; // should never come here
[1020]1076}
1077
1078
[1968]1079bool Preprocessor::InitRayCast(const string externKdTree,
1080                                                           const string internKdTree)
[1221]1081{
[1633]1082        // always try to load the kd tree
[1664]1083        cout << "loading kd tree file " << internKdTree << " ... " << endl;
[1634]1084
1085        if (!LoadKdTree(internKdTree))
1086        {
[1968]1087                cout << "error loading kd tree with filename "
1088                         << internKdTree << ", rebuilding it instead ... " << endl;
[1633]1089                // build new kd tree from scene geometry
[1415]1090                BuildKdTree();
1091
[1627]1092                // export kd tree?
[1633]1093                const long startTime = GetTime();
[1627]1094                cout << "exporting kd tree ... ";
1095
1096                if (!ExportKdTree(internKdTree))
1097                {
[1968]1098                        cout << " error exporting kd tree with filename "
1099                                 << internKdTree << endl;
[1627]1100                }
1101                else
1102                {
[1968]1103                        cout << "finished in "
1104                                 << TimeDiff(startTime, GetTime()) * 1e-3
1105                                 << " secs" << endl;
[1627]1106                }
[1415]1107        }
[1633]1108       
1109        KdTreeStatistics(cout);
1110        cout << mKdTree->GetBox() << endl;
[1415]1111
[1633]1112        if (0)
1113        {
1114                Exporter *exporter = Exporter::GetExporter("dummykd.x3d");
1115                       
1116                if (exporter)
1117                {
1118                        exporter->ExportKdTree(*mKdTree, true);
1119                        delete exporter;
1120                }
1121        }
1122
[1520]1123        int rayCastMethod;
[1968]1124        Environment::GetSingleton()->
1125                GetIntValue("Preprocessor.rayCastMethod", rayCastMethod);
[1520]1126
1127        if (rayCastMethod == 0)
[1221]1128        {
[1867]1129                cout << "ray cast method: internal" << endl;
[1520]1130                mRayCaster = new InternalRayCaster(*this, mKdTree);
[1221]1131        }
[1251]1132        else
[1520]1133        {
[1524]1134#ifdef GTP_INTERNAL
[1867]1135          cout << "ray cast method: intel" << endl;
1136          mRayCaster = new IntelRayCaster(*this, externKdTree);
[1524]1137#endif
[1251]1138        }
[1867]1139       
[1520]1140        return true;
[1281]1141}
1142
[1292]1143
[1281]1144void
1145Preprocessor::CastRays(
1146                                           SimpleRayContainer &rays,
[1520]1147                                           VssRayContainer &vssRays,
[1528]1148                                           const bool castDoubleRays,
[1990]1149                                           const bool pruneInvalidRays,
1150                                           const bool keepOrigin
[1281]1151                                           )
1152{
[1344]1153
[1974]1154
1155  const long t1 = GetTime();
1156
1157#if 0
1158  mRayCaster->SortRays(rays);
1159  cout<<"Rays sorted in "<<TimeDiff(t1, GetTime())<<" s."<<endl;
[1984]1160
1161  if (1) {
1162        VssRayContainer tmpRays;
1163        for (int i=0; i < 200; i++) {
1164          tmpRays.push_back(new VssRay(rays[i].mOrigin,
1165                                                                   rays[i].mOrigin + 100.0f*rays[i].mDirection,
1166                                                                   NULL,
1167                                                                   NULL
1168                                                                   )
1169                                                );
1170         
1171        }
1172        ExportRays("sorted_rays.x3d", tmpRays, 200);
1173  }
[1974]1174#endif
1175 
[1972]1176        SimpleRayContainer::const_iterator rit, rit_end = rays.end();
1177
1178        SimpleRayContainer rayBucket;
1179        int i = 0;
1180        for (rit = rays.begin(); rit != rit_end; ++ rit, ++ i)
[1292]1181        {
[1972]1182                SimpleRay ray = *rit;
[1990]1183#ifdef USE_CG
[1972]1184                // HACK: global lines must be treated special
1185                if (ray.mDistribution == SamplingStrategy::HW_GLOBAL_LINES_DISTRIBUTION)
[1968]1186                {
[1972]1187                        mGlobalLinesRenderer->CastGlobalLines(ray, vssRays);
[1968]1188                        continue;
1189                }
[1990]1190#endif
[1972]1191                rayBucket.push_back(ray);
1192
1193                // 16 rays gathered => do ray casting
1194                if ((int)rayBucket.size() >= 16)
[1520]1195                {
[1524]1196                        mRayCaster->CastRays16(
[1972]1197                                                                   rayBucket,                           
[1584]1198                                                                   vssRays,
1199                                                                   mViewCellsManager->GetViewSpaceBox(),
1200                                                                   castDoubleRays,
[1990]1201                                                                   pruneInvalidRays,
1202                                                                   keepOrigin);
[1972]1203
1204                        rayBucket.clear();
[1292]1205                }
[1972]1206       
1207                if ((int)rays.size() > 10000 && i % 10000 == 0)
[1984]1208                  cout<<"\r"<<i<<"/"<<(int)rays.size()<<"\r";
[1972]1209        }
1210   
1211        // cast rest of rays
1212        SimpleRayContainer::const_iterator sit, sit_end = rayBucket.end();
1213
1214        for (sit = rayBucket.begin(); sit != sit_end; ++ sit)
1215        {
1216                SimpleRay ray = *sit;
[1990]1217
1218#ifdef USE_CG
[1972]1219                // HACK: global lines must be treated special
1220                if (ray.mDistribution == SamplingStrategy::HW_GLOBAL_LINES_DISTRIBUTION)
[1932]1221                {
[1972]1222                        mGlobalLinesRenderer->CastGlobalLines(ray, vssRays);
[1990]1223                        continue;
[1972]1224                }
[1990]1225#endif
1226                mRayCaster->CastRay(
1227                                                        ray,
1228                                                        vssRays,
1229                                                        mViewCellsManager->GetViewSpaceBox(),
1230                                                        castDoubleRays,
1231                                                        pruneInvalidRays,
1232                                                        keepOrigin);
1233               
[1524]1234        }
[1743]1235
[1972]1236        if ((int)rays.size() > 10000)
[1932]1237        {
[1968]1238                cout << endl;
[1972]1239        long t2 = GetTime();
[1286]1240
[1584]1241#if SHOW_RAYCAST_TIMING
[1968]1242                if (castDoubleRays)
[1972]1243                        cout << 2 * rays.size() / (1e3f * TimeDiff(t1, t2)) << "M rays/s" << endl;
[1968]1244                else
1245                        cout << rays.size() / (1e3f * TimeDiff(t1, t2)) << "M rays/s" << endl;
[1743]1246#endif
1247        }
[1251]1248}
1249
1250
[1381]1251bool Preprocessor::GenerateRayBundle(SimpleRayContainer &rayBundle,                                                                     
1252                                                                         const SimpleRay &mainRay,
1253                                                                         const int number,
1254                                                                         const int pertubType) const
1255{
1256        rayBundle.push_back(mainRay);
1257
[1564]1258        const float pertubOrigin = 0.0f;
1259        const float pertubDir = 0.2f;
[1381]1260
1261        for (int i = 0; i < number - 1; ++ i)
1262        {
1263                Vector3 pertub;
1264
1265                pertub.x = RandomValue(0.0f, pertubDir);
1266                pertub.y = RandomValue(0.0f, pertubDir);
1267                pertub.z = RandomValue(0.0f, pertubDir);
[1713]1268
[1381]1269                const Vector3 newDir = mainRay.mDirection + pertub;
1270                //const Vector3 newDir = mainRay.mDirection;
1271
1272                pertub.x = RandomValue(0.0f, pertubOrigin);
1273                pertub.y = RandomValue(0.0f, pertubOrigin);
1274                pertub.z = RandomValue(0.0f, pertubOrigin);
[1713]1275
[1381]1276                const Vector3 newOrigin = mainRay.mOrigin + pertub;
1277                //const Vector3 newOrigin = mainRay.mOrigin;
1278
[1883]1279                rayBundle.push_back(SimpleRay(newOrigin, newDir, 0, 1.0f));
[1381]1280        }
1281
1282        return true;
1283}
1284
1285
[1251]1286void Preprocessor::SetupRay(Ray &ray,
1287                                                        const Vector3 &point,
[1713]1288                                                        const Vector3 &direction) const
[1251]1289{
1290        ray.Clear();
1291        // do not store anything else then intersections at the ray
[1713]1292        ray.Init(point, direction, Ray::LOCAL_RAY);     
[1251]1293}
1294
[1968]1295
[1917]1296void Preprocessor::EvalViewCellHistogram()
[1771]1297{
[1917]1298        char filename[256];
1299        Environment::GetSingleton()->GetStringValue("Preprocessor.histogram.file", filename);
[1771]1300 
[1917]1301        // mViewCellsManager->EvalViewCellHistogram(filename, 1000000);
1302        mViewCellsManager->EvalViewCellHistogramForPvsSize(filename, 1000000);
[1771]1303}
[1520]1304
[1900]1305
1306bool
1307Preprocessor::ExportRays(const char *filename,
1308                                                 const VssRayContainer &vssRays,
1309                                                 const int number
1310                                                 )
1311{
1312  cout<<"Exporting vss rays..."<<endl<<flush;
1313 
1314  Exporter *exporter = NULL;
1315  exporter = Exporter::GetExporter(filename);
1316
1317  if (0) {
1318        exporter->SetWireframe();
1319        exporter->ExportKdTree(*mKdTree);
1320  }
1321 
1322  exporter->SetFilled();
1323  // $$JB temporarily do not export the scene
1324  if (0)
1325        exporter->ExportScene(mSceneGraph->GetRoot());
1326
1327  exporter->SetWireframe();
1328
1329  if (1) {
1330        exporter->SetForcedMaterial(RgbColor(1,0,1));
1331        exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
1332        exporter->ResetForcedMaterial();
1333  }
1334 
1335  VssRayContainer rays;
1336  vssRays.SelectRays(number, rays);
1337  exporter->ExportRays(rays, RgbColor(1, 0, 0));
1338  delete exporter;
1339  cout<<"done."<<endl<<flush;
1340
1341  return true;
[1251]1342}
[1900]1343
[1931]1344void
1345Preprocessor::ComputeRenderError()
1346{
1347  // compute rendering error
1348       
1349  if (renderer && renderer->mPvsStatFrames) {
1350        //      emit EvalPvsStat();
1351        //      QMutex mutex;
1352        //      mutex.lock();
1353        //      renderer->mRenderingFinished.wait(&mutex);
1354        //      mutex.unlock();
1355       
1356        renderer->EvalPvsStat();
1357
1358        mStats <<
1359          "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
1360          "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
1361          "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
1362          "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
1363  }
[1900]1364}
[1931]1365
[1958]1366
1367Intersectable *Preprocessor::GetObjectById(const int id)
1368{
[1960]1369#if 1
[1958]1370        // create a dummy mesh instance to be able to use stl
1371        MeshInstance object(NULL);
1372        object.SetId(id);
1373
1374        ObjectContainer::const_iterator oit =
1375                lower_bound(mObjects.begin(), mObjects.end(), &object, ilt);
1376                               
1377        // objects sorted by id
1378        if ((oit != mObjects.end()) && ((*oit)->GetId() == object.GetId()))
1379        {
1380                return (*oit);
1381        }
1382        else
1383        {
1384                return NULL;
1385        }
1386#else
[1960]1387        return mObjects[id - 1];
[1958]1388#endif
[1931]1389}
[1958]1390
[1968]1391
1392void Preprocessor::PrepareHwGlobalLines()
1393{
1394        int texHeight, texWidth;
1395        float eps;
1396        int maxDepth;
1397        bool sampleReverse;
1398
1399        Environment::GetSingleton()->GetIntValue("Preprocessor.HwGlobalLines.texHeight", texHeight);
[1969]1400        Environment::GetSingleton()->GetIntValue("Preprocessor.HwGlobalLines.texWidth", texWidth);
[1968]1401        Environment::GetSingleton()->GetFloatValue("Preprocessor.HwGlobalLines.stepSize", eps);
1402        Environment::GetSingleton()->GetIntValue("Preprocessor.HwGlobalLines.maxDepth", maxDepth);
1403        Environment::GetSingleton()->GetBoolValue("Preprocessor.HwGlobalLines.sampleReverse", sampleReverse);
1404
[1969]1405        Debug << "****** hw global line options *******" << endl;
1406        Debug << "texWidth: " << texWidth << endl;
1407        Debug << "texHeight: " << texHeight << endl;
1408        Debug << "sampleReverse: " << sampleReverse << endl;
1409        Debug << "max depth: " << maxDepth << endl;
1410        Debug << "step size: " << eps << endl;
1411        Debug << endl;
1412
[1990]1413#ifdef USE_CG
[1969]1414        globalLinesRenderer = mGlobalLinesRenderer =
[1968]1415                new GlobalLinesRenderer(this,
1416                                                                texHeight,
1417                                                                texWidth,
1418                                                                eps,
1419                                                                maxDepth,
1420                                                                sampleReverse);
[1990]1421
[1968]1422        mGlobalLinesRenderer->InitGl();
[1990]1423
1424#endif
[1958]1425}
[1968]1426
1427}
Note: See TracBrowser for help on using the repository browser.