forked from iautom8things/uno.hpc.ljones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadjacents.c
172 lines (158 loc) · 4.85 KB
/
adjacents.c
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
#include "pib.h"
particle* getParticles(int * neighbors, int num_neighbors, cube * cubes, int * total_particles)
{
int i, j;//iterators
int totalParticles = 0;//the total number of particles in the system
int resultIndex = 0;//the index for the particle array that is returned
particle * result;//the particle array to be returned
result = (particle *)malloc(0);//initialize the array to zero elements
//go through each neighbor cube
for(i = 0; i < num_neighbors; i++)
{
cube * tempCube = &cubes[neighbors[i]];//get the first cube
totalParticles += (*tempCube).number_of_particles;//increase the total number of particles
result = (particle *)realloc(result , sizeof(particle)*totalParticles);//grow the array to hold the new particles
//add the particles from each cube to the resulting array
for(j = 0; j < (*tempCube).number_of_particles; j++ )
{
result[resultIndex] = tempCube->particles[j];
resultIndex ++;
}
}
//set the total number of particles
*total_particles = totalParticles;
return result;
}
// Written by Manuel Zubieta
unsigned char inBounds(int row, int column, int height){
// is the specefied cube (row, columne, height) on the grid?
if (row>=0 && column>=0 && height>=0 && row<SIZE && column<SIZE && height<SIZE)
return 't';
return 'f';
}
int belongs_to_cube(int x, int y, int z){
return ( (x) * SIZE) + (y) + ( (z) *(SIZE*SIZE));
}
/**
* Determine the adjacent cubes of a given cube index.
*
* Returns the number of adjacent cubes (including the given cube) and writes the list of adjacents to *adjs
*
* @param adjs
* @param index
*/
int adjacents(int *adjs, int index){
adjs[0]=index; // Whichever cube we're looking for the adjs of, should also be included in the list
int x = index%(SIZE*SIZE)/SIZE;
int y = index%SIZE;
int z = index/(SIZE*SIZE);
int n = 1;
// This code is trivial beyond this point...
// Check in every possible direction, from the given x,y,z coordinate, if
// it is 'in the box' and if so, add the corresponding index to the array of adjs
if (inBounds(x+1,y,z) == 't'){//+__
adjs[n] = (belongs_to_cube(x+1,y,z));
n++;
}
if (inBounds(x+1,y+1,z) == 't'){//++_
adjs[n] = (belongs_to_cube(x+1,y+1,z));
n++;
}
if (inBounds(x+1,y-1,z) == 't'){//+-_
adjs[n] = (belongs_to_cube(x+1,y-1,z));
n++;
}
if (inBounds(x+1,y,z+1) == 't'){//+_+
adjs[n] = (belongs_to_cube(x+1,y,z+1));
n++;
}
if (inBounds(x+1,y,z-1) == 't'){//+_-
adjs[n] = (belongs_to_cube(x+1,y,z-1));
n++;
}
if (inBounds(x-1,y,z) == 't'){//-__
adjs[n] = (belongs_to_cube(x-1,y,z));
n++;
}
if (inBounds(x-1,y+1,z) == 't'){//-+_
adjs[n] = (belongs_to_cube(x-1,y+1,z));
n++;
}
if (inBounds(x-1,y-1,z) == 't'){//--_
adjs[n] = (belongs_to_cube(x-1,y-1,z));
n++;
}
if (inBounds(x-1,y,z+1) == 't'){//-_+
adjs[n] = (belongs_to_cube(x-1,y,z+1));
n++;
}
if (inBounds(x-1,y,z-1) == 't'){//-_-
adjs[n] = (belongs_to_cube(x-1,y,z-1));
n++;
}
if (inBounds(x,y+1,z) == 't'){//_+_
adjs[n] = (belongs_to_cube(x,y+1,z));
n++;
}
if (inBounds(x,y+1,z+1) == 't'){//_++
adjs[n] = (belongs_to_cube(x,y+1,z+1));
n++;
}
if (inBounds(x,y+1,z-1) == 't'){//_+-
adjs[n] = (belongs_to_cube(x,y+1,z-1));
n++;
}
if (inBounds(x,y-1,z) == 't'){//_-_
adjs[n] = (belongs_to_cube(x,y-1,z));
n++;
}
if (inBounds(x,y-1,z+1) == 't'){//_-+
adjs[n] = (belongs_to_cube(x,y-1,z+1));
n++;
}
if (inBounds(x,y-1,z-1) == 't'){//_--
adjs[n] = (belongs_to_cube(x,y-1,z-1));
n++;
}
if (inBounds(x,y,z+1) == 't'){//__+
adjs[n] = (belongs_to_cube(x,y,z+1));
n++;
}
if (inBounds(x,y,z-1) == 't'){//__-
adjs[n] = (belongs_to_cube(x,y,z-1));
n++;
}
if (inBounds(x+1,y+1,z+1) == 't'){//+++
adjs[n] = (belongs_to_cube(x+1,y+1,z+1));
n++;
}
if (inBounds(x-1,y+1,z+1) == 't'){//-++
adjs[n] = (belongs_to_cube(x-1,y+1,z+1));
n++;
}
if (inBounds(x+1,y-1,z+1) == 't'){//+-+
adjs[n] = (belongs_to_cube(x+1,y-1,z+1));
n++;
}
if (inBounds(x+1,y+1,z-1) == 't'){//++-
adjs[n] = (belongs_to_cube(x+1,y+1,z-1));
n++;
}
if (inBounds(x-1,y+1,z-1) == 't'){//-+-
adjs[n] = (belongs_to_cube(x-1,y+1,z-1));
n++;
}
if (inBounds(x-1,y-1,z+1) == 't'){//--+
adjs[n] = (belongs_to_cube(x-1,y-1,z+1));
n++;
}
if (inBounds(x+1,y-1,z-1) == 't'){//+--
adjs[n] = (belongs_to_cube(x+1,y-1,z-1));
n++;
}
if (inBounds(x-1,y-1,z-1) == 't'){//---
adjs[n] = (belongs_to_cube(x-1,y-1,z-1));
n++;
}
return n;
}