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