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

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