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

Revision 406, 3.3 KB checked in by mattausch, 19 years ago (diff)

started kd based bottom-up view cells

RevLine 
[177]1#ifndef __PVS_H
2#define __PVS_H
3
4#include <map>
5
6class KdNode;
[240]7class BspNode;
[191]8class Ray;
[308]9class Intersectable;
[406]10class ViewCellKdNode;
[177]11
[310]12template<typename T>
13struct 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
22template<typename T>
23struct PvsData {
24  int mVisibleSamples;
25  PvsData<T>() {}
[311]26  PvsData<T>(const int samples): mVisibleSamples(samples) {}
[310]27};
28
29template<typename T>
[311]30class Pvs
[310]31{
32public:
[369]33        Pvs(): mSamples(0), mEntries() {}
34
[310]35        int mSamples;
36
[311]37        int Compress() {return 0;}
[385]38        int GetSize() const {return (int)mEntries.size();}
39        bool Empty() const {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]55template <typename T>
[362]56int 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
71template <typename T>
[350]72void 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
[310]93template <typename T>
[311]94PvsData<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
103template <typename T>
[311]104void 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]115template <typename T>
116int 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*/
138class KdPvs: public Pvs<KdNode *>
[308]139{
[311]140        int Compress();
[308]141};
142
[311]143typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
[406]144typedef std::map<ViewCellKdNode *, PvsData<ViewCellKdNode *>, LtSample<ViewCellKdNode *> > ViewCellKdPvsMap;
[312]145typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ViewCellPvsMap;
146typedef PvsData<Intersectable *> ViewCellPvsData;
[311]147typedef PvsData<KdNode *> KdPvsData;
[406]148typedef Pvs<ViewCellKdNode *> ViewCellKdPvs;
[311]149typedef Pvs<Intersectable *> ViewCellPvs;
[308]150
[366]151
[177]152#endif
153
Note: See TracBrowser for help on using the repository browser.