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

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