2 * increases the resolution of a conformation by interpolation
7 #define OXBITS (XBITS-1)
13 /* filled in by read_input: */
14 static struct Vertices all;
15 static int computed_count[N];
17 static void characterise_input(void) {
21 r= fstat(0,&stab); if (r) diee("fstat input to find length");
23 if (!stab.st_size || stab.st_size % (sizeof(double)*D3) ||
24 stab.st_size > INT_MAX)
25 fail("input file is not reasonable whole number of vertices\n");
27 fprintf(stderr,"XBITS=%d XY=%d*%d=%d\n", XBITS, X,Y,N);
28 fprintf(stderr,"OXBITS=%d OXY=%d*%d=%d\n", OXBITS, OX,OY,ON);
29 fprintf(stderr,"sizeof(double)=%d st_size=%lu\n",
30 (int)sizeof(double), (unsigned long)stab.st_size);
32 if (stab.st_size != (ON*sizeof(double)*D3))
33 fail("input file wrong size\n");
36 static void read_input(void) {
39 for (oy=y=0; y<Y; oy++, y+=INC) {
40 fprintf(stderr, "y=%2d>%2d", oy,y);
41 for (ox=x=0; x<X; ox++, x+=INC) {
43 ov= (oy << OXBITS) | ox;
45 fprintf(stderr, " 0%02o->0x%02x", ov, v);
46 if (fread(all.a[v], sizeof(double), D3, stdin) != D3)
48 computed_count[v]= -1;
54 /* We use GSL's interpolation functions. Each xa array is simple
55 * integers, and we do the interpolation three times for each line
56 * of topologically colinear vertices (once, separately, for each of
57 * the three spatial coordinates in the ya array). The `horizontal'
58 * lines are done with periodic boundary conditions, obviously.
60 * This directly gives the required additional vertices:
62 * ,O. O original vertex
64 * ,* - - *. dashed lines are new edges,
65 * / ` ' \ not directly represented
66 * O----`*'----O or manipulated by this program
74 static void traverse_next(Traverse *t) {
76 v2= EDGE_END2(t->v, t->e);
78 int e2= edge_reverse(t->v, t->e);
79 assert(EDGE_END2(v2,e2) == t->v);
85 static void interpolate_line(int startvertex,
86 int direction /* edge number */,
88 double xa[nmax], ya[D3][nmax];
89 const gsl_interp_type *it;
91 gsl_interp_accel *accel;
95 #define STARTV (traverse.v=startvertex, traverse.e=direction)
96 #define NEXTV (traverse_next(&traverse), fprintf(stderr,"-%02x",traverse.v))
98 fprintf(stderr,"interpolate_line 0x%02x,%d nmax=%d ",
99 startvertex,direction,nmax);
104 assert(traverse.v>=0);
107 K ya[k][i]= all.a[traverse.v][k];
109 if (i && traverse.v==startvertex) { fputc('@',stderr); break; }
112 if (traverse.v<0) break;
116 it= gsl_interp_akima_periodic;
120 it= gsl_interp_akima;
125 fprintf(stderr," n=%d->%d loop=0x%02x", nold,nnew,traverse.v);
128 fprintf(stderr,"\n k=%d ", k);
130 interp= gsl_interp_alloc(it,nold); if (!interp) diee("gsl_interp_alloc");
131 accel= gsl_interp_accel_alloc(); if(!accel) diee("gsl_interp_accel_alloc");
132 GA( gsl_interp_init(interp,xa,ya[k],nold) );
137 assert(traverse.v>=0); NEXTV; assert(traverse.v>=0);
139 assert(computed_count[traverse.v] == k);
140 GA( gsl_interp_eval_e(interp,xa,ya[k], i+0.5, accel,
141 &all.a[traverse.v][k]) );
142 computed_count[traverse.v]++;
145 gsl_interp_accel_free(accel);
146 gsl_interp_free(interp);
148 fprintf(stderr," done\n");
151 static void interpolate(void) {
154 for (y=0; y<(Y+1)/2; y+=INC) {
155 interpolate_line(y<<YSHIFT, 0, OX*2+1);
157 for (x=0; x<X; x+=INC) {
158 interpolate_line( x, 5, OY);
159 interpolate_line((Y-1)<<YSHIFT | x, 1, OY);
163 static void write_output(void) {
164 int x, y, v, c, bad=0;
165 for (y=0; y<Y; y++) {
166 fprintf(stderr,"checking y=%d ", y);
167 for (x=0; x<X; x++) {
169 fprintf(stderr," %02x",v);
170 c= computed_count[v];
171 if (c==D3) fputc('#',stderr);
172 else if (c==-1) fputc('*',stderr);
173 else { fputc('!',stderr); bad++; }
178 if (fwrite(all.a,sizeof(all.a),1,stdout) != 1) diee("fwrite");
179 fprintf(stderr,"interpolated\n");
182 int main(int argc, const char **argv) {
183 if (argc!=1 || isatty(0) || isatty(1))
184 { fputs("usage: interpolate <INPUT.cfm >OUTPUT.cfm\n",stderr); exit(8); }
186 characterise_input();
190 if (fclose(stdout)) diee("fclose stdout");