def createSpecimen(self): stake = messup_stake(V2dList.from_dalmatian_string(choice(self.pool["stakes"]), sep=",")) spaghetti = randint(1, 2) is 1 fxWeight = FractionList.from_string(self.pool["fx-weights"]).choice() deltas = V2dList.from_dalmatian_list(self.fractionList.signed_sample_list(len(stake), 2)) tweaks = self.fractionList.signed_sample_list(len(stake), 5) points = stake + (deltas*fxWeight) product = ProductionGame(chainlength = len(points) -1) product.set_constants("ZMLCQST").set_vars("IJK") product.init_with_random_rules(levels = 2, keyrules = self.pool["rules"]) product.produce() product_obj = product.to_obj() brush = toBrush(product.core_chain(), points, fxWeight, tweaks, spaghetti) frequency_info = ", ".join(["{}:{}".format(k,v) for k, v in brush.action_frequency().items()]) ruleInfo = ", ".join([r["s"] + "->" + r["r"] for r in product_obj["rules"]]) summary = "Brush based on a stake of {} points, a weight of {} and the following rules {} starting with {} resulting in frequency {}".format(len(stake), fxWeight, ruleInfo , product_obj["start"], frequency_info) return { "id": self.incId(), "fx-weight": str(fxWeight), "stake": stake.to_dalmatian_list(), "spaghetti": spaghetti, "deltas": deltas.to_dalmatian_list(), "tweaks": tweaks, "points": points.to_dalmatian_list(), "product": product_obj, "brush": brush.to_dalmatian_string(), "brush-svg": brush.to_svg_string(BRUSH_WIDTH), "brush-cartesian": brush.to_core_cartesian_string(BRUSH_WIDTH), "summary": summary, "tags": "" }
def test_to_from_dalmatian_string(self): self.assertEqual(listABCDE.to_dalmatian_string(), "1/4 1/3 1/5 1/6 1/7 -1/9 -1/13 -1/23 1/17 4/5") self.assertEqual( V2dList.from_dalmatian_string(listABCDE.to_dalmatian_string()), listABCDE) self.assertEqual( V2dList.from_dalmatian_string(listABCDE.to_dalmatian_string(";"), ";"), listABCDE)
def test_to_dalmatian_list(self): self.assertEqual( listABCDE.to_dalmatian_list(), ["1/4 1/3", "1/5 1/6", "1/7 -1/9", "-1/13 -1/23", "1/17 4/5"]) self.assertEqual( V2dList.from_dalmatian_list(listABCDE.to_dalmatian_list()), listABCDE)
def messup_stake(stake: V2dList): if randint(1, 7) is 1: return stake newstake = stake.clone() if randint(1, 4) is 1: newstake = newstake.reverse() if randint(1, 10) is 1: newstake = newstake.mirror() length = len(stake) endslice = randint(4, length) inc = randint(1, 4) newstake = V2dList(newstake[:endslice:inc]) if len(newstake)<5: return messup_stake(stake) else: return newstake
def get_brushstokes_points(self)-> V2dList: return V2dList([bs.xy for bs in self.brushstrokes])
def mutation_specimen(self, specimen): # Create L-System product = ProductionGame.from_obj(specimen["product"]) product.produce() product_obj = product.to_obj() # Convert chain to brushstokes tortugaconfig = TortugaConfig().set_magnitude_page_ratio( self.init.magnitude_page_ratio) tortugaconfig.set_scale_magnitude_ratio_string( self.init.scale_magnitude_ratio) tortugaconfig.set_brushstoke_angle_offset( self.init.brushstoke_angle_offset) tortugaconfig.set_xy(self.init.xy) angles = mutate_fraction(specimen["angles"], self.pool.angles) magnitudes = mutate_fraction(specimen["magnitudes"], self.pool.magnitudes) tortugaconfig.set_angles_string(angles) tortugaconfig.set_magnitudes_string(magnitudes) tortugaconfig.set_brush_ids(self.init.brushids) tortugaconfig.set_chain(product.chain) brushstokes = TortugaProducer(tortugaconfig).produce() bstats = V2dList([bs.xy for bs in brushstokes]) # Create stencil aka DalmatianMedia stencil = DalmatianMedia(DlmtHeaders().set_brush_page_ratio( self.init.brush_page_ratio)) stencil.add_view_string( "view i:1 lang en xy 0 0 width 1 height 1 flags o tags all but [ ] -> everything" ) stencil.add_tag_description_string( "tag i:1 lang en same-as [] -> default") for brush in self.init.brushes: stencil.add_brush_string(brush) stencil.set_brushstrokes(brushstokes) allbr = stencil.page_brushstroke_list_for_view_string( "view i:1 lang en xy 0 0 width 1 height 1 flags o tags all but [ ] -> everything" ) fitbr = stencil.page_brushstroke_list_for_view_string( "view i:1 lang en xy 0 0 width 1 height 1 flags O tags all but [ ] -> everything" ) fitness = Fraction(len(fitbr), len(allbr)) ruleInfo = ", ".join( [r["s"] + "->" + r["r"] for r in product_obj["rules"]]) correlation = bstats.get_correlation() medianpoint = bstats.get_median_range(8) if correlation > 0.9 or correlation < -0.9: print("C", end="") return None if float(fitness) < 0.8: print("F", end="") return None if medianpoint.x < self.init.min_median_range.x: print("X", end="") return None if medianpoint.y < self.init.min_median_range.y: print("Y", end="") return None summary = "Stencil manually selected from carefully crafted generations using a Lindenmayer system approach based on angles [ {} ], magnitudes [ {} ] and the rules {} starting with {} resulting in {} brushstokes with a visibility of {:.2%}, correlation of {} and a median range of {}".format( angles, magnitudes, ruleInfo, product_obj["start"], len(brushstokes), float(fitness), correlation, medianpoint.to_float_string()) return { "id": self.inc_id(), "product": product_obj, "angles": angles, "magnitudes": magnitudes, "stencil": stencil.to_obj(), "summary": summary, "tags": "" }
def test_mirror(self): self.assertEqual(listCDE.clone(), listCDE) self.assertEqual(listCDE.reverse(), V2dList([ptE, ptD, ptC])) self.assertEqual(listCDE.mirror(), V2dList([ptC, ptD, ptE, ptE, ptD, ptC]))
import unittest from fractions import Fraction from fracgeometry import V2d, V2dList, VSegment, VPath, FractionList pt0 = V2d.from_string("0/1 0/1") ptA = V2d.from_string("1/4 1/3") ptB = V2d.from_string("1/5 1/6") ptC = V2d.from_string("1/7 -1/9") ptD = V2d.from_string("-1/13 -1/23") ptE = V2d.from_string("1/17 4/5") listABCDE = V2dList([ptA, ptB, ptC, ptD, ptE]) listCDE = V2dList([ptC, ptD, ptE]) class TestV2d(unittest.TestCase): def test_create(self): self.assertEqual(str(ptA), "1/4 1/3") def test_to_cartesian_string(self): self.assertEqual(ptA.to_cartesian_string(100), "(25.000,33.333)") def test_to_svg_string(self): self.assertEqual(ptA.to_svg_string(100, 0), "25.000 -33.333") def test_add(self): self.assertEqual(str(pt0 + ptA), "1/4 1/3") self.assertEqual(str(ptA + ptB), "9/20 1/2") def test_neg(self): self.assertEqual(str(-ptA), "-1/4 -1/3") self.assertEqual(str(ptA.neg_x()), "-1/4 1/3")