Skip to content

Commit

Permalink
New better tiling.
Browse files Browse the repository at this point in the history
  • Loading branch information
rec committed Jan 11, 2015
1 parent e392604 commit f524c21
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 41 deletions.
13 changes: 8 additions & 5 deletions code/cpp/echomesh/color/ColorList.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ cdef class ColorList:
def __getitem__(self, object key):
if isinstance(key, slice):
indices = range(*key.indices(len(self)))
cl = ColorList(columns=self.columns)
cl = ColorList()
cl.thisptr.resize(len(indices))
i = 0
for j in indices:
Expand Down Expand Up @@ -263,10 +263,13 @@ cdef class ColorList:
return super(ColorList, self).__sizeof__() + 4 * len(self)

def __str__(self):
s = '[%s]' % ', '.join(str(c) for c in self)
if self._columns:
s = '%s, columns=%d' % (s, self._columns)
return s
if not self._columns:
return '[%s]' % ', '.join(str(c) for c in self)
result = []
for c in xrange(0, len(self), self._columns):
result.append(str(self[c:c + self._columns]))
joined = ',\n '.join(result)
return '[%s]' % (('\n ' + joined) if joined else joined)

def _check_key(self, int key):
if key >= 0:
Expand Down
58 changes: 43 additions & 15 deletions code/cpp/echomesh/color/Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,55 @@
namespace echomesh {
namespace color {

FColorList tile(const FColorList& fcl, int xMult, int yMult, int columns) {
static
FColorList tile(FColorList const& fcl,
Point const& tileSize,
Point const& boardSize,
Point const& offset) {
FColorList result;
auto rows = computeRows(fcl.size(), columns);
auto newColumns = columns * xMult;
auto newRows = rows * yMult;
result.resize(newColumns * newRows);

for (auto my = 0; my < yMult; ++my) {
for (auto mx = 0; mx < xMult; ++mx) {
for (auto y = 0; y < rows; ++y) {
for (auto x = 0; x < columns; ++x) {
auto newIndex = x + mx * columns + newColumns * (y + my * rows);
auto oldIndex = x + y * columns;
result[newIndex] = fcl.get(oldIndex);
}
}
result.reserve(boardSize.first * boardSize.second);

for (auto y = 0; y < boardSize.second; ++y) {
auto tileY = ((y + offset.second) % tileSize.second) * tileSize.first;
for (auto x = 0; x < boardSize.first; ++x) {
auto tileX = (x + offset.first) % tileSize.first;
result.push_back(fcl.get(tileY + tileX));
}
}

return result;
}

static int getOffset(int centering, int tileSize, int boardSize) {
if (centering < 0)
return 0;

int offset = abs(boardSize - tileSize);
if (!centering)
offset /= 2;
return offset;
}

FColorList tile(const FColorList& fcl, int xMult, int yMult, int columns) {

auto rows = computeRows(fcl.size(), columns);
auto newColumns = columns * xMult;
auto newRows = rows * yMult;

return tile(fcl, {columns, rows}, {newColumns, newRows}, {});
}

FColorList tile_pieces(const FColorList& fcl, int tileColumns,
int boardColumns, int boardRows,
int xCenter, int yCenter) {
auto tileRows = computeRows(fcl.size(), tileColumns);
auto offsetColumn = getOffset(xCenter, tileColumns, boardColumns);
auto offsetRow = getOffset(yCenter, tileRows, boardRows);
return tile(fcl,
{tileColumns, tileRows},
{boardColumns, boardRows},
{offsetColumn, offsetRow});
}

} // namespace color
} // namespace echomesh
4 changes: 4 additions & 0 deletions code/cpp/echomesh/color/Tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ namespace color {

FColorList tile(const FColorList&, int xMult, int yMult, int columns);

FColorList tile_pieces(
const FColorList&, int columns, int newColumns, int newRows,
int xCenter, int yCenter);

} // namespace color
} // namespace echomesh
34 changes: 29 additions & 5 deletions code/cpp/echomesh/color/Tile.pyx
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
cdef enum Centering:
BEGIN = -1
MIDDLE = 0
END = 1

cdef extern from "echomesh/color/Tile.h" namespace "echomesh::color":
FColorList tile(FColorList, int xMult, int yMult, int columns)
FColorList tile_pieces(FColorList, int columns, int newColumns, int newRows,
int xCenter, int yCenter)

def tile_color_list(object fcl, int x_mult, int y_mult, int columns=0):
def tile_color_list(object fcl, int x_mult, int y_mult):
cdef ColorList source
cdef ColorList result

source = toColorList(fcl)
columns = columns or fcl.columns
if not columns:
if not fcl.columns:
return source

result = ColorList(columns=columns * x_mult)
result.thisptr.copy(tile(source.thisptr[0], x_mult, y_mult, columns))
result = ColorList(columns=fcl.columns * x_mult)
result.thisptr.copy(tile(source.thisptr[0], x_mult, y_mult, fcl.columns))
return result

def tile_colors(object fcl, int new_columns, int new_rows,
int x_center=-1, int y_center=-1):

cdef ColorList source
cdef ColorList result

source = toColorList(fcl)
if not fcl.columns:
return source

result = ColorList(columns=new_columns)
result.thisptr.copy(tile_pieces(
source.thisptr[0], fcl.columns,
new_columns, new_rows, x_center, y_center))
return result

#
12 changes: 7 additions & 5 deletions code/python/echomesh/color/ColorList_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,24 @@ def test_combine_columns(self):
columns=2)
self.cl.combine(ColorList(['yellow', 'white', 'black', 'green', 'red'],
columns=3))
self.assertResult('[yellow, white, black, yellow, magenta, black, '
'green, black, black], columns=3')
self.assertResult('[\n [yellow, white, black],\n'
' [yellow, magenta, black],\n'
' [green, black, black]]')

def test_combine_columns2(self):
self.cl = ColorList(['yellow', 'white', 'red', 'blue', 'green'],
columns=3)
self.cl.combine(ColorList(['yellow', 'white', 'red', 'green', 'coral'],
columns=2))
self.assertResult('[yellow, white, red, magenta, green, black, coral, '
'black, black], columns=3')
self.assertResult('[\n [yellow, white, red],\n'
' [magenta, green, black],\n'
' [coral, black, black]]')

def test_combine_columns4(self):
self.cl = ColorList()
self.cl.combine(ColorList(['yellow', 'white', 'red', 'green', 'coral'],
columns=2))
self.assertResult('[yellow, white, red, green, coral], columns=2')
self.assertResult('[\n [yellow, white],\n [red, green],\n [coral]]')

def test_count(self):
self.cl.extend(['green', 'red', 'blue', 'red', 'pink'])
Expand Down
4 changes: 2 additions & 2 deletions code/python/echomesh/color/Tile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

class TestTile(TestCase):
def test_empty(self):
before = ColorList()
before = ColorList(columns=1)
self.assertEqual(tile_color_list(before, 1, 1), before)
self.assertEqual(tile_color_list(before, 1, 1, 1), ColorList(columns=1))
self.assertEqual(tile_color_list(before, 1, 1), ColorList(columns=1))

def test_trivial(self):
before = ColorList(['red', 'yellow', 'green', 'blue'], columns=2)
Expand Down
1 change: 1 addition & 0 deletions code/python/echomesh/command/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__all__ = 'Alias', 'Broadcast', 'Transfer', 'Save', 'Window'
2 changes: 1 addition & 1 deletion code/python/echomesh/settings/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ execution:
autostart: false

# Handling of the visualiser windows.
close_button_quits: false
close_button_quits: true
close_button_closes_window: true

# Do we get and process commands from the keyboard?
Expand Down
22 changes: 15 additions & 7 deletions code/python/echomesh/util/command/Command.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,33 @@

class Command(Formatted):
FORMAT_MEMBERS = 'name', 'help'
DEFER_LOADING = True

def __init__(self, classpath, name, importer=importer):
self.classpath = classpath
self.name = name
self.importer = importer
self._loaded = not Command.DEFER_LOADING
self._load()

def _load(self):
mod = self.importer('%s.%s' % (self.classpath, self.name))
get = lambda key: getattr(mod, key, None)
if not self._loaded:
mod = self.importer('%s.%s' % (self.classpath, self.name))
get = lambda key: getattr(mod, key, None)

self.call = get('COMMAND') or get(self.name.lower())
assert (self.call)
self.help = get('HELP')
self._loaded = True
self.call = get('COMMAND') or get(self.name.lower())
assert (self.call)
self._help = get('HELP')
self._loaded = True

def __call__(self, *args, **kwds):
self._load()
return self.call(*args, **kwds)

def help(self):
self._load()
return self._help


class Registry(PrefixDict):
def __init__(self, name, base_path, more_paths,
Expand All @@ -50,4 +58,4 @@ def __init__(self, name, base_path, more_paths,
self.help_table.update(getattr(module, 'HELP', {}))

def help(self, key):
return self.help_table.get(key) or getattr(self[key], 'HELP', '')
return self.help_table.get(key) or self[key].help()
2 changes: 1 addition & 1 deletion code/python/echomesh/util/command/Command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_simple(self):
registry = Command.Registry('test', 'echomesh.util.command.test', '')
foo = registry['f']
self.assertEquals(foo(), 'foo')
self.assertEquals(registry.help('foo'), '')
self.assertEquals(registry.help('foo'), 'Some help text')

def test_two(self):
registry = Command.Registry('test', 'echomesh.util.command.test',
Expand Down

0 comments on commit f524c21

Please sign in to comment.