[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:
|
---|
[311] | 33 | Pvs(): mSamples(0), mEntries() {}
|
---|
[310] | 34 | int mSamples;
|
---|
| 35 |
|
---|
[311] | 36 | int Compress() {return 0;}
|
---|
[310] | 37 | int GetSize() {return (int)mEntries.size();}
|
---|
[341] | 38 | bool Empty() {return mEntries.size() == 0;}
|
---|
[362] | 39 | /** Merges pvs of a and pvs of b into this pvs.
|
---|
| 40 | */
|
---|
[350] | 41 | void Merge(const Pvs<T> &a, const Pvs<T> &b);
|
---|
[362] | 42 | /** Difference of pvs to pvs b.
|
---|
| 43 | @returns number of different entries.
|
---|
| 44 | */
|
---|
| 45 | int Diff(const Pvs<T> &b);
|
---|
[310] | 46 |
|
---|
| 47 | PvsData<T> *Find(T sample);
|
---|
| 48 | int AddSample(T sample);
|
---|
| 49 |
|
---|
[362] | 50 | void GetData(const int index, T &entry, PvsData<T> &data);
|
---|
[310] | 51 | std::map<T, PvsData<T>, LtSample<T> > mEntries;
|
---|
| 52 | };
|
---|
| 53 |
|
---|
[341] | 54 | template <typename T>
|
---|
[362] | 55 | int Pvs<T>::Diff(const Pvs<T> &b)
|
---|
| 56 | {
|
---|
| 57 | int dif = 0;
|
---|
| 58 |
|
---|
| 59 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
| 60 |
|
---|
| 61 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
| 62 | {
|
---|
| 63 | PvsData<T> *data = Find((*it).first);
|
---|
| 64 | if (!data) ++ dif;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | return dif;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | template <typename T>
|
---|
[350] | 71 | void Pvs<T>::Merge(const Pvs<T> &a, const Pvs<T> &b)
|
---|
[341] | 72 | {
|
---|
[362] | 73 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
| 74 |
|
---|
| 75 | // todo: copy all elements instead of inserting
|
---|
| 76 | for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
|
---|
| 77 | {
|
---|
| 78 | mEntries.insert(*it);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
| 82 | {
|
---|
| 83 | PvsData<T> *data = Find((*it).first);
|
---|
| 84 |
|
---|
| 85 | if (data)
|
---|
| 86 | data->mVisibleSamples += (*it).second.mVisibleSamples;
|
---|
| 87 | else
|
---|
| 88 | mEntries.insert(*it);
|
---|
| 89 | }
|
---|
[341] | 90 | }
|
---|
| 91 |
|
---|
[362] | 92 |
|
---|
[310] | 93 | template <typename T>
|
---|
[311] | 94 | PvsData<T> *Pvs<T>::Find(T sample)
|
---|
[310] | 95 | {
|
---|
[311] | 96 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
|
---|
[310] | 97 | if (i != mEntries.end()) {
|
---|
| 98 | return &(*i).second;
|
---|
[311] | 99 | } else
|
---|
[310] | 100 | return NULL;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | template <typename T>
|
---|
[311] | 104 | void Pvs<T>::GetData(const int index,
|
---|
[310] | 105 | T &entry,
|
---|
| 106 | PvsData<T> &data)
|
---|
| 107 | {
|
---|
[311] | 108 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
|
---|
[310] | 109 | for (int k = 0; k != index && i != mEntries.end(); i++, k++);
|
---|
| 110 |
|
---|
| 111 | entry = (*i).first;
|
---|
[311] | 112 | data = (*i).second;
|
---|
[310] | 113 | }
|
---|
| 114 |
|
---|
[311] | 115 | template <typename T>
|
---|
| 116 | int Pvs<T>::AddSample(T sample)
|
---|
[177] | 117 | {
|
---|
[311] | 118 | int result;
|
---|
| 119 | PvsData<T> *data = Find(sample);
|
---|
[177] | 120 |
|
---|
[311] | 121 | if (data)
|
---|
| 122 | {
|
---|
| 123 | data->mVisibleSamples ++;
|
---|
| 124 | result = 0;
|
---|
[240] | 125 |
|
---|
[311] | 126 | }
|
---|
| 127 | else
|
---|
| 128 | {
|
---|
| 129 | mEntries[sample] = PvsData<T>(1);
|
---|
| 130 | result = 1;
|
---|
| 131 | }
|
---|
[240] | 132 |
|
---|
[311] | 133 | return result;
|
---|
| 134 | }
|
---|
[308] | 135 |
|
---|
[311] | 136 | /** Class instantiating the Pvs template for kd tree nodes.
|
---|
| 137 | */
|
---|
| 138 | class KdPvs: public Pvs<KdNode *>
|
---|
[308] | 139 | {
|
---|
[311] | 140 | int Compress();
|
---|
[308] | 141 | };
|
---|
| 142 |
|
---|
[311] | 143 | typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
|
---|
[312] | 144 | typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ViewCellPvsMap;
|
---|
| 145 | typedef PvsData<Intersectable *> ViewCellPvsData;
|
---|
[311] | 146 | typedef PvsData<KdNode *> KdPvsData;
|
---|
| 147 | //typedef Pvs<KdNode *> KdPvs;
|
---|
| 148 | typedef Pvs<Intersectable *> ViewCellPvs;
|
---|
[308] | 149 |
|
---|
[366] | 150 |
|
---|
[177] | 151 | #endif
|
---|
| 152 |
|
---|