From 4676d2c74e1a29d8aa5c0da56cb67ba7a6bb7e0f Mon Sep 17 00:00:00 2001 From: taw27 Date: Tue, 30 Oct 2007 17:29:17 +0000 Subject: Refinement framework git-svn-id: svn://cook.msm.cam.ac.uk:745/diff-tomo/dtr@185 bf6ca9ba-c028-0410-8290-897cf20841d1 --- src/control.c | 1 + src/control.h | 9 ++-- src/displaywindow.c | 2 +- src/gtk-valuegraph.c | 2 +- src/image.c | 10 +++- src/image.h | 6 +++ src/mapping.c | 2 +- src/mapping.h | 1 + src/refine.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++----- src/refine.h | 4 +- src/reproject.c | 5 +- 11 files changed, 152 insertions(+), 27 deletions(-) diff --git a/src/control.c b/src/control.c index 6efae0f..f5b5b12 100644 --- a/src/control.c +++ b/src/control.c @@ -29,6 +29,7 @@ ControlContext *control_ctx_new() { ctx->dirax = NULL; ctx->images = image_list_new(); ctx->reflectionlist = NULL; + ctx->refine_window = NULL; return ctx; diff --git a/src/control.h b/src/control.h index 7de8025..df6eb02 100644 --- a/src/control.h +++ b/src/control.h @@ -88,12 +88,6 @@ typedef struct cctx_struct { GtkWidget *checkbox_finecentering; GtkWidget *cache_file_selector; - /* IPR stuff */ - int ipr_cur_image; - struct imagedisplay_struct *ipr_id; - struct reflectionlist_struct *ipr_lat; - struct basis_struct *ipr_basis; - /* DirAx low-level stuff */ GIOChannel *dirax; int dirax_pty; @@ -110,6 +104,9 @@ typedef struct cctx_struct { int reproject_cur_image; struct imagedisplay_struct *reproject_id; + /* Refinement stuff */ + GtkWidget *refine_window; + } ControlContext; extern ControlContext *control_ctx_new(void); diff --git a/src/displaywindow.c b/src/displaywindow.c index 1ef4aab..324b921 100644 --- a/src/displaywindow.c +++ b/src/displaywindow.c @@ -1088,7 +1088,7 @@ static gint displaywindow_setaxis(GtkWidget *widget, DisplayWindow *dw) { } static gint displaywindow_refine(GtkWidget *widget, DisplayWindow *dw) { - refine_open(dw); + refine_open(dw->ctx); return 0; } diff --git a/src/gtk-valuegraph.c b/src/gtk-valuegraph.c index 47c6060..b5d2bf1 100644 --- a/src/gtk-valuegraph.c +++ b/src/gtk-valuegraph.c @@ -164,7 +164,7 @@ static gint gtk_value_graph_draw(GtkWidget *graph, GdkEventExpose *event, gpoint cairo_line_to(cr, bw_left+((double)i/vg->xmax)*(width-bw_left-bw_right), height-bw_bottom-1-scale*(vg->data[i]-vg->ymin)); } - cairo_set_line_width(cr, 1.0); + cairo_set_line_width(cr, 0.5); cairo_set_source_rgb(cr, 0.0, 0.0, 1.0); cairo_stroke(cr); diff --git a/src/image.c b/src/image.c index 00848cf..96afe3c 100644 --- a/src/image.c +++ b/src/image.c @@ -65,7 +65,8 @@ ImageList *image_list_new() { } -void image_add_feature(ImageFeatureList *flist, double x, double y, ImageRecord *parent, double intensity) { +void image_add_feature_index(ImageFeatureList *flist, double x, double y, ImageRecord *parent, double intensity, + signed int h, signed int k, signed int l) { if ( flist->features ) { flist->features = realloc(flist->features, (flist->n_features+1)*sizeof(ImageFeature)); @@ -80,11 +81,18 @@ void image_add_feature(ImageFeatureList *flist, double x, double y, ImageRecord flist->features[flist->n_features].parent = parent; flist->features[flist->n_features].partner = NULL; flist->features[flist->n_features].partner_d = 0.0; + flist->features[flist->n_features].h = h; + flist->features[flist->n_features].k = k; + flist->features[flist->n_features].l = l; flist->n_features++; } +void image_add_feature(ImageFeatureList *flist, double x, double y, ImageRecord *parent, double intensity) { + image_add_feature_index(flist, x, y, parent, intensity, 0, 0, 0); +} + ImageFeatureList *image_feature_list_new() { ImageFeatureList *flist; diff --git a/src/image.h b/src/image.h index c87498c..87f330e 100644 --- a/src/image.h +++ b/src/image.h @@ -25,6 +25,10 @@ typedef struct imagefeature_struct { double y; double intensity; + signed int h; + signed int k; + signed int l; + struct imagefeature_struct *partner; /* Partner for this feature (in another feature list) or NULL */ double partner_d; /* Distance between this feature and its partner, if any. */ @@ -71,6 +75,8 @@ extern int image_add(ImageList *list, uint16_t *image, int width, int height, do extern ImageFeatureList *image_feature_list_new(void); extern void image_feature_list_free(ImageFeatureList *flist); extern void image_add_feature(ImageFeatureList *flist, double x, double y, ImageRecord *parent, double intensity); +extern void image_add_feature_index(ImageFeatureList *flist, double x, double y, ImageRecord *parent, double intensity, + signed int h, signed int k, signed int l); #endif /* IMAGE_H */ diff --git a/src/mapping.c b/src/mapping.c index def29c4..668bfc4 100644 --- a/src/mapping.c +++ b/src/mapping.c @@ -21,7 +21,7 @@ #include "displaywindow.h" #include "cache.h" -static int mapping_map_to_space(ImageFeature *refl, double *ddx, double *ddy, double *ddz, double *twotheta) { +int mapping_map_to_space(ImageFeature *refl, double *ddx, double *ddy, double *ddz, double *twotheta) { /* "Input" space */ double d, x, y; diff --git a/src/mapping.h b/src/mapping.h index ca4a087..380e876 100644 --- a/src/mapping.h +++ b/src/mapping.h @@ -21,6 +21,7 @@ extern ReflectionList *mapping_create(ControlContext *ctx); extern void mapping_adjust_axis(ControlContext *ctx, double offset); +extern int mapping_map_to_space(ImageFeature *refl, double *ddx, double *ddy, double *ddz, double *twotheta); #endif /* MAPPING_H */ diff --git a/src/refine.c b/src/refine.c index ede96cb..aaefdf9 100644 --- a/src/refine.c +++ b/src/refine.c @@ -24,17 +24,17 @@ #include "reflections.h" #include "image.h" #include "reproject.h" +#include "control.h" +#include "mapping.h" +/* Return the root sum squared deviation distance for all the "reprojectable" features in an image */ static double refine_image_deviation(ImageRecord *image, ReflectionList *cell_lattice) { ImageFeatureList *rflist; ImageFeatureList *flist; - double xc, yc; int i; double total; - xc = image->x_centre; - yc = image->y_centre; rflist = reproject_get_reflections(image, cell_lattice); flist = image->features; @@ -56,12 +56,59 @@ static double refine_image_deviation(ImageRecord *image, ReflectionList *cell_la } -void refine_open(DisplayWindow *dw) { +/* Use the IPR algorithm to make "cell" fit the given image */ +static void refine_fit_image(Basis *cell, ImageRecord *image) { + + ImageFeatureList *rflist; + ImageFeatureList *flist; + int i; + ReflectionList *cell_lattice; + + cell_lattice = reflection_list_from_cell(cell); + rflist = reproject_get_reflections(image, cell_lattice); + flist = image->features; + reproject_partner_features(rflist, image); + + for ( i=0; in_features; i++ ) { + + double dx, dy; + double x, y, z, twotheta; + double old_x, old_y; + + /* Skip if no partner */ + if ( !rflist->features[i].partner ) continue; + + /* Determine the difference vector */ + dx = rflist->features[i].partner->x - rflist->features[i].x; + dy = rflist->features[i].partner->y - rflist->features[i].y; + + /* Map the difference vector to the relevant tilted plane */ + old_x = rflist->features[i].partner->x; + old_y = rflist->features[i].partner->y; + rflist->features[i].partner->x = dx; + rflist->features[i].partner->y = dy; + mapping_map_to_space(rflist->features[i].partner, &x, &y, &z, &twotheta); + rflist->features[i].partner->x = old_x; + rflist->features[i].partner->y = old_y; + + printf("Feature %3i: %3i %3i %3i dev=%8e %8e %8e (%5f mrad)\n", + i, rflist->features[i].h, rflist->features[i].k, rflist->features[i].l, x, y, z, twotheta*1e3); + + /* Work out weightings for a, b and c */ + + + } + + image_feature_list_free(rflist); + +} + +/* Display a graph of root sum squared deviation distance against some other parameter */ +static void refine_show_graph(ControlContext *ctx, int n) { GtkWidget *window; GtkWidget *graph; double old_tilt; - int n; double *values; size_t idx; double tilt; @@ -71,16 +118,16 @@ void refine_open(DisplayWindow *dw) { gtk_window_set_title(GTK_WINDOW(window), "Refinement Graph"); graph = gtk_value_graph_new(); - dw->ctx->cell_lattice = reflection_list_from_cell(dw->ctx->cell); - n = 0; - idx = 0; - old_tilt = dw->ctx->images->images[n].tilt; + ctx->cell_lattice = reflection_list_from_cell(ctx->cell); + old_tilt = ctx->images->images[n].tilt; values = malloc(401*sizeof(double)); - for ( tilt=old_tilt-0.2; tilt<=old_tilt+0.2; tilt+=0.001 ) { - dw->ctx->images->images[n].tilt = tilt; - values[idx++] = refine_image_deviation(&dw->ctx->images->images[n], dw->ctx->cell_lattice); + tilt = old_tilt-0.2; + for ( idx=0; idx<401; idx++ ) { + ctx->images->images[n].tilt = tilt; + values[idx] = refine_image_deviation(&ctx->images->images[n], ctx->cell_lattice); + tilt += 0.001; } - dw->ctx->images->images[n].tilt = old_tilt; + ctx->images->images[n].tilt = old_tilt; gtk_value_graph_set_data(GTK_VALUE_GRAPH(graph), values, idx); gtk_container_add(GTK_CONTAINER(window), graph); @@ -88,3 +135,67 @@ void refine_open(DisplayWindow *dw) { } +static gint refine_graph(GtkWidget *step_button, ControlContext *ctx) { + refine_show_graph(ctx, ctx->reproject_cur_image); + return 0; +} + +static gint refine_step(GtkWidget *step_button, ControlContext *ctx) { + + if ( ctx->reproject_id ) { + refine_fit_image(ctx->cell, &ctx->images->images[ctx->reproject_cur_image]); + } else { + displaywindow_error("Please first open the reprojection window and select the image to fit", ctx->dw); + } + + return 0; + +} + +static gint refine_response(GtkWidget *refine_window, gint response, ControlContext *ctx) { + + ctx->refine_window = NULL; + gtk_widget_destroy(refine_window); + + return 0; + +} + +void refine_open(ControlContext *ctx) { + + GtkWidget *vbox; + GtkWidget *hbox; + GtkWidget *table; + GtkWidget *step_button; + GtkWidget *graph_button; + + if ( ctx->refine_window ) return; + + ctx->refine_window = gtk_dialog_new_with_buttons("Refine Reconstruction", GTK_WINDOW(ctx->dw->window), + GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, NULL); + gtk_window_set_default_size(GTK_WINDOW(ctx->refine_window), 256, -1); + + vbox = gtk_vbox_new(FALSE, 0); + hbox = gtk_hbox_new(TRUE, 0); + gtk_box_pack_start(GTK_BOX(GTK_DIALOG(ctx->refine_window)->vbox), GTK_WIDGET(hbox), FALSE, FALSE, 7); + gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), FALSE, FALSE, 5); + + table = gtk_table_new(2, 1, FALSE); + gtk_table_set_row_spacings(GTK_TABLE(table), 5); + gtk_table_set_col_spacings(GTK_TABLE(table), 5); + gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(table), FALSE, FALSE, 0); + + step_button = gtk_button_new_with_label("Refine Lattice to Fit Current Pattern"); + gtk_table_attach_defaults(GTK_TABLE(table), step_button, 1, 2, 1, 2); + g_signal_connect(G_OBJECT(step_button), "clicked", G_CALLBACK(refine_step), ctx); + + graph_button = gtk_button_new_with_label("Show Graph of Deviation Against Parameter"); + gtk_table_attach_defaults(GTK_TABLE(table), graph_button, 1, 2, 2, 3); + g_signal_connect(G_OBJECT(graph_button), "clicked", G_CALLBACK(refine_graph), ctx); + + g_signal_connect(G_OBJECT(ctx->refine_window), "response", G_CALLBACK(refine_response), ctx); + gtk_widget_show_all(ctx->refine_window); + + +} + diff --git a/src/refine.h b/src/refine.h index 679c47f..f52d130 100644 --- a/src/refine.h +++ b/src/refine.h @@ -16,9 +16,9 @@ #include #endif -#include "displaywindow.h" +#include "control.h" -extern void refine_open(DisplayWindow *dw); +extern void refine_open(ControlContext *ctx); #endif /* REFINE_H */ diff --git a/src/reproject.c b/src/reproject.c index e919523..c41f173 100644 --- a/src/reproject.c +++ b/src/reproject.c @@ -160,7 +160,8 @@ ImageFeatureList *reproject_get_reflections(ImageRecord *image, ReflectionList * if ( (xtest>=0) && (xtestwidth) && (ytest>=0) && (ytestheight) ) { /* Record the reflection */ - image_add_feature(flist, x, y, image, reflection->intensity); + image_add_feature_index(flist, x, y, image, reflection->intensity, + reflection->h, reflection->k, reflection->l); //printf("Reflection at %f, %f\n", x, y); } /* else it's outside the picture somewhere */ @@ -198,7 +199,7 @@ static ImageFeature *reproject_find_partner(ImageFeature *feature, ImageRecord * } - if ( dmin <= 70.0 ) { + if ( dmin <= 20.0 ) { *d = dmin; return &image->features->features[closest]; } -- cgit v1.2.3