[177] | 1 | #ifndef __PVS_H
|
---|
| 2 | #define __PVS_H
|
---|
| 3 |
|
---|
| 4 | #include <map>
|
---|
| 5 |
|
---|
| 6 | class KdNode;
|
---|
[240] | 7 | class BspNode;
|
---|
[191] | 8 | class Ray;
|
---|
[308] | 9 | class Intersectable;
|
---|
[177] | 10 |
|
---|
[310] | 11 |
|
---|
| 12 | template<typename T>
|
---|
| 13 | struct LtSample {
|
---|
[311] | 14 | bool operator()(const T a, const T b) const
|
---|
[310] | 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>() {}
|
---|
[311] | 26 | PvsData<T>(const int samples): mVisibleSamples(samples) {}
|
---|
[310] | 27 | };
|
---|
| 28 |
|
---|
| 29 | template<typename T>
|
---|
[311] | 30 | class Pvs
|
---|
[310] | 31 | {
|
---|
| 32 | public:
|
---|
[369] | 33 | Pvs(): mSamples(0), mEntries() {}
|
---|
| 34 |
|
---|
[310] | 35 | int mSamples;
|
---|
| 36 |
|
---|
[311] | 37 | int Compress() {return 0;}
|
---|
[310] | 38 | int GetSize() {return (int)mEntries.size();}
|
---|
[341] | 39 | bool Empty() {return mEntries.size() == 0;}
|
---|
[362] | 40 | /** Merges pvs of a and pvs of b into this pvs.
|
---|
| 41 | */
|
---|
[350] | 42 | void Merge(const Pvs<T> &a, const Pvs<T> &b);
|
---|
[362] | 43 | /** Difference of pvs to pvs b.
|
---|
| 44 | @returns number of different entries.
|
---|
| 45 | */
|
---|
| 46 | int Diff(const Pvs<T> &b);
|
---|
[310] | 47 |
|
---|
| 48 | PvsData<T> *Find(T sample);
|
---|
| 49 | int AddSample(T sample);
|
---|
| 50 |
|
---|
[362] | 51 | void GetData(const int index, T &entry, PvsData<T> &data);
|
---|
[310] | 52 | std::map<T, PvsData<T>, LtSample<T> > mEntries;
|
---|
| 53 | };
|
---|
| 54 |
|
---|
[341] | 55 | template <typename T>
|
---|
[362] | 56 | int 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 |
|
---|
| 71 | template <typename T>
|
---|
[350] | 72 | void Pvs<T>::Merge(const Pvs<T> &a, const Pvs<T> &b)
|
---|
[341] | 73 | {
|
---|
[362] | 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 | }
|
---|
[341] | 91 | }
|
---|
| 92 |
|
---|
[362] | 93 |
|
---|
[310] | 94 | template <typename T>
|
---|
[311] | 95 | PvsData<T> *Pvs<T>::Find(T sample)
|
---|
[310] | 96 | {
|
---|
[311] | 97 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
|
---|
[310] | 98 | if (i != mEntries.end()) {
|
---|
| 99 | return &(*i).second;
|
---|
[311] | 100 | } else
|
---|
[310] | 101 | return NULL;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | template <typename T>
|
---|
[311] | 105 | void Pvs<T>::GetData(const int index,
|
---|
[310] | 106 | T &entry,
|
---|
| 107 | PvsData<T> &data)
|
---|
| 108 | {
|
---|
[311] | 109 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
|
---|
[310] | 110 | for (int k = 0; k != index && i != mEntries.end(); i++, k++);
|
---|
| 111 |
|
---|
| 112 | entry = (*i).first;
|
---|
[311] | 113 | data = (*i).second;
|
---|
[310] | 114 | }
|
---|
| 115 |
|
---|
[311] | 116 | template <typename T>
|
---|
| 117 | int Pvs<T>::AddSample(T sample)
|
---|
[177] | 118 | {
|
---|
[311] | 119 | int result;
|
---|
| 120 | PvsData<T> *data = Find(sample);
|
---|
[177] | 121 |
|
---|
[311] | 122 | if (data)
|
---|
| 123 | {
|
---|
| 124 | data->mVisibleSamples ++;
|
---|
| 125 | result = 0;
|
---|
[240] | 126 |
|
---|
[311] | 127 | }
|
---|
| 128 | else
|
---|
| 129 | {
|
---|
| 130 | mEntries[sample] = PvsData<T>(1);
|
---|
| 131 | result = 1;
|
---|
| 132 | }
|
---|
[240] | 133 |
|
---|
[311] | 134 | return result;
|
---|
| 135 | }
|
---|
[308] | 136 |
|
---|
[311] | 137 | /** Class instantiating the Pvs template for kd tree nodes.
|
---|
| 138 | */
|
---|
| 139 | class KdPvs: public Pvs<KdNode *>
|
---|
[308] | 140 | {
|
---|
[311] | 141 | int Compress();
|
---|
[308] | 142 | };
|
---|
| 143 |
|
---|
[311] | 144 | typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
|
---|
[312] | 145 | typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ViewCellPvsMap;
|
---|
| 146 | typedef PvsData<Intersectable *> ViewCellPvsData;
|
---|
[311] | 147 | typedef PvsData<KdNode *> KdPvsData;
|
---|
| 148 | //typedef Pvs<KdNode *> KdPvs;
|
---|
| 149 | typedef Pvs<Intersectable *> ViewCellPvs;
|
---|
[308] | 150 |
|
---|
[366] | 151 |
|
---|
[177] | 152 | #endif
|
---|
| 153 |
|
---|