-
Notifications
You must be signed in to change notification settings - Fork 7
/
Grid2D.h
120 lines (98 loc) · 2.75 KB
/
Grid2D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/****************************************************************************
Copyright (C) 2012 Adrian Blumer ([email protected])
Copyright (C) 2012 Pascal Spörri ([email protected])
Copyright (C) 2012 Sabina Schellenberg ([email protected])
All Rights Reserved.
You may use, distribute and modify this code under the terms of the
MIT license (http://opensource.org/licenses/MIT).
*****************************************************************************/
#ifndef GRID2D_H
#define GRID2D_H
#include "platform_includes.h"
#include <vector>
template<typename T>
class Grid2D
{
protected:
uint _width;
uint _height;
uint _size;
std::vector<T> _data;
public:
Grid2D(uint w=0, uint h=0)
: _width(w), _height(h), _size(w*h), _data(_size)
{}
uint width() const { return _width; }
uint height() const { return _height;}
uint size() const { return _size; }
void resize(uint w, uint h)
{
_width = w;
_height = h;
_size = w*h;
_data.resize(w*h);
}
T& operator ()(uint y, uint x);
const T& operator ()(uint y, uint x) const;
T& operator ()(uint i);
const T& operator ()(uint i) const;
T* ptr() { return &_data[0]; }
const T* ptr() const { return &_data[0]; }
};
template<typename T>
T &Grid2D<T>::operator ()(uint y, uint x)
{
return _data[y*_width+x];
}
template<typename T>
const T& Grid2D<T>::operator ()(uint y, uint x) const
{
return _data[y*_width+x];
}
template<typename T>
T &Grid2D<T>::operator ()(uint i)
{
return _data[i];
}
template<typename T>
const T& Grid2D<T>::operator ()(uint i) const
{
return _data[i];
}
class Grid2DHelper
{
public:
static void MakeUniformGrid( Grid2D<glm::vec2>& outGrid, uint w, uint h)
{
// create coordinate grid
outGrid.resize(w,h);
uint idx=0;
for (uint y=0; y<h; y++)
{
for (uint x=0; x<w; x++)
{
outGrid(idx) = glm::vec2(float(x)/(w-1),float(y)/(h-1));
idx++;
}
}
}
static void MakeGridIndices( std::vector<uint>& outIndices, uint w, uint h)
{
outIndices.clear();
outIndices.reserve((w-1)*(h-1)*6);
for (uint y=0; y<h-1; y++)
{
for (uint x=0; x<w-1; x++)
{
uint p00 = y*w+x;
uint p10 = p00 + 1;
uint p01 = p00 + w;
uint p11 = p10 + w;
// add the two faces
outIndices.push_back(p00); outIndices.push_back(p11); outIndices.push_back(p10);
outIndices.push_back(p00); outIndices.push_back(p01); outIndices.push_back(p11);
}
}
}
};
#endif // GRID2D_H