# /usr/bin/python3 # Test program for module {raster_example} # Last edited on 2021-06-02 23:30:05 by jstolfi import raster_example import move import move_parms import path import path_hp import contact import contact_hp import job_parms import hacks import pyx import rn import sys from math import sqrt, sin, cos, floor, ceil, inf, nan, pi parms = job_parms.typical_js() parms['solid_raster_width'] = 1.00 parms['contour_trace_width'] = 0.50 mp_jump = move_parms.make_for_jumps(parms) mp_cont = move_parms.make_for_contours(parms) mp_fill = move_parms.make_for_fillings(parms) wd_fill = move_parms.width(mp_fill) wd_cont = move_parms.width(mp_cont) def show_and_plot(tag, OPHS, deco): # Plots the oriented paths in the list {OPHS} to files ending with # {tag}. If {deco} is true also prints the matter footprint and draws # axes, dots, and arrowheads on the traces. if OPHS == None: OPHS = [] assert type(OPHS) is tuple or type(OPHS) is list assert len(OPHS) >= 1 sys.stderr.write(" ... {show_and_plot} paths ...\n") path.show_list(sys.stderr, OPHS, True, 2) for oph in OPHS: path.validate(oph) dp = None nph = len(OPHS) CLRS = hacks.trace_colors(nph) # Plot the paths: rwd = 0.80 wd_axes = 0.15*min(wd_fill,wd_cont) # Width of jumps and axis lines. grid = True fname = "tests/out/raster_example_TST_" + tag path.plot_to_files(fname, OPHS, CLRS, rwd, wd_axes, grid, deco) return # ---------------------------------------------------------------------- def show_and_plot_figure_raster(tag, version, PCS,PFS,PLS): # Plot functions specific for the various raster type figure. # See {raster_example.raster_figure} for the meaning of {version,PCS,PFS,PLS}. sys.stderr.write(" ... contour paths ...\n") path.show_list(sys.stderr, PCS, True, 2) sys.stderr.write(" ... raster fill elements ...\n") path.show_list(sys.stderr, PFS, True, 2) if PLS != None and len(PLS) != 0: sys.stderr.write(" ... link paths ...\n") path.show_list(sys.stderr, PLS, True, 2) # Validate and show moves: MVS = set() for ph in PCS + PFS + PLS: path.validate(ph) for i in range(path.nelems(ph)): mvi, dri = move.unpack(path.elem(ph, i)) MVS.add(mvi) if len(MVS) != 0: sys.stderr.write(" ... moves ...\n") move.show_list(sys.stderr, tuple(MVS), 2) B = None if len(PCS) != 0: B = rn.box_join(B, path.bbox(PCS)) if len(PFS) != 0: B = rn.box_join(B, path.bbox(PFS)) if len(PLS) != 0: B = rn.box_join(B, path.bbox(PLS)) npasses = 2 if version == 0 else 1 for ps in range(npasses): # Pass 0 for all versions -- plot {PCS,PFS}. # Pass 1 only for version 0 -- plot {PCS,PLS}. dp = None c, szx, szy = hacks.make_canvas(B, dp, True, True, 1, nph) # Plot all the matter traces: jmp = False # Plot traces only. cmatter = pyx.color.rgb(0.900, 0.850, 0.800) # Material footprint color. rwd_matter = 1.13 # Relative width of material footprint. wd_matter = 0 # Extra width of material footprint. dashed = False wd_dots = 0 sz_arrows = 0 for ph in PCS + PFS + PLS: path.plot_layer(c, ph, dp, jmp, cmatter, rwd_matter, wd_matter, dashed, wd_dots, sz_arrows) if version == 0: # Plot the contour paths with a subdued color: rwd_cont = 0.80 # Relative width of trace sausages. ccont = pyx.color.rgb( 0.500, 0.600, 1.000 ) axes = True dots = True arrows = False matter = False path.plot_standard(c, PCS, dp, None, [ccont,], rwd_cont, wd_axes, axes, dots, arrows, matter) if version != 0 or ps == 0: # Plot the filling path(s): cfill = pyx.color.rgb(0.050, 0.800, 0.000) rwd_fill = 0.70 # Relative width of link trace sausages. axes = True dots = True arrows = True matter = False path.plot_standard(c, PFS, dp, None, [cfill], rwd_fill, wd_axes, axes, dots, arrows, matter) if version == 0 and ps == 1: # Plot individual links with various colors: nph = len(PLS) CLRS = hacks.trace_colors(nph) rwd_link = 0.50 # Relative width of link trace sausages. axes = True dots = True arrows = False matter = False path.plot_standard(c, PLS, dp, None, CLRS, rwd_link, wd_axes, axes, dots, arrows, matter) hacks.write_plot(c, "tests/out/raster_example_TST_" + tag + ("_v%dp%d" % (version,ps))) return # ---------------------------------------------------------------------- def test_patch_array(cols, rows, nx, ny, islands): sys.stderr.write("--- testing {patch_array} ---\n") sys.stderr.write("cols = %s\n" % cols) sys.stderr.write("rows = %s\n" % rows) sys.stderr.write("nx = %s\n" % nx) sys.stderr.write("ny = %s\n" % ny) sys.stderr.write("islands = %s\n" % islands) split = True max_lines = 5 tag = \ "patch_array" + \ ("_c%02d" % cols) + \ ("_r%02d" % rows) + \ ("_nx%02d" % nx) + \ ("_ny%02d" % nx) + \ ("_is%s" % "FT"[islands]) mp_link = mp_fill OCRS, OPHS, CTS = raster_example.patch_array(cols, rows, nx,ny, islands, mp_cont, mp_fill, mp_link) sys.stderr.write(" ... contours ...\n") ncr = len(OCRS) path.show_list(sys.stderr, OCRS, True, 2) sys.stderr.write(" ... raster fill elements ...\n") nph = len(OPHS) path.show_list(sys.stderr, OPHS, True, 2) sys.stderr.write(" ... contacts ...\n") nct = len(CTS) contact.show_list(sys.stderr, CTS, 2) # ??? Should validate output -- contours, links, contacts, etc. ??? rwd = 0.80 wd_axes = 0.05*wd_fill tics = False arrows = False clt_ct = pyx.color.rgb(1.000, 0.050, 0.000) # Contact color. clr_cr = pyx.color.rgb(0.200, 1.000, 1.000) # Contour color. CLRS = [clr_cr,]*ncr + hacks.trace_colors(nph) fname = "tests/out/raster_example_TST_" + tag contact.plot_to_files(fname, CTS, clt_ct, OCRS + OPHS, CLRS, rwd, wd_axes, tics, arrows) return # ---------------------------------------------------------------------- def test_figure_raster(): sys.stderr.write("--- testing {figure_raster} ---\n") for version in range(4): sys.stderr.write(" ... version %d ...\n" % version) tag = "figure_raster" PCS,PFS,PLS = raster_example.raster_figure(version, mp_cont, mp_fill, mp_jump) show_and_plot_figure_raster(tag, version, PCS,PFS,PLS) return # ---------------------------------------------------------------------- def test_rasters_A(): sys.stderr.write("--- testing {rasters_A} ---\n") tag = ("rasters_A") xdir = ( cos(pi/6), sin(pi/6) ) ydir = ( -xdir[1], +xdir[0] ) yphase = 0.45 eps = 0.15*wd_fill # Magnitude of perturbations. OPHS = raster_example.rasters_A(mp_fill, xdir, ydir, yphase, eps) sys.stderr.write(" ... raster fill elements ...\n") path.show_list(sys.stderr, OPHS, True, 2) sys.stderr.write(" ... moves ...\n") OMVS = [ path.elem(oph, 0) for oph in OPHS ] move.show_list(sys.stderr, OMVS, 2) deco = True show_and_plot(tag, OPHS, deco) # ---------------------------------------------------------------------- test_rasters_A() nx = 3 ny = 20 test_patch_array( 5, 2, nx, ny, True) test_patch_array( 7, 1, nx, ny, False) test_patch_array(10, 1, nx, ny, False) test_patch_array(13, 1, nx, ny, False) test_figure_raster()