# Python list operation - merge adjacent cubes into minimum number of shapes

I am hoping to find a way to make the process a lot faster. The lists are made of rows of 6 numbers, i.e. [[float] * 6, ...]. The intent is to pass a list of cube co-ordinates (only two 3D co-ordinates, which are opposite corners) and get a shorter list of co-ordinates of 3D quadrilaterals.

# Merging by creating a list with the first cube and another with the rest.
# If the cube in the first list can be merged with a cube in the second list,
# the cube in the second list is removed and the cube in the first list is
# replaced with the merged shape.
# If there is no suitable merge to be done, the lowest positioned cube in
# the second list is moved to the first list and then we check if
# anything in the second list can be merged with it and so on.

# loops until qbs has been emptied into css
# the condition for merging is matching faces
def xmerge(qbs, css):
tot = len(qbs)
j = 0
while len(qbs) > 0:
i = 0
k = 0
printProgressBar(tot - len(qbs)+1, tot, prefix = ' Merging along x:',
length = 50)
while i < len(qbs):
# first check if faces are touching
if (abs(css[j][0] - css[j][3]) == abs(css[j][0] - qbs[i][0])
and css[j][1] == qbs[i][1]
and css[j][2] == qbs[i][2]
# if true up to here then corners are touching
# below we check if the faces match (same size)
and abs(css[j][1] - css[j][4]) == abs(qbs[i][1] - qbs[i][4])
and abs(css[j][2] - css[j][5]) == abs(qbs[i][2] - qbs[i][5])):
css[j] = [css[j][0],css[j][1],css[j][2],
qbs[i][3],qbs[i][4],qbs[i][5]]
qbs = np.delete(qbs, i, axis=0)
k = 1
i += 1

if k == 0:
css = np.vstack([css, qbs[np.argmin(qbs[:,0])]])
qbs = np.delete(qbs, np.argmin(qbs[:,0]), axis=0)
j += 1

print("")
return css

• Welcome to Code Review! Please make your code complete, i.e. provide all the necessary imports (e.g. I see numpy in there) and also the code of other functions (like printProgressBar) used in the code. If you didn't write them yourself or exclude them from the review, at least provide a reference where to find them. – AlexV May 29 at 11:37
• This is all the code leading up to the above function: pastebin.com/3h9zFmXM – alex.l May 29 at 12:27
• Do the input lists have a specific order? I notice if a = np.array([[0,0,0,1,1,1]]) and b = np.array([[1,0,0,2,1,1]]), then xmerge(a, b) and xmerge(b, a) give different results, where the 2nd gave the result I expected ([0, 0, 0, 2, 1, 1]). – Sedsarq May 29 at 12:31
• There is no particular order. There is an initial lists with all the cube co-ordinates. I take a random one off the top to start the second list and search the first list for something to merge onto that and so on. xmerge(a, b) and xmerge(b, a) should return the same thing, but the list that is being added to must start with only one set of co-ordinates. – alex.l May 29 at 13:07
• Doesn't my previous comment show a bug then? Because order seems to matter. – Sedsarq May 29 at 13:10