#! /bin/gawk -f # Last edited on 2003-04-07 04:14:21 by stolfi BEGIN { usage = ( \ "draw-grid -f r3x3.gawk \\\n" \ " -v nx=NUM -v ny=NUM [ -v scale=NUM ] \\\n" \ " < IMAGE.grmap > IMAGE.cmds" \ ); # Outputs a string of commands, suitable as the "-draw" option # of "convert", that draws a floor grid of {nx} by {ny} cm (at Z=0). # Assumes that it will be used on a copy of the image that has been # scaled by {scale}. abort = -1; np = 0; if (scale == "") { scale = 1.0; } if (nx == "") { arg_error(("must specify \"nx\"")); } if (ny == "") { arg_error(("must specify \"ny\"")); } split("", Pd); split("", Pi); nd = 0; ni = 0; nmax = 5; } (abort >= 0) { exit abort; } /^ *[-+]?[0-9][.]/{ if (NF != 3) { data_error("bad map"); } if (nd < 3) { for (j = 0; j < 3; j++) { Pd[nd,j] = $(j+1); } nd++; } else if(ni < 3) { for (j = 0; j < 3; j++) { Pi[ni,j] = $(j+1); } ni++; } else { data_error("bad map"); } } END{ if (abort >= 0) { exit abort; } if ((nd != 3) || (ni != 3)) { data_error("bad map"); } draw_grid(Pd,Pi,nx,ny); } function draw_grid(Pd,Pi,nx,ny, i,a,b,p,q,step) { split("", a); split("", b); split("", p); split("", q); for (i = 0; i <= nx; i++) { a[0] = 1.0; a[1] = 10.0*i; a[2] = 0.0; r3x3_mul_row(a, Pi, p); b[0] = 1.0; b[1] = 10.0*i; b[2] = 10.0*ny; r3x3_mul_row(b, Pi, q); printf "line %d,%d %d,%d\n", \ int(scale*p[1]/p[0]), int(scale*p[2]/p[0]), int(scale*q[1]/q[0]), int(scale*q[2]/q[0]); } for (i = 0; i <= ny; i++) { a[0] = 1; a[1] = 0; a[2] = 10*i; r3x3_mul_row(a, Pi, p); b[0] = 1; b[1] = 10*nx; b[2] = 10*i; r3x3_mul_row(b, Pi, q); printf "line %d,%d %d,%d\n", \ int(scale*p[1]/p[0]), int(scale*p[2]/p[0]), int(scale*q[1]/q[0]), int(scale*q[2]/q[0]); } } function data_error(msg) { printf "%d: **%s\n", FNR, msg > "/dev/stderr"; abort = 1; exit abort; } function arg_error(msg) { printf "**%s\n", msg > "/dev/stderr"; abort = 1; exit abort; }