SGrid.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <ionMath.h>
5 
6 
7 template <typename T>
8 struct SGrid
9 {
10  std::vector<T> Values;
12 
13  SGrid(vec2i const & dimensions = vec2i())
14  : Dimensions(dimensions), Values()
15  {
16  Allocate();
17  }
18 
19  SGrid(T const & init, vec2i const & dimensions = vec2i())
20  : Dimensions(dimensions), Values()
21  {
22  Allocate(init);
23  }
24 
25  virtual void Allocate()
26  {
27  Values.resize(Dimensions.X * Dimensions.Y);
28  }
29 
30  virtual void Allocate(T const & value)
31  {
32  Values.resize(Dimensions.X * Dimensions.Y, value);
33  }
34 
35  virtual bool const InBounds(vec2i const Index) const
36  {
37  return InBounds(Index.X, Index.Y);
38  }
39 
40  virtual bool const InBounds(s32 const X, s32 const Y) const
41  {
42  return X >= 0 && X < Dimensions.X &&
43  Y >= 0 && Y < Dimensions.Y;
44  }
45 
46  virtual T & Get(vec2i const Index)
47  {
48  return Get(Index.X, Index.Y);
49  }
50 
51  virtual T const & Get(vec2i const Index) const
52  {
53  return Get(Index.X, Index.Y);
54  }
55 
56  virtual T & Get(s32 const X, s32 const Y)
57  {
58  s32 const Index = Clamp(X, 0, Dimensions.X - 1) + Dimensions.X * Clamp(Y, 0, Dimensions.Y - 1);
59  return Values[Index];
60  }
61 
62  virtual T const & Get(s32 const X, s32 const Y) const
63  {
64  s32 const Index = Clamp(X, 0, Dimensions.X - 1) + Dimensions.X * Clamp(Y, 0, Dimensions.Y - 1);
65  return Values[Index];
66  }
67 
68  class SRowAccess
69  {
70  SGrid<T> & Volume;
71  u32 const X;
72 
73  friend struct SGrid<T>;
74 
75  SRowAccess(SGrid<T> & volume, u32 const xIndex)
76  : Volume(volume), X(xIndex)
77  {}
78 
79  public:
80 
81  T & operator[] (u32 const Y)
82  {
83  return Volume.Get(X, Y);
84  }
85 
86  };
87 
89  {
90  SGrid<T> const & Volume;
91  u32 const X;
92 
93  friend struct SGrid<T>;
94 
95  SConstRowAccess(SGrid<T> const & volume, u32 const xIndex)
96  : Volume(volume), X(xIndex)
97  {}
98 
99  public:
100 
101  T const & operator[] (u32 const Y) const
102  {
103  return Volume.Get(X, Y);
104  }
105 
106  };
107 
108  virtual SRowAccess operator[] (u32 const X)
109  {
110  return SRowAccess(* this, X);
111  }
112 
113  virtual SConstRowAccess operator[] (u32 const X) const
114  {
115  return SConstRowAccess(* this, X);
116  }
117 
118  virtual T & operator[] (vec2i const & index)
119  {
120  return Get(index.X, index.Y);
121  }
122 
123  virtual T const & operator[] (vec2i const & index) const
124  {
125  return Get(index.X, index.Y);
126  }
127 
128  typedef typename std::vector<T>::iterator iterator;
129  typedef typename std::vector<T>::const_iterator const_iterator;
130 
131  virtual iterator begin()
132  {
133  return Values.begin();
134  }
135 
136  virtual iterator end()
137  {
138  return Values.end();
139  }
140 
141  virtual const_iterator begin() const
142  {
143  return Values.begin();
144  }
145 
146  virtual const_iterator end() const
147  {
148  return Values.end();
149  }
150 };
virtual void Allocate(T const &value)
Definition: SGrid.h:30
virtual iterator end()
Definition: SGrid.h:136
T X
Definition: SVector2.h:14
virtual T const & Get(vec2i const Index) const
Definition: SGrid.h:51
virtual bool const InBounds(s32 const X, s32 const Y) const
Definition: SGrid.h:40
virtual void Allocate()
Definition: SGrid.h:25
T Y
Definition: SVector2.h:15
Definition: SGrid.h:88
Definition: SGrid.h:68
virtual T & Get(s32 const X, s32 const Y)
Definition: SGrid.h:56
std::vector< T >::const_iterator const_iterator
Definition: SGrid.h:129
Int32 s32
Definition: ionTypes.h:88
virtual const_iterator begin() const
Definition: SGrid.h:141
vec2i Dimensions
Definition: SGrid.h:11
std::vector< T > Values
Definition: SGrid.h:10
T & operator[](u32 const Y)
Definition: SGrid.h:81
virtual bool const InBounds(vec2i const Index) const
Definition: SGrid.h:35
Uint32 u32
Definition: ionTypes.h:89
Definition: SGrid.h:8
virtual T & Get(vec2i const Index)
Definition: SGrid.h:46
T Clamp(T const &v, T const &min, T const &max)
Definition: ionUtils.h:53
SGrid(vec2i const &dimensions=vec2i())
Definition: SGrid.h:13
std::vector< T >::iterator iterator
Definition: SGrid.h:128
virtual T const & Get(s32 const X, s32 const Y) const
Definition: SGrid.h:62
SGrid(T const &init, vec2i const &dimensions=vec2i())
Definition: SGrid.h:19
virtual iterator begin()
Definition: SGrid.h:131
virtual const_iterator end() const
Definition: SGrid.h:146
vec2< s32 > vec2i
Definition: SVector2.h:560