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

Revision 466, 3.5 KB checked in by bittner, 19 years ago (diff)

changed the viewcellsmanager interface to use vssrays - some functionality like the bsp merging is now restricted

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  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
56template <typename T>
57int 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
72template <typename T>
73void 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
94template <typename T>
95PvsData<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
104template <typename T>
105void 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
116template <typename T>
117int Pvs<T>::AddSample(T sample)
118{
119  float dummy;
120  return AddSample(sample, dummy) ? 1 : 0;
121}
122
123template <typename T>
124bool 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*/
142class KdPvs: public Pvs<KdNode *>
143{
144        int Compress();
145};
146
147typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
148typedef std::map<ViewCellKdNode *, PvsData<ViewCellKdNode *>, LtSample<ViewCellKdNode *> > ViewCellKdPvsMap;
149typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ViewCellPvsMap;
150typedef PvsData<Intersectable *> ViewCellPvsData;
151typedef PvsData<KdNode *> KdPvsData;
152typedef Pvs<ViewCellKdNode *> ViewCellKdPvs;
153typedef Pvs<Intersectable *> ViewCellPvs;
154
155
156#endif
157
Note: See TracBrowser for help on using the repository browser.