source: trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h @ 406

Revision 406, 3.3 KB checked in by mattausch, 19 years ago (diff)

started kd based bottom-up view cells

Line 
1#ifndef __PVS_H
2#define __PVS_H
3
4#include <map>
5
6class KdNode;
7class BspNode;
8class Ray;
9class Intersectable;
10class ViewCellKdNode;
11
12template<typename T>
13struct LtSample {
14    bool operator()(const T a, const T b) const
15    {
16                return a < b;
17        }
18};
19
20//typedef std::map<T *, PvsData<T>, LtSample<T>> PvsMap<T>;
21
22template<typename T>
23struct PvsData {
24  int mVisibleSamples;
25  PvsData<T>() {}
26  PvsData<T>(const int samples): mVisibleSamples(samples) {}
27};
28
29template<typename T>
30class Pvs
31{
32public:
33        Pvs(): mSamples(0), mEntries() {}
34
35        int mSamples;
36
37        int Compress() {return 0;}
38        int GetSize() const {return (int)mEntries.size();}
39        bool Empty() const {return mEntries.size() == 0;}
40        /** Merges pvs of a and pvs of b into this pvs.
41        */
42        void Merge(const Pvs<T> &a, const Pvs<T> &b);
43    /** Difference of pvs to pvs b.
44                @returns number of different entries.
45        */
46        int Diff(const Pvs<T> &b);
47
48        PvsData<T> *Find(T sample);
49        int AddSample(T sample);
50
51        void GetData(const int index, T &entry, PvsData<T> &data);
52        std::map<T, PvsData<T>, LtSample<T> > mEntries;
53};
54
55template <typename T>
56int Pvs<T>::Diff(const Pvs<T> &b)
57{
58        int dif = 0;
59
60        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
61
62        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
63        {
64                PvsData<T> *data = Find((*it).first);           
65                if (!data) ++ dif;
66        }
67
68        return dif;
69}
70
71template <typename T>
72void Pvs<T>::Merge(const Pvs<T> &a, const Pvs<T> &b)
73{
74        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
75
76        // todo: copy all elements instead of inserting
77        for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
78        {
79                mEntries.insert(*it);
80        }
81
82        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
83        {
84                PvsData<T> *data = Find((*it).first);
85               
86                if (data)
87                        data->mVisibleSamples += (*it).second.mVisibleSamples;
88                else
89                        mEntries.insert(*it);
90        }
91}
92
93template <typename T>
94PvsData<T> *Pvs<T>::Find(T sample)
95{
96  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
97  if (i != mEntries.end()) {
98    return &(*i).second;
99  } else
100    return NULL;
101}
102
103template <typename T>
104void Pvs<T>::GetData(const int index,
105               T &entry,
106               PvsData<T> &data)
107{
108  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
109  for (int k = 0; k != index && i != mEntries.end(); i++, k++);
110
111  entry = (*i).first;
112  data = (*i).second;
113}
114
115template <typename T>
116int Pvs<T>::AddSample(T sample)
117{
118        int result;
119        PvsData<T> *data = Find(sample);
120
121        if (data)
122        {
123                data->mVisibleSamples ++;
124                result = 0;
125       
126        }
127        else
128        {
129                mEntries[sample] = PvsData<T>(1);
130                result = 1;
131        }
132
133        return  result;
134}
135
136/** Class instantiating the Pvs template for kd tree nodes.
137*/
138class KdPvs: public Pvs<KdNode *>
139{
140        int Compress();
141};
142
143typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
144typedef std::map<ViewCellKdNode *, PvsData<ViewCellKdNode *>, LtSample<ViewCellKdNode *> > ViewCellKdPvsMap;
145typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ViewCellPvsMap;
146typedef PvsData<Intersectable *> ViewCellPvsData;
147typedef PvsData<KdNode *> KdPvsData;
148typedef Pvs<ViewCellKdNode *> ViewCellKdPvs;
149typedef Pvs<Intersectable *> ViewCellPvs;
150
151
152#endif
153
Note: See TracBrowser for help on using the repository browser.