-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.cpp
484 lines (444 loc) · 15 KB
/
main.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#define IM_VEC2_CLASS_EXTRA
#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui.h"
#define IMAPP_IMPL
#include "ImApp.h"
#include <math.h>
#include <vector>
#include <algorithm>
#include <map>
#include "imgui_internal.h"
#define MAX_NEIGHBOURS 6
struct Hexagrid
{
void Init(int sideSize, int seed, int searchIterationCount)
{
mSearchIterationCount = searchIterationCount;
mPoints.clear();
mTriangles.clear();
mQuads.clear();
mNeighbours.clear();
if (sideSize < 2)
{
return;
}
mSideSize = sideSize;
mPoints.clear();
mTriangles.clear();
mQuads.clear();
static const float sideLength = 0.5f * tanf(3.141592f * 0.166666f * 2.f); // 0.5f * tanf(60deg)
for (int x = 0; x < (sideSize * 2 - 1) ; x++)
{
int height = (x < sideSize) ? (sideSize + x) : (sideSize * 3 - 2 - x);
float deltaHeight = sideSize - height * 0.5f;
//deltaHeight = 0.f;
for (int y = 0; y < height; y++)
{
bool isSide = x == 0 || x == (sideSize * 2 - 2) || y == 0 || y == height-1;
mPoints.push_back({ImVec2((x - sideSize + 1) * sideLength, y + deltaHeight), isSide});
}
}
int offset = 0;
for (int x = 0; x < (sideSize * 2 - 2); x++)
{
int height = (x < sideSize) ? (sideSize + x) : (sideSize * 3 - 2 - x);
if (x < sideSize - 1)
{
// left side
for (int y = 0; y < height; y++)
{
mTriangles.push_back({ offset + y, offset + y + height, offset + y + height + 1 });
if (y >= height - 1)
{
break;
}
mTriangles.push_back({ offset + y + height + 1, offset + y + 1, offset + y });
}
}
else
{
// right side
for (int y = 0; y < height - 1; y++)
{
mTriangles.push_back({ offset + y, offset + y + height, offset + y + 1 });
if (y >= height - 2)
{
break;
}
mTriangles.push_back({ offset + y + 1, offset + y + height, offset + y + height + 1 });
}
}
offset += height;
}
// triangles to quads
int triIndex;
int adjacents[3];
srand(seed);
while(1)
{
int searchCount = 0;
do
{
triIndex = rand() % mTriangles.size();
searchCount++;
}
while(searchCount < mSearchIterationCount && !mTriangles[triIndex].mValid);
if (searchCount == mSearchIterationCount)
{
break;
}
int adjacentCount = GetAdjacentTriangles(triIndex, adjacents);
if (adjacentCount > 0)
{
int i1 = triIndex;
int i2 = adjacents[0];
int indices[6] = { mTriangles[i1].mA, mTriangles[i1].mB, mTriangles[i1].mC,
mTriangles[i2].mA, mTriangles[i2].mB, mTriangles[i2].mC };
std::sort(&indices[0], &indices[6]);
int quadIndices[4];
int quadIndexCount = 1;
quadIndices[0] = indices[0];
for (int i = 1; i < 6; i++)
{
if (indices[i] != indices[i-1])
{
quadIndices[quadIndexCount++] = indices[i];
}
}
assert(quadIndexCount == 4);
mQuads.push_back({ quadIndices[0], quadIndices[2], quadIndices[3], quadIndices[1]});
mTriangles[triIndex].mValid = false;;
mTriangles[adjacents[0]].mValid = false;
}
}
std::map<uint32_t, int> middles;
// quads to 4 quads
mBaseQuadCount = (int)mQuads.size();
for (int i = 0; i < mBaseQuadCount; i++)
{
auto quad = mQuads[i];
const int* index = &quad.mA;
int indexCenter = (int)mPoints.size();
mPoints.push_back({(mPoints[quad.mA].mPosition + mPoints[quad.mB].mPosition + mPoints[quad.mC].mPosition + mPoints[quad.mD].mPosition) * 0.25f});
Subdivide<4>(index, middles, indexCenter);
}
// triangles to quads
for(const auto& triangle : mTriangles)
{
if (triangle.mValid)
{
const int* index = &triangle.mA;
int indexCenter = (int)mPoints.size();
mPoints.push_back({(mPoints[triangle.mA].mPosition + mPoints[triangle.mB].mPosition + mPoints[triangle.mC].mPosition) * 0.3333f});
Subdivide<3>(index, middles, indexCenter);
}
}
// neighbours
mNeighbours.resize(mPoints.size());
for (size_t i = mBaseQuadCount; i < mQuads.size(); i++)
{
auto quad = mQuads[i];
int* index = &quad.mA;
for (int j = 0; j < 4; j++)
{
int index1 = index[j];
int index2 = index[ (j + 1) & 3];
{
auto& neighbour = mNeighbours[index1];
// check
bool good = true;
for (int k = 0; k < neighbour.mNeighbourCount; k++)
{
if(neighbour.mNeighbour[k] == index2)
{
good=false;
break;
}
}
if (good)
{
assert(neighbour.mNeighbourCount < MAX_NEIGHBOURS);
neighbour.mNeighbour[neighbour.mNeighbourCount++] = index2;
}
}
{
auto& neighbour = mNeighbours[index2];
// check
bool good = true;
for (int k = 0; k < neighbour.mNeighbourCount; k++)
{
if(neighbour.mNeighbour[k] == index1)
{
good=false;
break;
}
}
if (good)
{
assert(neighbour.mNeighbourCount < MAX_NEIGHBOURS);
neighbour.mNeighbour[neighbour.mNeighbourCount++] = index1;
}
}
}
}
}
void Relax()
{
for (int i = 0; i < mPoints.size(); i++)
{
if (mPoints[i].mSide)
{
continue;
}
const auto& neighbour = mNeighbours[i];
ImVec2 sum(0.f, 0.f);
for (int j = 0; j < neighbour.mNeighbourCount; j++)
{
sum += mPoints[neighbour.mNeighbour[j]].mPosition;
}
sum /= (float)neighbour.mNeighbourCount;
mPoints[i].mPosition = sum;
}
}
void RelaxSide()
{
const float radius = mSideSize - 1.f;
ImVec2 center(0.f, (mSideSize * 2 - 1) * 0.5f);
for (int i = 0; i < mPoints.size(); i++)
{
if (!mPoints[i].mSide)
{
continue;
}
ImVec2 D = mPoints[i].mPosition - center;
float distance = radius - sqrtf(D.x * D.x + D.y * D.y);
mPoints[i].mPosition += (D * distance) * 0.1f;
}
}
template<int count>void Subdivide(const int* index, std::map<uint32_t, int>& middles, int indexCenter)
{
int halfSegmentIndex[count];
for (int j = 0; j < count; j++)
{
int indexA = index[j];
int indexB = index[(j + 1) % count];
uint32_t key = (ImMin(indexA, indexB) << 16) + ImMax(indexA, indexB);
auto iter = middles.find(key);
if (iter == middles.end())
{
halfSegmentIndex[j] = (int)mPoints.size();
bool isSide = mPoints[indexA].mSide && mPoints[indexB].mSide;
mPoints.push_back({(mPoints[indexA].mPosition + mPoints[indexB].mPosition) * 0.5f, isSide });
middles.insert(std::make_pair(key, halfSegmentIndex[j]));
}
else
{
halfSegmentIndex[j] = iter->second;
}
}
for (int j = 0; j < count; j++)
{
int nextIndex = (j + 1) % count;
mQuads.push_back({ indexCenter, halfSegmentIndex[j], index[nextIndex], halfSegmentIndex[nextIndex] });
}
}
int GetAdjacentTriangles(int triIndex, int* adjacents)
{
int ref[] = {mTriangles[triIndex].mA,
mTriangles[triIndex].mB,
mTriangles[triIndex].mC};
int index = 0;
for (int i = 0;i< mTriangles.size();i++)
{
if (i == triIndex || !mTriangles[i].mValid)
{
continue;
}
int local[] = { mTriangles[i].mA,
mTriangles[i].mB,
mTriangles[i].mC };
int shareCount = 0;
for(int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
if (ref[j] == local[k])
{
shareCount ++;
break;
}
}
}
assert(shareCount < 3);
if (shareCount == 2)
{
assert(index < 3);
adjacents[index++] = i;
}
}
return index;
}
ImVec2 worldToScreen(ImVec2 point)
{
ImGuiIO& io = ImGui::GetIO();
float ratio = io.DisplaySize.x / io.DisplaySize.y;
ImVec2 res = (point / (mSideSize * 3.5f));
res.y *= ratio;
res = res * io.DisplaySize;
res += ImVec2(io.DisplaySize.x / 2.f, 0.f);
return res;
}
int mSideSize = 0;
int mBaseQuadCount = 0;
int mSearchIterationCount = 0;
struct Point
{
ImVec2 mPosition;
bool mSide = false;
};
struct Triangle
{
int mA, mB, mC;
bool mValid = true;
};
struct Quad
{
int mA, mB, mC, mD;
};
struct Neighbours
{
int mNeighbourCount = 0;
int mNeighbour[MAX_NEIGHBOURS];
};
std::vector<Point> mPoints;
std::vector<Triangle> mTriangles;
std::vector<Quad> mQuads;
std::vector<Neighbours> mNeighbours;
};
Hexagrid grid;
void DrawGrid(bool drawPositions, bool drawSector)
{
ImDrawList* mDrawList;
ImGuiIO& io = ImGui::GetIO();
const ImU32 flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoBringToFrontOnFocus;
#ifdef IMGUI_HAS_VIEWPORT
ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size);
ImGui::SetNextWindowPos(ImGui::GetMainViewport()->Pos);
#else
ImGui::SetNextWindowSize(io.DisplaySize);
ImGui::SetNextWindowPos(ImVec2(0, 0));
#endif
ImGui::PushStyleColor(ImGuiCol_WindowBg, 0);
ImGui::PushStyleColor(ImGuiCol_Border, 0);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::Begin("debugDraw", NULL, flags);
mDrawList = ImGui::GetWindowDrawList();
ImGui::End();
ImGui::PopStyleVar();
ImGui::PopStyleColor(2);
const auto& points = grid.mPoints;
if (drawPositions)
{
for (auto& point : grid.mPoints)
{
mDrawList->AddCircleFilled(grid.worldToScreen(point.mPosition), 5, point.mSide?0xFF303030:0xFF404040);
}
}
if (drawSector)
{
for (auto& triangle : grid.mTriangles)
{
if (!triangle.mValid)
{
continue;
}
ImVec2 screenPoint[3] = {
grid.worldToScreen(points[triangle.mA].mPosition),
grid.worldToScreen(points[triangle.mB].mPosition),
grid.worldToScreen(points[triangle.mC].mPosition),
};
mDrawList->AddPolyline(screenPoint, 3, 0xFFA0A0A0, true, 2.f);
}
for (int i = 0; i < grid.mBaseQuadCount; i++)
{
const auto& quad = grid.mQuads[i];
ImVec2 screenPoint[4] = {
grid.worldToScreen(points[quad.mA].mPosition),
grid.worldToScreen(points[quad.mB].mPosition),
grid.worldToScreen(points[quad.mC].mPosition),
grid.worldToScreen(points[quad.mD].mPosition),
};
mDrawList->AddPolyline(screenPoint, 4, 0xFFFFFFFF, true, 2.f);
}
}
else
{
for (int i = 0; i < grid.mPoints.size(); i++)
{
const auto& neighbour = grid.mNeighbours[i];
ImVec2 ptA = grid.worldToScreen(points[i].mPosition);
for (int j = 0; j < neighbour.mNeighbourCount; j++)
{
ImVec2 ptB = grid.worldToScreen(points[neighbour.mNeighbour[j]].mPosition);
mDrawList->AddLine(ptA, ptB, 0xFFB0B0B0, 2.f);
}
}
}
}
int main(int, char**)
{
ImApp::ImApp imApp;
ImApp::Config config;
config.mWidth = 1280;
config.mHeight = 720;
//config.mFullscreen = true;
imApp.Init(config);
// Main loop
while (!imApp.Done())
{
imApp.NewFrame();
ImGuiIO& io = ImGui::GetIO();
static bool firstFrame = true;
static bool relaxIt = true;
static bool relaxSide = false;
static bool drawPositions = false;
static bool drawSector = false;
bool dirty = firstFrame;
ImGui::Begin("Parameters");
static int sideCount = 6;
static int searchIterationCount = 6;
static int seed = 1337;
dirty |= ImGui::SliderInt("Sides", &sideCount, 2, 12);
dirty |= ImGui::SliderInt("Seed", &seed, 1, 65535);
dirty |= ImGui::SliderInt("Grouping", &searchIterationCount, 1, 20);
dirty |= ImGui::Checkbox("Relax", &relaxIt);
if (relaxIt)
{
dirty |= ImGui::Checkbox("Relax Side", &relaxSide);
}
ImGui::Checkbox("Draw Positions", &drawPositions);
ImGui::Checkbox("Draw Sectors", &drawSector);
ImGui::End();
if (dirty)
{
grid.Init(sideCount, seed, searchIterationCount);
firstFrame = false;
}
if (relaxIt)
{
grid.Relax();
}
if (relaxIt&&relaxSide)
{
grid.RelaxSide();
}
DrawGrid(drawPositions, drawSector);
// render everything
glClearColor(0.48f, 0.38f, 0.4f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui::Render();
imApp.EndFrame();
}
imApp.Finish();
return 0;
}