Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobarkoski committed Oct 9, 2024
1 parent 1936dd8 commit b67dafa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
6 changes: 3 additions & 3 deletions openage/convert/processor/export/texture_merge.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ cdef void cmerge_frames(texture, packer_type=PackerType.BINPACK, cache=None) exc
RowPacker(margin=MARGIN),
ColumnPacker(margin=MARGIN)])

packer.pack(frames)
packer.pack([(frame.width, frame.height) for frame in frames])

cdef int width = packer.width()
cdef int height = packer.height()
Expand All @@ -106,11 +106,11 @@ cdef void cmerge_frames(texture, packer_type=PackerType.BINPACK, cache=None) exc
cdef int sub_h

cdef list drawn_frames_meta = []
for sub_frame in frames:
for index, sub_frame in enumerate(frames):
sub_w = sub_frame.width
sub_h = sub_frame.height

pos_x, pos_y = packer.pos(sub_frame)
pos_x, pos_y = packer.pos(index)

spam("drawing frame %03d on atlas at %d x %d...",
len(drawn_frames_meta), pos_x, pos_y)
Expand Down
6 changes: 3 additions & 3 deletions openage/convert/service/export/png/binpack.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cdef class Packer:
cdef dict mapping

cdef void pack(self, list blocks)
cdef (unsigned int, unsigned int) pos(self, block)
cdef (unsigned int, unsigned int) pos(self, int index)
cdef unsigned int width(self)
cdef unsigned int height(self)
cdef list get_mapping_hints(self, list blocks)
Expand All @@ -22,7 +22,7 @@ cdef class BestPacker:

cdef void pack(self, list blocks)
cdef Packer best_packer(self)
cdef (unsigned int, unsigned int) pos(self, block)
cdef (unsigned int, unsigned int) pos(self, int index)
cdef unsigned int width(self)
cdef unsigned int height(self)
cdef list get_mapping_hints(self, list blocks)
Expand All @@ -38,7 +38,7 @@ cdef class BinaryTreePacker(Packer):
cdef unsigned int aspect_ratio
cdef packer_node *root

cdef void fit(self, block)
cdef void fit(self, int index, block)
cdef tuple get_packer_settings(self)
cdef packer_node *find_node(self, packer_node *root, unsigned int width, unsigned int height)
cdef packer_node *split_node(self, packer_node *node, unsigned int width, unsigned int height)
Expand Down
71 changes: 36 additions & 35 deletions openage/convert/service/export/png/binpack.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ cdef class Packer:
"""
raise NotImplementedError

cdef (unsigned int, unsigned int) pos(self, block):
return self.mapping[block]
cdef (unsigned int, unsigned int) pos(self, int index):
return self.mapping[index]

cdef unsigned int width(self):
"""
Gets the total width of the packing.
"""
return max(self.pos(block)[0] + block.width for block in self.mapping)
return max(self.pos(i)[0] + block[0] for i, block in enumerate(self.mapping.values()))

cdef unsigned int height(self):
"""
Gets the total height of the packing.
"""
return max(self.pos(block)[1] + block.height for block in self.mapping)
return max(self.pos(i)[1] + block[1] for i, block in enumerate(self.mapping.values()))

cdef tuple get_packer_settings(self):
"""
Expand All @@ -67,8 +67,9 @@ cdef class Packer:

cdef list get_mapping_hints(self, list blocks):
cdef list hints = []
for block in blocks:
hints.append(self.pos(block))
cdef int n = len(blocks)
for index in range(n):
hints.append(self.pos(index))

return hints

Expand All @@ -86,7 +87,7 @@ cdef class DeterministicPacker(Packer):
cdef int i
cdef int n = len(blocks)
for i in range(n):
self.mapping[self.blocks[i]] = self.hints[i]
self.mapping[i] = self.hints[i]


cdef class BestPacker:
Expand All @@ -107,8 +108,8 @@ cdef class BestPacker:
cdef Packer best_packer(self):
return min(self.packers, key=lambda Packer p: p.width() * p.height())

cdef (unsigned int, unsigned int) pos(self, block):
return self.current_best.pos(block)
cdef (unsigned int, unsigned int) pos(self, int index):
return self.current_best.pos(index)

cdef unsigned int width(self):
return self.current_best.width()
Expand Down Expand Up @@ -148,11 +149,11 @@ cdef class RowPacker(Packer):
for row in rows:
x = 0

for block in row:
self.mapping[block] = (x, y)
x += block.width + self.margin
for index, block in enumerate(row):
self.mapping[index] = (x, y)
x += block[0] + self.margin

y += max(block.height for block in row) + self.margin
y += max(block[1] for block in row) + self.margin


cdef class ColumnPacker(Packer):
Expand Down Expand Up @@ -180,21 +181,21 @@ cdef class ColumnPacker(Packer):
for column in columns:
y = 0

for block in column:
self.mapping[block] = (x, y)
y += block.height + self.margin
for index, block in enumerate(column):
self.mapping[index] = (x, y)
y += block[1] + self.margin

x += max(block.width for block in column) + self.margin
x += max(block[0] for block in column) + self.margin


cdef inline (unsigned int, unsigned int, unsigned int, unsigned int) maxside_heuristic(block):
"""
Heuristic: Order blocks by maximum side.
"""
return (max(block.width, block.height),
min(block.width, block.height),
block.height,
block.width)
return (max(block[0], block[1]),
min(block[0], block[1]),
block[1],
block[0])


cdef class BinaryTreePacker(Packer):
Expand All @@ -217,41 +218,41 @@ cdef class BinaryTreePacker(Packer):
self.mapping = {}
self.root = NULL

for block in sorted(blocks, key=maxside_heuristic, reverse=True):
self.fit(block)
for i, block in enumerate(sorted(blocks, key=maxside_heuristic, reverse=True)):
self.fit(i, block)

cdef (unsigned int, unsigned int) pos(self, block):
node = self.mapping[block]
cdef (unsigned int, unsigned int) pos(self, int index):
node = self.mapping[index]
return node[0], node[1]

cdef tuple get_packer_settings(self):
return (self.margin,)

cdef void fit(self, block):
cdef void fit(self, int index, block):
cdef packer_node *node
if self.root == NULL:
self.root = <packer_node *>malloc(sizeof(packer_node))
self.root.x = 0
self.root.y = 0
self.root.width = block.width + self.margin
self.root.height = block.height + self.margin
self.root.width = block[0] + self.margin
self.root.height = block[1] + self.margin
self.root.used = False
self.root.down = NULL
self.root.right = NULL

node = self.find_node(self.root,
block.width + self.margin,
block.height + self.margin)
block[0] + self.margin,
block[1] + self.margin)

if node != NULL:
node = self.split_node(node,
block.width + self.margin,
block.height + self.margin)
block[0] + self.margin,
block[1] + self.margin)
else:
node = self.grow_node(block.width + self.margin,
block.height + self.margin)
node = self.grow_node(block[0] + self.margin,
block[1] + self.margin)

self.mapping[block] = (node.x, node.y)
self.mapping[index] = (node.x, node.y)

cdef packer_node *find_node(self, packer_node *root, unsigned int width, unsigned int height):
if root.used:
Expand Down

0 comments on commit b67dafa

Please sign in to comment.