def test_is_inside_rect(self): width = Fraction("1/2") delta = V2d.from_string("1/10 1/10") self.assertEqual(ptA.is_inside_rect(ptA, width, width), True) self.assertEqual((ptA + delta).is_inside_rect(ptA, width, width), True) self.assertEqual((ptA - delta).is_inside_rect(ptA, width, width), False) self.assertEqual( (ptA + delta.neg_y()).is_inside_rect(ptA, width, width), False)
def from_string(cls, line: str): cmd, brushId, xyKey, x, y, scaleKey, scale, angleKey, angle, tagsKey, tagsInfo = line.split(" ", 10 ) assert cmd == "brushstroke", line assert xyKey == "xy", line assert scaleKey == "scale", line assert angleKey == "angle", line assert tagsKey == "tags", line return cls(brushid = brushId, xy = V2d.from_string(x + " " + y), scale = Fraction(scale), angle = Fraction(angle), tags = parse_dlmt_array(tagsInfo))
def actionToSegment(action, ptStart, ptEnd, fxWeight, tweaks) -> VSegment: # not fully sure the calculations are what we really expect oneThird = ptStart + ((ptEnd-ptStart)*Fraction("1/3")) twoThird = ptStart + ((ptEnd-ptStart)*Fraction("2/3")) halfway = ptStart + ((ptEnd-ptStart)*Fraction("1/2")) if action is "L": return VSegment.from_line_to(ptEnd) if action is "T": return VSegment.from_fluid_bezier(ptEnd) if action is "C": startCtlPt = oneThird + (V2d(tweaks[0], tweaks[1])* fxWeight) endCtlPt = twoThird + (V2d(tweaks[2], tweaks[3])* fxWeight) return VSegment.from_cubic_bezier(ptEnd, startCtlPt, endCtlPt) if action is "S": ctlPt = halfway + (V2d(tweaks[0], tweaks[1])* fxWeight) return VSegment.from_smooth_bezier(ptEnd, ctlPt) if action is "Q": ctlPt = halfway + (V2d(tweaks[0], tweaks[1])* fxWeight) return VSegment.from_quadratic_bezier(ptEnd, ctlPt)
def __init__(self): self.chain = "" self.xy = V2d.from_string("0/1 0/1") self.angles = FractionList.from_string("0/4 1/4 1/2 3/4") self.magnitudes = FractionList.from_string("1 2 3 4") self.brushids = ["i:1"] self.tags = ["i:1"] self.magnitude_page_ratio = Fraction("1/100") self.scale_magnitude_ratio = Fraction("1/1") self.brushstoke_angle_offset = Fraction(0)
def from_string(cls, line: str): other, description = line.split("->") cmd, viewId, langKey, langId, xyKey, x, y, widthKey, width, heightKey, height, flagsKey, flags, tagsKey, everything, butKey, tagsInfo = other.split(" ", 16) assert cmd == "view", line assert langKey == "lang", line assert xyKey == "xy", line assert widthKey == "width", line assert heightKey == "height", line assert flagsKey == "flags", line assert tagsKey == "tags", line assert butKey == "but", line return cls(id = viewId, xy = V2d.from_string(x + " " + y), width = Fraction(width), height = Fraction(height), lang = langId, description= description.strip(), flags = flags, everything = everything == "all", tags = parse_dlmt_array(tagsInfo) )
def __init__(self, content): self.population: int = content["population"] self.angles: int = content["angles"] self.magnitudes: int = content["magnitudes"] self.brushids: List[str] = content["brushids"] self.brushes: List[str] = content["brushes"] self.xy: V2d = V2d.from_string(content["xy"]) self.magnitude_page_ratio: Fraction = Fraction( content["magnitude-page-ratio"]) # 1/100 self.brush_page_ratio: Fraction = Fraction( content["brush-page-ratio"] ) # 1/100 better to keep consistent across stencils self.scale_magnitude_ratio: Fraction = Fraction( content["scale-magnitude-ratio"]) # 1 self.brushstoke_angle_offset: Fraction = Fraction( content["brushstoke-angle-offset"]) # 0 self.vars: str = content["vars"] # IJ self.supported_targets: str = content["supported-targets"] self.actions_ranges: str = content["actions-ranges"] self.max_chain_length: int = content["max-chain-length"] self.specimen_attempts: int = content["specimen-attempts"] self.min_median_range: V2d = V2d.from_string( content["min-median-range"])
def create_brushstroke(self): angle = self.angle_previous_vector( ) + self.sign_angle() * self.current_angle() xy_delta = V2d.from_amplitude_angle( self.current_magnitude() * self.config.magnitude_page_ratio * self.sign_magnitude(), angle) new_xy = self.xy + xy_delta self.set_position(new_xy, self.xy) scale = self.current_magnitude() * self.config.scale_magnitude_ratio brush_angle = angle + self.config.brushstoke_angle_offset tags = [] if self.current_tag() == "" else [self.current_tag()] return DlmtBrushstroke(brushid=self.current_brush(), xy=new_xy, scale=scale, angle=brush_angle, tags=tags)
def accept_point(self, xy: V2d)->bool: return xy.is_inside_rect(xy = self.xy, width = self.width, height = self.height)
def to_brush_view_box(self): return "{} {}".format(V2d(-self.brush_width/2, -self.brush_width/2).to_float_string(), V2d(self.brush_width, self.brush_width).to_float_string())
def to_page_view_box(self): return "0 0 {}".format(V2d(self.view_pixel_width, self.view_pixel_height).to_float_string())
import unittest from fractions import Fraction from fracgeometry import V2d, V2dList, VSegment, VPath, FractionList from dalmatianmedia import DlmtView, DlmtTagDescription, DlmtBrush, DlmtBrushstroke, DlmtCoordinateSystem, DlmtBrushCoordinateSystem, DlmtHeaders, DalmatianMedia, SvgRenderingConfig 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") class TestDlmtView(unittest.TestCase): def test_convert(self): line = "view i:1 lang en-gb xy 1/2 -1/3 width 1 height 1/2 flags OC tags all but [ i:1, i:2 ] -> everything" self.assertEqual(str(DlmtView.from_string(line)), line) def test_accept(self): common = "view i:1 lang en-gb xy 1/2 -1/3 width 1 height 1/2 flags OC " self.assertTrue( DlmtView.from_string(common + "tags all but [ i:1, i:2 ] ->").accept_tags( set(["i:3", "i:4"]))) self.assertFalse( DlmtView.from_string(common + "tags all but [ i:1, i:2 ] ->").accept_tags( set(["i:3", "i:2"]))) self.assertTrue( DlmtView.from_string(common + "tags none but [ i:3 ] ->").accept_tags(
def set_xy_string(self, value: str): return self.set_xy(V2d.from_string(value))
def test_rotate(self): self.assertEqual(ptA.rotate(Fraction("1/2")), -ptA) self.assertEqual(ptA.rotate(Fraction("1/4")), V2d.from_string("-1/3 1/4")) self.assertEqual(ptA.rotate(Fraction("-1/4")), V2d.from_string("1/3 -1/4"))