-
Notifications
You must be signed in to change notification settings - Fork 6
/
Grid.cpp
254 lines (227 loc) · 6.77 KB
/
Grid.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* The MIT License (MIT)
*
* Copyright (c) 2015 Giovanni Ortolani, Taneli Mikkonen, Pingjiang Li, Tommi Puolamaa, Mitra Vahida
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. */
#include "Grid.h"
#include "Common.h"
Grid::Grid(unsigned int size, const Ogre::Matrix3 face,
Ogre::Vector2 UpperLeft, Ogre::Vector2 LowerRight)
{
value = allocate2DArray<int>(size, size);
memset(value[0], 0, sizeof(int)*size*size);
gridSize = size;
orientation = face;
this->UpperLeft = UpperLeft;
this->LowerRight = LowerRight;
xplusNeighbour = NULL;
xminusNeighbour = NULL;
yplusNeighbour = NULL;
yminusNeighbour = NULL;
}
Grid::~Grid()
{
free2DArray(value);
}
unsigned int Grid::getSize()
{
return gridSize;
}
Ogre::Matrix3 Grid::getOrientation()
{
return orientation;
}
void Grid::setValue(unsigned int x, unsigned y, int val)
{
value[y][x] = val;
}
int Grid::getValue(unsigned int x, unsigned int y)
{
return value[y][x];
}
/* Return vector for a normalized (radius 1.0) sphere */
Ogre::Vector3 Grid::projectToSphere(unsigned int x, unsigned int y)
{
Ogre::Vector3 pos;
Ogre::Vector2 subtracted, xyPos, posTemp;
float mSizeFloat;
mSizeFloat = static_cast<float>(gridSize-1);
xyPos.x = static_cast<float>(x)/mSizeFloat;
xyPos.y = static_cast<float>(y)/mSizeFloat;
subtracted = this->LowerRight - this->UpperLeft;
posTemp = this->UpperLeft + xyPos*subtracted;
/* For convenience treat xy-grid as a xz-plane in sphere-coordinates which
* means that rotating with identity matrix, normal of a grid-plane points
* toward +y. */
pos.x = posTemp.x;
pos.z = posTemp.y;
pos.y = 1.0f;
// reorientate
pos = orientation*pos;
// project grid to unit sphere
pos.normalise();
return pos;
}
/* Function to set neighboring HeightMaps */
void Grid::setNeighbours(Grid *xPlus, Grid *xMinus, Grid *yPlus, Grid *yMinus)
{
xplusNeighbour = xPlus;
xminusNeighbour = xMinus;
yplusNeighbour = yPlus;
yminusNeighbour = yMinus;
}
/* Get pointer for neighboring grid by using an enumerator */
Grid *Grid::getNeighbourPtr(Grid_neighbour neighbour)
{
if (neighbour == neighbour_XP)
return xplusNeighbour;
else if (neighbour == neighbour_XM)
return xminusNeighbour;
else if (neighbour == neighbour_YP)
return yplusNeighbour;
else if (neighbour == neighbour_YM)
return yminusNeighbour;
else
return NULL;
}
/* When entry_x or entry_y, which are used to point positions on 2D-grids,
* is on grid-boundary and switch to neighboring HeightMap is desired,
* this can be used to output new values of entry_x and entry_y for referenced
* HeightMap.
* Return values:
* On success, returns new entry_x, entry_y and function return value true.
* On failure, entry_x, entry_y are not modified and function return value is false. */
bool Grid::getNeighbourEntryCoordinates(Grid_neighbour neighbour
, unsigned int &entry_x, unsigned int &entry_y)
{
Grid *gridNeighbour;
// Is entry coordinates at the edge of map
if ( !( (entry_x == this->getSize()-1) || (entry_x == 0)
|| (entry_y == this->getSize()-1) || entry_y == 0 ) )
return false;
gridNeighbour = this->getNeighbourPtr(neighbour);
if (gridNeighbour == NULL)
return false;
if (this->gridSize != gridNeighbour->getSize())
{
std::cerr << "Grid and neighbour sizes differ!" << std::endl;
return false;
}
/* Assuming both square-grids face their frontface in same direction,
* there are 16 different neighboring combinations. */
// If current HeightMap is x-minus neighbour for neighbour of interest
if (this == gridNeighbour->getNeighbourPtr(Grid::neighbour_XM))
{
// Check all 4 side-orientations for a square.
if (neighbour == neighbour_XP)
{
entry_x = 0;
}
else if (neighbour == neighbour_XM)
{
/* There is no usecase for this combination in the current code. */
std::cerr << "This should not happen! XM and Neighbour XM" << std::endl;
return false;
}
else if (neighbour == neighbour_YP)
{
entry_y = gridNeighbour->getSize() - entry_x - 1;
entry_x = 0;
}
else if (neighbour == neighbour_YM)
{
entry_y = entry_x;
entry_x = 0;
}
}
else if (this == gridNeighbour->getNeighbourPtr(Grid::neighbour_XP))
{
if (neighbour == neighbour_XP)
{
/* There is no usecase for this combination in the current code. */
std::cerr << "This should not happen! XP and Neighbour XP" << std::endl;
return false;
}
else if (neighbour == neighbour_XM)
{
entry_x = gridNeighbour->getSize()-1;
}
else if (neighbour == neighbour_YP)
{
entry_y = entry_x;
entry_x = gridNeighbour->getSize()-1;
}
else if (neighbour == neighbour_YM)
{
entry_y = gridNeighbour->getSize()-entry_x-1;
entry_x = gridNeighbour->getSize()-1;
}
}
else if (this == gridNeighbour->getNeighbourPtr(Grid::neighbour_YM))
{
if (neighbour == neighbour_XP)
{
entry_x = gridNeighbour->getSize() - entry_y -1;
entry_y = 0;
}
else if (neighbour == neighbour_XM)
{
entry_x = entry_y;
entry_y = 0;
}
else if (neighbour == neighbour_YP)
{
entry_y = 0;
}
else if (neighbour == neighbour_YM)
{
entry_x = gridNeighbour->getSize() - entry_x -1;
entry_y = 0;
}
}
else if (this == gridNeighbour->getNeighbourPtr(Grid::neighbour_YP))
{
if (neighbour == neighbour_XP)
{
entry_x = entry_y;
entry_y = gridNeighbour->getSize()-1;
}
else if (neighbour == neighbour_XM)
{
entry_x = gridNeighbour->getSize() - entry_y - 1;
entry_y = gridNeighbour->getSize() - 1;
}
else if (neighbour == neighbour_YP)
{
entry_x = gridNeighbour->getSize()-entry_x-1;
entry_y = gridNeighbour->getSize()-1;
}
else if (neighbour == neighbour_YM)
{
entry_y = gridNeighbour->getSize() - 1;
}
}
else
{
std::cerr << "Neighbouring grid does not have this Grid as a neighbour"
<< std::endl;
return false;
}
return true;
}