-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridLayout.cpp
More file actions
102 lines (65 loc) · 2.52 KB
/
Copy pathGridLayout.cpp
File metadata and controls
102 lines (65 loc) · 2.52 KB
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
#pragma once
#include <array>
#include <cmath>
#include <vector>
#include <algorithm>
#include "Window.hpp"
#include "Particle.hpp"
class GridLayout {
private:
std::vector<int> next;
std::vector<int> head;
int nextIndex;
public:
int maxRows;
int maxColumns;
GridLayout(int maxRows, int maxColumns, int maxParticles)
: next(maxParticles),
head(maxRows * maxColumns),
maxRows(maxRows),
maxColumns(maxColumns),
nextIndex(0)
{
}
int getSectorFromParticle(Particle p){
int column = std::min(static_cast<int>(std::floor((std::max(p.position[0], -1.0f) + 1)/2 * maxColumns)), maxColumns-1);
int row = std::min(static_cast<int>(std::floor((std::max(p.position[1], -1.0f) + 1)/2 * maxRows)), maxRows-1);
return (row * maxColumns) + column;
}
int getNextParticleIndex(int currentParticleIndex) {
if (currentParticleIndex == -1) return -1;
return next[currentParticleIndex];
}
int getFirstParticleFromSector(int sector) {
return head[sector];
}
void mapParticlesToSectors(const std::vector<Particle>& particles){
std::fill(head.begin(), head.end(), -1);
std::fill(next.begin(), next.end(), -1);
for(int i = 0 ; i < particles.size() ; i++){
int sector = getSectorFromParticle(particles.at(i));
next[i] = head[sector];
head[sector] = i;
}
}
std::array<int, 4> getNeighborSectors(int sector){
std::array<int, 4> neighbors = {-1,-1,-1,-1};
int column = sector % maxColumns;
int row = sector / maxColumns;
// (row we want * max columns) + column we want
// myyy brainnnnnn
if(column + 1 < maxColumns){
neighbors[0] = (row * maxColumns) + column + 1;
}
if(column - 1 >= 0 && row + 1 < maxRows ){
neighbors[1] = ((row + 1) * maxColumns) + column - 1;
}
if( row + 1 < maxRows ){
neighbors[2] = ((row + 1) * maxColumns) + column;
}
if(column + 1 < maxColumns && row + 1 < maxRows ){
neighbors[3] = ((row + 1) * maxColumns) + column + 1;
}
return neighbors;
}
};