00001
00002
00003 #ifndef __PHOTON_MAP_H
00004 #define __PHOTON_MAP_H
00005
00006 #include <esg/energy/Photon.h>
00007 #include <esg/energy/PhotonsIterator.h>
00008 #include <esg/List.h>
00009 #include <vector>
00010
00011 namespace esg {
00012
00021 class OGSCENE_EXPORT PhotonMap {
00022 friend class Iterator;
00023
00024 public:
00025 class Iterator : public PhotonsIterator {
00026 protected:
00027 const PhotonMap * _pMap;
00028 unsigned _actPhoton;
00029
00030 public:
00031 Iterator(const PhotonMap *pm)
00032 : _pMap(pm), _actPhoton(1) {}
00033
00034 virtual bool hasNext() {
00035 return (_actPhoton <= _pMap->_storedPhotons);
00036 }
00037
00038 virtual Photon* next() {
00039 if (_actPhoton > _pMap->_storedPhotons)
00040 return NULL;
00041 Photon* ret = &(_pMap->_map[_actPhoton]);
00042 _actPhoton++;
00043 return ret;
00044 }
00045 };
00046
00047
00048 protected:
00049 Photon* _map;
00050 unsigned _storedPhotons;
00051 float _bboxMin[3];
00052 float _bboxMax[3];
00053
00054 class NearestPhotons;
00055
00056 protected:
00057 void _build (const vector<Photon>&);
00058 void _median_split (Photon**, int, int, int, const int);
00059 void _balance_segment (Photon **, Photon **, int, int, int);
00060 void _locate_photons (NearestPhotons * const, unsigned) const;
00061
00062 public:
00066 PhotonMap();
00067
00073 PhotonMap(const vector<Photon>& photons);
00074
00078 virtual ~PhotonMap();
00079
00085 virtual void set(const vector<Photon>& photons);
00086
00090 virtual unsigned numPhotons (void) const;
00091
00105 virtual vector<Photon*>* getNeighbourhood(const Vector3& spoint,
00106 const Vector3* snormal,
00107 float maxdist,
00108 unsigned nphotons
00109 ) const;
00110
00123 virtual bool getIrradiance(const Vector3& spoint,
00124 const Vector3* snormal,
00125 float maxdist,
00126 unsigned nphotons,
00127 Vector3& irrad) const;
00128
00132 virtual PhotonsIterator* iterator() const {
00133 return new PhotonMap::Iterator(this);
00134 }
00135 };
00136
00137
00141 class PhotonMap::NearestPhotons {
00142 public:
00143 int max;
00144 int found;
00145 bool gotHeap;
00146 float pos[3];
00147 float *dist2;
00148 const Photon **index;
00149 const Photon *photons;
00150 unsigned halfStoredPhotons;
00151
00152 public:
00153 NearestPhotons(unsigned n, const Vector3 p, float maxDist,
00154 const Photon * pa, unsigned hsp)
00155 : max(n), found(0), gotHeap(false),
00156 photons(pa), halfStoredPhotons(hsp) {
00157 pos[0] = p.x; pos[1] = p.y; pos[2] = p.z;
00158 dist2 = new float [n+1];
00159 index = new const Photon* [n+1];
00160 dist2[0] = maxDist * maxDist;
00161 }
00162
00163 ~NearestPhotons() { delete [] dist2; delete [] index; }
00164
00165 Photon& operator ()(int i) const {
00166 return (Photon&) *(index[i+1]);
00167 }
00168
00169 int numPhotons () const { return found-1; }
00170
00171 Vector3 pointLocation() const { return Vector3(pos[0],pos[1],pos[2]); }
00172 };
00173
00174
00175 };
00176
00177 #endif // __PHOTON_MAP_H