forked from awesomescot-zz/cs184-fluid-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.cpp
478 lines (463 loc) · 25.7 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
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
#include <iostream>
#include "algebra3.h"
#include "grid.h"
/*
struct cube{
float u, v, w, temp;
vec3 x, y, z, start;
};
class grid {
public:
vec3 base, x, y, z;
int xSplit, ySplit, zSplit;
vec3 xCubeSize, yCubeSize, zCubeSize;
grid(){};
grid(int, vec3, vec3, vec3, vec3, int, int, int);
cube getCube(int, int, int);
vec3 getVelosity(vec3);
};
*/
/*
* things to do:
* 1) velocity advection. for every velocity vector trace it back wards delta T, and reset it to that velocity.
* 2) smoothing. for every cube determine its presure. if it's not zero we must change velocities so that it will reach zero.
* There are coeficients that we use to know how much we need to change the velocities by.
* 3) move particles. Particles move forward as you think they would.
*
* velocity advection(){
* for every velocity{
* calculate xyz vector for that side
* move negative xyz*delta T in that direction
* calculate the velocity at that spot
* copy that velocity to the new grid at our original location(a sides velocity vector)
*
* smoothing:
* do over and over:
* for every cube:
* calc pressure, from vel vectors coming in and out
* use coeficients from the paper to determine how much to change the velocity vectors.
*/
grid::grid(float X, float Y, float Z, int xs, int ys, int zs){
x = X;
y = Y;
z = Z;
xSplit = xs;
ySplit = ys;
zSplit = zs;
xCubeSize = x / xSplit;
yCubeSize = y / ySplit;
zCubeSize = z / zSplit;
cubeGrid = new cube** [xSplit];
for(int i=0; i<xSplit; i++){
cubeGrid[i] = new cube* [ySplit];
for(int j=0; j<ySplit; j++){
cubeGrid[i][j] = new cube[zSplit];
for(int k=0; k<zSplit; k++){
cubeGrid[i][j][k] = cube();
cubeGrid[i][j][k].u = 0;
cubeGrid[i][j][k].v = 0;
cubeGrid[i][j][k].w = 0;
cubeGrid[i][j][k].temp = 0;
}
}
}
}
cube grid::getCube(int a, int b, int c){
return cubeGrid[a][b][c];
}
void grid::print(){
printf("Print out of grid:\n");
for(int i=0; i<xSplit; i++){
for(int j=0; j<ySplit; j++){
for(int k=0; k<zSplit; k++){
printf("cubeGrid[%d][%d][%d] U = %f, V = %f, W = %f\n", i,j,k,cubeGrid[i][j][k].u,cubeGrid[i][j][k].v,cubeGrid[i][j][k].w);
}
}
}
}
cube grid::getCube(vec3 location){
int gridSpotx = location[0] / xCubeSize;
int gridSpoty = location[1] / yCubeSize;
int gridSpotz = location[2] / zCubeSize;
return cubeGrid[gridSpotx][gridSpoty][gridSpotz];
}
vec3 grid::getVelosity(vec3 location){
//check for out of bounds locations
//printf("x = %f, y = %f, z=%f",x,y,z);
if(location[0] >= x || location[0] < 0) return vec3(0,0,0);
if(location[1] >= y || location[1] < 0) return vec3(0,0,0);
if(location[2] >= z || location[2] < 0) return vec3(0,0,0);
//cout << "here" << endl;
int gridSpotx = location[0] / xCubeSize;
int gridSpoty = location[1] / yCubeSize;
int gridSpotz = location[2] / zCubeSize;
// float secondRightSideAverage, secondLeftSideAverage, secondTopSideAverage, secondBottomSideAverage, secondCloseSideAverage, secondFarSideAverage, fullFarSideAverage, firstCloseSideAverage, firstFarSideAverage, fullCloseSideAverage, firstRightSideAverage, firstLeftSideAverage, firstBottomSideAverage, firstTopSideAverage, fullRightSideAverage, fullLeftSideAverage, fullTopSideAverage, fullBottomSideAverage, ufullAverage, vfullAverage, wfullAverage;
float secondRightSideAverage = 0;
float secondLeftSideAverage = 0;
float secondTopSideAverage = 0;
float secondBottomSideAverage = 0;
float secondCloseSideAverage = 0;
float secondFarSideAverage = 0;
float fullFarSideAverage = 0;
float firstCloseSideAverage = 0;
float firstFarSideAverage = 0;
float fullCloseSideAverage = 0;
float firstRightSideAverage = 0;
float firstLeftSideAverage = 0;
float firstBottomSideAverage = 0;
float firstTopSideAverage = 0;
float fullRightSideAverage = 0;
float fullLeftSideAverage = 0;
float fullTopSideAverage = 0;
float fullBottomSideAverage = 0;
float ufullAverage = 0;
float vfullAverage = 0;
float wfullAverage = 0;
//W
float zdistBack = location[2] - (gridSpotz*zCubeSize);
float zdistForward = (gridSpotz+1)*zCubeSize - location[2];
if( (gridSpotx*xCubeSize) + .5*xCubeSize < location[0]){//if the point is over half way through the cube in the x direction
float xdistBack = location[0] - ((gridSpotx*xCubeSize) + .5*xCubeSize);
float xdistForward = (gridSpotx+1)*xCubeSize + .5*xCubeSize - location[0];
if(gridSpotx+1 >= xSplit){ //check x bounds
firstCloseSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].w;
if(gridSpotz+1 >= zSplit) firstFarSideAverage = 0; //check z bounds
else firstFarSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz+1].w;
} else {
firstCloseSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty][gridSpotz].w;
if(gridSpotz+1 >= zSplit) firstFarSideAverage = 0;
else firstFarSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz+1].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty][gridSpotz+1].w;
}
if((gridSpoty*yCubeSize) + .5*yCubeSize < location[1]){//over y half way point calculate for y+1
float ydistBack = location[1] - ((gridSpoty*yCubeSize) + .5*yCubeSize);
float ydistForward = (gridSpoty+1)*yCubeSize + .5*yCubeSize - location[1];
if(gridSpotx+1 >= xSplit){
if(gridSpoty+1 >= ySplit){
secondCloseSideAverage = 0;
secondFarSideAverage = 0;
}else{
secondCloseSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].w;
if(gridSpotz+1 >= zSplit) secondFarSideAverage = 0;
else secondFarSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz+1].w;
}
} else {
if(gridSpoty+1 < ySplit){
secondCloseSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty+1][gridSpotz].w;
if(gridSpotz+1 < zSplit) secondFarSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz+1].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty+1][gridSpotz+1].w;
else secondFarSideAverage = 0;
}else {
secondCloseSideAverage = 0;
secondFarSideAverage = 0;
}
}
fullCloseSideAverage = (ydistForward/yCubeSize)*firstCloseSideAverage + (ydistBack/yCubeSize)*secondCloseSideAverage;
fullFarSideAverage = (ydistForward/yCubeSize)*firstFarSideAverage + (ydistBack/yCubeSize)*secondFarSideAverage;
} else{//calculate for y-1
float ydistBack = location[1] - ((gridSpoty-1)*yCubeSize + .5*yCubeSize);
float ydistForward = (gridSpoty)*yCubeSize + .5*yCubeSize - location[1];
if(gridSpoty > 0){
if(gridSpotx+1 < xSplit){
secondCloseSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty-1][gridSpotz].w;
if(gridSpotz+1<zSplit) secondFarSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz+1].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty-1][gridSpotz+1].w;
else secondFarSideAverage = 0;
} else {
secondCloseSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz].w;
if(gridSpotz+1<zSplit) secondFarSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz+1].w;
else secondFarSideAverage = 0;
}
}else{
secondCloseSideAverage = 0;
secondFarSideAverage = 0;
}
fullCloseSideAverage = (ydistBack/yCubeSize)*firstCloseSideAverage + (ydistForward/yCubeSize)*secondCloseSideAverage;
fullFarSideAverage = (ydistBack/yCubeSize)*firstFarSideAverage + (ydistForward/yCubeSize)*secondFarSideAverage;
}
wfullAverage = (zdistBack/zCubeSize)*fullFarSideAverage + (zdistForward/zCubeSize)*fullCloseSideAverage;
}else{
float xdistBack = location[0] - ((gridSpotx-1)*xCubeSize + .5*xCubeSize);
float xdistForward = (gridSpotx)*xCubeSize + .5*xCubeSize - location[0];
if(gridSpotx > 0){
firstCloseSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty][gridSpotz].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].w;
if(gridSpotz+1 < zSplit) firstFarSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty][gridSpotz+1].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz+1].w;
else firstFarSideAverage = 0;
}else {
firstCloseSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].w;
if(gridSpotz+1 < zSplit) firstFarSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz+1].w;
else firstFarSideAverage = 0;
}
if((gridSpoty*yCubeSize) + .5*yCubeSize < location[1]){//over y half way point calculate for y+1
float ydistBack = location[1] - ((gridSpoty*yCubeSize) + .5*yCubeSize);
float ydistForward = (gridSpoty+1)*yCubeSize + .5*yCubeSize - location[1];
if(gridSpoty+1 < ySplit){
if(gridSpotx > 0){
secondCloseSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty+1][gridSpotz].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].w;
if(gridSpotz+1 < zSplit) secondFarSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty+1][gridSpotz+1].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz+1].w;
else secondFarSideAverage = 0;
}else {
secondCloseSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].w;
if(gridSpotz+1 < zSplit) secondFarSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz+1].w;
else secondFarSideAverage = 0;
}
}else{
secondCloseSideAverage = 0;
secondFarSideAverage = 0;
}
fullCloseSideAverage = (ydistForward/yCubeSize)*firstCloseSideAverage + (ydistBack/yCubeSize)*secondCloseSideAverage;
fullFarSideAverage = (ydistForward/yCubeSize)*firstFarSideAverage + (ydistBack/yCubeSize)*secondFarSideAverage;
} else{//calculate for y-1
float ydistBack = location[1] - ((gridSpoty-1)*yCubeSize + .5*yCubeSize);
float ydistForward = (gridSpoty)*yCubeSize + .5*yCubeSize - location[1];
if(gridSpoty > 0){
if(gridSpotx > 0){
secondCloseSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty-1][gridSpotz].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz].w;
if(gridSpotz+1 < zSplit) secondFarSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty-1][gridSpotz+1].w + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz+1].w;
else secondFarSideAverage = 0;
}else {
secondCloseSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz].w;
if(gridSpotz+1 < zSplit) secondFarSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz+1].w;
else secondFarSideAverage = 0;
}
}else{
secondCloseSideAverage = 0;
secondFarSideAverage = 0;
}
fullCloseSideAverage = (ydistBack/yCubeSize)*firstCloseSideAverage + (ydistForward/yCubeSize)*secondCloseSideAverage;
fullFarSideAverage = (ydistBack/yCubeSize)*firstFarSideAverage + (ydistForward/yCubeSize)*secondFarSideAverage;
}
wfullAverage = (zdistBack/zCubeSize)*fullFarSideAverage + (zdistForward/zCubeSize)*fullCloseSideAverage;
}
/*
* _______
* /| /|
* / | / |
* / | / |
* / | / |
* -------____|
* | / | /
* | / | /
* | / | /
* |/ |/
* -------
*/
//U
float xdistBack = location[0] - (gridSpotx*xCubeSize);
float xdistForward = (gridSpotx+1)*xCubeSize - location[0];
if( (gridSpotz*zCubeSize) + .5*zCubeSize < location[2]){//if the point is over half way through the cube in the z direction
float zdistBack = location[2] - ((gridSpotz*zCubeSize) + .5*zCubeSize);
float zdistForward = (gridSpotz+1)*zCubeSize + .5*zCubeSize - location[2];
if(gridSpoty+1 < zSplit){
firstLeftSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].u + (zdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz+1].u;
if(gridSpotx+1<xSplit) firstRightSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty][gridSpotz].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty][gridSpotz+1].u;
else firstRightSideAverage = 0;
}
if((gridSpoty*yCubeSize) + .5*yCubeSize < location[1]){//over y half way point calculate for y+1
float ydistBack = location[1] - ((gridSpoty*yCubeSize) + .5*yCubeSize);
float ydistForward = (gridSpoty+1)*yCubeSize + .5*yCubeSize - location[1];
if(gridSpoty+1<ySplit){
if(gridSpotz+1<zSplit){
secondLeftSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz+1].u;
if(gridSpotx+1<xSplit) secondRightSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty+1][gridSpotz].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty+1][gridSpotz+1].u;
else secondRightSideAverage = 0;
} else{
secondLeftSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].u;
if(gridSpotx+1<xSplit) secondRightSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty+1][gridSpotz].u;
else secondRightSideAverage = 0;
}
}else {
secondLeftSideAverage = 0;
secondRightSideAverage = 0;
}
fullLeftSideAverage = (ydistForward/yCubeSize)*firstLeftSideAverage + (ydistBack/yCubeSize)*secondLeftSideAverage;
fullRightSideAverage = (ydistForward/yCubeSize)*firstRightSideAverage + (ydistBack/yCubeSize)*secondRightSideAverage;
} else{//calculate for y-1
float ydistBack = location[1] - ((gridSpoty-1)*yCubeSize + .5*yCubeSize);
float ydistForward = (gridSpoty)*yCubeSize + .5*yCubeSize - location[1];
if(gridSpoty>0){
if(gridSpotz+1<zSplit){
secondLeftSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz+1].u;
if(gridSpotx+1<xSplit) secondRightSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty-1][gridSpotz].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty-1][gridSpotz+1].u;
else secondRightSideAverage = 0;
}else{
secondLeftSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz].u;
if(gridSpotx+1<xSplit) secondRightSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty-1][gridSpotz].u;
else secondRightSideAverage = 0;
}
}else {
secondLeftSideAverage = 0;
secondRightSideAverage = 0;
}
fullLeftSideAverage = (ydistBack/yCubeSize)*firstLeftSideAverage + (ydistForward/yCubeSize)*secondLeftSideAverage;
fullRightSideAverage = (ydistBack/yCubeSize)*firstRightSideAverage + (ydistForward/yCubeSize)*secondRightSideAverage;
}
ufullAverage = (xdistBack/xCubeSize)*fullRightSideAverage + (xdistForward/xCubeSize)*fullLeftSideAverage;
}else{
float zdistBack = location[2] - ((gridSpotz-1)*zCubeSize + .5*zCubeSize);
float zdistForward = (gridSpotz)*zCubeSize + .5*zCubeSize - location[2];
if(gridSpotx+1<xSplit){
if(gridSpotz>0){
firstLeftSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz-1].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].u;
firstRightSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty][gridSpotz-1].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty][gridSpotz].u;
}else{
firstLeftSideAverage = (zdistBack/zCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].u;
firstRightSideAverage = (zdistBack/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty][gridSpotz].u;
}
}else{
firstRightSideAverage = 0;
if(gridSpotz>0){
firstLeftSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz-1].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].u;
}else{
firstLeftSideAverage = (zdistBack/zCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].u;
}
}
if((gridSpoty*yCubeSize) + .5*yCubeSize < location[1]){//over y half way point calculate for y+1
float ydistBack = location[1] - ((gridSpoty*yCubeSize) + .5*yCubeSize);
float ydistForward = (gridSpoty+1)*yCubeSize + .5*yCubeSize - location[1];
if(gridSpoty+1<ySplit){
if(gridSpotz>0){
secondLeftSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz-1].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].u;
if(gridSpotx+1<xSplit) secondRightSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty+1][gridSpotz-1].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty+1][gridSpotz].u;
else secondRightSideAverage = 0;
}else{
secondLeftSideAverage = (zdistBack/zCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].u;
if(gridSpotx+1<xSplit) secondRightSideAverage = (zdistBack/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty+1][gridSpotz].u;
else secondRightSideAverage = 0;
}
}else{
secondLeftSideAverage = 0;
secondRightSideAverage = 0;
}
fullLeftSideAverage = (ydistForward/yCubeSize)*firstLeftSideAverage + (ydistBack/yCubeSize)*secondLeftSideAverage;
fullRightSideAverage = (ydistForward/yCubeSize)*firstRightSideAverage + (ydistBack/yCubeSize)*secondRightSideAverage;
} else{//calculate for y-1
float ydistBack = location[1] - ((gridSpoty-1)*yCubeSize + .5*yCubeSize);
float ydistForward = (gridSpoty)*yCubeSize + .5*yCubeSize - location[1];
if(gridSpoty>0){
if(gridSpotz>0){
secondLeftSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz-1].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz].u;
if(gridSpotx+1<xSplit) secondRightSideAverage = (zdistForward/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty-1][gridSpotz-1].u + (zdistBack/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty-1][gridSpotz].u;
else secondRightSideAverage = 0;
}else{
secondLeftSideAverage = (zdistBack/zCubeSize)*cubeGrid[gridSpotx][gridSpoty-1][gridSpotz].u;
if(gridSpotx+1<xSplit) secondRightSideAverage = (zdistBack/zCubeSize)*cubeGrid[gridSpotx+1][gridSpoty-1][gridSpotz].u;
else secondRightSideAverage = 0;
}
}else{
secondLeftSideAverage = 0;
secondRightSideAverage = 0;
}
fullLeftSideAverage = (ydistBack/yCubeSize)*firstLeftSideAverage + (ydistForward/yCubeSize)*secondLeftSideAverage;
fullRightSideAverage = (ydistBack/yCubeSize)*firstRightSideAverage + (ydistForward/yCubeSize)*secondRightSideAverage;
}
ufullAverage = (xdistBack/xCubeSize)*fullRightSideAverage + (xdistForward/xCubeSize)*fullLeftSideAverage;
}
//V
float ydistBack = location[1] - (gridSpoty*yCubeSize);
float ydistForward = (gridSpoty+1)*yCubeSize - location[1];
if( (gridSpotx*xCubeSize) + .5*xCubeSize < location[0]){//if the point is over half way through the cube in the x direction
float xdistBack = location[0] - ((gridSpotx*xCubeSize) + .5*xCubeSize);
float xdistForward = (gridSpotx+1)*xCubeSize + .5*xCubeSize - location[0];
if(gridSpoty+1<ySplit){
if(gridSpotx+1<xSplit){
firstBottomSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty][gridSpotz].v;
firstTopSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty+1][gridSpotz].v;
}else{
firstBottomSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].v;
firstTopSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].v;
}
}else{
firstTopSideAverage = 0;
if(gridSpotx+1<xSplit) firstBottomSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty][gridSpotz].v;
else firstBottomSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].v;
}
if((gridSpotz*zCubeSize) + .5*zCubeSize < location[2]){//over z half way point calculate for z+1
float zdistBack = location[2] - ((gridSpotz*zCubeSize) + .5*zCubeSize);
float zdistForward = (gridSpotz+1)*zCubeSize + .5*zCubeSize - location[2];
if(gridSpotz+1<zSplit){
if(gridSpotx+1<xSplit){
secondBottomSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz+1].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty][gridSpotz+1].v;
if(gridSpoty+1<ySplit) secondTopSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz+1].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty+1][gridSpotz+1].v;
else secondTopSideAverage = 0;
}else {
secondBottomSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz+1].v;
if(gridSpoty+1<ySplit) secondTopSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz+1].v;
else secondTopSideAverage = 0;
}
}else {
secondBottomSideAverage = 0;
secondTopSideAverage = 0;
}
fullBottomSideAverage = (zdistForward/zCubeSize)*firstBottomSideAverage + (zdistBack/zCubeSize)*secondBottomSideAverage;
fullTopSideAverage = (zdistForward/zCubeSize)*firstTopSideAverage + (zdistBack/zCubeSize)*secondTopSideAverage;
} else{//calculate for y-1
float zdistBack = location[2] - ((gridSpotz-1)*zCubeSize + .5*zCubeSize);
float zdistForward = (gridSpotz)*zCubeSize + .5*zCubeSize - location[2];
if(gridSpotz>0){
if(gridSpotx+1<xSplit){
secondBottomSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz-1].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty][gridSpotz-1].v;
if(gridSpoty+1<ySplit) secondTopSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz-1].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx+1][gridSpoty+1][gridSpotz-1].v;
else secondTopSideAverage = 0;
}else{
secondBottomSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz-1].v;
if(gridSpoty+1<ySplit) secondTopSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz-1].v;
else secondTopSideAverage = 0;
}
}else{
secondBottomSideAverage = 0;
secondTopSideAverage = 0;
}
fullBottomSideAverage = (zdistBack/zCubeSize)*firstBottomSideAverage + (zdistForward/zCubeSize)*secondBottomSideAverage;
fullTopSideAverage = (zdistBack/zCubeSize)*firstTopSideAverage + (zdistForward/zCubeSize)*secondTopSideAverage;
}
vfullAverage = (ydistBack/yCubeSize)*fullTopSideAverage + (ydistForward/yCubeSize)*fullBottomSideAverage;
}else{
float xdistBack = location[0] - ((gridSpotx-1)*xCubeSize + .5*xCubeSize);
float xdistForward = (gridSpotx)*xCubeSize + .5*xCubeSize - location[0];
if(gridSpotx>0){
firstBottomSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty][gridSpotz].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].v;
if(gridSpoty+1<ySplit) firstTopSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty+1][gridSpotz].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].v;
else firstTopSideAverage = 0;
}else{
firstBottomSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz].v;
if(gridSpoty+1<ySplit) firstTopSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz].v;
else firstTopSideAverage = 0;
}
if((gridSpotz*zCubeSize) + .5*zCubeSize < location[2]){//over z half way point calculate for z+1
float zdistBack = location[2] - ((gridSpotz*zCubeSize) + .5*zCubeSize);
float zdistForward = (gridSpotz+1)*zCubeSize + .5*zCubeSize - location[2];
if(gridSpotz+1<zSplit){
if(gridSpotx>0){
secondBottomSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty][gridSpotz+1].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz+1].v;
if(gridSpoty+1<ySplit) secondTopSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty+1][gridSpotz+1].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz+1].v;
else secondTopSideAverage = 0;
}else{
secondBottomSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz+1].v;
if(gridSpoty+1<ySplit) secondTopSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz+1].v;
else secondTopSideAverage = 0;
}
}
fullBottomSideAverage = (zdistForward/zCubeSize)*firstBottomSideAverage + (zdistBack/zCubeSize)*secondBottomSideAverage;
fullTopSideAverage = (zdistForward/zCubeSize)*firstTopSideAverage + (zdistBack/zCubeSize)*secondTopSideAverage;
} else{//calculate for y-1
float zdistBack = location[2] - ((gridSpotz-1)*zCubeSize + .5*zCubeSize);
float zdistForward = (gridSpotz)*zCubeSize + .5*zCubeSize - location[2];
if(gridSpotz>0){
if(gridSpotx>0){
secondBottomSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty][gridSpotz-1].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz-1].v;
if(gridSpoty+1<ySplit) secondTopSideAverage = (xdistForward/xCubeSize)*cubeGrid[gridSpotx-1][gridSpoty+1][gridSpotz-1].v + (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz-1].v;
else secondTopSideAverage = 0;
}else{
secondBottomSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty][gridSpotz-1].v;
if(gridSpoty+1<ySplit) secondTopSideAverage = (xdistBack/xCubeSize)*cubeGrid[gridSpotx][gridSpoty+1][gridSpotz-1].v;
else secondTopSideAverage = 0;
}
}else{
secondBottomSideAverage = 0;
secondTopSideAverage = 0;
}
fullBottomSideAverage = (zdistBack/zCubeSize)*firstBottomSideAverage + (zdistForward/zCubeSize)*secondBottomSideAverage;
fullTopSideAverage = (zdistBack/zCubeSize)*firstTopSideAverage + (zdistForward/zCubeSize)*secondTopSideAverage;
}
vfullAverage = (ydistBack/yCubeSize)*fullTopSideAverage + (ydistForward/yCubeSize)*fullBottomSideAverage;
}
return vec3(ufullAverage, vfullAverage, wfullAverage);
}