MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MDist.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*- */
2 
3 /*
4  MDist.h Support for Manhattan distance calculations
5  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20  USA
21 */
22 
30 #ifndef MDIST_H
31 #define MDIST_H
32 
33 #include "itype.h"
34 #include "Point.h"
35 #include "Random.h"
36 #include "Dirs.h"
37 
38 namespace MFM
39 {
40 
46 #define EVENT_WINDOW_SITES(radius) ((((radius)*2+1)*((radius)*2+1))/2+1)
47 
52  typedef enum
53  {
54  MANHATTAN_TABLE_RADIUS_0 = 0,
55  MANHATTAN_TABLE_RADIUS_1,
56  MANHATTAN_TABLE_RADIUS_2,
57  MANHATTAN_TABLE_RADIUS_3,
58  MANHATTAN_TABLE_RADIUS_4,
59  MANHATTAN_TABLE_SHORT = MANHATTAN_TABLE_RADIUS_2,
60  MANHATTAN_TABLE_LONG = MANHATTAN_TABLE_RADIUS_4,
61  MANHATTAN_TABLE_EVENT = MANHATTAN_TABLE_RADIUS_4
62  } TableType;
63 
68  template <u32 R>
69  class MDist
70  {
71  private:
72  MDist(const MDist &) ; // Singleton: Declare away copy ctor
73  MDist & operator=(const MDist &) ; // Don't want this either
74 
75  public:
79  static const u32 EVENT_WINDOW_DIAMETER = R*2+1;
80 
84  static MDist<R> & get();
85 
94  void FillRandomSingleDir(SPoint& pt,Random & random) const;
95 
102  u32 GetTableSize(u32 maxRadius) const;
103 
112  u32 GetFirstIndex(const u32 radius) const
113  {
114  if (radius >= sizeof(m_firstIndex)/sizeof(m_firstIndex[0]))
115  {
116  FAIL(ILLEGAL_ARGUMENT);
117  }
118  return m_firstIndex[radius];
119  }
120 
129  u32 GetLastIndex(const u32 radius) const
130  {
131  return GetFirstIndex(radius+1)-1;
132  }
133 
134  const SPoint & GetPoint(const u32 index) const
135  {
136  if (index >= ARRAY_LENGTH)
137  {
138  FAIL(ILLEGAL_ARGUMENT);
139  }
140  return m_indexToPoint[index];
141  }
142 
143  MDist();
144 
149  s32 FromPoint(const Point<s32>& offset, u32 radius) const;
150 
151  /*
152  * Fills pt with the point represented by bits.
153  * Uses a 4-bit rep if maxRadius less than 3
154  */
155  void FillFromBits(SPoint& pt, u8 bits, u32 maxRadius);
156 
157  private:
158  static const u32 ARRAY_LENGTH = EVENT_WINDOW_SITES(R);
159 
160  static inline u32 ManhattanArea(u32 maxDistance)
161  {
162  return EVENT_WINDOW_SITES(maxDistance);
163  }
164 
165  Point<s32> m_indexToPoint[ARRAY_LENGTH];
166  s32 m_pointToIndex[EVENT_WINDOW_DIAMETER][EVENT_WINDOW_DIAMETER];
167 
168  u32 m_firstIndex[R+2]; // m_firstIndex[R+1] holds 'lastIndex[R]'
169 
170  void InitEscapesByDirTable();
171  u8 m_escapesByDirection[Dirs::DIR_COUNT][ARRAY_LENGTH];
172 
173  void InitHorizonsByDirTable();
174  u8 m_horizonsByDirection[Dirs::DIR_COUNT][ARRAY_LENGTH];
175 
176  };
177 } /* namespace MFM */
178 
179 #include "MDist.tcc"
180 
181 #endif /*MDIST_H*/
u32 GetFirstIndex(const u32 radius) const
Definition: MDist.h:112
Definition: Random.h:45
u32 GetLastIndex(const u32 radius) const
Definition: MDist.h:129
void FillRandomSingleDir(SPoint &pt, Random &random) const
Definition: MDist.tcc:233
static const u32 EVENT_WINDOW_DIAMETER
Definition: MDist.h:79
s32 FromPoint(const Point< s32 > &offset, u32 radius) const
Definition: MDist.tcc:201
u32 GetTableSize(u32 maxRadius) const
Definition: MDist.tcc:187
Definition: MDist.h:69
#define EVENT_WINDOW_SITES(radius)
Definition: MDist.h:46