Mercator
RandCache.h
1 // This file may be redistributed and modified only under the terms of
2 // the GNU General Public License (See COPYING for details).
3 // Copyright (C) 2004 Damien McGinnes, Ron Steinke, Alistair Riddoch
4 
5 #ifndef MERCATOR_RANDCACHE_H
6 #define MERCATOR_RANDCACHE_H
7 
8 #include <vector>
9 #include <cstdlib>
10 #include <wfmath/MersenneTwister.h>
11 
12 // construct with something like:
13 // RandCache r(seed, new MyOrdering(args));
14 // where MyOrdering is derived from RandCache::Ordering.
15 
17 class RandCache
18 {
19  public:
21  typedef WFMath::MTRand::uint32 uint32;
23  typedef std::vector<uint32>::size_type size_type;
24 
26  struct Ordering {
27  virtual ~Ordering() {}
29  virtual size_type operator()(int x, int y) = 0;
30  };
31 
36  RandCache(uint32 seed, Ordering* o) :
37  m_rand(seed), m_ordering(o) {}
43  RandCache(uint32* seed, uint32 seed_len, Ordering* o) :
44  m_rand(seed, seed_len), m_ordering(o) {}
45  ~RandCache() {delete m_ordering;}
46 
51  double operator()(int x, int y)
52  {
53  size_type cache_order = (*m_ordering)(x, y);
54 
55  // make sure we've cached the value
56  if(cache_order >= m_cache.size()) {
57  size_type old_size = m_cache.size();
58  m_cache.resize(cache_order + 64); //do 64 at once
59  while(old_size < m_cache.size())
60  m_cache[old_size++] = m_rand.randInt();
61  }
62 
63  return double(m_cache[cache_order] * (1.0/4294967295.0));
64  }
65 
66  private:
68  WFMath::MTRand m_rand;
70  std::vector<uint32> m_cache;
73 };
74 
77 {
78 public:
80  {
81  if (x==0 && y==0) return 0;
82 
83  int d=std::max(std::abs(x), std::abs(y));
84  int min=(2*d-1)*(2*d-1);
85 
86  if (y == d) return min + 2*d - x;
87  if (x == -d) return min + 4*d - y;
88  if (y == -d) return min + 6*d + x;
89  else { //if (x == d) {
90  if (y >=0) return min + y;
91  else return min + 8*d + y;
92  }
93  }
94 };
95 
98 {
99 private:
101  int m_x;
103  int m_y;
104 public:
109  SpiralOrdering(int x, int y) : ZeroSpiralOrdering(), m_x(x), m_y(y) {}
111  {
112  return ZeroSpiralOrdering::operator()(x-m_x, y-m_y);
113  }
114 };
115 
116 
117 #endif
118 
119 
120 
A cache of random values.
Definition: RandCache.h:17
RandCache(uint32 seed, Ordering *o)
Constructor.
Definition: RandCache.h:36
RandCache(uint32 *seed, uint32 seed_len, Ordering *o)
Constructor.
Definition: RandCache.h:43
int m_x
The centre x coordinate of the spiral.
Definition: RandCache.h:101
RandCache::size_type operator()(int x, int y)
Determine the order.
Definition: RandCache.h:79
A spiral around x,y.
Definition: RandCache.h:97
A spiral around 0,0.
Definition: RandCache.h:76
WFMath::MTRand m_rand
Source random number generator.
Definition: RandCache.h:68
WFMath::MTRand::uint32 uint32
Unsigned 32bit integer.
Definition: RandCache.h:21
std::vector< uint32 >::size_type size_type
Size type of std::vector.
Definition: RandCache.h:23
int m_y
The centre y coordinate of the spiral.
Definition: RandCache.h:103
double operator()(int x, int y)
Retrieve a random value associated with parameters.
Definition: RandCache.h:51
std::vector< uint32 > m_cache
Store for the cache of values.
Definition: RandCache.h:70
Interface to define the ordering of the random number cache.
Definition: RandCache.h:26
Ordering * m_ordering
Ordering object that defines the ordering of the cache.
Definition: RandCache.h:72
virtual size_type operator()(int x, int y)=0
Determine the order.
SpiralOrdering(int x, int y)
Constructor.
Definition: RandCache.h:109