forked from MichaelBlackwell/BoxFill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxFill.py
More file actions
147 lines (109 loc) · 4.28 KB
/
Copy pathBoxFill.py
File metadata and controls
147 lines (109 loc) · 4.28 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
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
import random, sys, csv, numpy
from PIL import Image
from numba import jit, int32
import time
mils = int(round(time.time() * 1000))
M = int(sys.argv[1])
N = int(sys.argv[2])
r = int(sys.argv[3])
grid = numpy.zeros((M,N,r))
maxBoxSizeX = 5
maxBoxSizeY = 5
maxBoxSizeZ = r
#start the count
count = []
for i in range(0, r):
count.append(0)
im = Image.open('england.jpg')
pix = im.load()
# Obtain the value of a pixel every box witdth/height from the heightmap.
for Y in range(0,M):
for X in range(0,N):
#if the height of the cube is less than the height of the fillgrid, have that filled space contain a 1, otherwise 0
xpic = int(X/M*im.size[0])
ypic = int(Y/N*im.size[1])
value = pix[xpic,ypic][0]
#if the height of the cube is less than the height given by its heightmap value, have that filled space contain a 1, otherwise 0
temp = int32(value * r / 255) + 1
grid[X][Y][0:temp - 1] = 1
grid[X][Y][temp:r] = 0
#count[int(grid[X][Y])-1] += 1
# go through every size box and try to find the one that fits and has the largest volume and fits.
# return its size and location and if a box was found
@jit(nopython=True)
def custom_convolution(A,B,tb):
dimA = A.shape
dimB = B.shape
tempVolume = dimA[0] * dimA[1] * dimA[2]
# check every position inside grid[][][] where the box would fit
for containerX in range(0, dimB[0] - dimA[0] + 1):
for containerY in range(0, dimB[1] - dimA[1] + 1):
for containerZ in range(0, dimB[2] - dimA[2] + 1):
#check each spot in box and compare it to the value in filled box
# if all are 1, then the box is valid
valid = True
for insideX in range(containerX, dimA[0] + containerX):
for insideY in range(containerY, dimA[1] + containerY):
for insideZ in range(containerZ, dimA[2] + containerZ):
if(B[insideX][insideY][insideZ] != 1):
valid = False
break
break
break
# if this is the largest box, save the size and location in tb[]
if(valid and tempVolume > tb[0]):
tb[0] = tempVolume
tb[1] = containerX
tb[2] = containerY
tb[3] = containerZ
tb[4] = dimA[0]
tb[5] = dimA[1]
tb[6] = dimA[2]
tb[7] = 1
# print("largestVolume: " + str(largestVolume))
return tb
lb = numpy.zeros((8,), dtype=numpy.int)
with open('Largestcubes.csv', 'w') as csvfile:
# Format line check
csvfile.write("0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0")
csvfile.write("\n")
lb[0] = 1
while lb[0] != 0:
lb[0:7] = 0
#go through every size box possible
for tempSizeX in reversed(range(1,maxBoxSizeX)):
for tempSizeY in reversed(range(1,maxBoxSizeY)):
for tempSizeZ in reversed(range(1,maxBoxSizeZ)):
tempGrid = numpy.ones((tempSizeX,tempSizeY,tempSizeZ))
# Call the custom_convolution function using GPU speedup
lb = custom_convolution(tempGrid, grid, lb)
# Display the largest box to the console
print( "")
print( "Largest: " + str(lb[0]))
print( "X: " + str(lb[1]))
print( "Y: " + str(lb[2]))
print( "Z: " + str(lb[3]))
print( "SizeY: " + str(lb[4]))
print( "SizeX: " + str(lb[5]))
print( "SizeZ: " + str(lb[6]))
# record the largest box's 8 vertices
if lb[7] and lb[0] > 0:
csvfile.write(str(lb[0]) + ",")
csvfile.write(str(lb[1]) + "," + str(lb[2]) + "," + str(lb[3]) + ",")
csvfile.write(str(lb[1]) + "," + str(lb[2] + lb[5]) + "," + str(lb[3]) + ",")
csvfile.write(str(lb[1] + lb[4]) + "," + str(lb[2]) + "," + str(lb[3]) + ",")
csvfile.write(str(lb[1] + lb[4]) + "," + str(lb[2] + lb[5]) + "," + str(lb[3]) + ",")
csvfile.write(str(lb[1]) + "," + str(lb[2]) + "," + str(lb[3] + lb[6]) + ",")
csvfile.write(str(lb[1]) + "," + str(lb[2] + lb[5]) + "," + str(lb[3] + lb[6]) + ",")
csvfile.write(str(lb[1] + lb[4]) + "," + str(lb[2]) + "," + str(lb[3] + lb[6]) + ",")
csvfile.write(str(lb[1] + lb[4]) + "," + str(lb[2] + lb[5]) + "," + str(lb[3] + lb[6]))
csvfile.write("\n")
#update grid with empty space from largest box position
for x in range(lb[1], lb[1] + lb[4]):
for y in range(lb[2], lb[2] + lb[5]):
for z in range(lb[3], lb[3] + lb[6]):
grid[x][y][z] = 0
aftermils = int(round(time.time() * 1000))
totaltime = aftermils - mils
print ("time in seconds: ", totaltime / 1000)
# def checkValid (a, b):