chiark / gitweb /
better error handling and gsl stuff etc.
[moebius2.git] / project.c
index 0921edfb52291b4c0f8addb866e920b026bf6b8c..b68e47393bb1b966579879cf0e54c0dd2513c764 100644 (file)
--- a/project.c
+++ b/project.c
@@ -15,11 +15,8 @@ static Triangle trisbuffer[MAXTRIS], *displaylist[MAXTRIS];
 static int ntris;
 static Vertices conformation;
 
-#define STATIC_MATRIX(x)                                       \
-  static double x[D3][D3];                                     \
-  static gsl_matrix x##_gsl= { D3,D3,D3,&x[0][0] };
-
-STATIC_MATRIX(transform);
+static double transform[D3][D3]= {{1,0,0}, {0,1,0}, {0,0,1}};
+STATIC_GSL_MATRIX(transform);
 
 const char *input_filename;
 
@@ -34,10 +31,16 @@ static void read_input(void) {
 }
 
 static void transform_coordinates(void) {
+  /*
+  static double result[D3];
+  static const gsl_vector result_gsl= { D3,1,&result[0]; }
+
   int v;
+  
 
   FOR_VERTEX(v) {
-  }
+    GA( gsl_blas_dgemv(CblasNoTrans,
+  }*/
 }
 
 static void addtriangle(int va, int vb, int vc) {
@@ -91,19 +94,13 @@ static Display *display;
 static Pixmap pixmap, doublebuffers[2];
 static Window window;
 static GC linegc, fillgc;
-static int wwidth=WSZ, wheight=WSZ, wmaxdim=WSZ, currentbuffer;
-static int ncut;
+static int wwidth=WSZ, wheight=WSZ, wmindim=WSZ, wmaxdim=WSZ;
+static int ncut, currentbuffer, x11depth, x11screen;
 
-static double scale= WSZ * 0.3;
+static double scale= 0.3;
 static double eye_z= -10, eye_x= 0;
 static double cut_z= -9;
 
-static void xdie(int l, const char *str) {
-  fprintf(stderr,"X library call failed, line %d: %s\n", l, str);
-}
-
-#define XA(w) ((w) ? (void)0 : xdie(__LINE__, #w))
-
 static void drawtriangle(const Triangle *t) {
   XPoint points[4];
   int i;
@@ -117,8 +114,8 @@ static void drawtriangle(const Triangle *t) {
     if (z < cut_z) { ncut++; return; }
     
     double zezezp= eye_z / (eye_z - z);
-    points[i].x= scale * (zezezp * (x - eye_x) + eye_x) + WSZ/2;
-    points[i].y= scale * (zezezp *  y                 ) + WSZ/2;
+    points[i].x= scale * wmindim * (zezezp * (x - eye_x) + eye_x) + wwidth/2;
+    points[i].y= scale * wmindim * (zezezp *  y                 ) + wheight/2;
   }
   points[3]= points[0];
 
@@ -129,21 +126,28 @@ static void drawtriangle(const Triangle *t) {
 static const unsigned long core_event_mask=
   ButtonPressMask|ButtonReleaseMask|StructureNotifyMask|ButtonMotionMask;
 
+static void mkpixmaps(void) {
+  for (currentbuffer=0; currentbuffer<2; currentbuffer++) {
+    XA( pixmap= XCreatePixmap(display,window,wwidth,wheight,x11depth) );
+    doublebuffers[currentbuffer]= pixmap;
+  }
+  currentbuffer= 0;
+}
+
 static void display_prepare(void) {
   XGCValues gcv;
   XSetWindowAttributes wa;
-  int screen, depth;
   XVisualInfo vinfo;
   XSizeHints hints;
   
   XA( display= XOpenDisplay(0) );
-  screen= DefaultScreen(display);
-  depth= DefaultDepth(display,screen);
-  XA( XMatchVisualInfo(display,screen,depth, TrueColor,&vinfo) );
+  x11screen= DefaultScreen(display);
+  x11depth= DefaultDepth(display,x11screen);
+  XA( XMatchVisualInfo(display,x11screen,x11depth, TrueColor,&vinfo) );
   
   wa.event_mask= core_event_mask;
   XA( window= XCreateWindow(display, DefaultRootWindow(display),
-                           0,0, wwidth,wheight, 0,depth,
+                           0,0, wwidth,wheight, 0,x11depth,
                            InputOutput, vinfo.visual,
                            CWEventMask, &wa) );
 
@@ -151,22 +155,17 @@ static void display_prepare(void) {
   hints.x= 10;
   hints.y= 10;
   XSetWMNormalHints(display,window,&hints);
-  
-  for (currentbuffer=0; currentbuffer<2; currentbuffer++) {
-    XA( pixmap= XCreatePixmap(display,window,wwidth,wheight,depth) );
-    doublebuffers[currentbuffer]= pixmap;
-  }
+
+  mkpixmaps();
 
   gcv.function= GXcopy;
   gcv.plane_mask= ~0UL;
-  gcv.foreground= WhitePixel(display,screen);
+  gcv.foreground= WhitePixel(display,x11screen);
   linegc= XCreateGC(display,pixmap, GCFunction|GCPlaneMask|GCForeground, &gcv);
   
   gcv.function= GXclear;
   gcv.plane_mask= ~0UL;
   fillgc= XCreateGC(display,pixmap, GCFunction|GCPlaneMask, &gcv);
-
-  currentbuffer= 0;
 }
 
 static void display_conformation(void) {
@@ -232,10 +231,14 @@ static void drag_rotate_delta(double dx, double dy) {
    * and then renormalise.
    */
 
-  STATIC_MATRIX(rotateby);
-  STATIC_MATRIX(qr);
+  static double rotateby[D3][D3]= {{1,0,0},{0,1,0},{-20,-20,1}};
+  STATIC_GSL_MATRIX(rotateby);
+
+  static double qr[D3][D3];
+  STATIC_GSL_MATRIX(qr);
+
   static double tau[D3];
-  static gsl_vector tau_gsl= { D3,1,&tau[0] };
+  STATIC_GSL_VECTOR(tau);
   
   int k;
   
@@ -243,11 +246,11 @@ static void drag_rotate_delta(double dx, double dy) {
   rotateby[0][2]= dx;
   rotateby[1][2]= dy;
   
-  gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1.0,
-                &rotateby_gsl,&transform_gsl, 0.0,&qr_gsl);
-  gsl_linalg_QR_decomp(&qr_gsl, &tau_gsl);
-  gsl_linalg_QR_unpack(&qr_gsl, &tau_gsl,
-                      &transform_gsl, &rotateby_gsl /*dummy*/);
+  GA( gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1.0,
+                    &rotateby_gsl,&transform_gsl, 0.0,&qr_gsl) );
+  GA( gsl_linalg_QR_decomp(&qr_gsl, &tau_gsl) );
+  GA( gsl_linalg_QR_unpack(&qr_gsl, &tau_gsl,
+                          &transform_gsl, &rotateby_gsl /*dummy*/) );
 
   printf("drag_rotate_delta...\n");
   show();
@@ -305,10 +308,25 @@ static void event_motion(int x, int y) {
   drag_position(x,y);
 }
 
-static void event_config(XConfigureEvent *e) { printf("configure\n"); }
+static void event_config(XConfigureEvent *e) {
+  if (e->width == wwidth && e->height == wheight)
+    return;
+
+  wwidth= e->width;  wheight= e->height;
+  wmaxdim= wwidth > wheight ? wwidth : wheight;
+  wmindim= wwidth < wheight ? wwidth : wheight;
+  
+  XA( XSetWindowBackground(display,window,BlackPixel(display,x11screen)) );
+  for (currentbuffer=0; currentbuffer<2; currentbuffer++)
+    XA( XFreePixmap(display, doublebuffers[currentbuffer]) );
+
+  mkpixmaps();
+  show();
+}
 
 int main(int argc, const char *const *argv) {
   XEvent event;
+  int k;
   int motion_deferred=0, motion_x=-1, motion_y=-1;
   
   if (argc != 2 || argv[1][0]=='-') {
@@ -317,6 +335,7 @@ int main(int argc, const char *const *argv) {
   input_filename= argv[1];
 
   read_input();
+  K transform[k][k]= 1.0;
   display_prepare();
   show();