Commit 4f264d66 authored by Adam P. Goucher's avatar Adam P. Goucher

Fixed transformations and added test

parent 5a7a4d98
......@@ -117,6 +117,11 @@ extern "C"
return new(std::nothrow) apg::pattern(ppat->shift(x, y, exponent));
}
void* TransformPattern(void* pat, const char *tfm) {
auto ppat = reinterpret_cast<apg::pattern*>(pat);
return new(std::nothrow) apg::pattern(ppat->transform(std::string(tfm), 0, 0));
}
void* FindPeriodOrAdvance(void* pat, int exponent) {
auto ppat = reinterpret_cast<apg::pattern*>(pat);
apg::pattern x = ppat->pdetect(1ull << exponent);
......
......@@ -437,8 +437,10 @@ namespace apg {
* Apply an isometry of the plane which sends (0, 0) to (x, y) with
* an optional rotation and/or reflection.
*/
int64_t nx = (perm & 64) ? x : (x + 1);
int64_t ny = (perm & 128) ? y : (y + 1);
uint8_t invperm = (1 << ((perm & 12) >> 1)) | (2 << ((perm & 48) >> 3)) | (3 << ((perm & 192) >> 5));
int64_t nx = (invperm & 64) ? x : (x + 1);
int64_t ny = (invperm & 128) ? y : (y + 1);
hypernode<I> hn = (perm == 228) ? hnode : transform_recurse(hnode, perm);
return shift_universe(hn, nx, ny, 0);
}
......
......@@ -70,6 +70,18 @@ class Pattern(object):
def __add__(self, other):
return self._boolean_immutable(other, 1)
def transform(self, tfm):
transforms = ["flip", "rot180", "identity", "transpose", "flip_x", "flip_y",
"rot90", "rot270", "swap_xy", "swap_xy_flip", "rcw", "rccw"]
if tfm not in transforms:
raise ValueError("Transformation must be one of %s" % str(transforms))
self.lifelib.TransformPattern.restype = c_void_p
newptr = c_void_p(self.lifelib.TransformPattern(self.ptr, binstring(tfm)))
return Pattern(self.session, newptr, self.owner)
def shift(self, x, y, exponent=0):
temp = self
......
......@@ -25,6 +25,13 @@ class TestGoL(unittest.TestCase):
lidka_30k = lidka[30000]
self.assertEqual(lidka_30k.population, 1623)
def test_p51(self):
part = self.lt.pattern("3b2o3b5o$3b2o3b2ob2o$8b2o2bo$2o7bobo$2o8bo$10bo2bo$11bo2bo$11bobo!", "b3s23").shift(-15, -15)
p51 = (part + part.transform('flip') + part.transform('flip_x') + part.transform('flip_y') + part.transform('rot90') + part.transform('rot270') + part.transform('swap_xy') + part.transform('swap_xy_flip'))
p51_period = p51.oscar()['period']
self.assertEqual(p51_period, 51)
def test_spacefiller(self):
sf = "20b3o3b3o$19bo2bo3bo2bo$4o18bo3bo18b4o$o3bo17bo3bo17bo3bo$o8bo12bo3bo"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment