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

Revision 366, 3.1 KB checked in by mattausch, 19 years ago (diff)

some ideas abou saving bspleaves with the ray and t

Line 
1#ifndef __PVS_H
2#define __PVS_H
3
4#include <map>
5
6class KdNode;
7class BspNode;
8class Ray;
9class Intersectable;
10
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        int mSamples;
35
36        int Compress() {return 0;}
37        int GetSize() {return (int)mEntries.size();}
38        bool Empty() {return mEntries.size() == 0;}
39        /** Merges pvs of a and pvs of b into this pvs.
40        */
41        void Merge(const Pvs<T> &a, const Pvs<T> &b);
42    /** Difference of pvs to pvs b.
43                @returns number of different entries.
44        */
45        int Diff(const Pvs<T> &b);
46
47        PvsData<T> *Find(T sample);
48        int AddSample(T sample);
49
50        void GetData(const int index, T &entry, PvsData<T> &data);
51        std::map<T, PvsData<T>, LtSample<T> > mEntries;
52};
53
54template <typename T>
55int 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
70template <typename T>
71void Pvs<T>::Merge(const Pvs<T> &a, const Pvs<T> &b)
72{
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        }
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<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ViewCellPvsMap;
145typedef PvsData<Intersectable *> ViewCellPvsData;
146typedef PvsData<KdNode *> KdPvsData;
147//typedef Pvs<KdNode *> KdPvs;
148typedef Pvs<Intersectable *> ViewCellPvs;
149
150
151#endif
152
Note: See TracBrowser for help on using the repository browser.