source: GTP/trunk/Lib/Vis/Preprocessing/src/DifferenceSampling.cpp @ 2560

Revision 2560, 3.5 KB checked in by mattausch, 17 years ago (diff)

added functionality for visualization

RevLine 
[2546]1#include "SamplingStrategy.h"
2#include "Intersectable.h"
3#include "DifferenceSampling.h"
4#include "ViewCell.h"
[2548]5#include "ViewCellsManager.h"
6#include "Preprocessor.h"
[2559]7#include "Timer/PerfTimer.h"
[2546]8
9
10#ifdef GTP_INTERNAL
11#include "ArchModeler2MLRT.hxx"
12#endif
13
[2559]14PerfTimer sFilterTimer;
[2546]15
[2559]16
[2546]17namespace GtpVisibilityPreprocessor {
18
19
[2548]20/** Collects the pvs of the objects which are
21        in the first but not in the second pvs.
22*/
23static void GetPvsDifference(const ObjectPvs &largerPvs,
24                                                         const ObjectPvs &smallerPvs,
25                                                         ObjectContainer &pvsDifference)
26{
27        Intersectable::NewMail();
28        Intersectable *obj;
29
30        ObjectPvsIterator bit = smallerPvs.GetIterator();
31
32        // mail smaller pvs
33        while (bit.HasMoreEntries())
34        {
35                obj = bit.Next();
36                obj->Mail();
37        }
38
39        ObjectPvsIterator ait = largerPvs.GetIterator();
40
41        // write differece pvs
42        while (ait.HasMoreEntries())
43        {
44                obj = ait.Next();
45                pvsDifference.push_back(obj);
46
47                //if (!obj->Mailed())
48                //      c.AddSampleDirty(obj, 0);
49        }
50}
51
52
[2546]53DifferenceSampling::DifferenceSampling(Preprocessor &preprocessor):
[2548]54SamplingStrategy(preprocessor), mCurrentViewCell(0), mNumSamples(0)
[2546]55{}
56
57
[2548]58void DifferenceSampling::ComputeDifferenceSet(ViewCell *vc,
59                                                                                          ObjectContainer &differenceSet)
[2546]60{
[2548]61        const ObjectPvs &pvs = vc->GetPvs();
62        const float filterSize = 100;
63        ObjectPvs filteredPvs;
64
[2559]65        sFilterTimer.Entry();
66   
67        PvsFilterStatistics pvsStats = mPreprocessor.
[2548]68                mViewCellsManager->ApplyFilter2(vc,
69                                                false,
70                                                                                filterSize,
71                                                                                filteredPvs);
72
[2559]73        sFilterTimer.Exit();
74
[2548]75        //mDifferencePvs.Clear(false);
76        //mDifferencePvs.Reserve(filteredPvs.GetSize());
77        mPvsDifference.clear();
78        mPvsDifference.reserve(filteredPvs.GetSize());
79
80        GetPvsDifference(filteredPvs, pvs, mPvsDifference);
[2559]81
82        cerr << "found " << mPvsDifference.size() << " different objects" << endl;
[2546]83}
84
[2548]85
86bool DifferenceSampling::GenerateSample(SimpleRay &ray)
87{
[2559]88        if (mNumSamples <= 0)
[2548]89        {
[2559]90                // choose random view cell
[2548]91                ViewCellContainer &viewCells = mPreprocessor.mViewCellsManager->GetViewCells();
92                const int vcIdx = (int)RandomValue(0, (float)viewCells.size() - 0.5f);
93
94                mCurrentViewCell = viewCells[vcIdx];
95
[2559]96                if (mCurrentViewCell->GetPvs().Empty())
97                        return false;
98
99                cout << "computing new  difference set" << endl;
100
[2548]101                ComputeDifferenceSet(mCurrentViewCell, mPvsDifference);
102
103                // n samples per object
104                const int n = 50;
[2560]105                mNumSamples = (int)mPvsDifference.size() * n;
[2548]106        }
107
[2559]108        if (mPvsDifference.empty())
109                return false;
110
111        -- mNumSamples;
112
113        cout << "x";
[2548]114        Vector3 origin, direction;
115        Vector3 point;
116        Vector3 normal, vcnormal;
117
118        // get a new object from the difference set
119        const int objIdx = (int)RandomValue(0, (float)mPvsDifference.size() - 0.5f);
120    Intersectable *object = mPvsDifference[objIdx];
121
122        // cast a ray from the view cells border towards the object
123        object->GetRandomSurfacePoint(point, normal);
124        mCurrentViewCell->GetRandomEdgePoint(origin, vcnormal);
125
126        direction = point - origin;
127
128        // $$ jb the pdf is yet not correct for all sampling methods!
129        const float c = Magnitude(direction);
130
131        if ((c <= Limits::Small) /*|| (DotProd(direction, normal) < 0)*/)
132        {
133                return false;
134        }
135
136        // $$ jb the pdf is yet not correct for all sampling methods!
137        const float pdf = 1.0f;
138        //cout << "p: " << point << " ";
139        direction *= 1.0f / c;
140        ray = SimpleRay(origin, direction, DIFFERENCE_SAMPLING_BASED_DISTRIBUTION, pdf);
141
142        //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl;
143
144        return true;
145}
146
147
[2546]148}
Note: See TracBrowser for help on using the repository browser.