source: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp @ 1949

Revision 1949, 165.0 KB checked in by mattausch, 17 years ago (diff)

worked on depth peeling

Line 
1#include "ViewCellsManager.h"
2#include "RenderSimulator.h"
3#include "Mesh.h"
4#include "Triangle3.h"
5#include "ViewCell.h"
6#include "Environment.h"
7#include "X3dParser.h"
8#include "ViewCellBsp.h"
9#include "KdTree.h"
10#include "HierarchyManager.h"
11#include "Exporter.h"
12#include "VspBspTree.h"
13#include "ViewCellsParser.h"
14#include "Beam.h"
15#include "VssPreprocessor.h"
16#include "RssPreprocessor.h"
17#include "BoundingBoxConverter.h"
18#include "GlRenderer.h"
19#include "ResourceManager.h"
20#include "IntersectableWrapper.h"
21#include "VspTree.h"
22#include "OspTree.h"
23#include "BvHierarchy.h"
24#include "SamplingStrategy.h"
25#include "SceneGraph.h"
26
27
28// $$JB HACK
29#define KD_PVS_AREA (1e-5f)
30
31#define USE_RAY_LENGTH_AS_CONTRIBUTION 0
32#define DIST_WEIGHTED_CONTRIBUTION 0
33#define SUM_RAY_CONTRIBUTIONS 1
34#define AVG_RAY_CONTRIBUTIONS 0
35#define CONTRIBUTION_RELATIVE_TO_PVS_SIZE 0
36#define PVS_ADD_DIRTY 1
37
38namespace GtpVisibilityPreprocessor {
39
40
41// HACK
42const static bool SAMPLE_AFTER_SUBDIVISION = true;
43//const static bool CLAMP_TO_BOX = false;
44const static bool CLAMP_TO_BOX = true;
45
46
47int ViewCellsManager::sRenderCostEvaluationType = 0;
48
49
50template <typename T> class myless
51{
52public:
53        //bool operator() (HierarchyNode *v1, HierarchyNode *v2) const
54        bool operator() (T v1, T v2) const
55        {
56                return (v1->GetMergeCost() < v2->GetMergeCost());
57        }
58};
59
60
61ViewCellsManager::ViewCellsManager(ViewCellsTree *viewCellsTree):
62mRenderer(NULL),
63mInitialSamples(0),
64mConstructionSamples(0),
65mPostProcessSamples(0),
66mVisualizationSamples(0),
67mTotalAreaValid(false),
68mTotalArea(0.0f),
69mViewCellsFinished(false),
70mMaxPvsSize(9999999),
71mMinPvsSize(0),
72mMaxPvsRatio(1.0),
73mViewCellPvsIsUpdated(false),
74mPreprocessor(NULL),
75mViewCellsTree(viewCellsTree),
76mUsePredefinedViewCells(false)
77{
78        mViewSpaceBox.Initialize();
79        ParseEnvironment();
80
81        mViewCellsTree->SetViewCellsManager(this);
82}
83
84
85int ViewCellsManager::CastPassSamples2(const int samplesPerPass,
86                                                                           VssRayContainer &passSamples) const
87{
88        long startTime = GetTime();
89
90        SimpleRayContainer simpleRays;
91       
92        simpleRays.reserve(samplesPerPass);
93        passSamples.reserve(samplesPerPass * 2); // always creates double rays
94
95        // create one third of each type
96        int castRays = 0;
97
98        mMixtureDistribution->GenerateSamples(mSamplesPerPass, simpleRays);
99
100        cout << "generated " << (int)simpleRays.size() << " samples in "
101                 << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
102
103        startTime = GetTime();
104
105        // shoot simple ray and add it to importance samples
106        mPreprocessor->CastRays(simpleRays, passSamples, true);
107
108    cout << "cast " <<  samplesPerPass << " samples in "
109                 << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
110
111        mMixtureDistribution->ComputeContributions(passSamples);
112
113        return (int)passSamples.size();
114}
115
116
117void ViewCellsManager::ParseEnvironment()
118{
119        // visualization stuff
120        Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.exportRays", mExportRays);
121        Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.exportGeometry", mExportGeometry);
122        Environment::GetSingleton()->GetFloatValue("ViewCells.maxPvsRatio", mMaxPvsRatio);
123       
124        Environment::GetSingleton()->GetBoolValue("ViewCells.processOnlyValidViewCells", mOnlyValidViewCells);
125
126        Environment::GetSingleton()->GetIntValue("ViewCells.Construction.samples", mConstructionSamples);
127        Environment::GetSingleton()->GetIntValue("ViewCells.PostProcess.samples", mPostProcessSamples);
128        Environment::GetSingleton()->GetBoolValue("ViewCells.PostProcess.useRaysForMerge", mUseRaysForMerge);
129
130        Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.samples", mVisualizationSamples);
131
132        Environment::GetSingleton()->GetIntValue("ViewCells.Construction.samplesPerPass", mSamplesPerPass);
133        Environment::GetSingleton()->GetBoolValue("ViewCells.exportToFile", mExportViewCells);
134       
135        Environment::GetSingleton()->GetIntValue("ViewCells.active", mNumActiveViewCells);
136        Environment::GetSingleton()->GetBoolValue("ViewCells.PostProcess.compress", mCompressViewCells);
137        Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.useClipPlane", mUseClipPlaneForViz);
138        Environment::GetSingleton()->GetBoolValue("ViewCells.PostProcess.merge", mMergeViewCells);
139        Environment::GetSingleton()->GetBoolValue("ViewCells.evaluateViewCells", mEvaluateViewCells);
140        Environment::GetSingleton()->GetBoolValue("ViewCells.showVisualization", mShowVisualization);
141        Environment::GetSingleton()->GetIntValue("ViewCells.Filter.maxSize", mMaxFilterSize);
142        Environment::GetSingleton()->GetFloatValue("ViewCells.Filter.width", mFilterWidth);
143        Environment::GetSingleton()->GetIntValue("ViewCells.renderCostEvaluationType", sRenderCostEvaluationType);
144
145        Environment::GetSingleton()->GetBoolValue("ViewCells.exportBboxesForPvs", mExportBboxesForPvs);
146        Environment::GetSingleton()->GetBoolValue("ViewCells.exportPvs", mExportPvs);
147
148        Environment::GetSingleton()->GetBoolValue("ViewCells.useKdPvs", mUseKdPvs);
149
150        char buf[100];
151        Environment::GetSingleton()->GetStringValue("ViewCells.samplingType", buf);
152
153        // mix of sampling strategies
154        if (0)
155        {
156                mStrategies.push_back(SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION);
157        }
158        else
159        {
160                /*mStrategies.push_back(SamplingStrategy::OBJECT_BASED_DISTRIBUTION);
161                mStrategies.push_back(SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION);
162                mStrategies.push_back(SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION);
163                */
164       
165                mStrategies.push_back(SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION);
166        }
167               
168    Debug << "casting strategies: ";
169        for (int i = 0; i < (int)mStrategies.size(); ++ i)
170                Debug << mStrategies[i] << " ";
171        Debug << endl;
172
173        // now decode distribution string
174        char buff[1024];
175        Environment::GetSingleton()->GetStringValue("RssPreprocessor.distributions", buff);
176
177        mMixtureDistribution = new MixtureDistribution(*mPreprocessor);
178        mMixtureDistribution->Construct(buff);
179
180
181        // sampling type for view cells construction samples
182        if (strcmp(buf, "object") == 0)
183        {
184                mSamplingType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
185        }
186        else if (strcmp(buf, "box") == 0)
187        {
188                mSamplingType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
189        }
190        else if (strcmp(buf, "directional") == 0)
191        {
192                mSamplingType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
193        }
194        else if (strcmp(buf, "object_directional") == 0)
195        {
196                mSamplingType = SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION;
197        }
198        else if (strcmp(buf, "reverse_object") == 0)
199        {
200                mSamplingType = SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION;
201        }
202        /*else if (strcmp(buf, "interior") == 0)
203        {
204                mSamplingType = Preprocessor::OBJECTS_INTERIOR_DISTRIBUTION;
205        }*/
206        else
207        {
208                Debug << "error! wrong sampling type" << endl;
209                exit(0);
210        }
211
212        // sampling type for evaluation samples
213        Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.samplingType", buf);
214       
215        if (strcmp(buf, "object") == 0)
216        {
217                mEvaluationSamplingType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
218        }
219        else if (strcmp(buf, "box") == 0)
220        {
221                mEvaluationSamplingType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
222        }
223        else if (strcmp(buf, "directional") == 0)
224        {
225                mEvaluationSamplingType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
226        }
227        else if (strcmp(buf, "object_directional") == 0)
228        {
229                mEvaluationSamplingType = SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION;
230        }
231        else if (strcmp(buf, "reverse_object") == 0)
232        {
233                mEvaluationSamplingType = SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION;
234        }
235        /*else if (strcmp(buf, "interior") == 0)
236        {
237                mEvaluationSamplingType = SamplingStrategy::OBJECTS_INTERIOR_DISTRIBUTION;
238        }*/
239        else
240        {
241                mEvaluationSamplingType = -1;
242                Debug << "error! wrong sampling type" << endl;
243                exit(0);
244        }
245
246        Environment::GetSingleton()->GetStringValue("ViewCells.renderCostEvaluationType", buf);
247       
248        if (strcmp(buf, "perobject") == 0)
249        {
250                sRenderCostEvaluationType = ViewCellsManager::PER_OBJECT;
251        }
252        else if (strcmp(buf, "pertriangle") == 0)
253        {
254                sRenderCostEvaluationType = ViewCellsManager::PER_TRIANGLE;
255        }
256        else
257        {
258                Debug << "error! wrong sampling type" << endl;
259                exit(0);
260        }
261
262    Environment::GetSingleton()->GetStringValue("ViewCells.Visualization.colorCode", buf);
263
264        if (strcmp(buf, "PVS") == 0)
265                mColorCode = 1;
266        else if (strcmp(buf, "MergedLeaves") == 0)
267                mColorCode = 2;
268        else if (strcmp(buf, "MergedTreeDiff") == 0)
269                mColorCode = 3;
270        else
271                mColorCode = 0;
272
273
274        Debug << "************ View Cells Manager options ***************" << endl;
275        Debug << "color code: " << mColorCode << endl;
276
277        Debug << "export rays: " << mExportRays << endl;
278        Debug << "export geometry: " << mExportGeometry << endl;
279        Debug << "max pvs ratio: " << mMaxPvsRatio << endl;
280       
281        Debug << "process only valid view cells: " << mOnlyValidViewCells << endl;
282        Debug << "construction samples: " << mConstructionSamples << endl;
283        Debug << "post process samples: " << mPostProcessSamples << endl;
284        Debug << "post process use rays for merge: " << mUseRaysForMerge << endl;
285        Debug << "visualization samples: " << mVisualizationSamples << endl;
286        Debug << "construction samples per pass: " << mSamplesPerPass << endl;
287        Debug << "export to file: " << mExportViewCells << endl;
288       
289        Debug << "active view cells: " << mNumActiveViewCells << endl;
290        Debug << "post process compress: " << mCompressViewCells << endl;
291        Debug << "visualization use clipPlane: " << mUseClipPlaneForViz << endl;
292        Debug << "post process merge: " << mMergeViewCells << endl;
293        Debug << "evaluate view cells: " << mEvaluateViewCells << endl;
294        Debug << "sampling type: " << mSamplingType << endl;
295        Debug << "render cost evaluation type: " << sRenderCostEvaluationType << endl;
296        Debug << "evaluation sampling type: " << mEvaluationSamplingType << endl;
297        Debug << "show visualization: " << mShowVisualization << endl;
298        Debug << "filter width: " << mFilterWidth << endl;
299        Debug << "sample after subdivision: " << SAMPLE_AFTER_SUBDIVISION << endl;
300
301        Debug << "export bounding boxes: " << mExportBboxesForPvs << endl;
302        Debug << "export pvs for view cells: " << mExportPvs << endl;
303        Debug << "use kd pvs " << mUseKdPvs << endl;
304        Debug << endl;
305}
306
307
308ViewCellsManager::~ViewCellsManager()
309{
310        // HACK: if view cells tree does not
311        // handle view cells, we have to do it here
312        // question: rather create view cells resource manager?
313        if (!ViewCellsTreeConstructed())
314        {
315                CLEAR_CONTAINER(mViewCells);
316        }
317        else
318        {
319                DEL_PTR(mViewCellsTree);
320        }
321
322        DEL_PTR(mMixtureDistribution);
323}
324
325
326Intersectable *
327ViewCellsManager::GetIntersectable(const VssRay &ray, const bool isTermination) const
328{
329        if (mUseKdPvs)
330        {
331                float area = GetPreprocessor()->mKdTree->GetBox().SurfaceArea()*KD_PVS_AREA;
332                KdNode *node = GetPreprocessor()->mKdTree->GetNode(isTermination ?
333                                                                                                                 ray.mTermination : ray.mOrigin,
334                                                                                                                 area);
335        return GetPreprocessor()->mKdTree->GetOrCreateKdIntersectable(node);
336        }
337        else
338        {
339                return isTermination ? ray.mTerminationObject : ray.mOriginObject;
340        }
341}
342
343
344void
345ViewCellsManager::CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects)
346{
347  GetPreprocessor()->mKdTree->CollectObjects(box, objects);
348}
349
350AxisAlignedBox3 ViewCellsManager::GetViewCellBox(ViewCell *vc)
351{
352  Mesh *m = vc->GetMesh();
353 
354  if (m)
355  {
356          m->ComputeBoundingBox();
357          return m->mBox;
358  }
359
360  AxisAlignedBox3 box;
361  box.Initialize();
362 
363  if (!vc->IsLeaf()) {
364        ViewCellInterior *vci = (ViewCellInterior *) vc;
365       
366        ViewCellContainer::iterator it = vci->mChildren.begin();
367        for (; it != vci->mChildren.end(); ++it) {
368          box.Include(GetViewCellBox(*it));
369        }
370  }
371 
372  return box;
373}
374
375
376int ViewCellsManager::CastPassSamples(const int samplesPerPass,
377                                                                          const vector<int> &strategies,
378                                                                          VssRayContainer &passSamples
379                                                                          ) const
380{
381        long startTime = GetTime();
382
383        SimpleRayContainer simpleRays;
384       
385        simpleRays.reserve(samplesPerPass);
386        passSamples.reserve(samplesPerPass * 2); // always creates double rays
387
388        // create one third of each type
389        int castRays = 0;
390       
391        const int numRaysPerPass = samplesPerPass / (int)strategies.size();
392        vector<int>::const_iterator iit, iit_end = strategies.end();
393
394        for (iit = strategies.begin(); iit != iit_end; ++ iit)
395        {
396                const int stype = *iit;
397                const int newRays =
398                        mPreprocessor->GenerateRays(numRaysPerPass, stype, simpleRays);
399
400                cout << "cast " << newRays << " rays of strategy " << stype << endl;
401                castRays += newRays;
402        }
403
404        cout << "generated " << samplesPerPass << " samples in "
405                 << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
406
407        startTime = GetTime();
408
409        // shoot simple ray and add it to importance samples
410        mPreprocessor->CastRays(simpleRays, passSamples, true);
411        cout << "cast " <<  samplesPerPass << " samples in "
412                 << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
413
414        return (int)passSamples.size();
415}
416
417
418/// helper function which destroys rays or copies them into the output ray container
419inline void disposeRays(VssRayContainer &rays, VssRayContainer *outRays)
420{
421        cout << "disposing samples ... ";
422        long startTime = GetTime();
423        int n = (int)rays.size();
424
425        if (outRays)
426        {
427                VssRayContainer::const_iterator it, it_end = rays.end();
428                for (it = rays.begin(); it != it_end; ++ it)
429                {
430                        outRays->push_back(*it);
431                }
432        }
433        else
434        {
435                VssRayContainer::const_iterator it, it_end = rays.end();
436                for (it = rays.begin(); it != it_end; ++ it)
437                {
438                        if (!(*it)->IsActive())
439                                delete (*it);
440                }
441        }
442
443        cout << "finished" << endl;
444        Debug << "disposed " << n << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
445}
446
447
448int ViewCellsManager::Construct(Preprocessor *preprocessor, VssRayContainer *outRays)
449{
450        int numSamples = 0;
451
452        SimpleRayContainer simpleRays;
453        VssRayContainer initialSamples;
454
455        // store pointer to preprocessor for further use during construction
456        mPreprocessor = preprocessor;
457       
458
459        ///////////////////////////////////////////////////////
460        //-- Initial sampling for the construction of the view cell hierarchy.
461        //-- We use uniform sampling / box based sampling.
462       
463        long startTime = GetTime();
464        cout << "view cell construction: casting " << mInitialSamples << " initial samples ... " << endl;
465
466        // cast initial samples
467       
468        // mix of sampling strategies
469        vector<int>dummy;
470        //dummy.push_back(SamplingStrategy::OBJECT_BASED_DISTRIBUTION);
471        //dummy.push_back(SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION);
472        //dummy.push_back(SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION);
473
474        CastPassSamples(mInitialSamples, mStrategies, initialSamples);
475
476        cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
477
478        // construct view cells
479        ConstructSubdivision(preprocessor->mObjects, initialSamples);
480
481        // initial samples count for overall samples ...
482        numSamples += mInitialSamples;
483
484        // rays can be passed or deleted
485        disposeRays(initialSamples, outRays);
486
487        cout << "time needed for initial construction: "
488                 << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
489
490        Debug << "time needed for initial construction: "
491                  << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
492
493        // collect view cells and compute statistics
494        ResetViewCells();
495
496
497        ///////////////////
498        //-- Initial hierarchy construction finished.
499        //-- We can do some stats and visualization
500       
501        if (0)
502        {
503                //-- export initial view cell partition
504                Debug << "\nView cells after initial sampling:\n" << mCurrentViewCellsStats << endl;
505
506                const string filename("viewcells.wrl");
507                Exporter *exporter = Exporter::GetExporter(filename.c_str());
508       
509                if (exporter)
510                {
511                        cout << "exporting initial view cells (=leaves) to " << filename.c_str() << " ... ";
512
513                        if (mExportGeometry)
514                        {
515                                exporter->ExportGeometry(preprocessor->mObjects);
516                        }
517
518                        exporter->SetWireframe();
519                        ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
520
521                        delete exporter;
522                        cout << "finished" << endl;
523                }
524        }
525
526
527        //////////////////////
528        //-- Cast some more sampling after initial construction.
529        //-- The additional rays can be used to gain
530        //-- some more information before the bottom-up merge
531        //-- note: guided rays could be used for this task
532
533        // time spent after construction of the initial partition
534        startTime = GetTime();
535        const int n = mConstructionSamples; //+initialSamples;
536
537        while (numSamples < n)
538        {
539                cout << "casting " << mSamplesPerPass << " samples of " << n << " ... ";
540                Debug << "casting " << mSamplesPerPass << " samples of " << n << " ... ";
541
542                VssRayContainer constructionSamples;
543
544                // cast new samples
545                numSamples += CastPassSamples(mSamplesPerPass,
546                                                                          mStrategies,
547                                                                          constructionSamples);
548
549                cout << "finished" << endl;
550                cout << "computing sample contribution for " << (int)constructionSamples.size() << " samples ... ";
551
552                // computes sample contribution of cast rays TODO: leak?
553                if (SAMPLE_AFTER_SUBDIVISION)
554                        ComputeSampleContributions(constructionSamples, true, false);
555
556                cout << "finished" << endl;
557
558                disposeRays(constructionSamples, outRays);
559                cout << "total samples: " << numSamples << endl;
560        }
561
562        if (0)
563        {
564                ///////////////
565                //-- Get stats after the additional sampling step
566                //-- and before the bottom-up merge step
567
568                EvaluateViewCellsStats();
569                Debug << "\noriginal view cell partition before post process:\n"
570                          << mCurrentViewCellsStats << endl;
571       
572                mRenderer->RenderScene();
573                SimulationStatistics ss;
574                dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
575
576                Debug << ss << endl;
577        }
578
579        ////////////////////
580        //-- post processing of the initial construction
581        //-- We can bottom-up merge the view cells in this step
582        //-- We can additionally cast some post processing sample rays.
583        //-- These rays can be used to store the view cells with the rays
584
585        VssRayContainer postProcessSamples;
586        cout << "casting " << mPostProcessSamples << " post processing samples ... ";
587       
588        CastPassSamples(mPostProcessSamples, mStrategies, postProcessSamples);
589
590        cout << "finished" << endl;
591        cout << "starting post processing and visualization" << endl;
592
593        // store view cells with rays for post processing?
594        const bool storeViewCells = true;
595
596        if (SAMPLE_AFTER_SUBDIVISION)
597                ComputeSampleContributions(postProcessSamples, true, storeViewCells);
598
599        PostProcess(preprocessor->mObjects, postProcessSamples);
600
601        const float secs = TimeDiff(startTime, GetTime()) * 1e-3f;
602        cout << "post processing (=merge) finished in " << secs << " secs" << endl;
603
604        Debug << "post processing time: " << secs << endl;
605        disposeRays(postProcessSamples, outRays);
606
607
608        ////////////////
609        //-- Evaluation of the resulting view cell partition.
610        //-- We cast a number of new samples and measure the render cost
611
612        if (mEvaluateViewCells)
613        {
614                EvalViewCellPartition();
615        }
616       
617        /////////////////
618        //-- Show some visualizations
619
620        if (mShowVisualization)
621        {
622                if (0)
623                {
624                        mStrategies.clear();
625                        mStrategies.push_back(SamplingStrategy::VIEWCELL_BORDER_BASED_DISTRIBUTION);
626                }
627
628                ///////////////
629                //-- visualization rays, e.g., to show some samples in the scene
630               
631                VssRayContainer visSamples;
632                int numSamples = CastPassSamples(mVisualizationSamples,
633                                                                                 mStrategies,
634                                                                                 visSamples);
635
636                if (SAMPLE_AFTER_SUBDIVISION)
637                        ComputeSampleContributions(visSamples, true, storeViewCells);
638
639                // various visualizations
640                Visualize(preprocessor->mObjects, visSamples);
641
642                disposeRays(visSamples, outRays);
643        }
644
645        // recalculate view cells
646        EvaluateViewCellsStats();
647
648        if (1) CompressViewCells();
649
650        // write view cells to disc
651        if (mExportViewCells)
652        {
653                char filename[100];
654
655                Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename);
656                ExportViewCells(filename, mExportPvs, mPreprocessor->mObjects);
657        }
658
659        return numSamples;
660}
661
662
663AxisAlignedPlane *ViewCellsManager::GetClipPlane()
664{
665        return mUseClipPlaneForViz ? &mClipPlaneForViz : NULL;
666}
667
668
669void ViewCellsManager::EvalViewCellHistogram(const string filename,
670                                                                                         const int nViewCells)
671{
672        std::ofstream outstream;
673        outstream.open(filename.c_str());
674
675        ViewCellContainer viewCells;
676        // $$ JB hack - the collect best viewcells does not work?
677#if 1
678        mViewCellsTree->CollectBestViewCellSet(viewCells, nViewCells);
679#else
680        viewCells = mViewCells;
681#endif
682
683        float maxRenderCost, minRenderCost;
684
685        // sort by render cost
686        sort(viewCells.begin(), viewCells.end(), ViewCell::SmallerRenderCost);
687
688        minRenderCost = viewCells.front()->GetRenderCost();
689        maxRenderCost = viewCells.back()->GetRenderCost();
690
691        Debug << "histogram min rc: " << minRenderCost << " max rc: " << maxRenderCost << endl;
692
693    int histoIntervals;
694        Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.intervals", histoIntervals);
695        const int intervals = min(histoIntervals, (int)viewCells.size());
696
697        int histoMaxVal;
698        Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
699        maxRenderCost = max((float)histoMaxVal, maxRenderCost);
700
701       
702        const float range = maxRenderCost - minRenderCost;
703        const float stepSize = range / (float)intervals;
704
705        float currentRenderCost = minRenderCost;//(int)ceil(minRenderCost);
706
707        const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
708        const float totalVol = GetViewSpaceBox().GetVolume();
709        //const float totalVol = mViewCellsTree->GetRoot()->GetVolume();
710
711        int j = 0;
712        int i = 0;
713       
714        ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();             
715
716        // count for integral
717        float volSum = 0;
718        int smallerCostSum = 0;
719       
720        // note can skip computations for view cells already
721        // evaluated and delete them from vector ...
722    while (1)
723        {
724                // count for histogram value
725                float volDif = 0;
726                int smallerCostDif = 0;
727
728                while ((i < (int)viewCells.size()) && (viewCells[i]->GetRenderCost() < currentRenderCost))
729                {
730                        volSum += viewCells[i]->GetVolume();
731                        volDif += viewCells[i]->GetVolume();
732
733                        ++ i;
734                        ++ smallerCostSum;
735                        ++ smallerCostDif;
736                }
737               
738                if ((i >= (int)viewCells.size()) || (currentRenderCost >= maxRenderCost))
739                        break;
740               
741                const float rcRatio = currentRenderCost / maxRenderCost;
742                const float volRatioSum = volSum / totalVol;
743                const float volRatioDif = volDif / totalVol;
744
745                outstream << "#Pass\n" << j ++ << endl;
746                outstream << "#RenderCostRatio\n" << rcRatio << endl;
747                outstream << "#WeightedCost\n" << currentRenderCost / totalVol << endl;
748                outstream << "#ViewCellsDif\n" << smallerCostDif << endl;
749                outstream << "#ViewCellsSum\n" << smallerCostSum << endl;       
750                outstream << "#VolumeDif\n" << volRatioDif << endl << endl;
751                outstream << "#VolumeSum\n" << volRatioSum << endl << endl;
752
753                // increase current render cost
754                currentRenderCost += stepSize;
755        }
756
757        outstream.close();
758}
759
760
761ViewCellsManager *ViewCellsManager::LoadViewCells(const string &filename,
762                                                                                                  ObjectContainer *objects,
763                                                                                                  bool finalizeViewCells,
764                                                                                                  BoundingBoxConverter *bconverter)
765                                                                                                 
766{
767        ViewCellsParser parser;
768        ViewCellsManager *vm = NULL;
769
770        const long startTime = GetTime();
771        bool success = parser.ParseViewCellsFile(filename, &vm, objects, bconverter);
772
773        cout<<"viewcells parsed "<<endl<<flush;
774       
775        if (success)
776        {
777                //vm->ResetViewCells();
778                //hack
779                vm->mViewCells.clear();
780                ViewCellContainer leaves;
781                vm->mViewCellsTree->CollectLeaves(vm->mViewCellsTree->GetRoot(), leaves);
782
783                ViewCellContainer::const_iterator it, it_end = leaves.end();
784
785                for (it = leaves.begin(); it != it_end; ++ it)
786                {
787                        vm->mViewCells.push_back(*it);
788                }
789                vm->mViewCellsFinished = true;
790                vm->mMaxPvsSize = (int)objects->size();
791
792                if (finalizeViewCells)
793                {
794                        // create the meshes and compute volumes
795                        vm->FinalizeViewCells(true);
796                        // vm->mViewCellsTree->AssignRandomColors();
797                }
798
799                Debug << (int)vm->mViewCells.size() << " view cells loaded in "
800                          << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
801        }
802        else
803        {
804                Debug << "Error: loading view cells failed!" << endl;
805                DEL_PTR(vm);
806        }
807
808        return vm;
809}
810
811
812bool VspBspViewCellsManager::ExportViewCells(const string filename,
813                                                                                         const bool exportPvs,
814                                                                                         const ObjectContainer &objects)
815{
816        if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
817        {
818                return false;
819        }
820
821        cout << "exporting view cells to xml ... ";
822
823        OUT_STREAM stream(filename.c_str());
824
825        // for output we need unique ids for each view cell
826        CreateUniqueViewCellIds();
827
828        stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
829        stream << "<VisibilitySolution>" << endl;
830
831        if (exportPvs)
832        {
833                //-- export bounding boxes
834                stream << "<BoundingBoxes>" << endl;
835
836                if (mUseKdPvs)
837                {
838                        vector<KdIntersectable *>::iterator kit, kit_end = GetPreprocessor()->mKdTree->mKdIntersectables.end();
839
840                        int id = 0;
841                        for (kit = GetPreprocessor()->mKdTree->mKdIntersectables.begin(); kit != kit_end; ++ kit, ++ id)
842                        {
843                                Intersectable *obj = *kit;
844                                const AxisAlignedBox3 box = obj->GetBox();
845                 
846                                obj->SetId(id);
847       
848                                stream << "<BoundingBox" << " id=\"" << id << "\""
849                                           << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
850                                           << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
851                        }
852                }
853                else
854                {
855                        ObjectContainer::const_iterator oit, oit_end = objects.end();
856               
857                        for (oit = objects.begin(); oit != oit_end; ++ oit)
858                        {
859                                const AxisAlignedBox3 box = (*oit)->GetBox();
860
861                                ////////////
862                                //-- the bounding boxes
863
864                                stream << "<BoundingBox" << " id=\"" << (*oit)->GetId() << "\""
865                                           << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
866                                           << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
867                        }
868                }
869
870                stream << "</BoundingBoxes>" << endl;
871        }
872
873       
874        /////////////
875        //-- export the view cells and the pvs
876
877        const int numViewCells = mCurrentViewCellsStats.viewCells;
878        stream << "<ViewCells number=\"" << numViewCells << "\" >" << endl;
879
880        mViewCellsTree->Export(stream, exportPvs);
881
882        stream << "</ViewCells>" << endl;
883
884
885        //////////
886        //-- export the view space hierarchy
887       
888        stream << "<ViewSpaceHierarchy type=\"bsp\""
889                   << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
890                   << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\">" << endl;
891
892        mVspBspTree->Export(stream);
893        stream << "</ViewSpaceHierarchy>" << endl;
894
895        stream << "</VisibilitySolution>" << endl;
896
897        stream.close();
898        cout << "finished" << endl;
899
900        return true;
901}
902
903
904void ViewCellsManager::EvalViewCellHistogramForPvsSize(const string filename,
905                                                                                                           const int nViewCells)
906{
907        std::ofstream outstream;
908        outstream.open(filename.c_str());
909
910        ViewCellContainer viewCells;
911       
912        // $$ JB hack - the collect best viewcells does not work?
913#if 0
914        mViewCellsTree->CollectBestViewCellSet(viewCells, nViewCells);
915#else
916        viewCells = mViewCells;
917#endif
918        ViewCellContainer::iterator it = viewCells.begin(), it_end = viewCells.end();           
919        for (; it != it_end; ++it)
920        {
921                if (1)
922                        (*it)->UpdatePvsCost();
923                else
924                        (*it)->SetPvsCost((float)(*it)->GetFilteredPvsSize());
925        }
926
927        float maxPvs, maxVal, minVal;
928       
929        // sort by pvs size
930        sort(viewCells.begin(), viewCells.end(), ViewCell::SmallerPvs);
931
932        maxPvs = viewCells.back()->GetPvsCost();
933        minVal = 0;
934
935        // hack: normalize pvs size
936        int histoMaxVal;
937        Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
938        maxVal = max((float)histoMaxVal, maxPvs);
939               
940        Debug << "histogram minpvssize: " << minVal << " maxpvssize: " << maxVal
941                << " real maxpvs " << maxPvs << endl;
942
943        int histoIntervals;
944        Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.intervals", histoIntervals);
945        const int intervals = min(histoIntervals, (int)viewCells.size());
946
947        const float range = maxVal - minVal;
948        int stepSize = (int)(range / intervals);
949
950        // set step size to avoid endless loop
951        if (!stepSize) stepSize = 1;
952       
953        Debug << "intervals " << histoIntervals << endl;
954        Debug << "stepsize: " << stepSize << endl;
955        cout << "intervals " << histoIntervals << endl;
956        cout << "stepsize: " << stepSize << endl;
957
958        const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
959        const float totalVol = GetViewSpaceBox().GetVolume();
960
961        float currentPvs = minVal;
962       
963        int i = 0;
964        int j = 0;
965        float volSum = 0;
966        int smallerSum = 0;
967
968        it = viewCells.begin();
969       
970        for (int j = 0; j < intervals; ++ j)
971        {
972                float volDif = 0;
973                int smallerDif = 0;
974
975                while ((i < (int)viewCells.size()) &&
976                           (viewCells[i]->GetPvsCost() < currentPvs))
977                {
978                        volDif += viewCells[i]->GetVolume();
979                        volSum += viewCells[i]->GetVolume();
980
981                        ++ i;
982                        ++ smallerDif;
983                        ++ smallerSum;
984                }
985               
986                //              if (0 && (i < (int)viewCells.size()))
987                //                Debug << "new pvs cost increase: " << mViewCellsTree->GetPvsCost(viewCells[i])
988                //                              << " " << currentPvs << endl;
989               
990                const float volRatioDif = volDif / totalVol;
991                const float volRatioSum = volSum / totalVol;
992
993                outstream << "#Pass\n" << j << endl;
994                outstream << "#Pvs\n" << currentPvs << endl;
995                outstream << "#ViewCellsDif\n" << smallerDif << endl;
996                outstream << "#ViewCellsSum\n" << smallerSum << endl;   
997                outstream << "#VolumeDif\n" << volRatioDif << endl << endl;
998                outstream << "#VolumeSum\n" << volRatioSum << endl << endl;
999       
1000                //-- increase current pvs size to define next interval
1001                currentPvs += stepSize;
1002        }
1003
1004        outstream.close();
1005}
1006
1007
1008void ViewCellsManager::EvalViewCellHistogramForPvsSize(const string filename,
1009                                                                                                           ViewCellContainer &viewCells)
1010{
1011        std::ofstream outstream;
1012        outstream.open(filename.c_str());
1013
1014        float maxPvs, maxVal, minVal;
1015       
1016        // sort by pvs size
1017        sort(viewCells.begin(), viewCells.end(), ViewCell::SmallerPvs);
1018
1019        maxPvs = viewCells.back()->GetPvsCost();
1020        minVal = 0;
1021
1022        // hack: normalize pvs size
1023        int histoMaxVal;
1024        Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
1025        maxVal = max((float)histoMaxVal, maxPvs);
1026               
1027        Debug << "histogram minpvssize: " << minVal << " maxpvssize: " << maxVal
1028                << " real maxpvs " << maxPvs << endl;
1029
1030        int histoIntervals;
1031        Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.intervals", histoIntervals);
1032        const int intervals = min(histoIntervals, (int)viewCells.size());
1033
1034        const float range = maxVal - minVal;
1035        int stepSize = (int)(range / intervals);
1036
1037        // set step size to avoid endless loop
1038        if (!stepSize) stepSize = 1;
1039       
1040        Debug << "intervals " << histoIntervals << endl;
1041        Debug << "stepsize: " << stepSize << endl;
1042        cout << "intervals " << histoIntervals << endl;
1043        cout << "stepsize: " << stepSize << endl;
1044
1045        const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
1046        const float totalVol = GetViewSpaceBox().GetVolume();
1047
1048        float currentPvs = minVal;
1049       
1050        int i = 0;
1051        int j = 0;
1052        float volSum = 0;
1053        int smallerSum = 0;
1054
1055        //ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();   
1056       
1057        for (int j = 0; j < intervals; ++ j)
1058        {
1059                float volDif = 0;
1060                int smallerDif = 0;
1061
1062                while ((i < (int)viewCells.size()) &&
1063                           (viewCells[i]->GetPvsCost() < currentPvs))
1064                {
1065                        volDif += viewCells[i]->GetVolume();
1066                        volSum += viewCells[i]->GetVolume();
1067
1068                        ++ i;
1069                        ++ smallerDif;
1070                        ++ smallerSum;
1071                }
1072               
1073                //              if (0 && (i < (int)viewCells.size()))
1074                //                Debug << "new pvs cost increase: " << mViewCellsTree->GetPvsCost(viewCells[i])
1075                //                              << " " << currentPvs << endl;
1076               
1077                const float volRatioDif = volDif / totalVol;
1078                const float volRatioSum = volSum / totalVol;
1079
1080                outstream << "#Pass\n" << j << endl;
1081                outstream << "#Pvs\n" << currentPvs << endl;
1082                outstream << "#ViewCellsDif\n" << smallerDif << endl;
1083                outstream << "#ViewCellsSum\n" << smallerSum << endl;   
1084                outstream << "#VolumeDif\n" << volRatioDif << endl << endl;
1085                outstream << "#VolumeSum\n" << volRatioSum << endl << endl;
1086       
1087                //-- increase current pvs size to define next interval
1088                currentPvs += stepSize;
1089        }
1090
1091        outstream.close();
1092}
1093
1094
1095bool ViewCellsManager::GetExportPvs() const
1096{
1097        return mExportPvs;
1098}
1099
1100
1101void ViewCellsManager::ResetPvs()
1102{
1103        if (ViewCellsTreeConstructed())
1104        {
1105                mViewCellsTree->ResetPvs();
1106        }
1107        else
1108        {
1109                cout << "view cells tree not constructed" << endl;
1110        }
1111}
1112
1113
1114void ViewCellsManager::ExportStats(const string &fileName)
1115{
1116        mViewCellsTree->ExportStats(fileName);
1117}
1118
1119
1120void ViewCellsManager::EvalViewCellPartition()
1121{
1122        int samplesPerPass;
1123        int numSamples;
1124        int castSamples = 0;
1125        char str[64];
1126        int oldSamples = 0;
1127
1128        int samplesForStats;
1129
1130        Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesPerPass", samplesPerPass);
1131        Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesForStats", samplesForStats);
1132        Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samples", numSamples);
1133
1134        char statsPrefix[100];
1135        Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.statsPrefix", statsPrefix);
1136
1137        Debug << "view cell evaluation samples per pass: " << samplesPerPass << endl;
1138        Debug << "view cell evaluation samples: " << numSamples << endl;
1139        Debug << "view cell stats prefix: " << statsPrefix << endl;
1140
1141        cout << "reseting pvs ... ";
1142               
1143        const bool startFromZero = true;
1144
1145        // reset pvs and start over from zero
1146        if (startFromZero)
1147        {
1148                mViewCellsTree->ResetPvs();
1149        }
1150        else // start from current sampless
1151        {
1152                // statistics before casting more samples
1153                cout << "compute new statistics ... ";
1154                sprintf(str, "-%09d-eval.log", castSamples);
1155                string fName = string(statsPrefix) + string(str);
1156
1157                mViewCellsTree->ExportStats(fName);
1158                cout << "finished" << endl;
1159        }
1160
1161        cout << "finished" << endl;
1162    cout << "Evaluating view cell partition ... " << endl;
1163
1164        while (castSamples < numSamples)
1165        {               
1166                ///////////////
1167                //-- we have to use uniform sampling strategy for construction rays
1168
1169                VssRayContainer evaluationSamples;
1170                const int samplingType = mEvaluationSamplingType;
1171
1172                long startTime = GetTime();
1173
1174                cout << "casting " << samplesPerPass << " samples ... ";
1175                Debug << "casting " << samplesPerPass << " samples ... ";
1176
1177                CastPassSamples(samplesPerPass, mStrategies, evaluationSamples);
1178               
1179                castSamples += samplesPerPass;
1180
1181                Real timeDiff = TimeDiff(startTime, GetTime());
1182               
1183                cout << "finished in " << timeDiff * 1e-3f << " secs" << endl;
1184                cout << "computing sample contributions of " << (int)evaluationSamples.size()  << " samples ... ";
1185               
1186                Debug << "finished in " << timeDiff * 1e-3f << " secs" << endl;
1187                Debug << "computing sample contributions of " << (int)evaluationSamples.size()  << " samples ... ";
1188
1189                startTime = GetTime();
1190
1191                ComputeSampleContributions(evaluationSamples, true, false);
1192
1193                timeDiff = TimeDiff(startTime, GetTime());
1194                cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
1195                Debug << "finished in " << timeDiff * 1e-3 << " secs" << endl;
1196
1197                if ((castSamples >= samplesForStats + oldSamples) || (castSamples >= numSamples))
1198                {
1199                        oldSamples += samplesForStats;
1200
1201                        ///////////
1202                        //-- output stats
1203
1204                        sprintf(str, "-%09d-eval.log", castSamples);
1205                        string fileName = string(statsPrefix) + string(str);
1206
1207                        ///////////////
1208                        //-- propagate pvs or pvs size information
1209
1210                        startTime = GetTime();
1211                        ObjectPvs pvs;
1212
1213                        cout << "updating pvs for evaluation ... " << endl;
1214
1215                        UpdatePvsForEvaluation(mViewCellsTree->GetRoot(), pvs);
1216
1217                        timeDiff = TimeDiff(startTime, GetTime());
1218                        cout << "finished updating the pvs in " << timeDiff * 1e-3 << " secs" << endl;
1219                        Debug << "pvs evaluated in " << timeDiff * 1e-3 << " secs" << endl;
1220               
1221                        startTime = GetTime();
1222                        cout << "compute new statistics ... " << endl;
1223
1224                        ExportStats(fileName);
1225
1226                        timeDiff = TimeDiff(startTime, GetTime());
1227                        cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
1228                        Debug << "statistis computed in " << timeDiff * 1e-3 << " secs" << endl;
1229                }
1230
1231                disposeRays(evaluationSamples, NULL);
1232        }
1233       
1234
1235        ////////////
1236        //-- histogram
1237
1238        const int numLeaves = mViewCellsTree->GetNumInitialViewCells(mViewCellsTree->GetRoot());
1239        bool useHisto;
1240        int histoStepSize;
1241
1242        Environment::GetSingleton()->GetBoolValue("ViewCells.Evaluation.histogram", useHisto);
1243        Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.histoStepSize", histoStepSize);
1244
1245        if (useHisto)
1246        {
1247                // evaluate view cells in a histogram           
1248                char s[64];
1249
1250                for (int pass = histoStepSize; pass <= numLeaves; pass += histoStepSize)
1251                {
1252                        string filename;
1253
1254                        cout << "computing histogram for " << pass << " view cells" << endl;
1255#if 0
1256                        //-- evaluate histogram for render cost
1257                        sprintf(s, "-%09d-histo.log", pass);
1258                        filename = string(statsPrefix) + string(s);
1259
1260                        EvalViewCellHistogram(filename, pass);
1261
1262#endif
1263                        //////////////////////////////////////////
1264            //-- evaluate histogram for pvs size
1265
1266                        cout << "computing pvs histogram for " << pass << " view cells" << endl;
1267
1268                        sprintf(s, "-%09d-histo-pvs.log", pass);
1269                        filename = string(statsPrefix) + string(s);
1270
1271                        EvalViewCellHistogramForPvsSize(filename, pass);
1272                }
1273        }
1274}
1275
1276
1277inline float EvalMergeCost(ViewCell *root, ViewCell *candidate)
1278{
1279        return root->GetPvs().GetPvsHomogenity(candidate->GetPvs());
1280}
1281
1282
1283/// Returns index of the best view cells of the neighborhood
1284static int GetBestViewCellIdx(ViewCell *root, const ViewCellContainer &neighborhood)
1285{
1286        int bestViewCellIdx = 0;
1287
1288        float mergeCost = Limits::Infinity;
1289        int i = 0;
1290
1291        ViewCellContainer::const_iterator vit, vit_end = neighborhood.end();
1292
1293        for (vit = neighborhood.begin(); vit != vit_end; ++ vit, ++ i)
1294        {
1295                const float mc = EvalMergeCost(root, *vit);
1296               
1297                if (mc < mergeCost)
1298                {
1299                        mergeCost = mc;
1300                        bestViewCellIdx = i;
1301                }
1302        }
1303
1304        return bestViewCellIdx;
1305}
1306
1307
1308void ViewCellsManager::SetMaxFilterSize(const int size)
1309{
1310        mMaxFilterSize = size;
1311}
1312
1313
1314float ViewCellsManager::EvalRenderCost(Intersectable *obj) //const
1315{
1316        switch (sRenderCostEvaluationType)
1317        {
1318        case PER_OBJECT:
1319                return 1.0f;
1320       
1321        case PER_TRIANGLE:
1322                {
1323                        return (float)obj->NumberOfFaces();
1324                }
1325        default:
1326                cout << "default" << endl;
1327                return 1.0f;
1328        }
1329
1330        // should not come here
1331        return 0.0f;
1332}
1333
1334
1335ViewCell *ViewCellsManager::ConstructLocalMergeTree(ViewCell *currentViewCell,
1336                                                                                                        const ViewCellContainer &viewCells)
1337{
1338        ViewCell *root = currentViewCell;
1339        ViewCellContainer neighborhood = viewCells;
1340
1341        ViewCellContainer::const_iterator it, it_end = neighborhood.end();
1342
1343        const int n = min(mMaxFilterSize, (int)neighborhood.size());
1344       
1345        /////////////////
1346        //-- use priority queue to merge leaf pairs
1347       
1348        for (int nMergedViewCells = 0; nMergedViewCells < n; ++ nMergedViewCells)
1349        {
1350                const int bestViewCellIdx = GetBestViewCellIdx(root, neighborhood);
1351               
1352                ViewCell *bestViewCell = neighborhood[bestViewCellIdx];
1353       
1354                // remove from vector
1355                swap(neighborhood[bestViewCellIdx], neighborhood.back());
1356                neighborhood.pop_back();
1357       
1358                if (!bestViewCell || !root)
1359            cout << "warning!!" << endl;
1360               
1361                // create new root of the hierarchy
1362                root = MergeViewCells(root, bestViewCell);
1363        }
1364
1365        return root;   
1366}
1367
1368
1369struct SortableViewCellEntry {
1370
1371        SortableViewCellEntry() {}
1372        SortableViewCellEntry(const float v, ViewCell *cell):mValue(v), mViewCell(cell) {}
1373
1374        float mValue;
1375        ViewCell *mViewCell;
1376
1377        friend bool operator<(const SortableViewCellEntry &a, const SortableViewCellEntry &b) {
1378                return a.mValue < b.mValue;
1379        }
1380};
1381
1382
1383ViewCell * ViewCellsManager::ConstructLocalMergeTree2(ViewCell *currentViewCell,
1384                                                                                                          const ViewCellContainer &viewCells)
1385{
1386 
1387  vector<SortableViewCellEntry> neighborhood(viewCells.size());
1388  int i, j;
1389  for (i = 0, j = 0; i < viewCells.size(); i++) {
1390        if (viewCells[i] != currentViewCell)
1391          neighborhood[j++] = SortableViewCellEntry(
1392                                                                                                EvalMergeCost(currentViewCell, viewCells[i]),
1393                                                                                                viewCells[i]);
1394  }
1395  neighborhood.resize(j);
1396 
1397  sort(neighborhood.begin(), neighborhood.end());
1398 
1399  ViewCell *root = currentViewCell;
1400 
1401  vector<SortableViewCellEntry>::const_iterator it, it_end = neighborhood.end();
1402 
1403  const int n = min(mMaxFilterSize, (int)neighborhood.size());
1404  //-- use priority queue to merge leaf pairs
1405 
1406  //cout << "neighborhood: " << neighborhood.size() << endl;
1407  for (int nMergedViewCells = 0; nMergedViewCells < n; ++ nMergedViewCells)
1408  {
1409          ViewCell *bestViewCell = neighborhood[nMergedViewCells].mViewCell;
1410          //cout <<nMergedViewCells<<":"<<"homogenity=" <<neighborhood[nMergedViewCells].mValue<<endl;
1411          // create new root of the hierarchy
1412          root = MergeViewCells(root, bestViewCell);
1413          // set negative cost so that this view cell gets deleted
1414          root->SetMergeCost(-1.0f);
1415  }
1416 
1417  return root; 
1418}
1419
1420void
1421ViewCellsManager::DeleteLocalMergeTree(ViewCell *vc
1422                                                                           ) const
1423{
1424        if (!vc->IsLeaf() && vc->GetMergeCost() < 0.0f)
1425        {       
1426                ViewCellInterior *vci = (ViewCellInterior *) vc;
1427                ViewCellContainer::const_iterator it, it_end = vci->mChildren.end();
1428
1429        for (it = vci->mChildren.begin(); it != it_end; ++ it)
1430                        DeleteLocalMergeTree(*it);
1431               
1432                vci->mChildren.clear();
1433               
1434                delete vci;
1435  }
1436}
1437
1438
1439bool ViewCellsManager::CheckValidity(ViewCell *vc,
1440                                                                         int minPvsSize,
1441                                                                         int maxPvsSize) const
1442{
1443
1444        if ((vc->GetPvs().EvalPvsCost() > maxPvsSize) ||
1445                (vc->GetPvs().EvalPvsCost() < minPvsSize))
1446        {
1447                return false;
1448        }
1449
1450        return true;
1451}
1452
1453
1454int ViewCellsManager::ComputeBoxIntersections(const AxisAlignedBox3 &box,
1455                                                                                          ViewCellContainer &viewCells) const
1456{
1457        return 0;
1458};
1459
1460
1461AxisAlignedBox3 ViewCellsManager::GetFilterBBox(const Vector3 &viewPoint,
1462                                                                                                const float width) const
1463{
1464  float w = Magnitude(mViewSpaceBox.Size())*width;
1465  Vector3 min = viewPoint - w * 0.5f;
1466  Vector3 max = viewPoint + w * 0.5f;
1467 
1468  return AxisAlignedBox3(min, max);
1469}
1470
1471
1472void ViewCellsManager::GetPrVS(const Vector3 &viewPoint,
1473                                                           PrVs &prvs,
1474                                                           const float filterWidth)
1475{
1476  ViewCell *currentViewCell = GetViewCell(viewPoint);
1477
1478  if (mMaxFilterSize < 1) {
1479        prvs.mViewCell = currentViewCell;
1480        return;
1481  }
1482 
1483  const AxisAlignedBox3 box = GetFilterBBox(viewPoint, filterWidth);
1484 
1485  if (currentViewCell)
1486        {
1487          ViewCellContainer viewCells;
1488          ComputeBoxIntersections(box, viewCells);
1489         
1490          ViewCell *root = ConstructLocalMergeTree2(currentViewCell, viewCells);
1491          prvs.mViewCell = root;
1492         
1493        }
1494  else
1495        {
1496          prvs.mViewCell = NULL;
1497          //prvs.mPvs = root->GetPvs();
1498        }
1499}
1500
1501
1502bool ViewCellsManager::ViewCellsTreeConstructed() const
1503{
1504    return (mViewCellsTree && mViewCellsTree->GetRoot());
1505}
1506
1507
1508void ViewCellsManager::SetValidity(ViewCell *vc,
1509                                                                   int minPvs,
1510                                                                   int maxPvs) const
1511{
1512        vc->SetValid(CheckValidity(vc, minPvs, maxPvs));
1513}
1514
1515
1516void
1517ViewCellsManager::SetValidity(
1518                                                          int minPvsSize,
1519                                                          int maxPvsSize) const
1520{
1521        ViewCellContainer::const_iterator it, it_end = mViewCells.end();
1522
1523
1524        for (it = mViewCells.begin(); it != it_end; ++ it)
1525        {
1526                SetValidity(*it, minPvsSize, maxPvsSize);
1527        }
1528}
1529
1530void
1531ViewCellsManager::SetValidityPercentage(
1532                                                                                const float minValid,
1533                                                                                const float maxValid
1534                                                                                )
1535{
1536  ObjectPvs dummyPvs;
1537  // update pvs sizes
1538  for (int i = 0; i < (int)mViewCells.size(); ++ i) {
1539        UpdatePvsForEvaluation(mViewCells[i], dummyPvs);
1540  }
1541 
1542  sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
1543 
1544  int start = (int)(mViewCells.size() * minValid);
1545  int end = (int)(mViewCells.size() * maxValid);
1546 
1547  for (int i = 0; i < (int)mViewCells.size(); ++ i)
1548        {
1549          //      cout<<"pvs:"<<mViewCells[i]->GetCachedPvsCost()<<endl;
1550          mViewCells[i]->SetValid(i >= start && i <= end);
1551        }
1552}
1553
1554
1555int ViewCellsManager::CountValidViewcells() const
1556{
1557        ViewCellContainer::const_iterator it, it_end = mViewCells.end();
1558        int valid = 0;
1559
1560        for (it = mViewCells.begin(); it != it_end; ++ it)
1561        {       
1562                if ((*it)->GetValid())
1563                        ++ valid;
1564        }
1565
1566        return valid;
1567}
1568
1569
1570bool ViewCellsManager::LoadViewCellsGeometry(const string filename,
1571                                                                                         const bool extrudeBaseTriangles)
1572{
1573        /// we use predefined view cells from now on
1574        mUsePredefinedViewCells = true;
1575        X3dParser parser;
1576       
1577        if (extrudeBaseTriangles)
1578        {
1579                Environment::GetSingleton()->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
1580                const bool success = parser.ParseFile(filename, *this);
1581
1582                if (!success)
1583                        return false;
1584        }
1585        else
1586        {
1587                // hack: use standard mesh loading
1588                // create temporary scene graph for loading the view cells geometry
1589                // note: delete the meshes as they are created two times for transformed mesh instances.
1590                SceneGraphNode *root = new SceneGraphNode();
1591                const bool success = parser.ParseFile(filename, root, true);
1592               
1593                if (!success)
1594                {
1595                        DEL_PTR(root);
1596                        return false;
1597                }
1598
1599                ObjectContainer::const_iterator oit, oit_end = root->mGeometry.end();
1600               
1601                for (oit = root->mGeometry.begin(); oit != oit_end; ++ oit)
1602                {
1603                        Mesh *mesh;
1604                        if ((*oit)->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
1605                        {
1606                                TransformedMeshInstance *mit = dynamic_cast<TransformedMeshInstance *>(*oit);
1607                                mesh = MeshManager::GetSingleton()->CreateResource();
1608                                mit->GetTransformedMesh(*mesh);
1609                        }
1610                        else if ((*oit)->Type() == Intersectable::MESH_INSTANCE)
1611                        {
1612                                MeshInstance *mit = dynamic_cast<MeshInstance *>(*oit);
1613                                mesh = mit->GetMesh();
1614                        }
1615                        mesh->ComputeBoundingBox();
1616                        mViewCells.push_back(GenerateViewCell(mesh));
1617                }
1618
1619                DEL_PTR(root);
1620        }
1621
1622        // set view space box to bounding box of the view cells
1623        AxisAlignedBox3 bbox;
1624        bbox.Initialize();
1625        ViewCellContainer::iterator it = mViewCells.begin(), it_end = mViewCells.end();
1626
1627        for (; it != it_end; ++ it)
1628        {
1629                bbox.Include((*it)->GetMesh()->mBox);
1630        }
1631
1632        SetViewSpaceBox(bbox);
1633        cout << "view space box: " << bbox << endl;
1634        cout << "generated " << (int)mViewCells.size() << " view cells using the geometry " << filename << endl;
1635
1636        return true;
1637}
1638
1639
1640bool ViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
1641{
1642  viewPoint = mViewSpaceBox.GetRandomPoint();
1643  return true;
1644}
1645
1646bool ViewCellsManager::GetViewPoint(Vector3 &viewPoint, const Vector3 &params) const
1647{
1648  viewPoint = mViewSpaceBox.GetRandomPoint(params);
1649  return true;
1650}
1651
1652
1653float ViewCellsManager::GetViewSpaceVolume()
1654{
1655        return mViewSpaceBox.GetVolume() * (2.0f * sqr((float)M_PI));
1656}
1657
1658
1659bool ViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
1660{
1661        if (!ViewCellsConstructed())
1662        {
1663                return mViewSpaceBox.IsInside(viewPoint);
1664        }
1665        else
1666        {
1667                if (!mViewSpaceBox.IsInside(viewPoint))
1668                        return false;
1669
1670                ViewCell *viewcell = GetViewCell(viewPoint);
1671
1672                if (!viewcell || !viewcell->GetValid())
1673                        return false;
1674        }
1675
1676        return true;
1677}
1678
1679
1680float
1681ViewCellsManager::ComputeSampleContributions(const VssRayContainer &rays,
1682                                                                                         const bool addRays,
1683                                                                                         const bool storeViewCells)
1684{
1685  float sum = 0.0f;
1686
1687  VssRayContainer::const_iterator it, it_end = rays.end();
1688 
1689  for (it = rays.begin(); it != it_end; ++ it)
1690        {
1691          if (!ViewCellsConstructed()) {
1692                // view cells not yet constructed
1693                // just take the lenghts of the rays as contributions
1694                if ((*it)->mTerminationObject)
1695                  sum += (*it)->Length();
1696          } else {
1697                sum += ComputeSampleContribution(*(*it), addRays, storeViewCells);
1698          }
1699        }
1700
1701  return sum;
1702}
1703
1704
1705void ViewCellsManager::EvaluateViewCellsStats()
1706{
1707        mCurrentViewCellsStats.Reset();
1708        ViewCellContainer::const_iterator it, it_end = mViewCells.end();
1709
1710        for (it = mViewCells.begin(); it != it_end; ++ it)
1711        {
1712                mViewCellsTree->UpdateViewCellsStats(*it, mCurrentViewCellsStats);
1713        }
1714}
1715
1716
1717void ViewCellsManager::EvaluateRenderStatistics(float &totalRenderCost,
1718                                                                                                float &expectedRenderCost,
1719                                                                                                float &deviation,
1720                                                                                                float &variance,
1721                                                                                                float &totalCost,
1722                                                                                                float &avgRenderCost)
1723{
1724        ////////////
1725        //-- compute expected value
1726
1727        totalRenderCost = 0;
1728        totalCost = 0;
1729
1730        ViewCellContainer::const_iterator it, it_end = mViewCells.end();
1731
1732        for (it = mViewCells.begin(); it != it_end; ++ it)
1733        {
1734                ViewCell *vc = *it;
1735                totalRenderCost += vc->GetPvs().EvalPvsCost() * vc->GetVolume();
1736                totalCost += (int)vc->GetPvs().EvalPvsCost();
1737        }
1738
1739        // normalize with view space box
1740        totalRenderCost /= mViewSpaceBox.GetVolume();
1741        expectedRenderCost = totalRenderCost / (float)mViewCells.size();
1742        avgRenderCost = totalCost / (float)mViewCells.size();
1743
1744
1745        ///////////
1746        //-- compute standard defiation
1747
1748        variance = 0;
1749        deviation = 0;
1750
1751        for (it = mViewCells.begin(); it != it_end; ++ it)
1752        {
1753                ViewCell *vc = *it;
1754
1755                float renderCost = vc->GetPvs().EvalPvsCost() * vc->GetVolume();
1756                float dev;
1757
1758                if (1)
1759                        dev = fabs(avgRenderCost - (float)vc->GetPvs().EvalPvsCost());
1760                else
1761                        dev = fabs(expectedRenderCost - renderCost);
1762
1763                deviation += dev;
1764                variance += dev * dev;
1765        }
1766
1767        variance /= (float)mViewCells.size();
1768        deviation /= (float)mViewCells.size();
1769}
1770
1771
1772float ViewCellsManager::GetArea(ViewCell *viewCell) const
1773{
1774        return viewCell->GetArea();
1775}
1776
1777
1778float ViewCellsManager::GetVolume(ViewCell *viewCell) const
1779{
1780        return viewCell->GetVolume();
1781}
1782
1783
1784void ViewCellsManager::CompressViewCells()
1785{
1786        if (!(ViewCellsTreeConstructed() && mCompressViewCells))
1787                return;
1788
1789        ////////////
1790        //-- compression
1791       
1792        int pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
1793
1794        cout << "number of entries before compress: " << pvsEntries << endl;
1795        Debug << "number of entries before compress: " << pvsEntries << endl;
1796
1797        mViewCellsTree->SetViewCellsStorage(ViewCellsTree::COMPRESSED);
1798
1799        pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
1800
1801        Debug << "number of entries after compress: " << pvsEntries << endl;
1802        cout << "number of entries after compress: " << pvsEntries << endl;
1803}
1804
1805
1806ViewCell *ViewCellsManager::ExtrudeViewCell(const Triangle3 &baseTri,
1807                                                                                        const float height) const
1808{
1809        // one mesh per view cell
1810        Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
1811
1812        ////////////
1813        //-- construct prism
1814
1815        // bottom
1816        mesh->mFaces.push_back(new Face(2,1,0));
1817        // top
1818    mesh->mFaces.push_back(new Face(3,4,5));
1819        // sides
1820        mesh->mFaces.push_back(new Face(1, 4, 3, 0));
1821        mesh->mFaces.push_back(new Face(2, 5, 4, 1));
1822        mesh->mFaces.push_back(new Face(3, 5, 2, 0));
1823
1824
1825        /////////////
1826        //-- extrude new vertices for top of prism
1827
1828        const Vector3 triNorm = baseTri.GetNormal();
1829        Triangle3 topTri;
1830
1831        // add base vertices and calculate top vertices
1832        for (int i = 0; i < 3; ++ i)
1833        {
1834                mesh->mVertices.push_back(baseTri.mVertices[i]);
1835        }
1836
1837        // add top vertices
1838        for (int i = 0; i < 3; ++ i)
1839        {
1840                mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
1841        }
1842
1843        // do we have to preprocess this mesh (we don't want to trace view cells!)?
1844        mesh->ComputeBoundingBox();
1845       
1846        return GenerateViewCell(mesh);
1847}
1848
1849
1850void ViewCellsManager::FinalizeViewCells(const bool createMesh)
1851{
1852        ViewCellContainer::const_iterator it, it_end = mViewCells.end();
1853
1854        // volume and area of the view cells are recomputed
1855        // a view cell mesh is created
1856        for (it = mViewCells.begin(); it != it_end; ++ it)
1857        {
1858                Finalize(*it, createMesh);
1859        }
1860
1861        mViewCellsTree->AssignRandomColors();
1862
1863        mTotalAreaValid = false;
1864}
1865
1866
1867void ViewCellsManager::Finalize(ViewCell *viewCell, const bool createMesh)
1868{
1869        // implemented in subclasses
1870}
1871
1872
1873/** fast way of merging 2 view cells.
1874*/
1875ViewCellInterior *ViewCellsManager::MergeViewCells(ViewCell *left, ViewCell *right) const
1876{
1877        // generate parent view cell
1878        ViewCellInterior *vc = new ViewCellInterior();
1879
1880        vc->GetPvs().Clear();
1881        ObjectPvs::Merge(vc->GetPvs(), left->GetPvs(), right->GetPvs());
1882
1883        // set only links to child (not from child to parent, maybe not wished!!)
1884        vc->mChildren.push_back(left);
1885        vc->mChildren.push_back(right);
1886
1887        // update pvs size
1888        UpdateScalarPvsSize(vc, vc->GetPvs().EvalPvsCost(), vc->GetPvs().GetSize());
1889
1890        return vc;
1891}
1892
1893
1894ViewCellInterior *ViewCellsManager::MergeViewCells(ViewCellContainer &children) const
1895{
1896        ViewCellInterior *vc = new ViewCellInterior();
1897        ViewCellContainer::const_iterator it, it_end = children.end();
1898
1899        for (it = children.begin(); it != it_end; ++ it)
1900        {
1901                vc->GetPvs().MergeInPlace((*it)->GetPvs());
1902       
1903                vc->mChildren.push_back(*it);
1904        }
1905
1906        return vc;
1907}
1908
1909
1910void ViewCellsManager::SetRenderer(Renderer *renderer)
1911{
1912        mRenderer = renderer;
1913}
1914
1915
1916ViewCellsTree *ViewCellsManager::GetViewCellsTree()
1917{
1918        return mViewCellsTree;
1919}
1920
1921
1922void ViewCellsManager::SetVisualizationSamples(const int visSamples)
1923{
1924        mVisualizationSamples = visSamples;
1925}
1926
1927
1928void ViewCellsManager::SetConstructionSamples(const int constructionSamples)
1929{
1930        mConstructionSamples = constructionSamples;
1931}
1932
1933
1934void ViewCellsManager::SetInitialSamples(const int initialSamples)
1935{
1936        mInitialSamples = initialSamples;
1937}
1938
1939
1940void ViewCellsManager::SetPostProcessSamples(const int postProcessSamples)
1941{
1942        mPostProcessSamples = postProcessSamples;
1943}
1944
1945
1946int ViewCellsManager::GetVisualizationSamples() const
1947{
1948        return mVisualizationSamples;
1949}
1950
1951
1952int ViewCellsManager::GetConstructionSamples() const
1953{
1954        return mConstructionSamples;
1955}
1956
1957
1958int ViewCellsManager::GetPostProcessSamples() const
1959{
1960        return mPostProcessSamples;
1961}
1962
1963
1964void ViewCellsManager::UpdatePvs()
1965{
1966  if (mViewCellPvsIsUpdated || !ViewCellsTreeConstructed())
1967        return;
1968 
1969  mViewCellPvsIsUpdated = true;
1970 
1971  ViewCellContainer leaves;
1972  mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
1973 
1974  ViewCellContainer::const_iterator it, it_end = leaves.end();
1975 
1976  for (it = leaves.begin(); it != it_end; ++ it)
1977        {
1978          mViewCellsTree->PropagatePvs(*it);
1979        }
1980}
1981
1982
1983void ViewCellsManager::GetPvsStatistics(PvsStatistics &stat)
1984{
1985  // update pvs of view cells tree if necessary
1986  UpdatePvs();
1987 
1988  ViewCellContainer::const_iterator it = mViewCells.begin();
1989 
1990  stat.viewcells = 0;
1991  stat.minPvs = 100000000;
1992  stat.maxPvs = 0;
1993  stat.avgPvs = 0.0f;
1994  stat.avgFilteredPvs = 0.0f;
1995  stat.avgFilterContribution = 0.0f;
1996  stat.avgFilterRadius = 0;
1997  stat.avgFilterRatio = 0;
1998  stat.avgRelPvsIncrease = 0.0f;
1999  stat.devRelPvsIncrease = 0.0f;
2000 
2001  if (mPerViewCellStat.size() != mViewCells.size()) {
2002        // reset the pvs size array after the first call to this routine
2003        mPerViewCellStat.resize(mViewCells.size());
2004        for (int i=0; i < mPerViewCellStat.size(); i++) {
2005          mPerViewCellStat[i].pvsSize = 0.0f;
2006          mPerViewCellStat[i].relPvsIncrease = 0.0f;
2007        }
2008  }
2009  int i;
2010  bool evaluateFilter;
2011  Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluateFilter", evaluateFilter);
2012
2013  for (i=0; it != mViewCells.end(); ++ it, i++)
2014        {
2015          ViewCell *viewcell = *it;
2016          if (viewcell->GetValid()) {
2017                const float pvsCost = mViewCellsTree->GetPvsCost(viewcell);
2018               
2019                if (pvsCost < stat.minPvs)
2020                  stat.minPvs = pvsCost;
2021                if (pvsCost > stat.maxPvs)
2022                  stat.maxPvs = pvsCost;
2023               
2024                stat.avgPvs += pvsCost;
2025
2026
2027                if (mPerViewCellStat[i].pvsSize > 0.0f)
2028                  mPerViewCellStat[i].relPvsIncrease = (pvsCost - mPerViewCellStat[i].pvsSize)/mPerViewCellStat[i].pvsSize;
2029               
2030                stat.avgRelPvsIncrease += mPerViewCellStat[i].relPvsIncrease;
2031               
2032                // update the pvs size
2033                mPerViewCellStat[i].pvsSize = pvsCost;
2034               
2035               
2036               
2037                if (evaluateFilter) {
2038                  ObjectPvs filteredPvs;
2039                  PvsFilterStatistics fstat = ApplyFilter2(viewcell,
2040                                                                                                   false,
2041                                                                                                   2.0f,
2042                                                                                                   filteredPvs);
2043                 
2044                  float filteredCost = filteredPvs.EvalPvsCost();
2045                  stat.avgFilteredPvs += filteredCost;
2046                  stat.avgFilterContribution += filteredCost - pvsCost;
2047                 
2048                  stat.avgFilterRadius += fstat.mAvgFilterRadius;
2049                  int sum = fstat.mGlobalFilterCount + fstat.mLocalFilterCount;
2050                  if (sum) {
2051                        stat.avgFilterRatio += fstat.mLocalFilterCount /
2052                          (float) sum;
2053                  }
2054                 
2055                } else {
2056                  stat.avgFilteredPvs += pvsCost;
2057                  stat.avgFilterContribution += 0;
2058                }
2059               
2060                ++ stat.viewcells;
2061          }
2062        }
2063
2064 
2065 
2066  if (stat.viewcells) {
2067        stat.avgPvs/=stat.viewcells;
2068        stat.avgFilteredPvs/=stat.viewcells;
2069        stat.avgFilterContribution/=stat.viewcells;
2070        stat.avgFilterRadius/=stat.viewcells;
2071        stat.avgFilterRatio/=stat.viewcells;
2072        stat.avgRelPvsIncrease/=stat.viewcells;
2073
2074        // evaluate std deviation of relPvsIncrease
2075        float sum=0.0f;
2076        for (i=0; i < stat.viewcells; i++) {
2077          sum += sqr(mPerViewCellStat[i].relPvsIncrease - stat.avgRelPvsIncrease);
2078        }
2079        stat.devRelPvsIncrease = sqrt(sum/stat.viewcells);
2080  }
2081 
2082}
2083
2084
2085void ViewCellsManager::PrintPvsStatistics(ostream &s)
2086{
2087  s<<"############# Viewcell PVS STAT ##################\n";
2088  PvsStatistics pvsStat;
2089  GetPvsStatistics(pvsStat);
2090  s<<"#AVG_PVS\n"<<pvsStat.avgPvs<<endl;
2091  s<<"#AVG_FILTERED_PVS\n"<<pvsStat.avgFilteredPvs<<endl;
2092  s<<"#AVG_FILTER_CONTRIBUTION\n"<<pvsStat.avgFilterContribution<<endl;
2093  s<<"#AVG_FILTER_RADIUS\n"<<pvsStat.avgFilterRadius<<endl;
2094  s<<"#AVG_FILTER_RATIO\n"<<pvsStat.avgFilterRatio<<endl;
2095  s<<"#MAX_PVS\n"<<pvsStat.maxPvs<<endl;
2096  s<<"#MIN_PVS\n"<<pvsStat.minPvs<<endl;
2097  s<<"#AVG_REL_PVS_INCREASE\n"<<pvsStat.avgRelPvsIncrease<<endl;
2098  s<<"#DEV_REL_PVS_INCREASE\n"<<pvsStat.devRelPvsIncrease<<endl;
2099}
2100
2101
2102int ViewCellsManager::CastBeam(Beam &beam)
2103{
2104        return 0;
2105}
2106
2107
2108ViewCellContainer &ViewCellsManager::GetViewCells()
2109{
2110        return mViewCells;
2111}
2112
2113
2114void ViewCellsManager::SetViewSpaceBox(const AxisAlignedBox3 &box)
2115{
2116        mViewSpaceBox = box;
2117       
2118        // hack: create clip plane relative to new view space box
2119        CreateClipPlane();
2120        // the total area of the view space has changed
2121        mTotalAreaValid = false;
2122}
2123
2124
2125void ViewCellsManager::CreateClipPlane()
2126{
2127        int axis = 0;
2128        float pos;
2129        bool orientation;
2130        Vector3 absPos;
2131
2132        Environment::GetSingleton()->GetFloatValue("ViewCells.Visualization.clipPlanePos", pos);
2133        Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.clipPlaneAxis", axis);
2134
2135        if (axis < 0)
2136        {
2137                axis = -axis;
2138                orientation = false;
2139                absPos = mViewSpaceBox.Max() -  mViewSpaceBox.Size() * pos;
2140        }
2141        else
2142        {
2143                orientation = true;
2144                absPos = mViewSpaceBox.Min() +  mViewSpaceBox.Size() * pos;
2145        }
2146
2147        mClipPlaneForViz = AxisAlignedPlane(axis, absPos[axis]);
2148        mClipPlaneForViz.mOrientation = orientation;
2149}
2150
2151
2152AxisAlignedBox3 ViewCellsManager::GetViewSpaceBox() const
2153{
2154        return mViewSpaceBox;
2155}
2156
2157
2158void ViewCellsManager::ResetViewCells()
2159{
2160        // recollect view cells
2161        mViewCells.clear();
2162        CollectViewCells();
2163       
2164        // stats are computed once more
2165        EvaluateViewCellsStats();
2166
2167        // has to be recomputed
2168        mTotalAreaValid = false;
2169}
2170
2171
2172int ViewCellsManager::GetMaxPvsSize() const
2173{
2174        return mMaxPvsSize;
2175}
2176
2177
2178int ViewCellsManager::GetMinPvsSize() const
2179{
2180        return mMinPvsSize;
2181}
2182
2183
2184
2185float ViewCellsManager::GetMaxPvsRatio() const
2186{
2187        return mMaxPvsRatio;
2188}
2189
2190
2191inline static void AddSampleToPvs(ObjectPvs &pvs,
2192                                                                  Intersectable *obj,
2193                                                                  const float pdf)
2194{
2195#if PVS_ADD_DIRTY
2196        pvs.AddSampleDirtyCheck(obj, pdf);
2197
2198        if (pvs.RequiresResort())
2199        {
2200                pvs.SimpleSort();
2201        }
2202#else
2203        pvs.AddSample(obj, pdf);
2204#endif
2205}
2206
2207
2208void ViewCellsManager::ComputeViewCellContribution(ViewCell *viewCell,
2209                                                                                                   VssRay &ray,
2210                                                                                                   Intersectable *obj,
2211                                                                                                   const Vector3 &pt,
2212                                                                                                   const bool addRays)
2213{
2214  // check if we are outside of view space
2215        if (!obj || !viewCell->GetValid())
2216                return;
2217       
2218        // if ray not outside of view space
2219        float relContribution = 0.0f;
2220        float absContribution = 0.0f;
2221       
2222        if (obj)
2223          {
2224                // todo: maybe not correct for kd node pvs
2225                if (addRays) {
2226                  float pdf = viewCell->GetPvs().AddSampleDirtyCheck(obj, ray.mPdf);
2227                  if (pdf == ray.mPdf) {
2228                        absContribution = 1.0f;
2229                        if (viewCell->GetPvs().RequiresResort())
2230                          viewCell->GetPvs().SimpleSort();
2231                  }
2232                          } else {
2233                                if (viewCell->GetPvs().GetSampleContribution(
2234                                                                                                                         obj,
2235                                                                                                                         ray.mPdf,
2236                                                                                                                         relContribution))
2237                                  absContribution = 1.0f;
2238                          }
2239                // $$ clear the relative contribution as it is currently not correct anyway
2240                relContribution = 0.0f;
2241               
2242                if (absContribution == 1.0f) {
2243                  ++ ray.mPvsContribution;
2244                  relContribution = 1.0f;
2245                 
2246                 
2247#if CONTRIBUTION_RELATIVE_TO_PVS_SIZE
2248                  relContribution /= viewcell->GetPvs().GetSize();
2249#endif
2250                 
2251#if DIST_WEIGHTED_CONTRIBUTION
2252                  // recalculate the contribution - weight the 1.0f contribution by the sqr distance to the
2253                  // object-> a new contribution in the proximity of the viewcell has a larger weight!
2254                  relContribution /= SqrDistance(GetViewCellBox(viewcell).Center(),
2255                                                                                 ray.mTermination);
2256                 
2257#endif
2258                }
2259               
2260#if SUM_RAY_CONTRIBUTIONS || AVG_RAY_CONTRIBUTIONS
2261                ray.mRelativePvsContribution += relContribution;
2262#else
2263                // recalculate relative contribution - use max of Rel Contribution
2264                if (ray.mRelativePvsContribution < relContribution)
2265                  ray.mRelativePvsContribution = relContribution;
2266#endif
2267          }
2268       
2269}
2270
2271
2272float
2273ViewCellsManager::ComputeSampleContribution(VssRay &ray,
2274                                                                                        const bool addRays,
2275                                                                                        const bool storeViewCells)
2276{
2277        ray.mPvsContribution = 0;
2278        ray.mRelativePvsContribution = 0.0f;
2279
2280        if (!ray.mTerminationObject)
2281                return 0.0f;
2282
2283        ViewCellContainer viewCells;
2284
2285        static Ray hray;
2286        hray.Init(ray);
2287
2288        float tmin = 0, tmax = 1.0;
2289
2290        if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax)) {
2291          //      cerr<<"ray outside view space box\n";
2292          return 0;
2293        }
2294
2295        Vector3 origin = hray.Extrap(tmin);
2296        Vector3 termination = hray.Extrap(tmax);
2297
2298        ViewCell::NewMail();
2299
2300        // traverse the view space subdivision
2301        CastLineSegment(origin, termination, viewCells);
2302
2303       
2304        if (storeViewCells)
2305        {       
2306          // copy viewcells memory efficiently
2307          ray.mViewCells.reserve(viewCells.size());
2308          ray.mViewCells = viewCells;
2309        }
2310
2311        // optain pvs entry (can be different from hit object)
2312        Intersectable *terminationObj = GetIntersectable(ray, true);
2313
2314        ViewCellContainer::const_iterator it = viewCells.begin();
2315
2316        for (; it != viewCells.end(); ++ it)
2317        {
2318                ComputeViewCellContribution(*it,
2319                                                                        ray,
2320                                                                        terminationObj,
2321                                                                        ray.mTermination,
2322                                                                        addRays);
2323        }
2324
2325#if AVG_RAY_CONTRIBUTIONS
2326        ray.mRelativePvsContribution /= (float)viewCells.size();
2327#endif
2328 
2329#if USE_RAY_LENGTH_AS_CONTRIBUTION
2330        float c = 0.0f;
2331        if (terminationObj)
2332          c = ray.Length();
2333        ray.mRelativePvsContribution = ray.mPvsContribution = c;
2334        return c;
2335#else
2336        return ABS_CONTRIBUTION_WEIGHT*ray.mPvsContribution +
2337          (1.0f - ABS_CONTRIBUTION_WEIGHT)*ray.mRelativePvsContribution;
2338#endif
2339}
2340
2341
2342void ViewCellsManager::GetRaySets(const VssRayContainer &sourceRays,
2343                                                                  const int maxSize,
2344                                                                  VssRayContainer &usedRays,
2345                                                                  VssRayContainer *savedRays) const
2346{
2347        const int limit = min(maxSize, (int)sourceRays.size());
2348        const float prop = (float)limit / ((float)sourceRays.size() + Limits::Small);
2349
2350        VssRayContainer::const_iterator it, it_end = sourceRays.end();
2351        for (it = sourceRays.begin(); it != it_end; ++ it)
2352        {
2353                if (Random(1.0f) < prop)
2354                        usedRays.push_back(*it);
2355                else if (savedRays)
2356                        savedRays->push_back(*it);
2357        }
2358}
2359
2360
2361float ViewCellsManager::GetRendercost(ViewCell *viewCell) const
2362{
2363        return (float)mViewCellsTree->GetPvsCost(viewCell);
2364}
2365
2366
2367float ViewCellsManager::GetAccVcArea()
2368{
2369        // if already computed
2370        if (mTotalAreaValid)
2371        {
2372                return mTotalArea;
2373        }
2374
2375        mTotalArea = 0;
2376        ViewCellContainer::const_iterator it, it_end = mViewCells.end();
2377
2378        for (it = mViewCells.begin(); it != it_end; ++ it)
2379        {
2380                //Debug << "area: " << GetArea(*it);
2381        mTotalArea += GetArea(*it);
2382        }
2383
2384        mTotalAreaValid = true;
2385
2386        return mTotalArea;
2387}
2388
2389
2390void ViewCellsManager::PrintStatistics(ostream &s) const
2391{
2392        s << mCurrentViewCellsStats << endl;
2393}
2394
2395
2396void ViewCellsManager::CreateUniqueViewCellIds()
2397{
2398        if (ViewCellsTreeConstructed())
2399        {
2400                mViewCellsTree->CreateUniqueViewCellsIds();
2401        }
2402        else // no view cells tree, handle view cells "myself"
2403        {
2404                int i = 0;
2405                ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
2406                for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
2407                {
2408                        if ((*vit)->GetId() != OUT_OF_BOUNDS_ID)
2409                        {
2410                                mViewCells[i]->SetId(i ++);
2411                        }
2412                }
2413        }
2414}
2415
2416
2417void ViewCellsManager::ExportViewCellsForViz(Exporter *exporter,
2418                                                                                         const AxisAlignedBox3 *sceneBox,
2419                                                                                         const bool colorCode,
2420                                                                                         const AxisAlignedPlane *clipPlane
2421                                                                                         ) const
2422{
2423        ViewCellContainer::const_iterator it, it_end = mViewCells.end();
2424
2425        for (it = mViewCells.begin(); it != it_end; ++ it)
2426        {
2427                if (!mOnlyValidViewCells || (*it)->GetValid())
2428                {
2429                        ExportColor(exporter, *it, colorCode); 
2430                        ExportViewCellGeometry(exporter, *it, sceneBox, clipPlane);
2431                }
2432        }
2433}
2434
2435
2436void ViewCellsManager::CreateViewCellMeshes()
2437{
2438        // convert to meshes
2439        ViewCellContainer::const_iterator it, it_end = mViewCells.end();
2440
2441        for (it = mViewCells.begin(); it != it_end; ++ it)
2442        {
2443                if (!(*it)->GetMesh())
2444                {
2445                        CreateMesh(*it);
2446                }
2447        }
2448}
2449
2450
2451bool ViewCellsManager::ExportViewCells(const string filename,
2452                                                                           const bool exportPvs,
2453                                                                           const ObjectContainer &objects)
2454{
2455        return false;
2456}
2457
2458
2459void ViewCellsManager::CollectViewCells(const int n)
2460{
2461        mNumActiveViewCells = n;
2462        mViewCells.clear();
2463        // implemented in subclasses
2464        CollectViewCells();
2465}
2466
2467
2468void ViewCellsManager::SetViewCellActive(ViewCell *vc) const
2469{
2470        ViewCellContainer leaves;
2471        // sets the pointers to the currently active view cells
2472        mViewCellsTree->CollectLeaves(vc, leaves);
2473
2474        ViewCellContainer::const_iterator lit, lit_end = leaves.end();
2475        for (lit = leaves.begin(); lit != lit_end; ++ lit)
2476        {
2477                dynamic_cast<ViewCellLeaf *>(*lit)->SetActiveViewCell(vc);
2478        }
2479}
2480
2481
2482void ViewCellsManager::SetViewCellsActive()
2483{
2484        // collect leaf view cells and set the pointers to
2485        // the currently active view cells
2486        ViewCellContainer::const_iterator it, it_end = mViewCells.end();
2487
2488        for (it = mViewCells.begin(); it != it_end; ++ it)
2489        {
2490                SetViewCellActive(*it);
2491        }
2492}
2493
2494
2495int ViewCellsManager::GetMaxFilterSize() const
2496{
2497        return mMaxFilterSize; 
2498}
2499
2500
2501static const bool USE_ASCII = true;
2502
2503
2504bool ViewCellsManager::ExportBoundingBoxes(const string filename,
2505                                                                                   const ObjectContainer &objects) const
2506{
2507        ObjectContainer::const_iterator it, it_end = objects.end();
2508       
2509        if (USE_ASCII)
2510        {
2511                ofstream boxesOut(filename.c_str());
2512                if (!boxesOut.is_open())
2513                        return false;
2514
2515                for (it = objects.begin(); it != it_end; ++ it)
2516                {
2517                        MeshInstance *mi = dynamic_cast<MeshInstance *>(*it);
2518                        const AxisAlignedBox3 box = mi->GetBox();
2519
2520                        boxesOut << mi->GetId() << " "
2521                                         << box.Min().x << " "
2522                                         << box.Min().y << " "
2523                                         << box.Min().z << " "
2524                                         << box.Max().x << " "
2525                                         << box.Max().y << " "
2526                     << box.Max().z << endl;   
2527                }
2528
2529                boxesOut.close();
2530        }
2531        else
2532        {
2533                ofstream boxesOut(filename.c_str(), ios::binary);
2534
2535                if (!boxesOut.is_open())
2536                        return false;
2537
2538                for (it = objects.begin(); it != it_end; ++ it)
2539                {       
2540                        MeshInstance *mi = dynamic_cast<MeshInstance *>(*it);
2541                        const AxisAlignedBox3 box = mi->GetBox();
2542                        Vector3 bmin = box.Min();
2543                        Vector3 bmax = box.Max();
2544                        int id = mi->GetId();
2545
2546                        boxesOut.write(reinterpret_cast<char *>(&id), sizeof(int));
2547                        boxesOut.write(reinterpret_cast<char *>(&bmin), sizeof(Vector3));
2548                        boxesOut.write(reinterpret_cast<char *>(&bmax), sizeof(Vector3));
2549                }
2550               
2551                boxesOut.close();
2552        }
2553
2554        return true;
2555}
2556
2557
2558bool ViewCellsManager::LoadBoundingBoxes(const string filename,
2559                                                                                 IndexedBoundingBoxContainer &boxes) const
2560{
2561        Vector3 bmin, bmax;
2562        int id;
2563
2564        if (USE_ASCII)
2565        {
2566                ifstream boxesIn(filename.c_str());
2567               
2568                if (!boxesIn.is_open())
2569                {
2570                        cout << "failed to open file " << filename << endl;
2571                        return false;
2572                }
2573
2574                string buf;
2575                while (!(getline(boxesIn, buf)).eof())
2576                {
2577                        sscanf(buf.c_str(), "%d %f %f %f %f %f %f",
2578                                   &id, &bmin.x, &bmin.y, &bmin.z,
2579                                   &bmax.x, &bmax.y, &bmax.z);
2580               
2581                        AxisAlignedBox3 box(bmin, bmax);
2582                        //      MeshInstance *mi = new MeshInstance();
2583                        // HACK: set bounding box to new box
2584                        //mi->mBox = box;
2585
2586                        boxes.push_back(IndexedBoundingBox(id, box));
2587                }
2588
2589                boxesIn.close();
2590        }
2591        else
2592        {
2593                ifstream boxesIn(filename.c_str(), ios::binary);
2594
2595                if (!boxesIn.is_open())
2596                        return false;
2597
2598                while (1)
2599                {
2600                        boxesIn.read(reinterpret_cast<char *>(&id), sizeof(Vector3));
2601                        boxesIn.read(reinterpret_cast<char *>(&bmin), sizeof(Vector3));
2602                        boxesIn.read(reinterpret_cast<char *>(&bmax), sizeof(Vector3));
2603                       
2604                        if (boxesIn.eof())
2605                                break;
2606
2607                       
2608                        AxisAlignedBox3 box(bmin, bmax);
2609                        MeshInstance *mi = new MeshInstance(NULL);
2610
2611                        // HACK: set bounding box to new box
2612                        //mi->mBox = box;
2613                        //boxes.push_back(mi);
2614                        boxes.push_back(IndexedBoundingBox(id, box));
2615                }
2616
2617                boxesIn.close();
2618        }
2619
2620        return true;
2621}
2622
2623
2624float ViewCellsManager::GetFilterWidth()
2625{
2626        return mFilterWidth;
2627}
2628
2629
2630float ViewCellsManager::GetAbsFilterWidth()
2631{
2632        return Magnitude(mViewSpaceBox.Size()) * mFilterWidth;
2633}
2634
2635
2636void ViewCellsManager::UpdateScalarPvsSize(ViewCell *vc,
2637                                                                                   const float pvsCost,
2638                                                                                   const int entriesInPvs) const
2639{
2640        vc->mPvsCost = pvsCost;
2641        vc->mEntriesInPvs = entriesInPvs;
2642
2643        vc->mPvsSizeValid = true;
2644}
2645
2646
2647void
2648ViewCellsManager::ApplyFilter(ViewCell *viewCell,
2649                                                          KdTree *kdTree,
2650                                                          const float viewSpaceFilterSize,
2651                                                          const float spatialFilterSize,
2652                                                          ObjectPvs &pvs
2653                                                          )
2654{
2655  // extend the pvs of the viewcell by pvs of its neighbors
2656  // and apply spatial filter by including all neighbors of the objects
2657  // in the pvs
2658
2659  // get all viewcells intersecting the viewSpaceFilterBox
2660  // and compute the pvs union
2661 
2662  //Vector3 center = viewCell->GetBox().Center();
2663  //  Vector3 center = m->mBox.Center();
2664
2665        //  AxisAlignedBox3 box(center - Vector3(viewSpaceFilterSize/2),
2666        //                                        center + Vector3(viewSpaceFilterSize/2));
2667        if (!ViewCellsConstructed())
2668                return;
2669
2670        if (viewSpaceFilterSize >= 0.0f) {
2671
2672                const bool usePrVS = false;
2673
2674                if (!usePrVS) {
2675                        AxisAlignedBox3 box = GetViewCellBox(viewCell);
2676                        box.Enlarge(Vector3(viewSpaceFilterSize/2));
2677
2678                        ViewCellContainer viewCells;
2679                        ComputeBoxIntersections(box, viewCells);
2680
2681                        //  cout<<"box="<<box<<endl;
2682                        ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
2683
2684                        for (; it != it_end; ++ it)
2685                        {
2686                                ObjectPvs interPvs;
2687                                //cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
2688                                ObjectPvs::Merge(interPvs, pvs, (*it)->GetPvs());
2689
2690                                pvs = interPvs;
2691                        }
2692                } else
2693                {
2694                        PrVs prvs;
2695                        AxisAlignedBox3 box = GetViewCellBox(viewCell);
2696
2697                        //  mViewCellsManager->SetMaxFilterSize(1);
2698                        GetPrVS(box.Center(), prvs, viewSpaceFilterSize);
2699                        pvs = prvs.mViewCell->GetPvs();
2700                        DeleteLocalMergeTree(prvs.mViewCell);
2701                }
2702        }
2703        else
2704        {
2705                pvs = viewCell->GetPvs();
2706        }
2707
2708        if (spatialFilterSize >=0.0f)
2709                ApplySpatialFilter(kdTree, spatialFilterSize, pvs);
2710
2711}
2712
2713
2714
2715void
2716ViewCellsManager::ApplyFilter(KdTree *kdTree,
2717                                                          const float relViewSpaceFilterSize,
2718                                                          const float relSpatialFilterSize
2719                                                          )
2720{
2721
2722        if (!ViewCellsConstructed())
2723                return;
2724
2725        ViewCellContainer::const_iterator it, it_end = mViewCells.end();
2726
2727        ObjectPvs *newPvs;
2728        newPvs = new ObjectPvs[mViewCells.size()];
2729
2730        float viewSpaceFilterSize = Magnitude(mViewSpaceBox.Size())*relViewSpaceFilterSize;
2731        float spatialFilterSize = Magnitude(kdTree->GetBox().Size())*relSpatialFilterSize;
2732       
2733        int i;
2734        for (i=0, it = mViewCells.begin(); it != it_end; ++ it, ++ i) {
2735          ApplyFilter(*it,
2736                                  kdTree,
2737                                  viewSpaceFilterSize,
2738                                  spatialFilterSize,
2739                                  newPvs[i]
2740                                  );
2741        }
2742       
2743        // now replace all pvss
2744        for (i = 0, it = mViewCells.begin(); it != it_end; ++ it, ++ i) {
2745         
2746          ObjectPvs &pvs = (*it)->GetPvs();
2747          pvs.Clear();
2748          pvs = newPvs[i];
2749          newPvs[i].Clear();
2750        }
2751
2752        delete [] newPvs;
2753}
2754
2755
2756void
2757ViewCellsManager::ApplySpatialFilter(
2758                                                                         KdTree *kdTree,
2759                                                                         const float spatialFilterSize,
2760                                                                         ObjectPvs &pvs
2761                                                                         )
2762{
2763  // now compute a new Pvs by including also objects intersecting the
2764  // extended boxes of visible objects
2765  Intersectable::NewMail();
2766
2767  ObjectPvsIterator pit = pvs.GetIterator();
2768
2769  while (pit.HasMoreEntries())
2770  {             
2771          ObjectPvsEntry entry = pit.Next();
2772 
2773          Intersectable *object = entry.mObject;
2774      object->Mail();
2775  }
2776
2777  ObjectPvs nPvs;
2778  int nPvsSize = 0;
2779 
2780  ObjectPvsIterator pit2 = pvs.GetIterator();
2781
2782  while (pit2.HasMoreEntries())
2783  {             
2784          // now go through the pvs again
2785          ObjectPvsEntry entry = pit2.Next();
2786          Intersectable *object = entry.mObject;
2787
2788          //    Vector3 center = object->GetBox().Center();
2789          //    AxisAlignedBox3 box(center - Vector3(spatialFilterSize/2),
2790          //                                            center + Vector3(spatialFilterSize/2));
2791
2792          AxisAlignedBox3 box = object->GetBox();
2793          box.Enlarge(Vector3(spatialFilterSize/2));
2794
2795          ObjectContainer objects;
2796
2797          // $$ warning collect objects takes only unmailed ones!
2798          kdTree->CollectObjects(box, objects);
2799          //    cout<<"collected objects="<<objects.size()<<endl;
2800          ObjectContainer::const_iterator noi = objects.begin();
2801          for (; noi != objects.end(); ++ noi)
2802          {
2803                  Intersectable *o = *noi;
2804                 
2805                  // $$ JB warning: pdfs are not correct at this point!   
2806                  nPvs.AddSample(o, Limits::Small);
2807                  nPvsSize ++;
2808          }
2809  }
2810
2811  // cout<<"nPvs size = "<<nPvsSize<<endl;
2812  pvs.MergeInPlace(nPvs);
2813}
2814
2815
2816void ViewCellsManager::MergeViewCellsRecursivly(ObjectPvs &pvs,
2817                                                                                                const ViewCellContainer &viewCells) const
2818{
2819        MergeViewCellsRecursivly(pvs, viewCells, 0, (int)viewCells.size() - 1);
2820}
2821
2822
2823void ViewCellsManager::MergeViewCellsRecursivly(ObjectPvs &pvs,
2824                                                                                                const ViewCellContainer &viewCells,
2825                                                                                                const int leftIdx,
2826                                                                                                const int rightIdx) const
2827{
2828        if (leftIdx == rightIdx)
2829        {
2830                pvs = viewCells[leftIdx]->GetPvs();
2831        }
2832        else
2833        {
2834                const int midSplit = (leftIdx + rightIdx) / 2;
2835       
2836                ObjectPvs leftPvs, rightPvs;
2837                MergeViewCellsRecursivly(leftPvs, viewCells, leftIdx, midSplit);
2838                MergeViewCellsRecursivly(rightPvs, viewCells, midSplit, rightIdx);
2839
2840        ObjectPvs::Merge(pvs, leftPvs, rightPvs);
2841        }
2842}
2843
2844
2845PvsFilterStatistics
2846ViewCellsManager::ApplyFilter2(ViewCell *viewCell,
2847                                                           const bool useViewSpaceFilter,
2848                                                           const float filterSize,
2849                                                           ObjectPvs &pvs,
2850                                                           vector<AxisAlignedBox3> *filteredBoxes
2851                                                           )
2852{
2853  //cout<<"y";
2854  PvsFilterStatistics stats;
2855
2856  AxisAlignedBox3 vbox = GetViewCellBox(viewCell);
2857  Vector3 center = vbox.Center();
2858  // copy the PVS
2859  Intersectable::NewMail();
2860  ObjectPvs basePvs = viewCell->GetPvs();
2861  ObjectPvsIterator pit = basePvs.GetIterator();
2862
2863  pvs.Reserve(viewCell->GetFilteredPvsSize());
2864
2865  if (!mUseKdPvs)
2866  {
2867          // first mark all objects from this pvs
2868          while (pit.HasMoreEntries()) 
2869          {
2870                  ObjectPvsEntry entry = pit.Next();
2871                  Intersectable *object = entry.mObject;
2872                  object->Mail();
2873          }
2874  }
2875 
2876  int pvsSize = 0;
2877  int nPvsSize = 0;
2878  float samples = (float)basePvs.GetSamples();
2879 
2880  Debug<<"f #s="<<samples<<"  pvs size = "<<basePvs.GetSize();
2881  //  cout<<"Filter size = "<<filterSize<<endl;
2882  //  cout<<"vbox = "<<vbox<<endl;
2883  //  cout<<"center = "<<center<<endl;
2884
2885
2886   // Minimal number of local samples to take into account
2887   // the local sampling density.
2888   // The size of the filter is a minimum of the conservative
2889   // local sampling density estimate (#rays intersecting teh viewcell and
2890   // the object)
2891   // and gobal estimate for the view cell
2892   // (total #rays intersecting the viewcell)
2893  int minLocalSamples = 2;
2894 
2895  float viewCellRadius = 0.5f*Magnitude(vbox.Diagonal());
2896 
2897  // now compute the filter box around the current viewCell
2898 
2899  if (useViewSpaceFilter) {
2900        //      float radius = Max(viewCellRadius/100.0f, avgRadius - viewCellRadius);
2901        float radius = viewCellRadius/100.0f;
2902        vbox.Enlarge(radius);
2903        cout<<"vbox = "<<vbox<<endl;
2904        ViewCellContainer viewCells;
2905        ComputeBoxIntersections(vbox, viewCells);
2906       
2907        ViewCellContainer::const_iterator it = viewCells.begin(),
2908          it_end = viewCells.end();
2909        int i = 0;
2910        for (i=0; it != it_end; ++ it, ++ i)
2911          if ((*it) != viewCell) {
2912                //cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
2913                basePvs.MergeInPlace((*it)->GetPvs());
2914          }
2915       
2916        // update samples and globalC
2917        samples = (float)pvs.GetSamples();
2918        //      cout<<"neighboring viewcells = "<<i-1<<endl;
2919        //      cout<<"Samples' = "<<samples<<endl;
2920  }
2921 
2922  // Minimal number of samples so that filtering takes place
2923#define MIN_SAMPLES  50
2924 
2925  if (samples > MIN_SAMPLES) {
2926        float globalC = 2.0f*filterSize/sqrt(samples);
2927       
2928        pit = basePvs.GetIterator();
2929       
2930        ObjectContainer objects;
2931       
2932        while (pit.HasMoreEntries()) {         
2933          ObjectPvsEntry entry = pit.Next();
2934         
2935          Intersectable *object = entry.mObject;
2936          // compute filter size based on the distance and the numebr of samples
2937          AxisAlignedBox3 box = object->GetBox();
2938         
2939          float distance = Distance(center, box.Center());
2940          float globalRadius = distance*globalC;
2941         
2942          int objectSamples = (int)entry.mData.mSumPdf;
2943          float localRadius = MAX_FLOAT;
2944         
2945          localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/
2946                sqrt((float)objectSamples);
2947         
2948          //      cout<<"os="<<objectSamples<<" lr="<<localRadius<<" gr="<<globalRadius<<endl;
2949         
2950          // now compute the filter size
2951          float radius;
2952         
2953#if 0
2954          if (objectSamples <= 1) {
2955                if (localRadius > globalRadius) {
2956                  radius = 0.5flRadius;
2957                  stats.mLocalFilterCount++;
2958                } else {
2959                  radius = globalRadius;
2960                  stats.mGlobalFilterCount++;
2961                }
2962          } else {
2963                radius = localRadius;
2964                stats.mLocalFilterCount++;
2965          }
2966#else
2967          radius = 0.5f*globalRadius + 0.5f*localRadius;
2968          stats.mLocalFilterCount++;
2969          stats.mGlobalFilterCount++;
2970#endif
2971         
2972          stats.mAvgFilterRadius += radius;
2973         
2974          // cout<<"box = "<<box<<endl;
2975          //    cout<<"distance = "<<distance<<endl;
2976          //    cout<<"radiues = "<<radius<<endl;
2977         
2978          box.Enlarge(Vector3(radius));
2979
2980          if (filteredBoxes)
2981                filteredBoxes->push_back(box);
2982
2983          objects.clear();
2984          // $$ warning collect objects takes only unmailed ones!
2985          CollectObjects(box, objects);
2986          //    cout<<"collected objects="<<objects.size()<<endl;
2987          ObjectContainer::const_iterator noi = objects.begin();
2988          for (; noi != objects.end(); ++ noi) {
2989                Intersectable *o = *noi;
2990                // $$ JB warning: pdfs are not correct at this point!     
2991                pvs.AddSampleDirty(o, Limits::Small);
2992          }
2993        }
2994        stats.mAvgFilterRadius /= (stats.mLocalFilterCount + stats.mGlobalFilterCount);
2995  }
2996 
2997  Debug<<" nPvs size = "<<pvs.GetSize()<<endl;
2998 
2999  if (!mUseKdPvs)
3000  {
3001          // copy the base pvs to the new pvs
3002          pit = basePvs.GetIterator();
3003          while (pit.HasMoreEntries())
3004          {             
3005                  ObjectPvsEntry entry = pit.Next();
3006                  pvs.AddSampleDirty(entry.mObject, entry.mData.mSumPdf);
3007          }
3008  }
3009 
3010  pvs.SimpleSort();
3011  viewCell->SetFilteredPvsSize(pvs.GetSize());
3012 
3013  Intersectable::NewMail();
3014  return stats;
3015}
3016
3017
3018
3019void ViewCellsManager::ExportColor(Exporter *exporter,
3020                                                                   ViewCell *vc,
3021                                                                   bool colorCode) const
3022{
3023        const bool vcValid = CheckValidity(vc, mMinPvsSize, mMaxPvsSize);
3024
3025        float importance = 0;
3026        static Material m;
3027        //cout << "color code: " << colorCode << endl;
3028        switch (mColorCode)
3029        {
3030        case 0: // Random
3031                {
3032                        if (vcValid)
3033                        {
3034                                m.mDiffuseColor.r = 0.2f + RandomValue(0.0f, 0.8f);
3035                                m.mDiffuseColor.g = 0.2f + RandomValue(0.0f, 0.8f);
3036                                m.mDiffuseColor.b = 0.2f + RandomValue(0.0f, 0.8f);
3037                        }
3038                        else
3039                        {
3040                                m.mDiffuseColor.r = 0.0f;
3041                                m.mDiffuseColor.g = 1.0f;
3042                                m.mDiffuseColor.b = 0.0f;
3043                        }
3044
3045                        exporter->SetForcedMaterial(m);
3046                        return;
3047                }
3048               
3049        case 1: // pvs
3050                {
3051                        if (mCurrentViewCellsStats.maxPvs)
3052                        {
3053                                importance = (float)mViewCellsTree->GetPvsCost(vc) /
3054                                                         (float)mCurrentViewCellsStats.maxPvs;
3055                        }
3056                }
3057                break;
3058        case 2: // merges
3059                {
3060            const int lSize = mViewCellsTree->GetNumInitialViewCells(vc);
3061                        importance = (float)lSize / (float)mCurrentViewCellsStats.maxLeaves;
3062                }
3063                break;
3064#if 0
3065        case 3: // merge tree differene
3066                {
3067                        importance = (float)GetMaxTreeDiff(vc) /
3068                                (float)(mVspBspTree->GetStatistics().maxDepth * 2);
3069
3070                }
3071                break;
3072#endif
3073        default:
3074                break;
3075        }
3076
3077        // special color code for invalid view cells
3078        m.mDiffuseColor.r = importance;
3079        m.mDiffuseColor.b = 1.0f;//vcValid ? 0.0f : 1.0f;
3080        m.mDiffuseColor.g = 1.0f - importance;
3081
3082        //Debug << "importance: " << importance << endl;
3083        exporter->SetForcedMaterial(m);
3084}
3085
3086
3087void ViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
3088                                                                                          vector<MergeCandidate> &candidates)
3089{
3090        // implemented in subclasses
3091}
3092
3093
3094void ViewCellsManager::UpdatePvsForEvaluation()
3095{
3096        ObjectPvs objPvs;
3097        UpdatePvsForEvaluation(mViewCellsTree->GetRoot(), objPvs);
3098}
3099
3100void ViewCellsManager::UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs)
3101{
3102        // terminate traversal
3103        if (root->IsLeaf())
3104        {
3105                //cout << "updating leaf" << endl;
3106                // we assume that pvs is explicitly stored in leaves
3107                pvs = root->GetPvs();
3108                UpdateScalarPvsSize(root, pvs.EvalPvsCost(), pvs.GetSize());
3109                return;
3110        }
3111
3112        ////////////////
3113        //-- interior node => propagate pvs up the tree
3114
3115        ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(root);
3116
3117        // reset interior pvs
3118        interior->GetPvs().Clear();
3119        // reset recursive pvs
3120        pvs.Clear();
3121
3122        // pvss of child nodes
3123        vector<ObjectPvs> pvsList;
3124        pvsList.resize((int)interior->mChildren.size());
3125
3126        ViewCellContainer::const_iterator vit, vit_end = interior->mChildren.end();
3127       
3128        int i = 0;
3129
3130        for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit, ++ i)
3131        {
3132                //////////////////
3133                //-- recursivly compute child pvss
3134                UpdatePvsForEvaluation(*vit, pvsList[i]/*objPvs*/);
3135        }
3136
3137#if 1
3138        Intersectable::NewMail();
3139
3140        //-- faster way of computing pvs:
3141        //-- construct merged pvs by adding
3142        //-- and only those of the next pvs which were not mailed.
3143        //-- note: sumpdf is not correct!!
3144
3145        vector<ObjectPvs>::iterator oit = pvsList.begin();
3146
3147        for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit, ++ oit)
3148        {
3149                ObjectPvsIterator pit = (*oit).GetIterator();
3150               
3151                // first mark all object from this pvs
3152                while (pit.HasMoreEntries())
3153                {               
3154                        ObjectPvsEntry entry = pit.Next();
3155
3156                        Intersectable *intersect = entry.mObject;
3157
3158                        if (!intersect->Mailed())
3159                        {
3160                                pvs.AddSample(intersect, entry.mData.mSumPdf);
3161                                intersect->Mail();
3162                        }
3163                }
3164        }
3165
3166        // store pvs in this node
3167        if (mViewCellsTree->ViewCellsStorage() == ViewCellsTree::PVS_IN_INTERIORS)
3168        {
3169                interior->SetPvs(pvs);
3170        }
3171       
3172        // set new pvs size
3173        UpdateScalarPvsSize(interior, pvs.EvalPvsCost(), pvs.GetSize());
3174       
3175#else
3176        // really merge cells: slow put sumPdf is correct
3177        viewCellInterior->GetPvs().Merge(backVc->GetPvs());
3178        viewCellInterior->GetPvs().Merge(frontVc->GetPvs());
3179#endif
3180}
3181
3182
3183
3184/*******************************************************************/
3185/*               BspViewCellsManager implementation                */
3186/*******************************************************************/
3187
3188
3189BspViewCellsManager::BspViewCellsManager(ViewCellsTree *vcTree, BspTree *bspTree):
3190ViewCellsManager(vcTree), mBspTree(bspTree)
3191{
3192        Environment::GetSingleton()->GetIntValue("BspTree.Construction.samples", mInitialSamples);
3193
3194        mBspTree->SetViewCellsManager(this);
3195        mBspTree->SetViewCellsTree(mViewCellsTree);
3196}
3197
3198
3199bool BspViewCellsManager::ViewCellsConstructed() const
3200{
3201        return mBspTree->GetRoot() != NULL;
3202}
3203
3204
3205ViewCell *BspViewCellsManager::GenerateViewCell(Mesh *mesh) const
3206{
3207        return new BspViewCell(mesh);
3208}
3209
3210
3211int BspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
3212                                                                                          const VssRayContainer &rays)
3213{
3214        // if view cells were already constructed, we can finish
3215        if (ViewCellsConstructed())
3216                return 0;
3217
3218        int sampleContributions = 0;
3219
3220        // construct view cells using the collected samples
3221        RayContainer constructionRays;
3222        VssRayContainer savedRays;
3223
3224        // choose a a number of rays based on the ratio of cast rays / requested rays
3225        const int limit = min(mInitialSamples, (int)rays.size());
3226        VssRayContainer::const_iterator it, it_end = rays.end();
3227
3228        const float prop = (float)limit / ((float)rays.size() + Limits::Small);
3229
3230        for (it = rays.begin(); it != it_end; ++ it)
3231        {
3232                if (Random(1.0f) < prop)
3233                        constructionRays.push_back(new Ray(*(*it)));
3234                else
3235                        savedRays.push_back(*it);
3236        }
3237
3238    if (!mUsePredefinedViewCells)
3239        {
3240                // no view cells loaded
3241                mBspTree->Construct(objects, constructionRays, &mViewSpaceBox);
3242                // collect final view cells
3243                mBspTree->CollectViewCells(mViewCells);
3244        }
3245        else
3246        {       
3247                // use predefined view cells geometry =>
3248                // contruct bsp hierarchy over them
3249                mBspTree->Construct(mViewCells);
3250        }
3251
3252        // destroy rays created only for construction
3253        CLEAR_CONTAINER(constructionRays);
3254
3255        Debug << mBspTree->GetStatistics() << endl;
3256        Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
3257
3258        // recast rest of the rays
3259        if (SAMPLE_AFTER_SUBDIVISION)
3260                ComputeSampleContributions(savedRays, true, false);
3261
3262        // real meshes are contructed at this stage
3263        if (0)
3264        {
3265                cout << "finalizing view cells ... ";
3266                FinalizeViewCells(true);
3267                cout << "finished" << endl;     
3268        }
3269
3270        return sampleContributions;
3271}
3272
3273
3274void BspViewCellsManager::CollectViewCells()
3275{       
3276        if (!ViewCellsTreeConstructed())
3277        {       // view cells tree constructed 
3278                mBspTree->CollectViewCells(mViewCells);
3279        }
3280        else
3281        {       // we can use the view cells tree hierarchy to get the right set
3282                mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumActiveViewCells);
3283        }
3284}
3285
3286
3287float BspViewCellsManager::GetProbability(ViewCell *viewCell)
3288{
3289        if (1)
3290                return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
3291        else
3292                // compute view cell area as subsititute for probability
3293                return GetArea(viewCell) / GetAccVcArea();
3294}
3295
3296
3297
3298int BspViewCellsManager::CastLineSegment(const Vector3 &origin,
3299                                                                                 const Vector3 &termination,
3300                                                                                 ViewCellContainer &viewcells)
3301{
3302        return mBspTree->CastLineSegment(origin, termination, viewcells);
3303}
3304
3305
3306void ViewCellsManager::ExportMergedViewCells(const ObjectContainer &objects)
3307{
3308        // save color code
3309        const int savedColorCode = mColorCode;
3310
3311        Exporter *exporter;
3312
3313#if 0
3314        // export merged view cells
3315        mColorCode = 0; // use random colors
3316
3317        exporter = Exporter::GetExporter("merged_view_cells.wrl");
3318
3319        cout << "exporting view cells after merge ... ";
3320
3321        if (exporter)
3322        {
3323                if (mExportGeometry)
3324                {
3325                        exporter->ExportGeometry(objects);
3326                }
3327
3328                exporter->SetFilled();
3329                ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
3330
3331                delete exporter;
3332        }
3333        cout << "finished" << endl;
3334#endif
3335
3336        // export merged view cells using pvs color coding
3337        exporter = Exporter::GetExporter("merged_view_cells_pvs.wrl");
3338        cout << "exporting view cells after merge (pvs size) ... ";     
3339
3340        if (exporter)
3341        {
3342                if (mExportGeometry)
3343                {
3344                        exporter->ExportGeometry(objects);
3345                }
3346
3347                exporter->SetFilled();
3348                mColorCode = 1;
3349
3350                ExportViewCellsForViz(exporter, NULL,  mColorCode, GetClipPlane());
3351
3352                delete exporter;
3353        }
3354        cout << "finished" << endl;
3355       
3356        mColorCode = savedColorCode;
3357}
3358
3359
3360int BspViewCellsManager::PostProcess(const ObjectContainer &objects,
3361                                                                         const VssRayContainer &rays)
3362{
3363        if (!ViewCellsConstructed())
3364        {
3365                Debug << "view cells not constructed" << endl;
3366                return 0;
3367        }
3368       
3369        // view cells already finished before post processing step,
3370        // i.e., because they were loaded from disc
3371        if (mViewCellsFinished)
3372        {
3373                FinalizeViewCells(true);
3374                EvaluateViewCellsStats();
3375
3376                return 0;
3377        }
3378
3379        //////////////////
3380        //-- merge leaves of the view cell hierarchy   
3381       
3382        cout << "starting post processing using " << mPostProcessSamples << " samples ... ";
3383        long startTime = GetTime();
3384       
3385        VssRayContainer postProcessRays;
3386        GetRaySets(rays, mPostProcessSamples, postProcessRays);
3387
3388        if (mMergeViewCells)
3389        {
3390                cout << "constructing visibility based merge tree" << endl;
3391                mViewCellsTree->ConstructMergeTree(rays, objects);
3392        }
3393        else
3394        {
3395                cout << "constructing spatial merge tree" << endl;
3396                ViewCell *root;
3397                // the spatial merge tree is difficult to build for
3398                // this type of construction, as view cells cover several
3399                // leaves => create dummy tree which is only 2 levels deep
3400                if (mUsePredefinedViewCells)
3401                {
3402                        root = ConstructDummyMergeTree(mBspTree->GetRoot());
3403                }
3404                else
3405                {
3406                        // create spatial merge hierarchy
3407                        root = ConstructSpatialMergeTree(mBspTree->GetRoot());
3408                }
3409               
3410                mViewCellsTree->SetRoot(root);
3411
3412                // recompute pvs in the whole hierarchy
3413                ObjectPvs pvs;
3414                UpdatePvsForEvaluation(root, pvs);
3415        }
3416
3417        cout << "finished" << endl;
3418        cout << "merged view cells in "
3419                 << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
3420
3421        Debug << "Postprocessing: Merged view cells in "
3422                << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl << endl;
3423
3424       
3425        ////////////////////////
3426        //-- visualization and statistics after merge
3427
3428        if (1)
3429        {
3430                char mstats[100];
3431                Environment::GetSingleton()->GetStringValue("ViewCells.mergeStats", mstats);
3432                mViewCellsTree->ExportStats(mstats);
3433        }
3434
3435        // recompute view cells and stats
3436        ResetViewCells();
3437        Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
3438
3439        //  visualization of the view cells
3440        if (1) ExportMergedViewCells(objects);
3441
3442        // compute final meshes and volume / area
3443        if (1) FinalizeViewCells(true);
3444       
3445        return 0;
3446}
3447
3448
3449BspViewCellsManager::~BspViewCellsManager()
3450{
3451}
3452
3453
3454int BspViewCellsManager::GetType() const
3455{
3456        return BSP;
3457}
3458
3459
3460void BspViewCellsManager::Visualize(const ObjectContainer &objects,
3461                                                                        const VssRayContainer &sampleRays)
3462{
3463        if (!ViewCellsConstructed())
3464                return;
3465       
3466        const int savedColorCode = mColorCode;
3467       
3468        if (1) // export final view cells
3469        {
3470                mColorCode = 1; // hack color code
3471                Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
3472       
3473                cout << "exporting view cells after merge (pvs size) ... ";     
3474
3475                if (exporter)
3476                {
3477                        if (mExportGeometry)
3478                        {
3479                                exporter->ExportGeometry(objects);
3480                        }
3481
3482                        ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
3483                        delete exporter;
3484                }
3485                cout << "finished" << endl;
3486        }
3487
3488        // reset color code
3489        mColorCode = savedColorCode;
3490
3491
3492        //////////////////
3493        //-- visualization of the BSP splits
3494
3495        bool exportSplits = false;
3496        Environment::GetSingleton()->GetBoolValue("BspTree.Visualization.exportSplits", exportSplits);
3497
3498        if (exportSplits)
3499        {
3500                cout << "exporting splits ... ";
3501                ExportSplits(objects);
3502                cout << "finished" << endl;
3503        }
3504
3505        int leafOut;
3506        Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
3507        const int raysOut = 100;
3508        ExportSingleViewCells(objects, leafOut, false, true, false, raysOut, "");
3509}
3510
3511
3512void BspViewCellsManager::ExportSplits(const ObjectContainer &objects)
3513{
3514        Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
3515
3516        if (exporter)
3517        {
3518                //exporter->SetFilled();
3519                if (mExportGeometry)
3520                {
3521                        exporter->ExportGeometry(objects);
3522                }
3523
3524                Material m;
3525                m.mDiffuseColor = RgbColor(1, 0, 0);
3526                exporter->SetForcedMaterial(m);
3527                exporter->SetWireframe();
3528
3529                exporter->ExportBspSplits(*mBspTree, true);
3530
3531                // NOTE: take forced material, else big scenes cannot be viewed
3532                m.mDiffuseColor = RgbColor(0, 1, 0);
3533                exporter->SetForcedMaterial(m);
3534                //exporter->ResetForcedMaterial();
3535
3536                delete exporter;
3537        }
3538}
3539
3540
3541void BspViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
3542                                                                                                const int maxViewCells,
3543                                                                                                const bool sortViewCells,
3544                                                                                                const bool exportPvs,
3545                                                                                                const bool exportRays,
3546                                                                                                const int maxRays,
3547                                                                                                const string prefix,
3548                                                                                                VssRayContainer *visRays)
3549{
3550        if (sortViewCells)
3551        {       // sort view cells to visualize the largest view cells
3552                sort(mViewCells.begin(), mViewCells.end(), ViewCell::LargerRenderCost);
3553        }
3554
3555        //////////
3556        //-- some view cells for output
3557
3558        ViewCell::NewMail();
3559        const int limit = min(maxViewCells, (int)mViewCells.size());
3560       
3561        for (int i = 0; i < limit; ++ i)
3562        {
3563                const int idx = sortViewCells ? (int)RandomValue(0, (float)mViewCells.size() - 0.5f) : i;
3564                ViewCell *vc = mViewCells[idx];
3565
3566                if (vc->Mailed() || vc->GetId() == OUT_OF_BOUNDS_ID)
3567                        continue;
3568
3569                vc->Mail();
3570
3571                ObjectPvs pvs;
3572                mViewCellsTree->GetPvs(vc, pvs);
3573
3574                char s[64]; sprintf(s, "%sviewcell-%04d.wrl", prefix.c_str(), i);
3575                Exporter *exporter = Exporter::GetExporter(s);
3576               
3577                cout << "view cell " << idx << ": pvs cost=" << (int)mViewCellsTree->GetPvsCost(vc) << endl;
3578
3579                if (exportRays)
3580                {
3581                        ////////////
3582                        //-- export rays piercing this view cell
3583
3584                        // use rays stored with the view cells
3585                        VssRayContainer vcRays, vcRays2, vcRays3;
3586            VssRayContainer collectRays;
3587
3588                        // collect initial view cells
3589                        ViewCellContainer leaves;
3590                        mViewCellsTree->CollectLeaves(vc, leaves);
3591
3592                        ViewCellContainer::const_iterator vit, vit_end = leaves.end();
3593                for (vit = leaves.begin(); vit != vit_end; ++ vit)
3594                        {       
3595                                // prepare some rays for output
3596                                VssRayContainer::const_iterator rit, rit_end = (*vit)->GetOrCreateRays()->end();
3597                                for (rit = (*vit)->GetOrCreateRays()->begin(); rit != rit_end; ++ rit)
3598                                {
3599                                        collectRays.push_back(*rit);
3600                                }
3601                        }
3602
3603                        const int raysOut = min((int)collectRays.size(), maxRays);
3604
3605                        // prepare some rays for output
3606                        VssRayContainer::const_iterator rit, rit_end = collectRays.end();
3607                        for (rit = collectRays.begin(); rit != rit_end; ++ rit)
3608                        {
3609                                const float p = RandomValue(0.0f, (float)collectRays.size());
3610                                if (p < raysOut)
3611                                {
3612                                        if ((*rit)->mFlags & VssRay::BorderSample)
3613                                        {
3614                                                vcRays.push_back(*rit);
3615                                        }
3616                                        else if ((*rit)->mFlags & VssRay::ReverseSample)
3617                                        {
3618                                                vcRays2.push_back(*rit);
3619                                        }
3620                                        else
3621                                        {
3622                                                vcRays3.push_back(*rit);
3623                                        }       
3624                                }
3625                        }
3626
3627                        exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
3628                        exporter->ExportRays(vcRays2, RgbColor(0, 1, 0));
3629                        exporter->ExportRays(vcRays3, RgbColor(1, 1, 1));
3630                }
3631               
3632                ////////////////
3633                //-- export view cell geometry
3634
3635                exporter->SetWireframe();
3636
3637                Material m;//= RandomMaterial();
3638                m.mDiffuseColor = RgbColor(0, 1, 0);
3639                exporter->SetForcedMaterial(m);
3640
3641                ExportViewCellGeometry(exporter, vc, NULL, NULL);
3642                exporter->SetFilled();
3643
3644                if (exportPvs)
3645                {
3646                        Intersectable::NewMail();
3647                        ObjectPvsIterator pit = pvs.GetIterator();
3648
3649                        while (pit.HasMoreEntries())
3650                        {               
3651                                ObjectPvsEntry entry = pit.Next();
3652
3653                // output PVS of view cell
3654                                Intersectable *intersect = entry.mObject;
3655                               
3656                                if (!intersect->Mailed())
3657                                {
3658                                        intersect->Mail();
3659
3660                                        m = RandomMaterial();
3661                                        exporter->SetForcedMaterial(m);
3662                                        exporter->ExportIntersectable(intersect);
3663                                }
3664                        }
3665                        cout << endl;
3666                }
3667               
3668                DEL_PTR(exporter);
3669                cout << "finished" << endl;
3670        }
3671}
3672
3673
3674void BspViewCellsManager::TestSubdivision()
3675{
3676        ViewCellContainer leaves;
3677        mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
3678
3679        ViewCellContainer::const_iterator it, it_end = leaves.end();
3680
3681        const float vol = mViewSpaceBox.GetVolume();
3682        float subdivVol = 0;
3683        float newVol = 0;
3684
3685        for (it = leaves.begin(); it != it_end; ++ it)
3686        {
3687                BspNodeGeometry geom;
3688                mBspTree->ConstructGeometry(*it, geom);
3689
3690                const float lVol = geom.GetVolume();
3691                newVol += lVol;
3692                subdivVol += (*it)->GetVolume();
3693
3694                const float thres = 0.9f;
3695                if ((lVol < ((*it)->GetVolume() * thres)) ||
3696                        (lVol * thres > ((*it)->GetVolume())))
3697                        Debug << "warning: " << lVol << " " << (*it)->GetVolume() << endl;
3698        }
3699       
3700        Debug << "exact volume: " << vol << endl;
3701        Debug << "subdivision volume: " << subdivVol << endl;
3702        Debug << "new volume: " << newVol << endl;
3703}
3704
3705
3706void BspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
3707                                                                                                 ViewCell *vc,
3708                                                                                                 const AxisAlignedBox3 *sceneBox,
3709                                                                                                 const AxisAlignedPlane *clipPlane
3710                                                                                                 ) const
3711{
3712        if (clipPlane)
3713        {
3714                const Plane3 plane = clipPlane->GetPlane();
3715
3716                ViewCellContainer leaves;
3717                mViewCellsTree->CollectLeaves(vc, leaves);
3718                ViewCellContainer::const_iterator it, it_end = leaves.end();
3719
3720                for (it = leaves.begin(); it != it_end; ++ it)
3721                {
3722                        BspNodeGeometry geom;
3723                        BspNodeGeometry front;
3724                        BspNodeGeometry back;
3725
3726                        mBspTree->ConstructGeometry(*it, geom);
3727
3728                        const float eps = 0.0001f;
3729                        const int cf = geom.Side(plane, eps);
3730
3731                        if (cf == -1)
3732                        {
3733                                exporter->ExportPolygons(geom.GetPolys());
3734                        }
3735                        else if (cf == 0)
3736                        {
3737                                geom.SplitGeometry(front,
3738                                                                   back,
3739                                                                   plane,
3740                                                                   mViewSpaceBox,
3741                                                                   eps);
3742
3743                                if (back.Valid())
3744                                {
3745                                        exporter->ExportPolygons(back.GetPolys());
3746                                }                       
3747                        }
3748                }
3749        }
3750        else
3751        {
3752                // export mesh if available
3753                // TODO: some bug here?
3754                if (1 && vc->GetMesh())
3755                {
3756                        exporter->ExportMesh(vc->GetMesh());
3757                }
3758                else
3759                {
3760                        BspNodeGeometry geom;
3761                        mBspTree->ConstructGeometry(vc, geom);
3762                        exporter->ExportPolygons(geom.GetPolys());
3763                }
3764        }
3765}
3766
3767
3768void BspViewCellsManager::CreateMesh(ViewCell *vc)
3769{
3770        // note: should previous mesh be deleted (via mesh manager?)
3771        BspNodeGeometry geom;
3772        mBspTree->ConstructGeometry(vc, geom);
3773
3774        Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
3775
3776        IncludeNodeGeomInMesh(geom, *mesh);
3777        vc->SetMesh(mesh);
3778}
3779
3780
3781void BspViewCellsManager::Finalize(ViewCell *viewCell,
3782                                                                   const bool createMesh)
3783{
3784        float area = 0;
3785        float volume = 0;
3786
3787        ViewCellContainer leaves;
3788        mViewCellsTree->CollectLeaves(viewCell, leaves);
3789
3790        ViewCellContainer::const_iterator it, it_end = leaves.end();
3791
3792    for (it = leaves.begin(); it != it_end; ++ it)
3793        {
3794                BspNodeGeometry geom;
3795
3796                mBspTree->ConstructGeometry(*it, geom);
3797
3798                const float lVol = geom.GetVolume();
3799                const float lArea = geom.GetArea();
3800
3801                area += lArea;
3802                volume += lVol;
3803       
3804                CreateMesh(*it);
3805        }
3806
3807        viewCell->SetVolume(volume);
3808        viewCell->SetArea(area);
3809}
3810
3811
3812ViewCell *BspViewCellsManager::GetViewCell(const Vector3 &point, const bool active) const
3813{
3814        if (!ViewCellsConstructed())
3815        {
3816                return NULL;
3817        }
3818        if (!mViewSpaceBox.IsInside(point))
3819        {
3820                return NULL;
3821        }
3822        return mBspTree->GetViewCell(point);
3823}
3824
3825
3826void BspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
3827                                                                                                 vector<MergeCandidate> &candidates)
3828{
3829        cout << "collecting merge candidates ... " << endl;
3830
3831        if (mUseRaysForMerge)
3832        {
3833                mBspTree->CollectMergeCandidates(rays, candidates);
3834        }
3835        else
3836        {
3837                vector<BspLeaf *> leaves;
3838                mBspTree->CollectLeaves(leaves);
3839                mBspTree->CollectMergeCandidates(leaves, candidates);
3840        }
3841
3842        cout << "fininshed collecting candidates" << endl;
3843}
3844
3845
3846
3847bool BspViewCellsManager::ExportViewCells(const string filename,
3848                                                                                  const bool exportPvs,
3849                                                                                  const ObjectContainer &objects)
3850{
3851        if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
3852        {
3853                return false;
3854        }
3855
3856        cout << "exporting view cells to xml ... ";
3857
3858        OUT_STREAM stream(filename.c_str());
3859
3860        // for output we need unique ids for each view cell
3861        CreateUniqueViewCellIds();
3862
3863        stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
3864        stream << "<VisibilitySolution>" << endl;
3865
3866        if (exportPvs)
3867        {
3868                //////////
3869                //-- export bounding boxes: they are used to identify the objects from the pvs and
3870                //-- assign them to the entities in the rendering engine
3871
3872                stream << "<BoundingBoxes>" << endl;
3873                ObjectContainer::const_iterator oit, oit_end = objects.end();
3874
3875                for (oit = objects.begin(); oit != oit_end; ++ oit)
3876                {
3877                        const AxisAlignedBox3 box = (*oit)->GetBox();
3878                       
3879                        stream << "<BoundingBox" << " id=\"" << (*oit)->GetId() << "\""
3880                                   << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
3881                                   << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
3882                }
3883
3884                stream << "</BoundingBoxes>" << endl;
3885        }
3886
3887        ///////////
3888        //-- export the view cells and the pvs
3889
3890        const int numViewCells = mCurrentViewCellsStats.viewCells;
3891        stream << "<ViewCells number=\"" << numViewCells << "\" >" << endl;
3892
3893        mViewCellsTree->Export(stream, exportPvs);
3894       
3895        stream << "</ViewCells>" << endl;
3896
3897        /////////////
3898        //-- export the view space hierarchy
3899        stream << "<ViewSpaceHierarchy type=\"bsp\""
3900                   << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
3901                   << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\">" << endl;
3902
3903        mBspTree->Export(stream);
3904
3905        // end tags
3906        stream << "</ViewSpaceHierarchy>" << endl;
3907        stream << "</VisibilitySolution>" << endl;
3908
3909        stream.close();
3910        cout << "finished" << endl;
3911
3912        return true;
3913}
3914
3915
3916ViewCell *BspViewCellsManager::ConstructDummyMergeTree(BspNode *root)
3917{
3918        ViewCellInterior *vcRoot = new ViewCellInterior();
3919               
3920        // evaluate merge cost for priority traversal
3921        const float mergeCost =  -(float)root->mTimeStamp;
3922        vcRoot->SetMergeCost(mergeCost);
3923
3924        float volume = 0;
3925        vector<BspLeaf *> leaves;
3926        mBspTree->CollectLeaves(leaves);
3927        vector<BspLeaf *>::const_iterator lit, lit_end = leaves.end();
3928        ViewCell::NewMail();
3929
3930        for (lit = leaves.begin(); lit != lit_end; ++ lit)
3931        {
3932                BspLeaf *leaf = *lit;
3933                ViewCell *vc = leaf->GetViewCell();
3934
3935                if (!vc->Mailed())
3936                {
3937                        vc->Mail();
3938                        vc->SetMergeCost(0.0f);
3939                        vcRoot->SetupChildLink(vc);
3940
3941                        volume += vc->GetVolume();
3942                        volume += vc->GetVolume();     
3943                        vcRoot->SetVolume(volume);
3944                }
3945        }
3946       
3947        return vcRoot;
3948}
3949
3950
3951ViewCell *BspViewCellsManager::ConstructSpatialMergeTree(BspNode *root)
3952{
3953        // terminate recursion
3954        if (root->IsLeaf())
3955        {
3956                BspLeaf *leaf = dynamic_cast<BspLeaf *>(root);
3957                leaf->GetViewCell()->SetMergeCost(0.0f);
3958                return leaf->GetViewCell();
3959        }
3960       
3961        BspInterior *interior = dynamic_cast<BspInterior *>(root);
3962        ViewCellInterior *viewCellInterior = new ViewCellInterior();
3963               
3964        // evaluate merge cost for priority traversal
3965        const float mergeCost = -(float)root->mTimeStamp;
3966        viewCellInterior->SetMergeCost(mergeCost);
3967
3968        float volume = 0;
3969       
3970        BspNode *front = interior->GetFront();
3971        BspNode *back = interior->GetBack();
3972
3973
3974        ////////////
3975        //-- recursivly compute child hierarchies
3976
3977        ViewCell *backVc = ConstructSpatialMergeTree(back);
3978        ViewCell *frontVc = ConstructSpatialMergeTree(front);
3979
3980        viewCellInterior->SetupChildLink(backVc);
3981        viewCellInterior->SetupChildLink(frontVc);
3982
3983        volume += backVc->GetVolume();
3984        volume += frontVc->GetVolume();
3985
3986        viewCellInterior->SetVolume(volume);
3987
3988        return viewCellInterior;
3989}
3990
3991
3992/************************************************************************/
3993/*                   KdViewCellsManager implementation                  */
3994/************************************************************************/
3995
3996
3997
3998KdViewCellsManager::KdViewCellsManager(ViewCellsTree *vcTree, KdTree *kdTree):
3999ViewCellsManager(vcTree), mKdTree(kdTree), mKdPvsDepth(100)
4000{
4001}
4002
4003
4004float KdViewCellsManager::GetProbability(ViewCell *viewCell)
4005{
4006        // compute view cell area / volume as subsititute for probability
4007        if (0)
4008                return GetArea(viewCell) / GetViewSpaceBox().SurfaceArea();
4009        else
4010                return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
4011}
4012
4013
4014
4015
4016void KdViewCellsManager::CollectViewCells()
4017{
4018        //mKdTree->CollectViewCells(mViewCells); TODO
4019}
4020
4021
4022int KdViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
4023                                                                  const VssRayContainer &rays)
4024{
4025        // if view cells already constructed
4026        if (ViewCellsConstructed())
4027                return 0;
4028
4029        mKdTree->Construct();
4030
4031        mTotalAreaValid = false;
4032        // create the view cells
4033        mKdTree->CreateAndCollectViewCells(mViewCells);
4034        // cast rays
4035        ComputeSampleContributions(rays, true, false);
4036
4037        EvaluateViewCellsStats();
4038        Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
4039
4040        return 0;
4041}
4042
4043
4044bool KdViewCellsManager::ViewCellsConstructed() const
4045{
4046        return mKdTree->GetRoot() != NULL;
4047}
4048
4049
4050int KdViewCellsManager::PostProcess(const ObjectContainer &objects,
4051                                                                        const VssRayContainer &rays)
4052{
4053        return 0;
4054}
4055
4056
4057void KdViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
4058                                                                                           const int maxViewCells,
4059                                                                                           const bool sortViewCells,
4060                                                                                           const bool exportPvs,
4061                                                                                           const bool exportRays,
4062                                                                                           const int maxRays,
4063                                                                                           const string prefix,
4064                                                                                           VssRayContainer *visRays)
4065{
4066        // TODO
4067}
4068
4069
4070void KdViewCellsManager::Visualize(const ObjectContainer &objects,
4071                                                                   const VssRayContainer &sampleRays)
4072{
4073        if (!ViewCellsConstructed())
4074                return;
4075
4076        // using view cells instead of the kd PVS of objects
4077        const bool useViewCells = true;
4078        bool exportRays = false;
4079
4080        int limit = min(mVisualizationSamples, (int)sampleRays.size());
4081        const int pvsOut = min((int)objects.size(), 10);
4082        VssRayContainer *rays = new VssRayContainer[pvsOut];
4083
4084        if (useViewCells)
4085        {
4086                const int leafOut = 10;
4087
4088                ViewCell::NewMail();
4089
4090                //-- some rays for output
4091                const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
4092                Debug << "visualization using " << raysOut << " samples" << endl;
4093
4094                //-- some random view cells and rays for output
4095                vector<KdLeaf *> kdLeaves;
4096
4097                for (int i = 0; i < leafOut; ++ i)
4098                        kdLeaves.push_back(dynamic_cast<KdLeaf *>(mKdTree->GetRandomLeaf()));
4099
4100                for (int i = 0; i < kdLeaves.size(); ++ i)
4101                {
4102                        KdLeaf *leaf = kdLeaves[i];
4103                        RayContainer vcRays;
4104
4105                        cout << "creating output for view cell " << i << " ... ";
4106#if 0
4107                        // check whether we can add the current ray to the output rays
4108                        for (int k = 0; k < raysOut; ++ k)
4109                        {
4110                                Ray *ray = sampleRays[k];
4111
4112                                for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
4113                                {
4114                                        BspLeaf *leaf2 = ray->bspIntersections[j].mLeaf;
4115
4116                                        if (leaf->GetViewCell() == leaf2->GetViewCell())
4117                                        {
4118                                                vcRays.push_back(ray);
4119                                        }
4120                                }
4121                        }
4122#endif
4123                        Intersectable::NewMail();
4124
4125                        ViewCell *vc = leaf->mViewCell;
4126                        char str[64]; sprintf(str, "viewcell%04d.wrl", i);
4127
4128                        Exporter *exporter = Exporter::GetExporter(str);
4129                        exporter->SetFilled();
4130
4131                        exporter->SetWireframe();
4132                        //exporter->SetFilled();
4133
4134                        Material m;//= RandomMaterial();
4135                        m.mDiffuseColor = RgbColor(1, 1, 0);
4136                        exporter->SetForcedMaterial(m);
4137
4138                        AxisAlignedBox3 box = mKdTree->GetBox(leaf);
4139                        exporter->ExportBox(box);
4140
4141                        // export rays piercing this view cell
4142                        exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));
4143
4144                        m.mDiffuseColor = RgbColor(1, 0, 0);
4145                        exporter->SetForcedMaterial(m);
4146
4147                        // exporter->SetWireframe();
4148                        exporter->SetFilled();
4149
4150                        ObjectPvsIterator pit = vc->GetPvs().GetIterator();
4151                       
4152                        while (pit.HasMoreEntries())
4153                        {               
4154                                ObjectPvsEntry entry = pit.Next();
4155                               
4156                                //-- output PVS of view cell
4157                                Intersectable *intersect = entry.mObject;
4158                                if (!intersect->Mailed())
4159                                {
4160                                        exporter->ExportIntersectable(intersect);
4161                                        intersect->Mail();
4162                                }
4163                        }
4164
4165                        DEL_PTR(exporter);
4166                        cout << "finished" << endl;
4167                }
4168
4169                DEL_PTR(rays);
4170        }
4171        else // using kd PVS of objects
4172        {
4173                for (int i = 0; i < limit; ++ i)
4174                {
4175                        VssRay *ray = sampleRays[i];
4176
4177                        // check whether we can add this to the rays
4178                        for (int j = 0; j < pvsOut; j++)
4179                        {
4180                                if (objects[j] == ray->mTerminationObject)
4181                                {
4182                                        rays[j].push_back(ray);
4183                                }
4184                        }
4185                }
4186
4187                if (exportRays)
4188                {
4189                        Exporter *exporter = NULL;
4190                        exporter = Exporter::GetExporter("sample-rays.x3d");
4191                        exporter->SetWireframe();
4192                        exporter->ExportKdTree(*mKdTree);
4193
4194                        for (int i = 0; i < pvsOut; i++)
4195                                exporter->ExportRays(rays[i], RgbColor(1, 0, 0));
4196
4197                        exporter->SetFilled();
4198                        delete exporter;
4199                }
4200
4201                for (int k=0; k < pvsOut; k++)
4202                {
4203                        Intersectable *object = objects[k];
4204                        char str[64]; sprintf(str, "viewcell%04d.wrl", k);
4205
4206                        Exporter *exporter = Exporter::GetExporter(str);
4207                        exporter->SetWireframe();
4208
4209                        // matt: no kd pvs
4210                        /*
4211                        KdPvsMap::iterator kit = object->mKdPvs.mEntries.begin();
4212                        Intersectable::NewMail();
4213
4214                        // avoid adding the object to the list
4215                        object->Mail();
4216                        ObjectContainer visibleObjects;
4217
4218                        for (; kit != object->mKdPvs.mEntries.end(); i++)
4219                        {
4220                                KdNode *node = (*kit).first;
4221                                exporter->ExportBox(mKdTree->GetBox(node));
4222
4223                                mKdTree->CollectObjects(node, visibleObjects);
4224                        }
4225
4226                        exporter->ExportRays(rays[k],  RgbColor(0, 1, 0));
4227                        exporter->SetFilled();
4228
4229                        for (int j = 0; j < visibleObjects.size(); j++)
4230                                exporter->ExportIntersectable(visibleObjects[j]);
4231
4232                        Material m;
4233                        m.mDiffuseColor = RgbColor(1, 0, 0);
4234                        exporter->SetForcedMaterial(m);
4235                        exporter->ExportIntersectable(object);
4236*/
4237                        delete exporter;
4238                }
4239        }
4240}
4241
4242
4243ViewCell *KdViewCellsManager::GenerateViewCell(Mesh *mesh) const
4244{
4245        return new KdViewCell(mesh);
4246}
4247
4248
4249void KdViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
4250                                                                                                ViewCell *vc,
4251                                                                                                const AxisAlignedBox3 *sceneBox,
4252                                                                                                const AxisAlignedPlane *clipPlane
4253                                                                                                ) const
4254{
4255        ViewCellContainer leaves;
4256        mViewCellsTree->CollectLeaves(vc, leaves);
4257        ViewCellContainer::const_iterator it, it_end = leaves.end();
4258
4259        for (it = leaves.begin(); it != it_end; ++ it)
4260        {
4261                KdViewCell *kdVc = dynamic_cast<KdViewCell *>(*it);
4262                exporter->ExportBox(mKdTree->GetBox(kdVc->mLeaves[0]));
4263        }
4264}
4265
4266
4267int KdViewCellsManager::GetType() const
4268{
4269        return ViewCellsManager::KD;
4270}
4271
4272
4273
4274KdNode *KdViewCellsManager::GetNodeForPvs(KdLeaf *leaf)
4275{
4276        KdNode *node = leaf;
4277
4278        while (node->mParent && node->mDepth > mKdPvsDepth)
4279                node = node->mParent;
4280
4281        return node;
4282}
4283
4284int KdViewCellsManager::CastLineSegment(const Vector3 &origin,
4285                                                                                const Vector3 &termination,
4286                                                                                ViewCellContainer &viewcells)
4287{
4288        return mKdTree->CastLineSegment(origin, termination, viewcells);
4289}
4290
4291
4292void KdViewCellsManager::CreateMesh(ViewCell *vc)
4293{
4294        // TODO
4295}
4296
4297
4298
4299void KdViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
4300                                                                                                vector<MergeCandidate> &candidates)
4301{
4302        // TODO
4303}
4304
4305
4306
4307/**************************************************************************/
4308/*                   VspBspViewCellsManager implementation                */
4309/**************************************************************************/
4310
4311
4312VspBspViewCellsManager::VspBspViewCellsManager(ViewCellsTree *vcTree, VspBspTree *vspBspTree):
4313ViewCellsManager(vcTree), mVspBspTree(vspBspTree)
4314{
4315        Environment::GetSingleton()->GetIntValue("VspBspTree.Construction.samples", mInitialSamples);
4316        mVspBspTree->SetViewCellsManager(this);
4317        mVspBspTree->mViewCellsTree = mViewCellsTree;
4318}
4319
4320
4321VspBspViewCellsManager::~VspBspViewCellsManager()
4322{
4323}
4324
4325
4326float VspBspViewCellsManager::GetProbability(ViewCell *viewCell)
4327{
4328        if (0 && mVspBspTree->mUseAreaForPvs)
4329                return GetArea(viewCell) / GetAccVcArea();
4330        else
4331                return GetVolume(viewCell) / mViewSpaceBox.GetVolume();
4332}
4333
4334
4335void VspBspViewCellsManager::CollectViewCells()
4336{
4337        // view cells tree constructed?
4338        if (!ViewCellsTreeConstructed())
4339        {
4340                mVspBspTree->CollectViewCells(mViewCells, false);
4341        }
4342        else
4343        {       
4344                // we can use the view cells tree hierarchy to get the right set
4345                mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumActiveViewCells);
4346        }
4347}
4348
4349
4350void VspBspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
4351                                                                                                        vector<MergeCandidate> &candidates)
4352{       
4353        cout << "collecting merge candidates ... " << endl;
4354
4355        if (mUseRaysForMerge)
4356        {
4357                mVspBspTree->CollectMergeCandidates(rays, candidates);
4358        }
4359        else
4360        {
4361                vector<BspLeaf *> leaves;
4362                mVspBspTree->CollectLeaves(leaves);
4363       
4364                mVspBspTree->CollectMergeCandidates(leaves, candidates);
4365        }
4366
4367        cout << "fininshed collecting candidates" << endl;
4368}
4369
4370
4371bool VspBspViewCellsManager::ViewCellsConstructed() const
4372{
4373        return mVspBspTree->GetRoot() != NULL;
4374}
4375
4376
4377ViewCell *VspBspViewCellsManager::GenerateViewCell(Mesh *mesh) const
4378{
4379        return new BspViewCell(mesh);
4380}
4381
4382
4383int VspBspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
4384                                                                                                 const VssRayContainer &rays)
4385{
4386        mMaxPvsSize = (int)(mMaxPvsRatio * (float)objects.size());
4387
4388        // if view cells were already constructed
4389        if (ViewCellsConstructed())
4390        {
4391                return 0;
4392        }
4393
4394        int sampleContributions = 0;
4395        VssRayContainer sampleRays;
4396
4397        const int limit = min(mInitialSamples, (int)rays.size());
4398
4399        Debug << "samples used for vsp bsp subdivision: " << mInitialSamples
4400                  << ", actual rays: " << (int)rays.size() << endl;
4401
4402        VssRayContainer savedRays;
4403
4404        if (SAMPLE_AFTER_SUBDIVISION)
4405        {
4406                VssRayContainer constructionRays;
4407               
4408                GetRaySets(rays, mInitialSamples, constructionRays, &savedRays);
4409
4410                Debug << "rays used for initial construction: " << (int)constructionRays.size() << endl;
4411                Debug << "rays saved for later use: " << (int)savedRays.size() << endl;
4412       
4413                mVspBspTree->Construct(constructionRays, &mViewSpaceBox);
4414        }
4415        else
4416        {
4417                Debug << "rays used for initial construction: " << (int)rays.size() << endl;
4418                mVspBspTree->Construct(rays, &mViewSpaceBox);
4419        }
4420
4421        // collapse invalid regions
4422        cout << "collapsing invalid tree regions ... ";
4423        long startTime = GetTime();
4424
4425        const int collapsedLeaves = mVspBspTree->CollapseTree();
4426        Debug << "collapsed in " << TimeDiff(startTime, GetTime()) * 1e-3
4427                  << " seconds" << endl;
4428
4429    cout << "finished" << endl;
4430
4431        /////////////////
4432        //-- stats after construction
4433
4434        Debug << mVspBspTree->GetStatistics() << endl;
4435
4436        ResetViewCells();
4437        Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
4438
4439
4440        //////////////////////
4441        //-- recast the rest of the rays
4442
4443        startTime = GetTime();
4444
4445        cout << "Computing remaining ray contributions ... ";
4446
4447        if (SAMPLE_AFTER_SUBDIVISION)
4448                ComputeSampleContributions(savedRays, true, false);
4449
4450        cout << "finished" << endl;
4451
4452        Debug << "Computed remaining ray contribution in " << TimeDiff(startTime, GetTime()) * 1e-3
4453                  << " secs" << endl;
4454
4455        cout << "construction finished" << endl;
4456
4457        if (0)
4458        {       ////////
4459                //-- real meshes are contructed at this stage
4460
4461                cout << "finalizing view cells ... ";
4462                FinalizeViewCells(true);
4463                cout << "finished" << endl;
4464        }
4465
4466        return sampleContributions;
4467}
4468
4469
4470void VspBspViewCellsManager::MergeViewCells(const VssRayContainer &rays,
4471                                                                                        const ObjectContainer &objects)
4472{
4473    int vcSize = 0;
4474        int pvsSize = 0;
4475
4476        //-- merge view cells
4477        cout << "starting merge using " << mPostProcessSamples << " samples ... " << endl;
4478        long startTime = GetTime();
4479
4480
4481        if (mMergeViewCells)
4482        {
4483                // TODO: should be done BEFORE the ray casting
4484                // compute tree by merging the nodes based on cost heuristics
4485                mViewCellsTree->ConstructMergeTree(rays, objects);
4486        }
4487        else
4488        {
4489                // compute tree by merging the nodes of the spatial hierarchy
4490                ViewCell *root = ConstructSpatialMergeTree(mVspBspTree->GetRoot());
4491                mViewCellsTree->SetRoot(root);
4492
4493                // compute pvs
4494                ObjectPvs pvs;
4495                UpdatePvsForEvaluation(root, pvs);
4496        }
4497
4498        if (1)
4499        {
4500                char mstats[100];
4501                ObjectPvs pvs;
4502
4503                Environment::GetSingleton()->GetStringValue("ViewCells.mergeStats", mstats);
4504                mViewCellsTree->ExportStats(mstats);
4505        }
4506
4507        cout << "merged view cells in "
4508                 << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
4509
4510        Debug << "Postprocessing: Merged view cells in "
4511                  << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl << endl;
4512       
4513
4514        //////////////////
4515        //-- stats and visualizations
4516
4517        int savedColorCode = mColorCode;
4518       
4519        // get currently active view cell set
4520        ResetViewCells();
4521        Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
4522       
4523        if (mShowVisualization) // export merged view cells
4524        {
4525                mColorCode = 0;
4526                Exporter *exporter = Exporter::GetExporter("merged_view_cells.wrl");
4527               
4528                cout << "exporting view cells after merge ... ";
4529
4530                if (exporter)
4531                {
4532                        if (0)
4533                                exporter->SetWireframe();
4534                        else
4535                                exporter->SetFilled();
4536
4537                        ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
4538
4539                        if (mExportGeometry)
4540                        {
4541                                Material m;
4542                                m.mDiffuseColor = RgbColor(0, 1, 0);
4543                                exporter->SetForcedMaterial(m);
4544                                exporter->SetFilled();
4545
4546                                exporter->ExportGeometry(objects);
4547                        }
4548
4549                        delete exporter;
4550                }
4551                cout << "finished" << endl;
4552        }
4553
4554        if (mShowVisualization)
4555        {
4556                // use pvs size for color coding
4557                mColorCode = 1;
4558                Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.wrl");
4559
4560                cout << "exporting view cells after merge (pvs size) ... ";     
4561
4562                if (exporter)
4563                {
4564                        exporter->SetFilled();
4565
4566                        ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
4567
4568                        if (mExportGeometry)
4569                        {
4570                                Material m;
4571                                m.mDiffuseColor = RgbColor(0, 1, 0);
4572                                exporter->SetForcedMaterial(m);
4573                                exporter->SetFilled();
4574
4575                                exporter->ExportGeometry(objects);
4576                        }
4577
4578                        delete exporter;
4579                }
4580                cout << "finished" << endl;
4581        }
4582
4583        mColorCode = savedColorCode;
4584}
4585
4586
4587void VspBspViewCellsManager::RefineViewCells(const VssRayContainer &rays,
4588                                                                                         const ObjectContainer &objects)
4589{
4590        mRenderer->RenderScene();
4591
4592        SimulationStatistics ss;
4593        dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
4594    Debug << "render time before refine\n\n" << ss << endl;
4595
4596        const long startTime = GetTime();
4597        cout << "Refining the merged view cells ... ";
4598
4599        // refining the merged view cells
4600        const int refined = mViewCellsTree->RefineViewCells(rays, objects);
4601
4602        //-- stats and visualizations
4603        cout << "finished" << endl;
4604        cout << "refined " << refined << " view cells in "
4605                 << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
4606
4607        Debug << "Postprocessing: refined " << refined << " view cells in "
4608                  << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl << endl;
4609}
4610
4611
4612int VspBspViewCellsManager::PostProcess(const ObjectContainer &objects,
4613                                                                                const VssRayContainer &rays)
4614{
4615        if (!ViewCellsConstructed())
4616        {
4617                Debug << "postprocess error: no view cells constructed" << endl;
4618                return 0;
4619        }
4620
4621        // view cells already finished before post processing step
4622        // (i.e. because they were loaded)
4623        if (mViewCellsFinished)
4624        {
4625                FinalizeViewCells(true);
4626                EvaluateViewCellsStats();
4627
4628                return 0;
4629        }
4630
4631        // check if new view cells turned invalid
4632        int minPvs, maxPvs;
4633
4634        if (0)
4635        {
4636                minPvs = mMinPvsSize;
4637                maxPvs = mMaxPvsSize;
4638        }
4639        else
4640        {
4641                // problem matt: why did I start here from zero?
4642                minPvs = 0;
4643                maxPvs = mMaxPvsSize;
4644        }
4645
4646        Debug << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
4647        cout << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
4648       
4649        SetValidity(minPvs, maxPvs);
4650
4651        // update valid view space according to valid view cells
4652        if (0) mVspBspTree->ValidateTree();
4653
4654        // area has to be recomputed
4655        mTotalAreaValid = false;
4656        VssRayContainer postProcessRays;
4657        GetRaySets(rays, mPostProcessSamples, postProcessRays);
4658
4659        Debug << "post processing using " << (int)postProcessRays.size() << " samples" << endl;
4660
4661        //////////
4662        //-- merge neighbouring view cells
4663        MergeViewCells(postProcessRays, objects);
4664       
4665        // refines the merged view cells
4666        if (0) RefineViewCells(postProcessRays, objects);
4667
4668
4669        ///////////
4670        //-- render simulation after merge + refine
4671
4672        cout << "\nview cells partition render time before compress" << endl << endl;;
4673        dynamic_cast<RenderSimulator *>(mRenderer)->RenderScene();
4674        SimulationStatistics ss;
4675        dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
4676        cout << ss << endl;
4677       
4678        if (0) CompressViewCells();
4679       
4680        // collapse sibling leaves that share the same view cell
4681        if (0) mVspBspTree->CollapseTree();
4682
4683        // recompute view cell list and statistics
4684        ResetViewCells();
4685
4686        // compute final meshes and volume / area
4687        if (1) FinalizeViewCells(true);
4688
4689        return 0;
4690}
4691
4692
4693int VspBspViewCellsManager::GetType() const
4694{
4695        return VSP_BSP;
4696}
4697
4698
4699ViewCell *VspBspViewCellsManager::ConstructSpatialMergeTree(BspNode *root)
4700{
4701        // terminate recursion
4702        if (root->IsLeaf())
4703        {
4704                BspLeaf *leaf = dynamic_cast<BspLeaf *>(root);
4705                leaf->GetViewCell()->SetMergeCost(0.0f);
4706                return leaf->GetViewCell();
4707        }
4708       
4709       
4710        BspInterior *interior = dynamic_cast<BspInterior *>(root);
4711        ViewCellInterior *viewCellInterior = new ViewCellInterior();
4712               
4713        // evaluate merge cost for priority traversal
4714        float mergeCost = 1.0f / (float)root->mTimeStamp;
4715        viewCellInterior->SetMergeCost(mergeCost);
4716
4717        float volume = 0;
4718       
4719        BspNode *front = interior->GetFront();
4720        BspNode *back = interior->GetBack();
4721
4722
4723        ObjectPvs frontPvs, backPvs;
4724
4725        //-- recursivly compute child hierarchies
4726        ViewCell *backVc = ConstructSpatialMergeTree(back);
4727        ViewCell *frontVc = ConstructSpatialMergeTree(front);
4728
4729
4730        viewCellInterior->SetupChildLink(backVc);
4731        viewCellInterior->SetupChildLink(frontVc);
4732
4733        volume += backVc->GetVolume();
4734        volume += frontVc->GetVolume();
4735
4736        viewCellInterior->SetVolume(volume);
4737
4738        return viewCellInterior;
4739}
4740
4741
4742bool VspBspViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
4743{
4744        if (!ViewCellsConstructed())
4745                return ViewCellsManager::GetViewPoint(viewPoint);
4746
4747        // TODO: set reasonable limit
4748        const int limit = 20;
4749
4750        for (int i = 0; i < limit; ++ i)
4751        {
4752                viewPoint = mViewSpaceBox.GetRandomPoint();
4753                if (mVspBspTree->ViewPointValid(viewPoint))
4754                {
4755                        return true;
4756                }
4757        }
4758
4759        Debug << "failed to find valid view point, taking " << viewPoint << endl;
4760        return false;
4761}
4762
4763
4764bool VspBspViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
4765{
4766        // $$JB -> implemented in viewcellsmanager (slower, but allows dynamic
4767        // validy update in preprocessor for all managers)
4768        return ViewCellsManager::ViewPointValid(viewPoint);
4769
4770        //      return mViewSpaceBox.IsInside(viewPoint) &&
4771        //                 mVspBspTree->ViewPointValid(viewPoint);
4772}
4773
4774
4775void VspBspViewCellsManager::Visualize(const ObjectContainer &objects,
4776                                                                           const VssRayContainer &sampleRays)
4777{
4778        if (!ViewCellsConstructed())
4779                return;
4780
4781        VssRayContainer visRays;
4782        GetRaySets(sampleRays, mVisualizationSamples, visRays);
4783       
4784        if (1)
4785        {       
4786                //////////////////
4787                //-- export final view cell partition
4788
4789                Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
4790               
4791                if (exporter)
4792                {
4793                        cout << "exporting view cells after post process ... ";
4794                        if (0)
4795                        {       // export view space box
4796                                exporter->SetWireframe();
4797                                exporter->ExportBox(mViewSpaceBox);
4798                                exporter->SetFilled();
4799                        }
4800
4801                        Material m;
4802                        m.mDiffuseColor.r = 0.0f;
4803                        m.mDiffuseColor.g = 0.5f;
4804                        m.mDiffuseColor.b = 0.5f;
4805
4806            exporter->SetForcedMaterial(m);
4807
4808                        if (1 && mExportGeometry)
4809                        {
4810                                exporter->ExportGeometry(objects);
4811                        }
4812
4813                        if (0 && mExportRays)
4814                        {
4815                                exporter->ExportRays(visRays, RgbColor(1, 0, 0));
4816                        }
4817                        ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
4818
4819                        delete exporter;
4820                        cout << "finished" << endl;
4821                }
4822        }
4823
4824        ////////////////
4825        //-- visualization of the BSP splits
4826
4827        bool exportSplits = false;
4828        Environment::GetSingleton()->GetBoolValue("VspBspTree.Visualization.exportSplits", exportSplits);
4829
4830        if (exportSplits)
4831        {
4832                cout << "exporting splits ... ";
4833                ExportSplits(objects, visRays);
4834                cout << "finished" << endl;
4835        }
4836
4837        ////////
4838        //-- export single view cells
4839       
4840        int leafOut;
4841        Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
4842        const int raysOut = 100;
4843       
4844        ExportSingleViewCells(objects, leafOut, false, true, false, raysOut, "");
4845}
4846
4847
4848void VspBspViewCellsManager::ExportSplits(const ObjectContainer &objects,
4849                                                                                  const VssRayContainer &rays)
4850{
4851        Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
4852
4853        if (exporter)
4854        {
4855                Material m;
4856                m.mDiffuseColor = RgbColor(1, 0, 0);
4857                exporter->SetForcedMaterial(m);
4858                exporter->SetWireframe();
4859
4860                exporter->ExportBspSplits(*mVspBspTree, true);
4861
4862                // take forced material, else big scenes cannot be viewed
4863                m.mDiffuseColor = RgbColor(0, 1, 0);
4864                exporter->SetForcedMaterial(m);
4865                exporter->SetFilled();
4866
4867                exporter->ResetForcedMaterial();
4868
4869                // export rays
4870                if (mExportRays)
4871                {
4872                        exporter->ExportRays(rays, RgbColor(1, 1, 0));
4873                }
4874
4875                if (mExportGeometry)
4876                {
4877                        exporter->ExportGeometry(objects);
4878                }
4879                delete exporter;
4880        }
4881}
4882
4883
4884void VspBspViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
4885                                                                                                   const int maxViewCells,
4886                                                                                                   const bool sortViewCells,
4887                                                                                                   const bool exportPvs,
4888                                                                                                   const bool exportRays,
4889                                                                                                   const int maxRays,
4890                                                                                                   const string prefix,
4891                                                                                                   VssRayContainer *visRays)
4892{       
4893        if (sortViewCells)
4894        {
4895                // sort view cells to visualize the largest view cells
4896                sort(mViewCells.begin(), mViewCells.end(), ViewCell::LargerRenderCost);
4897        }
4898
4899        //////////
4900        //-- some view cells for output
4901
4902        ViewCell::NewMail();
4903        const int limit = min(maxViewCells, (int)mViewCells.size());
4904       
4905        for (int i = 0; i < limit; ++ i)
4906        {
4907                cout << "creating output for view cell " << i << " ... ";
4908
4909                ViewCell *vc = sortViewCells ? // largest view cell pvs first?
4910                        mViewCells[(int)RandomValue(0, (float)mViewCells.size() - 0.5f)] : mViewCells[i];
4911
4912                if (vc->Mailed() || vc->GetId() == OUT_OF_BOUNDS_ID)
4913                        continue;
4914
4915                vc->Mail();
4916
4917                ObjectPvs pvs;
4918                mViewCellsTree->GetPvs(vc, pvs);
4919
4920                char s[64]; sprintf(s, "%sviewcell%04d.wrl", prefix.c_str(), i);
4921                Exporter *exporter = Exporter::GetExporter(s);
4922               
4923                const float pvsCost = mViewCellsTree->GetPvsCost(vc);
4924                cout << "view cell " << vc->GetId() << ": pvs cost=" << pvsCost << endl;
4925
4926                if (exportRays)
4927                {
4928                        ////////////
4929                        //-- export rays piercing this view cell
4930
4931                        // take rays stored with the view cells during subdivision
4932                        VssRayContainer vcRays;
4933            VssRayContainer collectRays;
4934
4935                        // collect initial view cells
4936                        ViewCellContainer leaves;
4937                        mViewCellsTree->CollectLeaves(vc, leaves);
4938
4939                        ViewCellContainer::const_iterator vit, vit_end = leaves.end();
4940                for (vit = leaves.begin(); vit != vit_end; ++ vit)
4941                        {       
4942                                BspLeaf *vcLeaf = dynamic_cast<BspViewCell *>(*vit)->mLeaves[0];
4943                                VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
4944
4945                                for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
4946                                {
4947                                        collectRays.push_back(*rit);
4948                                }
4949                        }
4950
4951                        const int raysOut = min((int)collectRays.size(), maxRays);
4952               
4953                        // prepare some rays for output
4954                        VssRayContainer::const_iterator rit, rit_end = collectRays.end();
4955                        for (rit = collectRays.begin(); rit != rit_end; ++ rit)
4956                        {
4957                                const float p = RandomValue(0.0f, (float)collectRays.size());
4958                       
4959                                if (p < raysOut)
4960                                {
4961                                        vcRays.push_back(*rit);
4962                                }
4963                        }
4964
4965                        exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
4966                }
4967               
4968                ////////////////
4969                //-- export view cell geometry
4970
4971                exporter->SetWireframe();
4972
4973                Material m;//= RandomMaterial();
4974                m.mDiffuseColor = RgbColor(0, 1, 0);
4975                exporter->SetForcedMaterial(m);
4976
4977                ExportViewCellGeometry(exporter, vc, NULL, NULL);
4978                exporter->SetFilled();
4979
4980                if (exportPvs)
4981                {
4982                        Intersectable::NewMail();
4983
4984                        ObjectPvsIterator pit = pvs.GetIterator();
4985
4986                        cout << endl;
4987
4988                        // output PVS of view cell
4989                        while (pit.HasMoreEntries())
4990                        {
4991                                ObjectPvsEntry entry = pit.Next();             
4992                                Intersectable *intersect = entry.mObject;
4993                               
4994                                if (!intersect->Mailed())
4995                                {
4996                                        intersect->Mail();
4997
4998                                        m = RandomMaterial();
4999                                        exporter->SetForcedMaterial(m);
5000                                        exporter->ExportIntersectable(intersect);
5001                                }
5002                        }
5003                        cout << endl;
5004                }
5005               
5006                DEL_PTR(exporter);
5007                cout << "finished" << endl;
5008        }
5009}
5010
5011
5012void VspBspViewCellsManager::TestFilter(const ObjectContainer &objects)
5013{
5014        Exporter *exporter = Exporter::GetExporter("filter.x3d");
5015
5016        Vector3 bsize = mViewSpaceBox.Size();
5017        const Vector3 viewPoint(mViewSpaceBox.Center());
5018        float w = Magnitude(mViewSpaceBox.Size()) * mFilterWidth;
5019        const Vector3 width = Vector3(w);
5020       
5021        PrVs testPrVs;
5022       
5023        if (exporter)
5024        {
5025                ViewCellContainer viewCells;
5026       
5027        const AxisAlignedBox3 tbox = GetFilterBBox(viewPoint, mFilterWidth);
5028
5029                GetPrVS(viewPoint, testPrVs, GetFilterWidth());
5030
5031                exporter->SetWireframe();
5032
5033                exporter->SetForcedMaterial(RgbColor(1,1,1));
5034                exporter->ExportBox(tbox);
5035               
5036                exporter->SetFilled();
5037
5038                exporter->SetForcedMaterial(RgbColor(0,1,0));
5039                ExportViewCellGeometry(exporter, GetViewCell(viewPoint), NULL, NULL);
5040
5041                //exporter->ResetForcedMaterial();
5042                exporter->SetForcedMaterial(RgbColor(0,0,1));
5043                ExportViewCellGeometry(exporter, testPrVs.mViewCell, NULL, NULL);
5044
5045        exporter->SetForcedMaterial(RgbColor(1,0,0));
5046                exporter->ExportGeometry(objects);
5047
5048                delete exporter;
5049        }
5050}
5051
5052
5053int VspBspViewCellsManager::ComputeBoxIntersections(const AxisAlignedBox3 &box,
5054                                                                                                        ViewCellContainer &viewCells) const
5055{
5056        return mVspBspTree->ComputeBoxIntersections(box, viewCells);
5057}
5058
5059
5060int VspBspViewCellsManager::CastLineSegment(const Vector3 &origin,
5061                                                                                        const Vector3 &termination,
5062                                                                                        ViewCellContainer &viewcells)
5063{
5064        return mVspBspTree->CastLineSegment(origin, termination, viewcells);
5065}
5066
5067
5068void VspBspViewCellsManager::VisualizeWithFromPointQueries()
5069{
5070        int numSamples;
5071       
5072        Environment::GetSingleton()->GetIntValue("RenderSampler.samples", numSamples);
5073        cout << "samples" << numSamples << endl;
5074
5075        vector<RenderCostSample> samples;
5076 
5077        if (!mPreprocessor->GetRenderer())
5078                return;
5079
5080        //start the view point queries
5081        long startTime = GetTime();
5082        cout << "starting sampling of render cost ... ";
5083       
5084        mPreprocessor->GetRenderer()->SampleRenderCost(numSamples, samples, true);
5085
5086        cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
5087
5088
5089        // for each sample:
5090        //    find view cells associated with the samples
5091        //    store the sample pvs with the pvs associated with the view cell
5092        //
5093        // for each view cell:
5094        //    compute difference point sampled pvs - view cell pvs
5095        //    export geometry with color coded pvs difference
5096       
5097    std::map<ViewCell *, ObjectPvs> sampleMap;
5098
5099        vector<RenderCostSample>::const_iterator rit, rit_end = samples.end();
5100
5101        for (rit = samples.begin(); rit != rit_end; ++ rit)
5102        {
5103                RenderCostSample sample = *rit;
5104       
5105                ViewCell *vc = GetViewCell(sample.mPosition);
5106
5107                std::map<ViewCell *, ObjectPvs>::iterator it = sampleMap.find(vc);
5108
5109                if (it == sampleMap.end())
5110                {
5111                        sampleMap[vc] = sample.mPvs;
5112                }
5113                else
5114                {
5115                        (*it).second.MergeInPlace(sample.mPvs);
5116                }
5117        }
5118
5119        // visualize the view cells
5120        std::map<ViewCell *, ObjectPvs>::const_iterator vit, vit_end = sampleMap.end();
5121
5122        Material m;//= RandomMaterial();
5123
5124        for (vit = sampleMap.begin(); vit != vit_end; ++ vit)
5125        {
5126                ViewCell *vc = (*vit).first;
5127               
5128                const int pvsVc = mViewCellsTree->GetPvsEntries(vc);
5129                const int pvsPtSamples = (*vit).second.GetSize();
5130
5131        m.mDiffuseColor.r = (float) (pvsVc - pvsPtSamples);
5132                m.mDiffuseColor.b = 1.0f;
5133                //exporter->SetForcedMaterial(m);
5134                //ExportViewCellGeometry(exporter, vc, mClipPlaneForViz);
5135
5136                /*      // counting the pvss
5137                for (rit = samples.begin(); rit != rit_end; ++ rit)
5138                {
5139                        RenderCostSample sample = *rit;
5140                        ViewCell *vc = GetViewCell(sample.mPosition);
5141
5142                        AxisAlignedBox3 box(sample.mPosition - Vector3(1, 1, 1), sample.mPosition + Vector3(1, 1, 1));
5143                        Mesh *hMesh = CreateMeshFromBox(box);
5144
5145                        DEL_PTR(hMesh);
5146                }
5147                */
5148        }
5149}
5150
5151
5152void VspBspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
5153                                                                                                        ViewCell *vc,
5154                                                                                                        const AxisAlignedBox3 *sceneBox,
5155                                                                                                        const AxisAlignedPlane *clipPlane
5156                                                                                                        ) const
5157{
5158        if (clipPlane)
5159        {
5160                const Plane3 plane = clipPlane->GetPlane();
5161
5162                ViewCellContainer leaves;
5163                mViewCellsTree->CollectLeaves(vc, leaves);
5164                ViewCellContainer::const_iterator it, it_end = leaves.end();
5165
5166                for (it = leaves.begin(); it != it_end; ++ it)
5167                {
5168                        BspNodeGeometry geom;
5169                        BspNodeGeometry front;
5170                        BspNodeGeometry back;
5171
5172                        mVspBspTree->ConstructGeometry(*it, geom);
5173
5174                        const float eps = 0.0001f;
5175                        const int cf = geom.Side(plane, eps);
5176
5177                        if (cf == -1)
5178                        {
5179                                exporter->ExportPolygons(geom.GetPolys());
5180                        }
5181                        else if (cf == 0)
5182                        {
5183                                geom.SplitGeometry(front,
5184                                                                   back,
5185                                                                   plane,
5186                                                                   mViewSpaceBox,
5187                                                                   eps);
5188
5189                                if (back.Valid())
5190                                {
5191                                        exporter->ExportPolygons(back.GetPolys());
5192                                }                       
5193                        }
5194                }
5195        }
5196        else
5197        {
5198                // export mesh if available
5199                // TODO: some bug here?
5200                if (1 && vc->GetMesh())
5201                {
5202                        exporter->ExportMesh(vc->GetMesh());
5203                }
5204                else
5205                {
5206                        BspNodeGeometry geom;
5207                        mVspBspTree->ConstructGeometry(vc, geom);
5208                        exporter->ExportPolygons(geom.GetPolys());
5209                }
5210        }
5211}
5212
5213
5214int VspBspViewCellsManager::GetMaxTreeDiff(ViewCell *vc) const
5215{
5216        ViewCellContainer leaves;
5217        mViewCellsTree->CollectLeaves(vc, leaves);
5218
5219        int maxDist = 0;
5220       
5221        // compute max height difference
5222        for (int i = 0; i < (int)leaves.size(); ++ i)
5223        {
5224                for (int j = 0; j < (int)leaves.size(); ++ j)
5225                {
5226                        BspLeaf *leaf = dynamic_cast<BspViewCell *>(leaves[i])->mLeaves[0];
5227
5228                        if (i != j)
5229                        {
5230                                BspLeaf *leaf2 =dynamic_cast<BspViewCell *>(leaves[j])->mLeaves[0];
5231                                const int dist = mVspBspTree->TreeDistance(leaf, leaf2);
5232                               
5233                                if (dist > maxDist)
5234                                        maxDist = dist;
5235                        }
5236                }
5237        }
5238
5239        return maxDist;
5240}
5241
5242
5243ViewCell *VspBspViewCellsManager::GetViewCell(const Vector3 &point, const bool active) const
5244{
5245        if (!ViewCellsConstructed())
5246                return NULL;
5247
5248        if (!mViewSpaceBox.IsInside(point))
5249          return NULL;
5250
5251        return mVspBspTree->GetViewCell(point, active);
5252}
5253
5254
5255void VspBspViewCellsManager::CreateMesh(ViewCell *vc)
5256{
5257        BspNodeGeometry geom;
5258        mVspBspTree->ConstructGeometry(vc, geom);
5259       
5260        Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
5261        IncludeNodeGeomInMesh(geom, *mesh);
5262
5263        vc->SetMesh(mesh);
5264}
5265
5266
5267int VspBspViewCellsManager::CastBeam(Beam &beam)
5268{
5269        return mVspBspTree->CastBeam(beam);
5270}
5271
5272
5273void VspBspViewCellsManager::Finalize(ViewCell *viewCell,
5274                                                                          const bool createMesh)
5275{
5276        float area = 0;
5277        float volume = 0;
5278
5279        ViewCellContainer leaves;
5280        mViewCellsTree->CollectLeaves(viewCell, leaves);
5281
5282        ViewCellContainer::const_iterator it, it_end = leaves.end();
5283
5284    for (it = leaves.begin(); it != it_end; ++ it)
5285        {
5286                BspNodeGeometry geom;
5287                mVspBspTree->ConstructGeometry(*it, geom);
5288
5289                const float lVol = geom.GetVolume();
5290                const float lArea = geom.GetArea();
5291
5292                area += lArea;
5293                volume += lVol;
5294
5295                if (createMesh)
5296                        CreateMesh(*it);
5297        }
5298
5299        viewCell->SetVolume(volume);
5300        viewCell->SetArea(area);
5301}
5302
5303
5304void VspBspViewCellsManager::TestSubdivision()
5305{
5306        ViewCellContainer leaves;
5307        mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
5308
5309        ViewCellContainer::const_iterator it, it_end = leaves.end();
5310
5311        const float vol = mViewSpaceBox.GetVolume();
5312        float subdivVol = 0;
5313        float newVol = 0;
5314
5315        for (it = leaves.begin(); it != it_end; ++ it)
5316        {
5317                BspNodeGeometry geom;
5318                mVspBspTree->ConstructGeometry(*it, geom);
5319
5320                const float lVol = geom.GetVolume();
5321               
5322                newVol += lVol;
5323                subdivVol += (*it)->GetVolume();
5324               
5325                float thres = 0.9f;
5326                if ((lVol < ((*it)->GetVolume() * thres)) || (lVol * thres > ((*it)->GetVolume())))
5327                        Debug << "warning: " << lVol << " " << (*it)->GetVolume() << endl;
5328        }
5329       
5330        Debug << "exact volume: " << vol << endl;
5331        Debug << "subdivision volume: " << subdivVol << endl;
5332        Debug << "new volume: " << newVol << endl;
5333}
5334
5335
5336void VspBspViewCellsManager::PrepareLoadedViewCells()
5337{
5338        // TODO: do I still need this here?
5339        if (0)
5340                mVspBspTree->RepairViewCellsLeafLists();
5341}
5342
5343
5344
5345/************************************************************************/
5346/*                 VspOspViewCellsManager implementation                */
5347/************************************************************************/
5348
5349
5350VspOspViewCellsManager::VspOspViewCellsManager(ViewCellsTree *vcTree,
5351                                                                                           const string &hierarchyType)
5352: ViewCellsManager(vcTree)
5353{
5354        Environment::GetSingleton()->GetIntValue("Hierarchy.Construction.samples", mInitialSamples);
5355
5356        Environment::GetSingleton()->GetBoolValue("ViewCells.compressObjects", mCompressObjects);
5357
5358        Debug << "compressing objects: " << mCompressObjects << endl;
5359        cout << "compressing objects: " << mCompressObjects << endl;
5360
5361        mHierarchyManager = CreateHierarchyManager(hierarchyType);
5362        mHierarchyManager->SetViewCellsManager(this);
5363        mHierarchyManager->SetViewCellsTree(mViewCellsTree);
5364}
5365
5366
5367VspOspViewCellsManager::VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm)
5368: ViewCellsManager(vcTree), mHierarchyManager(hm)
5369{
5370        Environment::GetSingleton()->GetIntValue("Hierarchy.Construction.samples", mInitialSamples);
5371        Environment::GetSingleton()->GetBoolValue("ViewCells.compressObjects", mCompressObjects);
5372
5373        Debug << "compressing objects: " << mCompressObjects << endl;
5374        cout << "compressing objects: " << mCompressObjects << endl;
5375
5376        mHierarchyManager->SetViewCellsManager(this);
5377        mHierarchyManager->SetViewCellsTree(mViewCellsTree);
5378}
5379
5380
5381Intersectable *
5382VspOspViewCellsManager::GetIntersectable(const VssRay &ray, const bool isTermination) const
5383{
5384        if (mUseKdPvs)
5385        {
5386                return ViewCellsManager::GetIntersectable(ray, isTermination);
5387        }
5388        else
5389        {
5390                return mHierarchyManager->GetIntersectable(ray, isTermination);
5391        }
5392}
5393
5394
5395HierarchyManager *VspOspViewCellsManager::CreateHierarchyManager(const string &hierarchyType)
5396{
5397        HierarchyManager *hierarchyManager;
5398
5399        if (strcmp(hierarchyType.c_str(), "osp") == 0)
5400        {
5401                Debug << "hierarchy manager: osp" << endl;
5402                hierarchyManager = new HierarchyManager(HierarchyManager::KD_BASED_OBJ_SUBDIV);
5403        }
5404        else if (strcmp(hierarchyType.c_str(), "bvh") == 0)
5405        {
5406                Debug << "hierarchy manager: bvh" << endl;
5407                hierarchyManager = new HierarchyManager(HierarchyManager::BV_BASED_OBJ_SUBDIV);
5408        }
5409        else // only view space partition
5410        {
5411                Debug << "hierarchy manager: obj" << endl;
5412                hierarchyManager = new HierarchyManager(HierarchyManager::NO_OBJ_SUBDIV);
5413        }
5414
5415        return hierarchyManager;
5416}
5417
5418
5419VspOspViewCellsManager::~VspOspViewCellsManager()
5420{
5421        DEL_PTR(mHierarchyManager);
5422}
5423
5424
5425float VspOspViewCellsManager::GetProbability(ViewCell *viewCell)
5426{
5427        return GetVolume(viewCell) / mViewSpaceBox.GetVolume();
5428}
5429
5430
5431void VspOspViewCellsManager::CollectViewCells()
5432{
5433        // view cells tree constructed
5434        if (!ViewCellsTreeConstructed())
5435        {
5436                mHierarchyManager->GetVspTree()->CollectViewCells(mViewCells, false);
5437        }
5438        else
5439        {       // we can use the view cells tree hierarchy to get the right set
5440                mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumActiveViewCells);
5441        }
5442}
5443
5444
5445bool VspOspViewCellsManager::ViewCellsConstructed() const
5446{
5447        return mHierarchyManager->GetVspTree()->GetRoot() != NULL;
5448}
5449
5450
5451ViewCell *VspOspViewCellsManager::GenerateViewCell(Mesh *mesh) const
5452{
5453        return new VspViewCell(mesh);
5454}
5455
5456
5457int VspOspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
5458                                                                                                 const VssRayContainer &rays)
5459{
5460        mMaxPvsSize = (int)(mMaxPvsRatio * (float)objects.size());
5461
5462        // skip rest if view cells were already constructed
5463        if (ViewCellsConstructed())
5464                return 0;
5465
5466        int sampleContributions = 0;
5467        VssRayContainer sampleRays;
5468
5469        int limit = min (mInitialSamples, (int)rays.size());
5470
5471        VssRayContainer constructionRays;
5472        VssRayContainer savedRays;
5473
5474        Debug << "samples used for vsp bsp subdivision: " << mInitialSamples
5475                  << ", actual rays: " << (int)rays.size() << endl;
5476
5477        GetRaySets(rays, mInitialSamples, constructionRays, &savedRays);
5478
5479        Debug << "initial rays used for construction: " << (int)constructionRays.size() << endl;
5480        Debug << "saved rays: " << (int)savedRays.size() << endl;
5481
5482        mHierarchyManager->Construct(constructionRays, objects, &mViewSpaceBox);
5483
5484#if TEST_EVALUATION
5485        VssRayContainer::const_iterator tit, tit_end = constructionRays.end();
5486        for (tit = constructionRays.begin(); tit != tit_end; ++ tit)
5487        {
5488                storedRays.push_back(new VssRay(*(*tit)));
5489        }
5490#endif
5491
5492        /////////////////////////
5493        //-- print satistics for subdivision and view cells
5494
5495        Debug << endl << endl << *mHierarchyManager << endl;
5496
5497        ResetViewCells();
5498        Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
5499
5500        //////////////
5501        //-- recast rest of rays
5502       
5503        const long startTime = GetTime();
5504        cout << "Computing remaining ray contributions ... ";
5505
5506        if (SAMPLE_AFTER_SUBDIVISION)
5507                ComputeSampleContributions(savedRays, true, false);
5508
5509        Debug << "finished computing remaining ray contribution in " << TimeDiff(startTime, GetTime()) * 1e-3
5510                  << " secs" << endl;
5511
5512        if (0)
5513        {       
5514                // real meshes are constructed at this stage
5515                cout << "finalizing view cells ... ";
5516        FinalizeViewCells(true);
5517                cout << "finished" << endl;
5518        }
5519
5520        return sampleContributions;
5521}
5522
5523
5524int VspOspViewCellsManager::PostProcess(const ObjectContainer &objects,
5525                                                                                const VssRayContainer &rays)
5526{
5527        if (!ViewCellsConstructed())
5528        {
5529                Debug << "post process error: no view cells constructed" << endl;
5530                return 0;
5531        }
5532
5533        // if view cells were already constructed before post processing step
5534        // (e.g., because they were loaded), we are finished
5535        if (mViewCellsFinished)
5536        {
5537                FinalizeViewCells(true);
5538                EvaluateViewCellsStats();
5539
5540                return 0;
5541        }
5542
5543        // check if new view cells turned invalid
5544        int minPvs, maxPvs;
5545
5546        if (0)
5547        {
5548                minPvs = mMinPvsSize;
5549                maxPvs = mMaxPvsSize;
5550        }
5551        else
5552        {
5553                // problem matt: why did I start here from zero?
5554                minPvs = 0;
5555                maxPvs = mMaxPvsSize;
5556        }
5557
5558        Debug << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
5559        cout << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
5560       
5561        SetValidity(minPvs, maxPvs);
5562
5563       
5564        // area is not up to date, has to be recomputed
5565        mTotalAreaValid = false;
5566        VssRayContainer postProcessRays;
5567        GetRaySets(rays, mPostProcessSamples, postProcessRays);
5568
5569        Debug << "post processing using " << (int)postProcessRays.size() << " samples" << endl;
5570
5571
5572        // compute tree by merging the nodes of the spatial hierarchy
5573        ViewCell *root = ConstructSpatialMergeTree(mHierarchyManager->GetVspTree()->GetRoot());
5574        mViewCellsTree->SetRoot(root);
5575
5576        //////////////////////////
5577        //-- update pvs up to the root of the hierarchy
5578
5579        ObjectPvs pvs;
5580        UpdatePvsForEvaluation(root, pvs);
5581
5582
5583        //////////////////////
5584        //-- render simulation after merge + refine
5585
5586        cout << "\nview cells partition render time before compress" << endl << endl;
5587        dynamic_cast<RenderSimulator *>(mRenderer)->RenderScene();
5588        SimulationStatistics ss;
5589        dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
5590        cout << ss << endl;
5591       
5592
5593        mHierarchyManager->CreateUniqueObjectIds();
5594
5595        ///////////
5596        //-- compression
5597
5598        if (0) CompressViewCells();
5599
5600        /////////////
5601        //-- some tasks still to do on the view cells:
5602        //-- Compute meshes from view cell geometry, evaluate volume and / or area
5603
5604        if (1) FinalizeViewCells(true);
5605
5606        return 0;
5607}
5608
5609
5610int VspOspViewCellsManager::GetType() const
5611{
5612        return VSP_OSP;
5613}
5614
5615
5616ViewCell *VspOspViewCellsManager::ConstructSpatialMergeTree(VspNode *root)
5617{
5618        // terminate recursion
5619        if (root->IsLeaf())
5620        {
5621                VspLeaf *leaf = dynamic_cast<VspLeaf *>(root);
5622                leaf->GetViewCell()->SetMergeCost(0.0f);
5623                return leaf->GetViewCell();
5624        }
5625       
5626        VspInterior *interior = dynamic_cast<VspInterior *>(root);
5627        ViewCellInterior *viewCellInterior = new ViewCellInterior();
5628               
5629        // evaluate merge cost for priority traversal
5630        const float mergeCost = -(float)root->mTimeStamp;
5631        viewCellInterior->SetMergeCost(mergeCost);
5632
5633        float volume = 0;
5634       
5635        VspNode *front = interior->GetFront();
5636        VspNode *back = interior->GetBack();
5637
5638        ObjectPvs frontPvs, backPvs;
5639
5640        /////////
5641        //-- recursivly compute child hierarchies
5642
5643        ViewCell *backVc = ConstructSpatialMergeTree(back);
5644        ViewCell *frontVc = ConstructSpatialMergeTree(front);
5645
5646        viewCellInterior->SetupChildLink(backVc);
5647        viewCellInterior->SetupChildLink(frontVc);
5648
5649        volume += backVc->GetVolume();
5650        volume += frontVc->GetVolume();
5651
5652        viewCellInterior->SetVolume(volume);
5653
5654        return viewCellInterior;
5655}
5656
5657
5658bool VspOspViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
5659{
5660        if (!ViewCellsConstructed())
5661                return ViewCellsManager::GetViewPoint(viewPoint);
5662
5663        // TODO: set reasonable limit
5664        const int limit = 20;
5665
5666        for (int i = 0; i < limit; ++ i)
5667        {
5668                viewPoint = mViewSpaceBox.GetRandomPoint();
5669
5670                if (mHierarchyManager->GetVspTree()->ViewPointValid(viewPoint))
5671                {
5672                        return true;
5673                }
5674        }
5675
5676        Debug << "failed to find valid view point, taking " << viewPoint << endl;
5677        return false;
5678}
5679
5680
5681void VspOspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
5682                                                                                                        ViewCell *vc,
5683                                                                                                        const AxisAlignedBox3 *sceneBox,
5684                                                                                                        const AxisAlignedPlane *clipPlane
5685                                                                                                        ) const
5686{
5687        ViewCellContainer leaves;
5688        mViewCellsTree->CollectLeaves(vc, leaves);
5689        ViewCellContainer::const_iterator it, it_end = leaves.end();
5690
5691        Plane3 plane;
5692        if (clipPlane)
5693        {
5694                // arbitrary plane definition
5695                plane = clipPlane->GetPlane();
5696        }
5697
5698        for (it = leaves.begin(); it != it_end; ++ it)
5699        {
5700                VspViewCell *vspVc = dynamic_cast<VspViewCell *>(*it);
5701                VspLeaf *l = vspVc->mLeaves[0];
5702
5703                const AxisAlignedBox3 box =
5704                        mHierarchyManager->GetVspTree()->GetBoundingBox(vspVc->mLeaves[0]);
5705               
5706                if (sceneBox && !Overlap(*sceneBox, box))
5707                        continue;
5708
5709                if (clipPlane)
5710                {
5711                        if (box.Side(plane) == -1)
5712                        {
5713                                exporter->ExportBox(box);
5714                        }
5715                        else if (box.Side(plane) == 0)
5716                        {
5717                                // intersection
5718                                AxisAlignedBox3 fbox, bbox;
5719                                box.Split(clipPlane->mAxis, clipPlane->mPosition, fbox, bbox);
5720                                exporter->ExportBox(bbox);
5721                        }
5722                }
5723                else
5724                {
5725                        exporter->ExportBox(box);
5726                }
5727        }
5728}
5729
5730
5731bool VspOspViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
5732{
5733  // $$JB -> implemented in viewcellsmanager (slower, but allows dynamic
5734  // validy update in preprocessor for all managers)
5735  return ViewCellsManager::ViewPointValid(viewPoint);
5736
5737  //    return mViewSpaceBox.IsInside(viewPoint) &&
5738  //               mVspTree->ViewPointValid(viewPoint);
5739}
5740
5741
5742float VspOspViewCellsManager::UpdateObjectCosts()
5743{
5744        float maxRenderCost = 0;
5745
5746        cout << "updating object pvs cost ... ";
5747        const long startTime = GetTime();
5748
5749        ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
5750
5751        Intersectable::NewMail();
5752
5753        const float invViewSpaceVol = 1.0f / GetViewSpaceBox().GetVolume();
5754
5755        for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
5756        {
5757                ViewCell *vc = *vit;
5758
5759                ObjectPvsIterator pit = vc->GetPvs().GetIterator();
5760
5761                // output PVS of view cell
5762                while (pit.HasMoreEntries())
5763                {               
5764                        ObjectPvsEntry entry = pit.Next();
5765                               
5766                        BvhNode *node = dynamic_cast<BvhNode *>(entry.mObject);
5767                       
5768                        // hack!!
5769                        if (!node->IsLeaf())
5770                        {
5771                                cout << "error, can only do leaves" << endl;
5772                                return 0;
5773                        }
5774       
5775                        if (!node->Mailed())
5776                        {
5777                                node->Mail();
5778                                node->mRenderCost = 0;
5779                        }
5780
5781                        const float rc = (float)((BvhLeaf *)node)->mObjects.size();
5782
5783                        node->mRenderCost += rc * vc->GetVolume() * invViewSpaceVol;
5784
5785                        if (node->mRenderCost > maxRenderCost)
5786                                maxRenderCost = node->mRenderCost;
5787                }
5788        }
5789
5790        cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3f << " secs" << endl;
5791
5792        return maxRenderCost;
5793}
5794
5795
5796void VspOspViewCellsManager::Visualize(const ObjectContainer &objects,
5797                                                                           const VssRayContainer &sampleRays)
5798{
5799        if (!ViewCellsConstructed())
5800                return;
5801
5802        VssRayContainer visRays;
5803        GetRaySets(sampleRays, mVisualizationSamples, visRays);
5804
5805        ////////////
5806        //-- export final view cells
5807
5808        Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
5809
5810        //Vector3 scale(0.9f, 0.9f, 0.9f);
5811        Vector3 scale(1.0f, 1.0f, 1.0f);
5812
5813        if (exporter)
5814        {
5815                if (CLAMP_TO_BOX)
5816                {       
5817                        exporter->mClampToBox = true;   
5818                }
5819
5820                EvaluateViewCellsStats();
5821
5822                const long starttime = GetTime();
5823                cout << "exporting final view cells (after initial construction + post process) ... ";
5824
5825                // matt: hack for clamping scene
5826                AxisAlignedBox3 bbox = mViewSpaceBox;
5827                bbox.Scale(scale);
5828
5829                if (0 && mExportRays)
5830                {       
5831                        exporter->ExportRays(visRays, RgbColor(0, 1, 0));
5832                }
5833
5834                // hack color code (show pvs size)
5835                const int savedColorCode = mColorCode;
5836
5837                const float maxRenderCost = UpdateObjectCosts();
5838                cout << "maxRenderCost: " << maxRenderCost << endl;
5839                mColorCode = 0; // 0 = random, 1 = export pvs
5840
5841                mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects,
5842                                                                                                          CLAMP_TO_BOX ? &bbox : NULL, maxRenderCost, false);
5843               
5844
5845                //ExportViewCellsForViz(exporter, CLAMP_TO_BOX ? &bbox : NULL, mColorCode, GetClipPlane());
5846                ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
5847
5848                delete exporter;
5849
5850                cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3f << " secs" << endl;
5851                mColorCode = savedColorCode;
5852        }
5853
5854        exporter = Exporter::GetExporter("final_object_partition.wrl");
5855
5856        if (exporter)
5857        {
5858                if (CLAMP_TO_BOX)
5859                {       
5860                        exporter->mClampToBox = true;   
5861                }
5862
5863                EvaluateViewCellsStats();
5864
5865                const long starttime = GetTime();
5866                cout << "exporting final objects (after initial construction + post process) ... ";
5867
5868                // matt: hack for clamping scene
5869                AxisAlignedBox3 bbox = mViewSpaceBox;
5870                bbox.Scale(scale);
5871
5872                // hack color code (show pvs size)
5873                const int savedColorCode = mColorCode;
5874
5875                mColorCode = 1; // 0 = random, 1 = export pvs
5876                const float maxRenderCost = -1;
5877
5878                mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects,
5879                                                                                                          CLAMP_TO_BOX ? &bbox : NULL, maxRenderCost, false);
5880               
5881
5882                //ExportViewCellsForViz(exporter, CLAMP_TO_BOX ? &bbox : NULL, mColorCode, GetClipPlane());
5883                ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
5884
5885                delete exporter;
5886
5887                cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3f << " secs" << endl;
5888                mColorCode = savedColorCode;
5889        }
5890
5891#if 0
5892        // export final object partition
5893        exporter = Exporter::GetExporter("final_object_partition.wrl");
5894
5895        if (exporter)
5896        {
5897                if (CLAMP_TO_BOX)
5898                {       
5899                        exporter->mClampToBox = true;   
5900                }
5901
5902                const long starttime = GetTime();
5903
5904                // matt: hack for making visualization smaller in size
5905                AxisAlignedBox3 bbox = mHierarchyManager->GetObjectSpaceBox();
5906                bbox.Scale(scale);
5907
5908                cout << "exporting object space hierarchy ... ";
5909                mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, CLAMP_TO_BOX ? &bbox : NULL, -1);
5910
5911                delete exporter;
5912                cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3f << " secs" << endl;
5913        }
5914#endif
5915
5916        // visualization of the view cells
5917    if (0)
5918        {       
5919                ExportMergedViewCells(objects);
5920        }
5921
5922        // export some view cells
5923        int leafOut;
5924        Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
5925
5926        const bool sortViewCells = false;
5927        const bool exportPvs = true;
5928        const bool exportRays = true;
5929        const int raysOut = 100;
5930
5931        ExportSingleViewCells(objects,
5932                                                  leafOut,
5933                                                  sortViewCells,
5934                                                  exportPvs,
5935                                                  exportRays,
5936                                                  raysOut,
5937                                                  "");
5938}
5939
5940
5941void VspOspViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
5942                                                                                                   const int maxViewCells,
5943                                                                                                   const bool sortViewCells,
5944                                                                                                   const bool exportPvs,
5945                                                                                                   const bool exportRays,
5946                                                                                                   const int maxRays,
5947                                                                                                   const string prefix,
5948                                                                                                   VssRayContainer *visRays)
5949{
5950        if (sortViewCells)
5951        {
5952                // sort view cells to visualize the view cells with highest render cost
5953                sort(mViewCells.begin(), mViewCells.end(), ViewCell::LargerRenderCost);
5954        }
5955
5956        ViewCell::NewMail();
5957        const int limit = min(maxViewCells, (int)mViewCells.size());
5958       
5959        cout << "\nExporting " << limit << " single view cells: " << endl;
5960       
5961        for (int i = 0; i < limit; ++ i)
5962        {
5963                cout << "creating output for view cell " << i << " ... ";
5964               
5965                // largest view cell pvs first of random view cell
5966                ViewCell *vc = sortViewCells ?
5967                        mViewCells[i] : mViewCells[(int)RandomValue(0, (float)mViewCells.size() - 1)];
5968               
5969                if (vc->Mailed()) // view cell already processed
5970                        continue;
5971
5972                vc->Mail();
5973
5974                ObjectPvs pvs;
5975                mViewCellsTree->GetPvs(vc, pvs);
5976
5977                char s[64]; sprintf(s, "%sviewcell%04d.wrl", prefix.c_str(), i);
5978                Exporter *exporter = Exporter::GetExporter(s);
5979               
5980                cout << "view cell " << vc->GetId() << ": pvs cost=" << mViewCellsTree->GetPvsCost(vc) << endl;
5981
5982                if (exportPvs)
5983                {
5984                        Material m;
5985
5986                        Intersectable::NewMail();
5987                       
5988                        ObjectPvsIterator pit = pvs.GetIterator();
5989
5990                        // output PVS of view cell
5991                        while (pit.HasMoreEntries())
5992                        {               
5993                                ObjectPvsEntry entry = pit.Next();
5994                               
5995                                Intersectable *intersect = entry.mObject;
5996
5997                                if (!intersect->Mailed())
5998                                {
5999                                        m = RandomMaterial();
6000                                        exporter->SetForcedMaterial(m);
6001
6002                                        exporter->ExportIntersectable(intersect);
6003                                        intersect->Mail();
6004                                }
6005                        }
6006                }
6007
6008                if (exportRays)
6009                {
6010                        ////////////
6011                        //-- export the sample rays
6012
6013                        // output rays stored with the view cells during subdivision
6014                        VssRayContainer vcRays;
6015                        VssRayContainer collectRays;
6016
6017                        // collect intial view cells
6018                        ViewCellContainer leaves;
6019                        mViewCellsTree->CollectLeaves(vc, leaves);
6020
6021                        ViewCellContainer::const_iterator vit, vit_end = leaves.end();
6022
6023                        for (vit = leaves.begin(); vit != vit_end; ++ vit)
6024                        {
6025                                VspLeaf *vcLeaf = dynamic_cast<VspViewCell *>(*vit)->mLeaves[0];
6026                                VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
6027
6028                                for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
6029                                {
6030                                        collectRays.push_back(*rit);
6031                                }
6032                        }
6033
6034                        const int raysOut = min((int)collectRays.size(), maxRays);
6035
6036                        VssRayContainer::const_iterator rit, rit_end = collectRays.end();
6037
6038                        for (rit = collectRays.begin(); rit != rit_end; ++ rit)
6039                        {
6040                                const float p = RandomValue(0.0f, (float)collectRays.size());
6041
6042                                if (p < raysOut)
6043                                        vcRays.push_back(*rit);
6044                        }
6045
6046                        exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
6047                }
6048               
6049       
6050                /////////////////
6051                //-- export view cell geometry
6052
6053                exporter->SetWireframe();
6054
6055                Material m;
6056                m.mDiffuseColor = RgbColor(0, 1, 0);
6057                exporter->SetForcedMaterial(m);
6058
6059                ExportViewCellGeometry(exporter, vc, NULL, NULL);
6060                exporter->SetFilled();
6061
6062                DEL_PTR(exporter);
6063                cout << "finished" << endl;
6064        }
6065
6066        cout << endl;
6067}
6068
6069
6070int VspOspViewCellsManager::ComputeBoxIntersections(const AxisAlignedBox3 &box,
6071                                                                                                        ViewCellContainer &viewCells) const
6072{
6073        return mHierarchyManager->GetVspTree()->ComputeBoxIntersections(box, viewCells);
6074}
6075
6076
6077int VspOspViewCellsManager::CastLineSegment(const Vector3 &origin,
6078                                                                                        const Vector3 &termination,
6079                                                                                        ViewCellContainer &viewcells)
6080{
6081        return mHierarchyManager->GetVspTree()->CastLineSegment(origin, termination, viewcells);
6082}
6083
6084
6085bool VspOspViewCellsManager::ExportViewCells(const string filename,
6086                                                                                         const bool exportPvs,
6087                                                                                         const ObjectContainer &objects)
6088{
6089        if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
6090                return false;
6091
6092        const long starttime = GetTime();
6093        cout << "exporting view cells to xml ... ";
6094       
6095        OUT_STREAM stream(filename.c_str());
6096
6097        // for output we need unique ids for each view cell
6098        CreateUniqueViewCellIds();
6099
6100        stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
6101        stream << "<VisibilitySolution>" << endl;
6102
6103        // §§ tmp matt: for storage cost
6104        if (0 && exportPvs)
6105        {
6106        ///////////////
6107                //-- export bounding boxes
6108                //-- The bounding boxes are used to identify
6109                //-- the objects in the rendering engine
6110                mHierarchyManager->ExportBoundingBoxes(stream, objects);
6111        }
6112
6113        //////////////////////////
6114        //-- export the view cells and the pvs
6115
6116        const int numViewCells = mCurrentViewCellsStats.viewCells;
6117
6118        stream << "<ViewCells number=\"" << numViewCells << "\" >" << endl;
6119        mViewCellsTree->Export(stream, exportPvs);
6120        stream << "</ViewCells>" << endl;
6121
6122        //////////////////////
6123        //-- export the view space hierarchy
6124       
6125        stream << "<ViewSpaceHierarchy type=\"vsp\""
6126                   << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
6127                   << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\">" << endl;
6128
6129        mHierarchyManager->GetVspTree()->Export(stream);
6130        stream << "</ViewSpaceHierarchy>" << endl;
6131
6132        ////////////////////// 
6133        //-- export the object space partition
6134       
6135        mHierarchyManager->ExportObjectSpaceHierarchy(stream);
6136       
6137        stream << "</VisibilitySolution>" << endl;
6138        stream.close();
6139       
6140        cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3 << " secs" << endl;
6141        return true;
6142}
6143
6144
6145
6146ViewCell *VspOspViewCellsManager::GetViewCell(const Vector3 &point,
6147                                                                                          const bool active) const
6148{
6149        if (!ViewCellsConstructed())
6150                return NULL;
6151
6152        if (!mViewSpaceBox.IsInside(point))
6153                return NULL;
6154
6155        return mHierarchyManager->GetVspTree()->GetViewCell(point, active);
6156}
6157
6158
6159void VspOspViewCellsManager::CreateMesh(ViewCell *vc)
6160{
6161        // matt: TODO
6162        Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
6163
6164        ViewCellContainer leaves;
6165        mViewCellsTree->CollectLeaves(vc, leaves);
6166
6167        ViewCellContainer::const_iterator it, it_end = leaves.end();
6168
6169    for (it = leaves.begin(); it != it_end; ++ it)
6170        {
6171                VspLeaf *leaf = dynamic_cast<VspViewCell *>(*it)->mLeaves[0];
6172                const AxisAlignedBox3 box = mHierarchyManager->GetVspTree()->GetBoundingBox(leaf);
6173        IncludeBoxInMesh(box, *mesh);
6174        }
6175
6176        vc->SetMesh(mesh);
6177}
6178
6179
6180int VspOspViewCellsManager::CastBeam(Beam &beam)
6181{
6182        // matt: TODO
6183        return 0;
6184}
6185
6186
6187void VspOspViewCellsManager::Finalize(ViewCell *viewCell, const bool createMesh)
6188{
6189        float area = 0;
6190        float volume = 0;
6191
6192        ViewCellContainer leaves;
6193        mViewCellsTree->CollectLeaves(viewCell, leaves);
6194
6195        ViewCellContainer::const_iterator it, it_end = leaves.end();
6196
6197    for (it = leaves.begin(); it != it_end; ++ it)
6198        {
6199                VspLeaf *leaf = dynamic_cast<VspViewCell *>(*it)->mLeaves[0];
6200               
6201                const AxisAlignedBox3 box = mHierarchyManager->GetVspTree()->GetBoundingBox(leaf);
6202
6203                const float lVol = box.GetVolume();
6204                const float lArea = box.SurfaceArea();
6205
6206                area += lArea;
6207                volume += lVol;
6208
6209        CreateMesh(*it);
6210        }
6211
6212        viewCell->SetVolume(volume);
6213        viewCell->SetArea(area);
6214}
6215
6216
6217void VspOspViewCellsManager::PrepareLoadedViewCells()
6218{
6219        // TODO
6220}
6221
6222
6223static void PrintCompressionStats(HierarchyManager *hm, const int pvsEntries)
6224{
6225        float mem = (float)pvsEntries * ObjectPvs::GetEntrySize();
6226               
6227        float fullmem = mem +
6228                        (hm->GetVspTree()->GetStatistics().Leaves() * 16 +
6229                         hm->mBvHierarchy->GetStatistics().Leaves() * 16) / float(1024 * 1024);
6230
6231        cout << "entries: " << pvsEntries << ", mem=" << mem << ", fullmem=" << fullmem <<endl;
6232        Debug << "entries: " << pvsEntries << ", mem=" << mem << ", fullmem=" << fullmem <<endl;
6233}
6234
6235
6236void VspOspViewCellsManager::CompressViewCells()
6237{
6238        if (!(ViewCellsTreeConstructed() && mCompressViewCells))
6239                return;
6240
6241        ////////////
6242        //-- compression
6243
6244        int pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
6245
6246        cout << "before compression: " << endl;
6247        Debug << "before compression: " << endl;
6248       
6249        PrintCompressionStats(mHierarchyManager, pvsEntries);
6250
6251        if (mCompressObjects)
6252        {
6253                cout << "compressing in the objects: " << endl;
6254                Debug << "compressing in the objects: " << endl;
6255
6256                pvsEntries = mHierarchyManager->CompressObjectSpace();
6257        }
6258        else
6259        {
6260                cout << "compressing in the view space: " << endl;
6261                Debug << "compressing in the view space: " << endl;
6262
6263                mViewCellsTree->SetViewCellsStorage(ViewCellsTree::COMPRESSED);
6264                pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
6265        }
6266
6267        PrintCompressionStats(mHierarchyManager, pvsEntries);
6268}
6269
6270
6271ViewCellsManager *VspOspViewCellsManager::LoadViewCells(const string &filename,
6272                                                                                                                ObjectContainer *objects,
6273                                                                                                                const bool finalizeViewCells,
6274                                                                                                                BoundingBoxConverter *bconverter)
6275                                                                                                 
6276{
6277        ViewCellsManager *vm =
6278                ViewCellsManager::LoadViewCells(filename, objects, finalizeViewCells, bconverter);
6279#if 0
6280        // insert scene objects in tree
6281        mOspTree->InsertObjects(mOspTree->GetRoot(), *objects);
6282#endif
6283        return vm;
6284}
6285
6286
6287void
6288VspOspViewCellsManager::CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects)
6289{
6290  mHierarchyManager->CollectObjects(box, objects);
6291}
6292
6293
6294#if 1
6295
6296void VspOspViewCellsManager::EvalViewCellPartition()
6297{
6298        int samplesPerPass;
6299        int numSamples;
6300        int castSamples = 0;
6301        int oldSamples = 0;
6302        int samplesForStats;
6303        char statsPrefix[100];
6304        char suffix[100];
6305        int splitsStepSize;
6306
6307        Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesPerPass", samplesPerPass);
6308        Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesForStats", samplesForStats);
6309        Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samples", numSamples);
6310        Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.statsPrefix", statsPrefix);
6311        Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.stepSize", splitsStepSize);
6312       
6313        bool useHisto;
6314        int histoMem;
6315
6316        Environment::GetSingleton()->GetBoolValue("ViewCells.Evaluation.histogram", useHisto);
6317        Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.histoMem", histoMem);
6318
6319        Debug << "step size: " << splitsStepSize << endl;
6320        Debug << "view cell evaluation samples per pass: " << samplesPerPass << endl;
6321        Debug << "view cell evaluation samples: " << numSamples << endl;
6322        Debug << "view cell stats prefix: " << statsPrefix << endl;
6323
6324    cout << "reseting pvs ... ";
6325               
6326        // reset pvs and start over from zero
6327        mViewCellsTree->ResetPvs();
6328       
6329        cout << "finished" << endl;
6330    cout << "Evaluating view cell partition ... " << endl;
6331
6332        vector<int> evalStrats;
6333
6334        // mix of sampling strategies
6335        if (0)
6336        {
6337                evalStrats.push_back(SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION);
6338        }
6339        else
6340        {
6341                /*evalStrats.push_back(SamplingStrategy::OBJECT_BASED_DISTRIBUTION);
6342                evalStrats.push_back(SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION);
6343                evalStrats.push_back(SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION);
6344                */
6345                evalStrats.push_back(SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION);
6346        }
6347                       
6348    Debug << "casting eval strategies: ";
6349        for (int i = 0; i < (int)evalStrats.size(); ++ i)
6350                Debug << evalStrats[i] << " ";
6351        Debug << endl;
6352
6353        cout << "casting eval strategies: ";
6354        for (int i = 0; i < (int)evalStrats.size(); ++ i)
6355                cout << evalStrats[i] << " ";
6356        cout << endl;
6357
6358        int pass = 0;
6359
6360        while (castSamples < numSamples)
6361        {               
6362                ///////////////
6363                //-- we have to use uniform sampling strategy for construction rays
6364
6365                VssRayContainer evaluationSamples;
6366                const int samplingType = mEvaluationSamplingType;
6367
6368                long startTime = GetTime();
6369
6370                cout << "casting " << samplesPerPass << " samples ... ";
6371                Debug << "casting " << samplesPerPass << " samples ... ";
6372       
6373                if (1)
6374                {
6375                        CastPassSamples(samplesPerPass, evalStrats, evaluationSamples);
6376                }
6377                else
6378                {
6379                        // use mixed distributions
6380                        CastPassSamples2(samplesPerPass, evaluationSamples);
6381                }
6382
6383                castSamples += samplesPerPass;
6384
6385                Real timeDiff = TimeDiff(startTime, GetTime());
6386               
6387                cout << "finished in " << timeDiff * 1e-3f << " secs" << endl;
6388                cout << "computing sample contributions of " << (int)evaluationSamples.size()  << " samples ... ";
6389               
6390                Debug << "finished in " << timeDiff * 1e-3f << " secs" << endl;
6391                Debug << "computing sample contributions of " << (int)evaluationSamples.size()  << " samples ... ";
6392
6393                startTime = GetTime();
6394
6395                ComputeSampleContributions(evaluationSamples, true, false);
6396
6397                timeDiff = TimeDiff(startTime, GetTime());
6398                cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
6399                Debug << "finished in " << timeDiff * 1e-3 << " secs" << endl;
6400
6401                if ((castSamples >= samplesForStats + oldSamples) ||
6402                        (castSamples >= numSamples))
6403                {
6404                        oldSamples += samplesForStats;
6405
6406                        ///////////
6407                        //-- output stats
6408
6409                        sprintf(suffix, "-%09d-eval.log", castSamples);
6410                        const string filename = string(statsPrefix) + string(suffix);
6411
6412                        startTime = GetTime();
6413                       
6414                        cout << "compute new statistics ... " << endl;
6415
6416                        ofstream ofstr(filename.c_str());
6417                        mHierarchyManager->EvaluateSubdivision2(ofstr, splitsStepSize, false, useHisto, histoMem, pass);
6418
6419                        timeDiff = TimeDiff(startTime, GetTime());
6420                        cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
6421                        cout << "*************************************" << endl;
6422
6423                        Debug << "statistics computed in " << timeDiff * 1e-3 << " secs" << endl;
6424
6425#if 0
6426                        //////////////
6427                        // filtered stats
6428
6429                        sprintf(suffix, "-%09d-eval-filter.log", castSamples);
6430                        const string filename2 = string(statsPrefix) + string(suffix);
6431
6432                        startTime = GetTime();
6433                       
6434                        cout << "compute new statistics for filtered pvs ... " << endl;
6435
6436                        ofstream ofstr2(filename2.c_str());
6437                        mHierarchyManager->EvaluateSubdivision2(ofstr2, splitsStepSize, true);
6438
6439                        timeDiff = TimeDiff(startTime, GetTime());
6440                        cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
6441                        cout << "*************************************" << endl;
6442                        Debug << "filtered statistics computed in " << timeDiff * 1e-3 << " secs" << endl;
6443#endif
6444
6445                        // only for debugging purpose
6446                        if (0)
6447                        {
6448                                ViewCellContainer viewCells;
6449                                mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), viewCells);
6450
6451                                ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
6452                                int pvsSize = 0;
6453
6454                                for (vit = viewCells.begin(); vit != vit_end; ++ vit)
6455                                {
6456                                        pvsSize += (*vit)->GetPvs().GetSize();
6457                                }
6458
6459                                cout << "debug entries: " << pvsSize << ", memcost: "
6460                                         << (float)pvsSize * ObjectPvs::GetEntrySize() << endl;
6461                        }
6462                        ++ pass;
6463                }
6464
6465                disposeRays(evaluationSamples, NULL);
6466        }
6467
6468        ////////////
6469        //-- histogram
6470#if 1
6471        const int numLeaves = mViewCellsTree->GetNumInitialViewCells(mViewCellsTree->GetRoot());
6472        int histoStepSize;
6473
6474        Environment::GetSingleton()->GetBoolValue("ViewCells.Evaluation.histogram", useHisto);
6475        Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.histoStepSize", histoStepSize);
6476
6477        if (useHisto)
6478        {
6479                // evaluate view cells in a histogram           
6480                char str[64];
6481
6482                // hack: just show final view cells
6483                int pass = (int)mViewCells.size();
6484                //for (int pass = histoStepSize; pass <= numLeaves; pass += histoStepSize)
6485                if (1)
6486                {
6487                        string filename;
6488
6489                        cout << "computing histogram for " << pass << " view cells" << endl;
6490
6491                        //////////////////////////////////////////
6492            //-- evaluate histogram for pvs size
6493
6494                        cout << "computing pvs histogram for " << pass << " view cells" << endl;
6495
6496                        sprintf(str, "-%09d-histo-pvs2.log", pass);
6497                        filename = string(statsPrefix) + string(str);
6498
6499                        EvalViewCellHistogramForPvsSize(filename, pass);
6500                }
6501        }
6502#endif
6503}
6504
6505#endif
6506
6507
6508
6509}
Note: See TracBrowser for help on using the repository browser.