示例#1
0
 def test_convert(self):
     headers = DlmtHeaders()
     headers.set_page_coordinate_system_string(
         "system cartesian right-dir + up-dir -")
     headers.set_brush_coordinate_system_string(
         "system cartesian right-dir + up-dir - origin-x 1/2 origin-y 1/2")
     headers.set_brush_page_ratio(Fraction("1/30"))
     headers.set_prefixes({
         "prefix1": "http://domain1.com",
         "prefix2": "http://domain2.com",
         "geospecies": "http://geospecies"
     })
     headers.set_text("license", "en", "Creative Commons")
     headers.set_text("license", "fr", "Creative Communs")
     headers.set_text("title", "en", "Some english title")
     headers.set_text("title", "fr", "Titre en francais")
     headers.set_url(
         "license-url", "html", "en",
         "https://creativecommons.org/licenses/by-sa/4.0/legalcode")
     headers.set_url(
         "brushes-license-url", "json", "en",
         "https://creativecommons.org/licenses/by/4.0/legalcode")
     media = DalmatianMedia(headers)
     media.add_view_string(
         "view i:1 lang en-gb xy -1/2 -1/2 width 1/1 height 1/1 flags OC tags all but [ ] -> everything"
     )
     media.add_tag_description_string(
         "tag i:1 lang en-gb same-as [ geospecies:bioclasses/P632y ] -> part of head"
     )
     media.add_tag_description_string(
         "tag i:1 lang fr-fr same-as [] -> visage")
     media.add_tag_description_string("tag i:2 lang fr same-as [] -> pied")
     media.add_brush_string(
         "brush i:1 ext-id brushes:abc3F path [ M -1/3 1/3,L 2/3 1/4,L 1/4 2/3,L -2/3 1/2 ]"
     )
     media.add_brush_string(
         "brush i:2 ext-id brushes:abc4F path [ M -1/3 1/3,L 2/3 3/4,L 1/4 2/3,L -2/3 1/2 ]"
     )
     media.add_brushstroke_string(
         "brushstroke i:1 xy 1/15 1/100 scale 1/10 angle 0/1 tags [ i:1 ]")
     media.add_brushstroke_string(
         "brushstroke i:1 xy 2/15 2/100 scale 2/10 angle 0/1 tags [ i:1 ]")
     media.add_brushstroke_string(
         "brushstroke i:1 xy 3/15 3/100 scale 3/10 angle 0/1 tags [ i:1 ]")
     media.add_brushstroke_string(
         "brushstroke i:2 xy 4/15 5/100 scale 5/10 angle 0/1 tags [ i:1, i:2 ]"
     )
     self.assertEqual(DalmatianMedia.from_obj(media.to_obj()), media)
     self.assertEqual(DalmatianMedia.from_string(str(media)), media)
     self.assertEqual(media.get_tag_ids(), set(["i:1", "i:2"]))
     self.assertEqual(media.get_brush_ids(), set(["i:1", "i:2"]))
     self.assertEqual(media.get_view_ids(), set(["i:1"]))
     self.assertEqual(media.get_used_brush_ids(), set(["i:1", "i:2"]))
     self.assertEqual(media.get_used_tag_ids(), set(["i:1", "i:2"]))
     self.assertEqual(media.get_used_short_prefixes(), set(["geospecies"]))
     self.assertEqual(media.check_references(), [])
示例#2
0
    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": ""
        }