flag=0 proc err () { xpanel("**** ERROR ****") xlabel($s1) xpanel() } strdef tstr if (unix_mac_pc() == 3) { chdir(neuronhome()) if (chdir("fctb")==0) flag=1 if (chdir("..\fctb")==0) flag=1 if (flag==0) { sprint(tstr,"ERROR: FCTB directory not found; should be in %s or %s/..\n",neuronhome(),neuronhome()) err(tstr) } else if (default_dll_loaded_== 0) { flag = nrn_load_dll("nrnmech.dll") if (flag==1) { default_dll_loaded_ = 1 } else { sprint(tstr,"ERROR loading %s/nrnmech.dll; remake with mknrndll.\n",getcwd()) err(tstr) } } } objref helpbox, helpdeck proc help_list() { helpbox=new HBox(2) helpbox.intercept(1) xpanel("") xbutton("General","helpdeck.flip_to(0)") xbutton("GUI -- left panel (picture size)","helpdeck.flip_to(1)") xbutton("GUI -- middle panel (drawing tools)","helpdeck.flip_to(2)") xbutton("GUI -- right panel (Input/Output)","helpdeck.flip_to(3)") xbutton("Vector language:","helpdeck.flip_to(4)") xbutton("File format","helpdeck.flip_to(5)") xbutton("Exercises","helpdeck.flip_to(6)") xpanel() helpdeck=new Deck() helpdeck.intercept(1) xpanel("") xlabel("From Fig. 4-2. Model uses a simple vector language to draw a picture of a") xlabel("baby. The drawing tools try to be pretty much like those of a real") xlabel("drawing program. However, the poor resolution of the picture (large") xlabel("pixels) makes things like circles come out pretty lumpy.") xlabel("") xpanel() xpanel("") xlabel("Reset: Reread original picture (baby)") xlabel("Width: Width of the bitmap") xlabel("Height: Height of the bitmap") xlabel("") xpanel() xpanel("") xlabel("Clear: set all bits to zero") xlabel("Random flips: take N pseudo-random locations and switch 0->1 or 1->0") xlabel(" used to add noise to a picture (enter value for N in box)") xlabel("") xlabel("Buttons: (choose one for drawing)") xlabel("Draw: freehand drawing -- can hold down the mouse and draw graceful") xlabel(" curves (slowly since program can't keep up);") xlabel(" sets individual bits to one") xlabel("Eraser: freehand erase; sets individual bits to zero") xlabel("Circle: mouse down for center, drag and release to set radius (only") xlabel(" draws after release)") xlabel("Line: mouse down to begin line, drag and release to draw line (only") xlabel(" draws after release)") xlabel("Rectangle: mouse down for corner, drag and release to opposite corner ") xlabel(" (only draws after release)") xlabel("Filled Rectangle: mouse down for corner, drag and release to opposite ") xlabel(" corner (only draws after release)") xlabel("") xlabel("Input location: location in the input vector to enter values as a") xlabel(" string or number (value must be from 0 to height*width-1)") xlabel("Input representation (menu): enter numbers to be copied onto bit map") xlabel(" at location \"Input location\"") xlabel(" eg: 01010101 (binary) or 55 (hex) at location 0 will give the") xlabel(" beginning of a checkerboard") xlabel("") xpanel() xpanel("") xlabel("Read from file: read a vector program or bitmap from a file") xlabel("Output to file (check box) when checked creates or overwrites file") xlabel("Append to file (check box) when checked adds to existing file") xlabel("Binary: save as zeros and ones") xlabel("Octal: save in octal (3 bits at a time)") xlabel("Hexadecimal: save in base 16 (4 bits at a time)") xlabel("6-bit padded ascii: save 6 bits at a time and make ascii by left") xlabel(" padding with a one") xlabel("7-bit ascii: save 7 bits at a time and write as ascii") xlabel("8-bit ascii: save 8 bits at a time, lower 7 bits are ascii and first") xlabel(" bit is assumed to be zero (bit will be lost if it's a one)") xlabel("") xpanel() xpanel("") xlabel("When entered by hand in a file the following commands are") xlabel("recognized: ") xlabel(" 1.setbit, 2.clrbit, 3.circ, 4.line, 5.rect, 6.frect") xlabel("These correspond to the following GUI buttons:") xlabel(" 1.Draw, 2.Eraser, 3.Circle, 4.Line, 5.Rectangle, 6.Filled Rectangle") xlabel("") xlabel("setbit and clrbit each take two arguments") xlabel(" numbers giving x and y coordinates of the location to set or clear ") xlabel(" x coordinate a number between 0 and width-1") xlabel(" y coordinate a number between 0 and height-1") xlabel("line, rect, frect each take four arguments: x0,y0,x1,y1") xlabel(" coordinates for the beginning (x0,y0) and end (x1,y1) of the line") xlabel(" or corners of the rectangle") xlabel("circ takes 3 coordinates: x0,y0,radius") xlabel(" a center (x0,y0) and a radius") xlabel("") xlabel("As an example here is the file for the baby picture; note that") xlabel("comments are preceded by '//.' The first line is an obligatory") xlabel("comment defining picture size and file type.") xlabel("") xlabel("// 32 32 -1") xlabel("// picture of a baby") xlabel("circ 15 23 6 // head: circle set mid-x (15/32) and high-y (23/32) with radius 6") xlabel("circ 15 10 6 // body: circle set mid-x (15/32) and low-y (6/32) with radius 6") xlabel("frect 12 15 17 17 // neck: filled rectangle connects the two circles") xlabel("line 4 1 11 4 // leg at lower left of picture (right leg)") xlabel("line 19 4 25 2 // leg at lower right of picture (left leg)") xlabel("setbit 13 24 // an eye") xlabel("setbit 17 24 // other eye") xlabel("line 3 10 9 13 // arm on left side (right arm)") xlabel("line 20 13 27 11 // arm on right side (left arm)") xlabel("line 13 20 16 20 // mouth") xlabel("") xpanel() xpanel("") xlabel("This program reads the same bitmap files as in the previous figure") xlabel("but also reads vector language files. For vector language the") xlabel("header gives") xlabel(" // WIDTH HEIGHT -1") xlabel("where the '-1' indicates that it will be a vector file. Vector file") xlabel("commands are given at the \"Vector language\" help button.") xlabel("") xlabel("The header (first line) for a bitmap file gives the image size and encoding as") xlabel("follows: ") xlabel(" // WIDTH HEIGHT CODE_SIZE") xlabel("") xlabel("All 3 values are integers. The CODE_SIZE tells whether the encoding") xlabel("is taken to be 8-bit ascii (8), 7-bit ascii (7), 6-bit padded ascii") xlabel("(6), hexadecimal (4), octal (3) or binary (1). Note that CODE_SIZE") xlabel("gives the number of bits needed to form a single character in that") xlabel("representation.") xlabel("") xlabel("The encoded bit-map follows the header. If the bits provided cannot") xlabel("fit within WIDTH x HEIGHT, they will be truncated. If they do not") xlabel("fill WIDTH x HEIGHT, they will be padded with zeros.") xlabel("") xlabel("Note that saving to 7-bit or 8-bit ascii will cause problems on some") xlabel("machines since may be saving non-printable characters. 6-bit ascii") xlabel("solves the problem by using almost only printable characters. See") xlabel("ASCII table below -- 6-bit ascii left pads with 01 to give") xlabel("8-bit ascii characters from '@' (01000000) to 'DELETE' (01111111).") xlabel("The latter is the only non-printable character. This sequence") xlabel("includes A-Z and a-z. ") xlabel("") xpanel() xpanel("") xlabel("1. Change the dimensions of the picture to reproduce the panels in") xlabel("the original figure. What happens when you make the picture smaller") xlabel("instead of bigger?") xlabel("") xlabel("2. Try printing out the picture in a variety of output formats.") xlabel("Note that the bitmap is very sparse -- most of the output values are") xlabel("zeros. ") xlabel("") xlabel("3. Draw and distort pictures of your loved ones. Save a few such") xlabel("pictures at low resolution (8x8) for later use in a memory network.") xlabel("") xlabel("4. Try writing programs in the vector language. Load in the program") xlabel("and see if the result was what you expected.") xlabel("") xlabel("5. If you read something in as a bitmap, you can't write it out in") xlabel("vector format. Sketch out a program that would allow this. ") xlabel("This is a \"shape from pixels\" problem that the brain solves") xlabel("using many additional clues. What clues are these?") xpanel() helpdeck.intercept(0) helpdeck.map() helpdeck.flip_to(0) helpbox.intercept(0) helpbox.map() } // Created 07/24/09 17:50:29 by "/usr/site/scripts/loadfiles -q -a REP_baby.hoc" //================================================================ // INSERTED REP_baby.hoc //* =Id= REP_baby.hoc,v 1.5 2002/09/09 15:07:02 billl Exp // load_file("REP_baby_.hoc") //================================================================ // INSERTED string2.hoc // =Id= string2.hoc,v 1.1 2002/09/07 12:55:06 billl Exp //*String2 template begintemplate String2 public s,t strdef s,t proc init() { if (numarg() == 1) { s=$s1 } if (numarg() == 2) { s=$s1 t=$s2 } } endtemplate String2 // END string2.hoc //================================================================ //================================================================ // INSERTED bkutils.hoc // =Id= bkutils.hoc,v 1.73 2004/04/30 15:35:32 billl Exp // load_file("stdgui.hoc") // load_file("setup.hoc") load_file("stdrun.hoc") if (name_declared("install_matrix")) execute("install_matrix()") if (name_declared("install_vecst")) execute("install_vecst()") //* objects objref g[4], stim[2], nc, gp, sns, ind, vec0, vec[2], rdm[2], XO, YO, cvode objref st,wt,scr,smat,box[4],insr,nil,sref,tmpfile,tmpobj,tmpvec,tmplist,sfunc,deck strdef mesg,tstr,tstr2,temp_string_,filename,section tmpfile = new File() tmplist = new List() fchooser_flag = 0 graph_flag = 1 double x[4],y[4] rdm = new Random() cvode = new CVode() sfunc = new StringFunctions() { ind=new Vector() vec[0]=ind.c vec[1]=ind.c vec0=ind.c} //* iterators and templates //** string iterator iterator case() { local i i1 = 0 for i = 2, numarg() { $&1 = $i iterator_statement i1+=1 } } iterator scase() { local i i1 = 0 for i = 1, numarg() { temp_string_ = $si iterator_statement i1+=1 } } // eg for scase2("a","b","c","d","e","f") print temp_string_,temp_string2_ iterator scase2() { local i i1 = 0 if (numarg()%2==1) {print "ERROR: scase2 needs even number of args" return } for i = 1, numarg() { tmpobj=new String2() tmpobj.s=$si i+=1 tmpobj.t=$si iterator_statement i1+=1 } } // like perl chop -- removes the last character proc chop () { sfunc.left($s1,sfunc.len($s1)-1) } //** list iterator ltr // usage 'for ltr(XO, tmplist) { print XO }' iterator ltr() { local i if (numarg()==3) {$&3=0} else {i1 = 0} for i = 0, $o2.count() - 1 { $o1 = $o2.object(i) iterator_statement if (numarg()==3) { $&3+=1 } else { i1+=1 } } $o1 = nil } //** list pairwise iterator ltrp // usage 'for ltrp(XO, YO, list) { print XO,YO }' takes them pairwise iterator ltrp() { local i if (numarg()==4) {$&4=0} else {i1 = 0} for (i=0;i<$o3.count()-1;i+=2) { $o1 = $o3.object(i) $o2 = $o3.object(i+1) iterator_statement if (numarg()==4) { $&4+=1 } else { i1+=1 } } $o1=nil $o2=nil } //** vector iterator vtr // usage 'for vtr(&x, vec) { print x }' iterator vtr() { local i if (numarg()==3) {$&3=0} else {i1 = 0} for i = 0, $o2.size() - 1 { $&1 = $o2.x[i] iterator_statement if (numarg()==3) { $&3+=1 } else { i1+=1 } } } //* vlk(vec) -- display vector slice -- more flexible than vec.printf // vlk(vec,max) // vlk(vec,min,max) // prints out a segment of a vector vlk_width=20 proc vlk () { local i,j,min,max,dual,wdh,nonl,nl j=dual=0 nl=1 wdh=vlk_width if (numarg()==1) { min=0 max=$o1.size-1 } if (numarg()==2) if ($2==0) { nl=min=0 max=$o1.size-1 // vlk(vec,0) flag to suppress new lines } else if ($2>0) { min=0 max=$2-1 } else { min=$o1.size+$2 max=$o1.size-1 } if (numarg()==3) if ($3>-1) { min=$2 max=$3 } else { min=0 max=$o1.size-1 dual=1 } if (numarg()==4) { min=$3 max=$4 dual=1 } if (min<0) min=0 if (max>$o1.size-1) max=$o1.size-1 if (dual) if (max>$o2.size-1) max=$o2.size-1 for i=min,max { if (dual) printf("%g:%g ",$o1.x[i],$o2.x[i]) else printf("%g ",$o1.x[i]) if ((j=j+1)%vlk_width==0 && nl) { print "" } } if (nl) print "" } //** savevec([list,]vec1[,vec2,...]) add vector onto veclist or other list if given as 1st arg objref veclist veclist = new List() proc savevec () { local i,flag,beg if (isobj($o1,"List")) beg=2 else beg=1 for i=beg, numarg() { tmpvec = new Vector($oi.size) tmpvec.copy($oi) if (beg==2) $o1.append(tmpvec) else veclist.append(tmpvec) tmpvec = nil } } //* graphics; symbols and colors {symnum = 7 colnum = 9} objectvar cl[colnum], sym[symnum] for ii=0,colnum-1 cl[ii]=new String() { cl[0].s ="white" cl[1].s ="black" cl[2].s ="red" cl[3].s ="blue" cl[4].s ="green" cl[5].s ="orange" cl[6].s ="brown" cl[7].s ="violet" cl[8].s ="yellow" } for ii=0,symnum-1 sym[ii]=new String() { sym[0].s = "o" sym[1].s = "t" sym[2].s = "s" sym[3].s = "O" sym[4].s = "T" sym[5].s = "S" sym[6].s = "+"} gcl=0 // graph color func color () { gcl = (gcl+1)%colnum if (gcl==0) gcl=1 // throw out white if (numarg()==1) $o1.color(gcl) return gcl } //** qhp() basic buttons proc qhp () { if (numarg()==1) xpanel("CONTROLS",1) xbutton("Quit","quit()") xbutton("Help","help_list()") xbutton("Print panel","pwman_place(50,50)") xbutton("Reset","reset()") if (numarg()==1) xvarlabel(mesg) if (numarg()==1) xpanel(0,0) mesg="" } //** allgraphs() proc remgrs () { local ii for ii=0,3 graphList[ii].remove_all() objref graphItem } proc allgraphs () { for ii=0,3 for ltr(XO,graphList[ii]) XO.exec_menu($s1) } proc vp() { allgraphs("View = plot") } proc allgrop () { for ii=0,3 for ltr(XO,graphList[ii]) { sprint(tstr,"%s.%s",XO,$s1) execute(tstr) } } //** grrtsize() use view=plot and then pad a little proc grrtsize () { local h,w,frac if (numarg()>=1) tmpobj=$o1 else tmpobj=graphItem if (numarg()>=2) frac = $2 else frac=.05 tmpobj.exec_menu("View = plot") tmpobj.size(&x) w=frac*(x[1]-x[0]) h=frac*(x[3]-x[2]) x[0]-=2*w x[1]+=w x[2]-=4*h x[3]+=h // need extra padding on bottom tmpobj.size(x[0],x[1],x[2],x[3]) } //** isobj(o1,s2) checks whether object $o1 is of type $s2 func isobj () { sprint(temp_string_,"%s",$o1) if (sfunc.substr(temp_string_,$s2)==0) { return 1 } else { return 0 } } // cbin(min,max): binary colormap proc cbin () { $o1.colormap(2) $o1.colormap(0, 255, 0, 0) $o1.colormap(1, 255, 255, 0) $o1.scale($2, $3) } // my version of newPlotV() leaves off the label proc newplotv () { newplot() graphItem.addvar("","v(.5)",1,3) } proc redraw0 () { plrdrf=0 } // stub with flag proc plcback () { } // callback objref oxv,oyv // coordinates: 0,1 line start; 2,3 last loc oxv=new Vector(2) oyv=new Vector(2) plrdrf=1 // flag will be unset if using redraw stub plhorz=0 // 1 for horizontal, 2 for verticle lines proc pl () { if ($1==2) { // mouse down -- new line redraw0() // unset plrdrf if redraw0() not overwritten oxv.x[0]=oxv.x[1]=$2 oyv.x[0]=oyv.x[1]=$3 } else { if (plrdrf) graphItem.erase_all oyv.line(graphItem,oxv,0,2) oxv.x[1]=$2 oyv.x[1]=$3 if (plhorz==1) oyv.x[1]=oyv.x[0] if (plhorz==2) oxv.x[1]=oxv.x[0] oyv.line(graphItem,oxv,2,2) redraw0() // will redraw if defined if ($1==3) plcback(oxv,oyv) } } // newplot([width,height,pointer]) OR newplot([pointer]) proc newplot () { local wd,ht graphItem = new Graph(0) if (numarg()==1) $o1=graphItem // make a pointer if (numarg()==3) $o3=graphItem if (numarg()>1) { wd=$1 ht=$2 } else { wd=500 ht=300 } graphItem.save_name("graphList[0].") graphList[0].append(graphItem) graphItem.view(0,-90,tstop,150,600,200,wd,ht) } //* cbw(min,max): b/w binary colormap proc cbw () { $o1.colormap(2) $o1.colormap(0, 0, 0, 0) $o1.colormap(1, 255, 255, 255) $o1.scale($2, $3) } //* I/O //** svrd(0/1,name,file_ext[,fflag]) save-0 and read-1 // eg svrd(1,"Image","img") will look for file.img files to read // fflag -- return filename only -- don't call parser proc svrd () { local ii,num,cnt,fflag rdflag=$1 if (numarg()==4) fflag=$4 else fflag=0 sprint(tstr2,"*.%s",$s3) if (rdflag==0) { // write sprint(tstr,"Write %s",$s2) tmpfile.chooser("w",tstr,tstr2,"WRITE","","") if (tmpfile.chooser()==1) { tmpfile.getname(filename) if (!fflag) parsr(rdflag) } } else if (rdflag==1) { // read sprint(tstr,"Read %s",$s2) tmpfile.chooser("r",tstr,tstr2,"READ","","") if (tmpfile.chooser()==1) { tmpfile.getname(filename) if (!fflag) parsr(rdflag) } } mesg=filename tmpfile.close } //* xgetargs, d2b //** range(val,min,max) -- return val only if in proper range // eg stim.noise=range(stim.noise,0,1) func range () { if ($1<$2) return $2 if ($1>$3) return $3 return $1 } //** err() proc err () { if (numarg()==1) mesg=$s1 print mesg } //** xvarstr(): display a list of strings in an xpanel using xvarlabel proc xvarstr () { local ii,cnt // ivoc_style("*font", "fixed") if (unix_mac_pc() == 1) { ivoc_style("*font", "*helvetica-bold-r-normal*--14*") } xpanel($s1) for ltr(XO,$o2) xvarlabel(XO.s) xpanel() } //** xgetargs(panel_name,command,arg1[,arg2,...],defaults) // xgetargs("Random session","newrand","# of patts","patt size ","overlap ","5,33,7") objref argv argv = new Vector() proc xgetargs () { local i,args args=numarg()-3 i=numarg() argv.resize(0) sprint(temp_string_,"argv.append(%s)",$si) execute(temp_string_) if (argv.size!=args) argv.resize(args) xpanel($s1) mesg=$s1 xvarlabel(mesg) for i=3,numarg()-1 { sprint(temp_string_,"argv.x[%d]",i-3) xvalue($si,temp_string_) } sprint(temp_string_,"xgetexec(\"%s\",%d)",$s2,args) xbutton("Execute",temp_string_) xpanel() } proc xgetexec () { local i,args args = $2 if (argv.size!=args) { mesg="Error-close & relaunch panel" return } sprint(temp_string_,"%s(",$s1) for i=0,args-2 sprint(temp_string_,"%s%g,",temp_string_,argv.x[i]) sprint(temp_string_,"%s%g)",temp_string_,argv.x[i]) print temp_string_ execute(temp_string_) } //** d2b(): decimal to binary, gives a binary appearing number for showing // eg for ii=0,100 printf("%010d\n",d2b(ii)) func d2b () { local num,ii,rem,res num=$1 ii=0 res=0 while (num>0) { rem = int(2*(num/2-int(num/2))) // right digit num=int(num/2) // remove right digit res+=rem*10^ii ii+=1 } return res } //* matrix stuff //** cvec(),rvec() put out col bzw row vecs in latex format proc cvec () { local i printf("\\bpm ") for vtr(&x,$o1) if (i1<$o1.size-1) printf("%g \\\\ ",x) printf("%g \\epm\n",x) } proc rvec () { local i printf("\\bpm ") for vtr(&x,$o1) if (i1<$o1.size-1) printf("%g & ",x) printf("%g \\epm\n",x) } //** matp(mat,rows,cols) prints out matrix in latex format proc matp () { local i,j,rows,cols rows=$2 cols=$3 if ($o1.size!=rows*cols) {print "ERR: matp - mat.size != rows*cols" return} printf("\\bpm\n") for (i=0;irows) for i=rows,cols-1 printf("%70.04g \n",$o2.x[i]) } //** mmlt(mdest,m1,m2,r2) matrix mult between 2 mats // r2==c1 gives shared dimension proc mmlt () { local i,j,r1,c1,r2,c2 c1 = r2 = $4 // shared dimension (cols of 1st/rows of second) r1=$o2.size/c1 c2=$o3.size/r2 printf("%dx%d * %dx%d = %dx%d\n",r1,c1,r2,c2,r1,c2) $o1.resize(r1*c2) vec[0].resize(c1) // length of a row of mat A vec[1].resize(r2) // length of a col of mat B for i=0,r1-1 for j=0,c2-1 { vec[0].mrow($o2,i,c1) vec[1].mcol($o3,j,c2) $o1.x[i*c2+j]=vec[0].dot(vec[1]) } $o1.mprintf(r1,c2) dealloc(a) } //* membrane and pp's //** runbutton running_ = 1 proc runbutton () { if (running_ == 0) { stoprun = 1 return } if (t>0 && t=vec.size) { sprint(mesg,"Input location must be from 0 to %d",vec.size-1) err() return } temp_string_=mesg temp_string_="" string_dialog("Enter \"number\"",temp_string_) mesg=temp_string_ sprint(fstr,"%s%dd","%0",sz) // sz many bits at a time -- eg %03d for octal while (grabchar(sz,temp_string_,temp_string2_,fstr)!=-1 && inloc0) { num=0 for ii=0,mdu-1 num+=vec.x[ii]*2^(mdu-ii-1) outchar(num,sz,flout) } for (ii=mdu;ii=0;jj-=1) { num+=vec.x[ii+kk]*(2^jj) kk+=1 } outchar(num,sz,flout) } printf("\n") } if (flout==1) tmpfile.close() } proc outchar () { local num,sz,flout num=$1 sz=$2 flout=$3 if (sz==6) num+=2^6 // left pad for 6 bit ascii if (flout==0) { if (sz>=6) spitchar(num) else printf(fstr,num) } else if (sz>=6) fspitchar(num,tmpfile) else tmpfile.printf(fstr,num) } //** readrep() -- calls chooser to read proc readrep () { local ii,cnt,sz,kk,extra if (fchooser_flag != 1) { // create panel first time only tmpfile.chooser("r","Read","*.rep","READ","","") fchooser_flag=1 } if (numarg()==1) { lstfile=$s1 tmpfile.ropen(lstfile) } else if (tmpfile.chooser()!=1) return // tmpfile.getname(lstfile) cnt = tmpfile.gets(temp_string_) sscanf(temp_string_,"// %d %d %d",&W,&H,&x) SZ = W*H // size of vector ASZ=sz=x // size of the encoding if (sz==-1) { vectin() return } // vector representation if (SZ%sz>0) extra=sz-SZ%sz else extra=0 // padding to throw away vec.resize(SZ+extra) vec.fill(0) if (sz<5) { cnt=tmpfile.gets(temp_string_) // binary,oct or hex can read with gets if (cnt*sz!=SZ+extra) { sprint(mesg,"WARNING: File length discrepancy: %d; %d x %d = %d != %d",cnt*sz-SZ,W,H,SZ,cnt*sz) err()} } kk=-1 sprint(fstr,"%s%dd","%0",sz) // sz many bits at a time -- eg %03d for octal // put next char as binary string in temp_string2_ while (grabchar(sz,temp_string_,temp_string2_,fstr)!=-1) { if (kk>=SZ+extra-1) break for ii=0,sz-1 { sscanf(temp_string2_,"%1d",&x) // grab rightmost char sfunc.right(temp_string2_,1) vec.x[kk+=1]=x } } vec.rotate(-extra) vec.resize(SZ) drawcells() lstsent="" tmpfile.seek(0,0) // do it again to save string cnt = tmpfile.gets(temp_string_) // throw away first line cnt = tmpfile.gets(lstsent) // throw away first line chop(lstsent) while (tmpfile.gets(temp_string_)>0) { chop(temp_string_) sprint(lstsent,"%s %s",lstsent,temp_string_) } tmpfile.close } //** rdsent() -- read sentence proc rdsent () { string_dialog("Sentence: ",lstsent) parsstr(lstsent,W,H,ASZ) } //** psrsstr(str,W,H,sz) -- calls string proc parsstr () { local ii,cnt,sz,kk,extra,ch W=$2 H=$3 sz=$4 // sz -- size of the encoding SZ = W*H // size of vector if (SZ%sz>0) extra=sz-SZ%sz else extra=0 // padding to throw away vec.resize(SZ+extra) vec.fill(0) kk=-1 sprint(fstr,"%s%dd","%0",sz) // sz many bits at a time -- eg %03d for octal // put next char as binary string in temp_string2_ temp_string_ = $s1 while (sscanf(temp_string_,"%c",&x)==1) { // print x // ascii value of the character sfunc.right(temp_string_,1) ch = d2b(x) // change to pseudo-binary sprint(temp_string2_,fstr,ch) // make result a string if (kk>=SZ+extra-1) break for ii=0,sz-1 { sscanf(temp_string2_,"%1d",&x) // grab rightmost char sfunc.right(temp_string2_,1) vec.x[kk+=1]=x } } vec.rotate(-extra) vec.resize(SZ) drawcells() } // grabchar -- note that strings are call by ref func grabchar () { local sz,ch sz = $1 // $s2 for input, $s3 for output, $s4 is format string if (sz<5) { if (sscanf($s2,"%1x",&x)!=1) return -1 sfunc.right($s2,1) // remove 1 char from string } else if (sz==6 && tmpfile.isopen==0) { // grab ascii from string if (sscanf($s2,"%c",&x)!=1) return -1 sfunc.right($s2,1) } else { // read from file rather than from $s2 string if ((x=hocgetc(tmpfile))==-1) return -1 } if (sz==6) x-=2^6 // left pad for 6 bit ascii ch = d2b(x) // change to pseudo-binary sprint($s3,$s4,ch) // make result a string if (0) { spitchar(x) printf(" %d %d %s\n",x,ch,temp_string2_) } // debugging block return 1 } //** wrtrep() -- calls chooser to write func wrtrep () { local sz sz = $1 if (fchooser_flag != 2) { // create panel first time only fchooser_flag=2 tmpfile.chooser("w","Write to file","*.rep","WRITE","","") } if (tmpfile.chooser()==1) { tmpfile.getname(lstfile) tmpfile.printf("// %d %d %d\n",W,H,sz) return 1 } else return -1 } //** vectout() proc vectout () { local ii,flout if (fileout==1 || fileapp==1) flout=1 else flout=0 if (flout==0) { for ltr(XO,prog) { if (i1>=progptr) break printf("%s\n",XO.s) } } else { if (fchooser_flag != 2) { // create panel first time only fchooser_flag=2 tmpfile.chooser("w","Write to file","*.rep","WRITE","","") } if (tmpfile.chooser()==1) { tmpfile.getname(lstfile) tmpfile.printf("// %d %d %d\n",W,H,-1) for ltr(XO,prog) { if (i1>=progptr) break tmpfile.printf(XO.s) tmpfile.printf("\n")} tmpfile.close } } } //** vectin() proc vectin () { local ii,comm,args ii=0 if (SZ!=vec.size) { vec.fill(0) drawcells() } clrprog() while (tmpfile.gets(prog.object(ii).s)>0) { // copy to memory chop(prog.object(ii).s) if (sfunc.head(prog.object(ii).s, "//", temp_string_)>-1) { prog.object(ii).s=temp_string_ } if (sfunc.len(prog.object(ii).s)>4) ii+=1 // ignore empty lines } progptr=ii tmpfile.close for ii=0,progptr-1 { XO = prog.object(ii) args = sscanf(XO.s,"%s %f %f %f %f",temp_string_,&x[0],&x[1],&x[2],&x[3]) args -= 1 // remove command name from arg count interp(temp_string_,args,i1+1) // command in temp_string_ } showsplt(-1) } //** interp proc interp () { local op,args,line args=$2 line=$3 // $s1 is the command if (args<2) {printf("ERROR line %d: COMM: %s, args %d\n",line,$s1,args) return} // nothing read or command without an arg if (strcmp($s1,"setbit")==0) { if (args!=2) {sprint(mesg,"line %d: %s called with %d args!",line,$s1,args) err() return} setbit(-1,x[0],x[1],1,"") } else if (strcmp($s1,"clrbit")==0) { if (args!=2) {sprint(mesg,"line %d: %s called with %d args!",line,$s1,args) err() return} setbit(-1,x[0],x[1],0,"") } else if (strcmp($s1,"circ")==0) { if (args!=3) {sprint(mesg,"line %d: %s called with %d args!",line,$s1,args) err() return} circ1(x[0],x[1],x[2],1) } else if (strcmp($s1,"line")==0) { if (args!=4) {sprint(mesg,"line %d: %s called with %d args!",line,$s1,args) err() return} line1(x[0],x[1],x[2],x[3]) } else if (strcmp($s1,"rect")==0) { if (args!=4) {sprint(mesg,"line %d: %s called with %d args!",line,$s1,args) err() return} rect1(x[0],x[1],x[2],x[3]) } else if (strcmp($s1,"frect")==0) { if (args!=4) {sprint(mesg,"line %d: %s called with %d args!",line,$s1,args) err() return} frect1(x[0],x[1],x[2],x[3]) } else { sprint(mesg,"%s command not recognized",$s1) err() } } //** whatcomm(): searches through commands to identify string func whatcomm () { local ii,flag flag=-1 for ltr(XO,mtooll) if (strcmp(XO.t,$s1)==0) flag=i1 return flag } //* screen drawing // trig=2 for press, 1 for drag, 3 for release //** proc pptr() wraps progptr around and generates error message proc pptr () { local ii,flag flag = 1 if (progptr>=NSTEPS) { sprint(mesg,"ERROR: VEC PROGRAM FILLED -- OVERWRITING PROGRAM") err() progptr=0 return } if (progptr>0) if (strcmp(prog.object(progptr-1).s,$s1)==0) flag=0 if (flag==1) { prog.object(progptr).s = $s1 progptr+=1 } } proc clrprog () { progptr = 0 for ltr(XO,prog) XO.s="" // clear } //** sbit,cbit proc setbit () { sbit1($1,$2,$3,1,"setbit") } proc clrbit () { sbit1($1,$2,$3,0,"clrbit") } proc sbit1 () { local xc,yc,num,trig,bitval,caller trig=$1 xc=$2 yc=$3 bitval=$4 caller=findmtool($s5) if (trig==3) return if (trig>-1) { // interactive xc/=wdt yc/=wdt sprint(mesg,"%s %d %d",$s5,xc,yc) pptr(mesg) } num=int(yc)*W+int(xc) if (num<0 || num>=SZ) return vec.x[num]=bitval if (trig>-1) showsplt(caller) } //** rect proc rect () { local ii,jj,xc,yc,num,trig,bitval,caller trig=$1 xc=$2 yc=$3 bitval=1 caller=findmtool("rect") if (trig==2) {x[0]=xc y[0]=yc return} if (trig==1) return if (xc0 && num0 && num0 && num0 && num0;jj+=(y1-y0)/30) { num=int(jj)*W+x0 if (num>0 && num0 && num-1) graphItem.exec_menu(mtooll.object($1).s) } //** clrall() proc clrall () { for ltr(XO,prog) XO.s="" clrprog() vec.fill(0) graphItem.exec_menu("Shape Plot") graphItem.flush() } //** flipem() proc flipem () { local ii ind.resize(flipnum) if (flipnum>vec.size) { for ii=0,vec.size-1 if (vec.x[ii]==0) vec.x[ii]=1 else vec.x[ii]=0 } else { // vec.flipbits(ind,flipnum) rdm.uniform(0,vec.size-1) ind.setrand(rdm) for vtr(&x,ind) if (vec.x[x]==0) vec.x[ii]=1 else vec.x[x]=0 } graphItem.exec_menu("Shape Plot") graphItem.flush() } //** anim -- run stored sequence proc anim () { local ii,jj,flag,cmp // need to create a veclist in readrep for ltr(XO,veclist) { vec.copy(XO) showsplt(-1) if (stopf==1) break } stopf=0 } // END rep.hoc //================================================================ // picture of baby saved in hex sentence = "000000000e000000038003c000e01e00000fd000003030000060180000400800004008000040080018400c00064009e001c00f000060180000303000001fe000000fc000000fc000003030000060180000478800004008000040080000400c0000444800004008000060100000303000001fe000000000000000000000000000" lstsent=sentence newshapeplot() graphItem.view(0,0,100,100,50,50,500,500) mkit1() // readrep("baby.rep") proc reset () { rdin(sentence, 32, 32, 4) } reset() // END REP_baby.hoc //================================================================