Skip to content

Commit

Permalink
convert: Use unordered_map for mapping positions to indices.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Dec 2, 2024
1 parent 40fa6ff commit 27db627
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
9 changes: 8 additions & 1 deletion openage/convert/service/export/png/binpack.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

from libcpp.memory cimport shared_ptr
from libcpp.vector cimport vector
from libcpp.unordered_map cimport unordered_map

cdef class Packer:
cdef unsigned int margin
cdef dict mapping
cdef unordered_map[unsigned int, position] mapping

cdef void pack(self, vector[sprite] blocks)
cdef (unsigned int, unsigned int) pos(self, int index)
Expand Down Expand Up @@ -60,3 +61,9 @@ cdef struct sprite:
unsigned int width
unsigned int height
unsigned int index

cdef struct position:
unsigned int x
unsigned int y
unsigned int width
unsigned int height
29 changes: 12 additions & 17 deletions openage/convert/service/export/png/binpack.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ cdef class Packer:
"""
def __init__(self, int margin):
self.margin = margin
self.mapping = {}

cdef void pack(self, vector[sprite] &blocks):
"""
Expand All @@ -45,19 +44,20 @@ cdef class Packer:
raise NotImplementedError

cdef (unsigned int, unsigned int) pos(self, int index):
return self.mapping[index]
node = self.mapping[index]
return node.x, node.y

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

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

cdef tuple get_packer_settings(self):
"""
Expand Down Expand Up @@ -127,8 +127,6 @@ cdef class RowPacker(Packer):
"""

cdef void pack(self, vector[sprite] &blocks):
self.mapping = {}

cdef unsigned int num_rows
cdef list rows

Expand All @@ -146,10 +144,10 @@ cdef class RowPacker(Packer):
x = 0

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

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


cdef class ColumnPacker(Packer):
Expand All @@ -158,8 +156,6 @@ cdef class ColumnPacker(Packer):
"""

cdef void pack(self, vector[sprite] &blocks):
self.mapping = {}

num_columns, _ = factor(len(blocks))
columns = [[] for _ in range(num_columns)]

Expand All @@ -174,10 +170,10 @@ cdef class ColumnPacker(Packer):
y = 0

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

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


cdef inline (unsigned int, unsigned int, unsigned int, unsigned int) maxside_heuristic(sprite block):
Expand Down Expand Up @@ -207,15 +203,14 @@ cdef class BinaryTreePacker(Packer):
self.root = NULL

cdef void pack(self, vector[sprite] &blocks):
self.mapping = {}
self.root = NULL

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

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

cdef tuple get_packer_settings(self):
return (self.margin,)
Expand Down Expand Up @@ -244,7 +239,7 @@ cdef class BinaryTreePacker(Packer):
node = self.grow_node(block.width + self.margin,
block.height + self.margin)

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

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

0 comments on commit 27db627

Please sign in to comment.