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

Revision 2001, 36.1 KB checked in by bittner, 17 years ago (diff)

mutation updates

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