1 | #ifndef __PVS_H
|
---|
2 | #define __PVS_H
|
---|
3 |
|
---|
4 | #include <map>
|
---|
5 |
|
---|
6 | class KdNode;
|
---|
7 | class BspNode;
|
---|
8 | class Ray;
|
---|
9 | class Intersectable;
|
---|
10 | class ViewCellKdNode;
|
---|
11 |
|
---|
12 | template<typename T>
|
---|
13 | struct 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 |
|
---|
22 | template<typename T>
|
---|
23 | struct PvsData {
|
---|
24 | int mVisibleSamples;
|
---|
25 | PvsData<T>() {}
|
---|
26 | PvsData<T>(const int samples): mVisibleSamples(samples) {}
|
---|
27 | };
|
---|
28 |
|
---|
29 | template<typename T>
|
---|
30 | class Pvs
|
---|
31 | {
|
---|
32 | public:
|
---|
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 | bool AddSample(T sample, float &contribution);
|
---|
50 | int AddSample(T sample);
|
---|
51 |
|
---|
52 | void GetData(const int index, T &entry, PvsData<T> &data);
|
---|
53 | std::map<T, PvsData<T>, LtSample<T> > mEntries;
|
---|
54 | };
|
---|
55 |
|
---|
56 | template <typename T>
|
---|
57 | int Pvs<T>::Diff(const Pvs<T> &b)
|
---|
58 | {
|
---|
59 | int dif = 0;
|
---|
60 |
|
---|
61 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
62 |
|
---|
63 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
64 | {
|
---|
65 | PvsData<T> *data = Find((*it).first);
|
---|
66 | if (!data) ++ dif;
|
---|
67 | }
|
---|
68 |
|
---|
69 | return dif;
|
---|
70 | }
|
---|
71 |
|
---|
72 | template <typename T>
|
---|
73 | void Pvs<T>::Merge(const Pvs<T> &a, const Pvs<T> &b)
|
---|
74 | {
|
---|
75 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
76 |
|
---|
77 | // todo: copy all elements instead of inserting
|
---|
78 | for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
|
---|
79 | {
|
---|
80 | mEntries.insert(*it);
|
---|
81 | }
|
---|
82 |
|
---|
83 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
84 | {
|
---|
85 | PvsData<T> *data = Find((*it).first);
|
---|
86 |
|
---|
87 | if (data)
|
---|
88 | data->mVisibleSamples += (*it).second.mVisibleSamples;
|
---|
89 | else
|
---|
90 | mEntries.insert(*it);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | template <typename T>
|
---|
95 | PvsData<T> *Pvs<T>::Find(T sample)
|
---|
96 | {
|
---|
97 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
|
---|
98 | if (i != mEntries.end()) {
|
---|
99 | return &(*i).second;
|
---|
100 | } else
|
---|
101 | return NULL;
|
---|
102 | }
|
---|
103 |
|
---|
104 | template <typename T>
|
---|
105 | void Pvs<T>::GetData(const int index,
|
---|
106 | T &entry,
|
---|
107 | PvsData<T> &data)
|
---|
108 | {
|
---|
109 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
|
---|
110 | for (int k = 0; k != index && i != mEntries.end(); i++, k++);
|
---|
111 |
|
---|
112 | entry = (*i).first;
|
---|
113 | data = (*i).second;
|
---|
114 | }
|
---|
115 |
|
---|
116 | template <typename T>
|
---|
117 | int Pvs<T>::AddSample(T sample)
|
---|
118 | {
|
---|
119 | float dummy;
|
---|
120 | return AddSample(sample, dummy) ? 1 : 0;
|
---|
121 | }
|
---|
122 |
|
---|
123 | template <typename T>
|
---|
124 | bool Pvs<T>::AddSample(T sample, float &contribution)
|
---|
125 | {
|
---|
126 | PvsData<T> *data = Find(sample);
|
---|
127 |
|
---|
128 | if (data) {
|
---|
129 | data->mVisibleSamples++;
|
---|
130 | contribution = 1.0f/data->mVisibleSamples;
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 | else {
|
---|
134 | mEntries[sample] = PvsData<T>(1);
|
---|
135 | contribution = 1.0f;
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** Class instantiating the Pvs template for kd tree nodes.
|
---|
141 | */
|
---|
142 | class KdPvs: public Pvs<KdNode *>
|
---|
143 | {
|
---|
144 | int Compress();
|
---|
145 | };
|
---|
146 |
|
---|
147 | typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
|
---|
148 | typedef std::map<ViewCellKdNode *, PvsData<ViewCellKdNode *>, LtSample<ViewCellKdNode *> > ViewCellKdPvsMap;
|
---|
149 | typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ViewCellPvsMap;
|
---|
150 | typedef PvsData<Intersectable *> ViewCellPvsData;
|
---|
151 | typedef PvsData<KdNode *> KdPvsData;
|
---|
152 | typedef Pvs<ViewCellKdNode *> ViewCellKdPvs;
|
---|
153 | typedef Pvs<Intersectable *> ViewCellPvs;
|
---|
154 |
|
---|
155 |
|
---|
156 | #endif
|
---|
157 |
|
---|