2 * Displays a conformation
16 typedef struct { double vertex[3][D3]; } Triangle;
18 static Triangle trisbuffer[MAXTRIS], *displaylist[MAXTRIS];
20 static Vertices conformation;
22 static double transform[D3][D3]= {{1,0,0}, {0,1,0}, {0,0,1}};
23 static GSL_MATRIX(transform);
26 static struct stat input_stab;
27 static const char *input_filename;
29 static void read_input(void) {
32 if (input_f) fclose(input_f);
33 input_f= fopen(input_filename, "rb"); if (!input_f) diee("input file");
35 if (fstat(fileno(input_f), &input_stab)) diee("fstat input file");
38 r= fread(&conformation,sizeof(conformation),1,input_f);
39 if (r!=1) diee("fread");
42 static void transform_coordinates(void) {
45 gsl_vector input_gsl= { D3,1 };
50 input_gsl.data= &conformation[v][0];
51 GA( gsl_blas_dgemv(CblasNoTrans, 1.0,&transform_gsl, &input_gsl,
53 K conformation[v][k]= result[k];
57 static int vertex_in_triangles[N], vertex_in_triangles_checked;
59 static void addtriangle(int va, int vb, int vc) {
60 Triangle *t= &trisbuffer[ntris];
63 assert(ntris < MAXTRIS);
65 t->vertex[0][k]= conformation[va][k];
66 t->vertex[1][k]= conformation[vb][k];
67 t->vertex[2][k]= conformation[vc][k];
69 if (!vertex_in_triangles_checked) {
70 vertex_in_triangles[va]++;
71 vertex_in_triangles[vb]++;
72 vertex_in_triangles[vc]++;
74 displaylist[ntris++]= t;
77 static void generate_display_list(void) {
82 /* We use the two triangles in the parallelogram vb, vb+e5, vb+e0, vb+e1.
83 * We go round each triangle clockwise (although our surface is non-
84 * orientable so it shouldn't matter). Picking the parallelogram
85 * to our right avoids getting it wrong at the join.
87 for (e=0; e<V6; e++) ve[e]= EDGE_END2(vb,e);
89 if (ve[5]>=0) addtriangle(vb,ve[0],ve[5]);
90 if (ve[1]>=0) addtriangle(vb,ve[1],ve[0]);
93 if (!vertex_in_triangles_checked) {
96 expd= RIM_VERTEX_P(v) ? 3 : 6;
97 if (vertex_in_triangles[v] != expd) {
98 fprintf(stderr,"vertex %02x used for %d triangles, expected %d\n",
99 v, vertex_in_triangles[v], expd);
103 vertex_in_triangles_checked= 1;
107 static int dl_compare(const void *tav, const void *tbv) {
109 const Triangle *const *tap= tav, *ta= *tap;
110 const Triangle *const *tbp= tbp, *tb= *tbp;
112 for (i=0; i<3; i++) {
113 za += ta->vertex[i][2];
114 zb += tb->vertex[i][2];
116 return za > zb ? -1 :
120 static void sort_display_list(void) {
121 qsort(displaylist, ntris, sizeof(*displaylist), dl_compare);
124 /*---------- X stuff ----------*/
128 typedef struct { GC fillgc, linegc; } DrawingMode;
130 static Display *display;
131 static Pixmap pixmap, doublebuffers[2];
132 static Window window;
134 static DrawingMode dmred, dmblue, dmwhite;
135 static const DrawingMode *dmcurrent;
136 static int wwidth=WSZ, wheight=WSZ, wmindim=WSZ, wmaxdim=WSZ;
137 static int ncut, currentbuffer, x11depth, x11screen, wireframe;
140 static double sizeadj_scale= 0.3, eyes_apart, scale_wmindim;
141 static double eye_z= -10, eye_x=0;
142 static double cut_z= -9;
143 static const double eyes_apart_preferred=0.05, eyes_apart_min= -0.02;
146 static void drawtriangle(const Triangle *t) {
150 for (i=0; i<3; i++) {
151 double *v= t->vertex[i];
156 if (z < cut_z) { ncut++; return; }
158 double zezezp= eye_z / (eye_z - z);
159 points[i].x= scale_wmindim * (zezezp * (x - eye_x) + eye_x) + wwidth/2;
160 points[i].y= scale_wmindim * (zezezp * y ) + wheight/2;
162 points[3]= points[0];
165 XA( XFillPolygon(display,pixmap, dmcurrent->fillgc,
166 points,3,Convex,CoordModeOrigin) );
167 XA( XDrawLines(display,pixmap, dmcurrent->linegc,
168 points, 4,CoordModeOrigin) );
171 static const unsigned long core_event_mask=
172 ButtonPressMask|ButtonReleaseMask|StructureNotifyMask|ButtonMotionMask|
175 static void mkpixmaps(void) {
176 for (currentbuffer=0; currentbuffer<2; currentbuffer++) {
177 XA( pixmap= XCreatePixmap(display,window,wwidth,wheight,x11depth) );
178 doublebuffers[currentbuffer]= pixmap;
183 static void mkgcs(DrawingMode *dm, unsigned long planes) {
186 gcv.function= GXcopy;
187 gcv.foreground= WhitePixel(display,x11screen);
188 gcv.plane_mask= planes;
189 dm->linegc= XCreateGC(display,pixmap,
190 GCFunction|GCForeground|GCPlaneMask,
193 gcv.function= GXclear;
194 dm->fillgc= XCreateGC(display,pixmap,
195 GCFunction|GCPlaneMask,
199 static void display_prepare(void) {
200 XSetWindowAttributes wa;
203 XA( display= XOpenDisplay(0) );
204 x11screen= DefaultScreen(display);
205 x11depth= DefaultDepth(display,x11screen);
206 XA( XMatchVisualInfo(display,x11screen,x11depth, TrueColor,&visinfo) );
208 wa.event_mask= core_event_mask;
209 XA( window= XCreateWindow(display, DefaultRootWindow(display),
210 0,0, wwidth,wheight, 0,x11depth,
211 InputOutput, visinfo.visual,
214 hints.flags= USPosition;
217 XSetWMNormalHints(display,window,&hints);
221 mkgcs(&dmwhite, AllPlanes);
222 mkgcs(&dmblue, visinfo.blue_mask);
223 mkgcs(&dmred, visinfo.red_mask);
226 static void drawtriangles(const DrawingMode *dm) {
231 for (i=0, t=displaylist, ncut=0; i<ntris; i++, t++)
235 static void display_conformation(void) {
236 pixmap= doublebuffers[currentbuffer];
238 XA( XFillRectangle(display,pixmap,dmwhite.fillgc,0,0,wwidth,wheight) );
240 if (eyes_apart > 0) {
241 const double stationary= 0.07;
243 eye_x= eyes_apart < eyes_apart_preferred
245 eyes_apart < (eyes_apart_preferred + stationary)
246 ? eyes_apart_preferred
247 : eyes_apart - stationary;
248 eye_x /= sizeadj_scale;
249 drawtriangles(&dmblue);
251 drawtriangles(&dmred);
253 drawtriangles(&dmwhite);
254 printf("shown, %d/%d triangles cut\n", ncut, ntris);
257 XA( XSetWindowBackgroundPixmap(display,window,pixmap) );
258 XA( XClearWindow(display,window) );
259 currentbuffer= !currentbuffer;
262 static void show(void) {
263 scale_wmindim= sizeadj_scale * wmindim;
265 transform_coordinates();
266 generate_display_list();
268 display_conformation();
273 void (*start)(const XButtonEvent *e);
274 void (*delta)(double dx, double dy);
275 void (*conclude)(void);
276 void (*abandon)(void);
280 static const Drag drag_##x= { \
281 #x, drag_##x##_start, drag_##x##_delta, \
282 drag_##x##_conclude, drag_##x##_abandon \
285 #define DRAG_SAVING(x, thing, hook) \
286 static typeof(thing) original_##thing; \
287 static void drag_##x##_start(const XButtonEvent *e) { \
288 memcpy(&original_##thing, &thing, sizeof(thing)); \
291 static void drag_##x##_conclude(void) { } \
292 static void drag_##x##_abandon(void) { \
293 memcpy(&thing, &original_##thing, sizeof(thing)); \
298 static void drag_none_start(const XButtonEvent *e) { }
299 static void drag_none_delta(double dx, double dy) { }
300 static void drag_none_conclude(void) { }
301 static void drag_none_abandon(void) { }
304 static void pvectorcore(const char *n, double v[D3]) {
307 K printf("%# 10.10f ",v[k]);
310 static void pvector(const char *n, double v[D3]) {
314 static void pmatrix(const char *n, double m[D3][D3]) {
316 for (j=0; j<D3; j++) { pvectorcore(n,m[j]); n=""; }
319 #define PMATRIX(x) pmatrix(#x,x);
321 static double drag_transform_conv_x_z= 0;
322 static double drag_transform_conv_y_z= 0;
324 static void drag_transform_prep(const XButtonEvent *e) {
325 static const double factor= 2.5;
326 drag_transform_conv_x_z= MAX( MIN(e->y * factor / wheight - (factor/2),
328 drag_transform_conv_y_z= MAX( MIN(e->x * factor / wwidth - (factor/2),
330 printf("drag_transform_conv_{x,y}_z = %g,%g\n",
331 drag_transform_conv_x_z, drag_transform_conv_y_z);
334 static void make_z_rotation(double rotz[D3][D3], double cz, double sz) {
335 rotz[0][0]= cz; rotz[0][1]= sz; rotz[0][2]= 0;
336 rotz[1][0]= -sz; rotz[1][1]= cz; rotz[1][2]= 0;
337 rotz[2][0]= 0; rotz[2][1]= 0; rotz[2][2]= 1;
340 static void drag_rotate_delta(double dx, double dy) {
341 /* We multiple our transformation matrix by a matrix:
343 * If we just had y movement, we would rotate about x axis:
344 * rotation X = [ 1 0 0 ]
347 * where cy and sy are sin and cos of y rotation
349 * But we should pre-rotate this by a rotation about the z axis
350 * to get it to the right angle (to include x rotation). So
351 * we make cy and sy be cos() and sin(hypot(x,y)) and use
352 * with cr,sr as cos() and sin(atan2(y,y)):
354 * Ie we would do T' = Z R^T X R T where
355 * or T' = Z C T where C = R^T X R and
357 * adjustment R = [ cr sr 0 ]
359 * [ 0 0 1 ] or make_z_rotation(cr,sr)
361 * rotation Z = [ cz sz 0 ]
363 * [ 0 0 1 ] or make_z_rotation(cz,sz)
366 double rotx[D3][D3], adjr[D3][D3], rotz[D3][D3];
371 static double temp[D3][D3], change[D3][D3];
372 static GSL_MATRIX(temp);
373 static GSL_MATRIX(change);
375 printf("\nTRANSFORM %g, %g\n", dx,dy);
377 double dz= -drag_transform_conv_x_z * dx +
378 drag_transform_conv_y_z * dy;
380 dx *= (1 - fabs(drag_transform_conv_x_z));
381 dy *= (1 - fabs(drag_transform_conv_y_z));
383 double d= hypot(dx,dy);
385 printf(" dx,dy,dz = %g, %g, %g d = %g\n", dx,dy,dz, d);
387 if (hypot(d,dz) < 1e-6) return;
389 printf(" no xy rotation\n");
404 printf("\n d=%g cy,sy=%g,%g cr,sr=%g,%g\n\n", d,cy,sy,cr,sr);
406 rotx[0][0]= 1; rotx[0][1]= 0; rotx[0][2]= 0;
407 rotx[1][0]= 0; rotx[1][1]= cy; rotx[1][2]= sy;
408 rotx[2][0]= 0; rotx[2][1]= -sy; rotx[2][2]= cy;
411 make_z_rotation(adjr,cr,sr);
414 GA( gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1.0,
417 pmatrix("X R", temp);
419 GA( gsl_blas_dgemm(CblasTrans,CblasNoTrans, 1.0,
425 make_z_rotation(rotz,cos(angz),sin(angz));
428 GA( gsl_blas_dgemm(CblasTrans,CblasNoTrans, 1.0,
429 &rotz_gsl,&change_gsl,
431 pmatrix("Z C", temp);
433 static double skew[D3][D3];
434 static GSL_MATRIX(skew);
436 GA( gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1.0,
437 &temp_gsl, &transform_gsl,
441 memcpy(&transform,&skew,sizeof(transform));
445 /* Now we want to normalise skew, the result becomes new transform */
446 double svd_v[D3][D3];
449 double sigma[D3], tau[D3];
453 /* We use notation from Wikipedia Polar_decomposition
454 * Wikipedia's W is GSL's U
455 * Wikipedia's Sigma is GSL's S
456 * Wikipedia's V is GSL's V
457 * Wikipedia's U is our desired result
458 * Wikipedia which says if the SVD is A = W Sigma V*
459 * then the polar decomposition is A = U P
460 * where P = V Sigma V*
464 GA( gsl_linalg_SV_decomp(&skew_gsl, &svd_v_gsl, &sigma_gsl, &tau_gsl) );
466 pvector("Sigma",sigma);
469 /* We only need U, not P. */
470 GA( gsl_blas_dgemm(CblasNoTrans,CblasTrans, 1.0,
471 &skew_gsl,&svd_v_gsl,
472 0.0,&transform_gsl) );
474 pmatrix("U", transform);
476 printf("drag_rotate_delta...\n");
479 DRAG_SAVING(rotate, transform, drag_transform_prep(e));
481 static void drag_sizeadj_delta(double dx, double dy) {
482 sizeadj_scale *= pow(3.0, -dy);
485 DRAG_SAVING(sizeadj, sizeadj_scale, );
487 static void drag_3d_delta(double dx, double dy) {
488 eyes_apart += dx * 0.1;
489 if (eyes_apart < eyes_apart_min) eyes_apart= eyes_apart_min;
490 printf("sizeadj eyes_apart %g\n", eyes_apart);
493 DRAG_SAVING(3d, eyes_apart, );
495 static const Drag *drag= &drag_none;
497 static int drag_last_x, drag_last_y;
499 static void drag_position(int x, int y) {
500 drag->delta((x - drag_last_x) * 1.0 / wmaxdim,
501 (y - drag_last_y) * 1.0 / wmaxdim);
506 static void event_button(XButtonEvent *e) {
507 if (e->window != window || !e->same_screen) return;
508 if (e->type == ButtonPress) {
509 if (e->state || drag != &drag_none) {
510 printf("drag=%s press state=0x%lx abandon\n",
511 drag->name, (unsigned long)e->state);
517 case Button1: drag= &drag_rotate; break;
518 case Button2: drag= &drag_sizeadj; break;
519 case Button3: drag= &drag_3d; break;
520 default: printf("unknown drag start %d\n", e->button);
522 printf("drag=%s press button=%lu start %d,%d\n",
523 drag->name, (unsigned long)e->button, e->x, e->y);
528 if (e->type == ButtonRelease) {
529 printf("drag=%s release %d,%d\n", drag->name, e->x, e->y);
530 drag_position(e->x, e->y);
536 static void event_motion(int x, int y) {
537 printf("drag=%s motion %d,%d\n", drag->name, x, y);
541 static void event_key(XKeyEvent *e) {
546 r= XLookupString(e,buf,sizeof(buf)-1,&ks,0);
548 printf("XLookupString keycode=%u state=0x%x gave %d\n",
549 e->keycode, e->state, r);
553 if (!strcmp(buf,"q")) exit(0);
554 if (!strcmp(buf,"w")) {
555 wireframe= !wireframe;
559 if (!strcmp(buf,"d")) {
560 eyes_apart= eyes_apart>0 ? eyes_apart_min : eyes_apart_preferred;
566 static void event_config(XConfigureEvent *e) {
567 if (e->width == wwidth && e->height == wheight)
570 wwidth= e->width; wheight= e->height;
571 wmaxdim= wwidth > wheight ? wwidth : wheight;
572 wmindim= wwidth < wheight ? wwidth : wheight;
574 XA( XSetWindowBackground(display,window,BlackPixel(display,x11screen)) );
575 for (currentbuffer=0; currentbuffer<2; currentbuffer++)
576 XA( XFreePixmap(display, doublebuffers[currentbuffer]) );
582 static void check_input(void) {
586 r= stat(input_filename, &newstab);
587 if (r<0) diee("could not check input");
589 #define CI(x) if (newstab.st_##x == input_stab.st_##x) ; else goto changed
601 static void topocheck(void) {
602 int v1,e,v2,eprime,v1prime, count;
605 FOR_VEDGE(v2,eprime,v1prime)
606 if (v1prime==v1) count++;
608 fprintf(stderr,"%02x -%d-> %02x reverse edge count = %d!\n",
610 FOR_VEDGE(v2,eprime,v1prime)
611 fprintf(stderr,"%02x -%d-> %02x -> %d -> %02x\n",
612 v1,e,v2,eprime,v1prime);
618 int main(int argc, const char *const *argv) {
619 static const int wantedevents= POLLIN|POLLPRI|POLLERR|POLLHUP;
622 int k, i, r, *xfds, nxfds, polls_alloc=0;
623 struct pollfd *polls=0;
624 int motion_deferred=0, motion_x=-1, motion_y=-1;
627 if (argc==1) { printf("topology self-consistent, ok\n"); exit(0); }
629 if (argc != 2 || argv[1][0]=='-') {
630 fputs("need filename\n",stderr); exit(8);
632 input_filename= argv[1];
635 K transform[k][k]= 1.0;
639 XMapWindow(display,window);
642 XA( XInternalConnectionNumbers(display, &xfds, &nxfds) );
643 if (polls_alloc <= nxfds) {
644 polls_alloc= nxfds + polls_alloc + 1;
645 polls= realloc(polls, sizeof(*polls) * polls_alloc);
646 if (!polls) diee("realloc for pollfds");
648 for (i=0; i<nxfds; i++) {
649 polls[i].fd= xfds[i];
650 polls[i].events= wantedevents;
655 polls[i].fd= ConnectionNumber(display);
656 polls[i].events= wantedevents;
658 r= poll(polls, nxfds+1, motion_deferred ? 0 : 200);
659 if (r<0) diee("poll");
661 for (i=0; i<nxfds; i++)
662 if (polls[i].revents)
663 XProcessInternalConnection(display, polls[i].fd);
665 r= XCheckMaskEvent(display,~0UL,&event);
667 if (motion_deferred) {
668 event_motion(motion_x, motion_y);
675 switch (event.type) {
678 case ButtonRelease: event_button(&event.xbutton); break;
680 case KeyPress: event_key(&event.xkey); break;
682 case ConfigureNotify: event_config(&event.xconfigure); break;
685 motion_x= event.xmotion.x;
686 motion_y= event.xmotion.y;
691 printf("unknown event type %u 0x%x\n", event.type,event.type);