aboutsummaryrefslogtreecommitdiff
path: root/src/iprtest.c
blob: 4a06a4aeebef2c86d086b75b1a28ac9d6908eb35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * iprtest.c
 *
 * Unit test for IPR
 *
 * (c) 2007-2008 Thomas White <taw27@cam.ac.uk>
 *
 *  dtr - Diffraction Tomography Reconstruction
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include <stdlib.h>
#include <math.h>

#include "basis.h"
#include "reflections.h"
#include "image.h"
#include "reproject.h"
#include "refine.h"
#include "utils.h"
#include "mapping.h"

typedef struct {
	
	ImageRecord *image;
	
	Basis *cell;
	ReflectionList *reflections;
	ReflectionList *gen;
	
} IPRTestContext;

static gint main_window_closed(GtkWidget *window, IPRTestContext *ctx) {
	gtk_exit(0);
	return 0;
}

static gboolean main_window_expose(GtkWidget *drawing, GdkEventExpose *event, IPRTestContext *ctx) {

	cairo_t *cr;
	ImageFeature *f;
	size_t i;
	
	cr = gdk_cairo_create(drawing->window);

	cairo_new_path(cr);
	cairo_rectangle(cr, 0.0, 0.0, 640.0, 640.0);
	cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
	cairo_fill(cr);
	
	f = ctx->image->features->features;
	cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
	for ( i=0; i<ctx->image->features->n_features; i++ ) {
		cairo_new_path(cr);
		cairo_arc(cr, f[i].x, f[i].y, 4.0, 0, 2*M_PI);
		cairo_fill(cr);
	}
	
	f = ctx->image->rflist->features;
	cairo_set_line_width(cr, 1.0);
	for ( i=0; i<ctx->image->rflist->n_features; i++ ) {
	
		cairo_new_path(cr);
		cairo_arc(cr, f[i].x, f[i].y, 8.0, 0, 2*M_PI);
		
		if ( f[i].partner != NULL ) {
		
			cairo_set_source_rgb(cr, 0.0, 0.8, 0.0);
			cairo_stroke(cr);
		
			cairo_new_path(cr);
			cairo_move_to(cr, f[i].x, f[i].y);
			cairo_line_to(cr, f[i].partner->x, f[i].partner->y);
			cairo_stroke(cr);
			
		} else {

			cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
			cairo_stroke(cr);
		
		}
		
	}
	
	cairo_destroy(cr);

	return FALSE;

}

static gboolean main_button_press(GtkWidget *drawing, GdkEventButton *event, IPRTestContext *ctx) {

	if ( (event->type == GDK_BUTTON_PRESS) && (event->button == 1) ) {
		refine_fit_image(ctx->cell, ctx->image);
		image_feature_list_free(ctx->image->rflist);
		reflection_list_from_new_cell(ctx->gen, ctx->cell);
		ctx->image->rflist = reproject_get_reflections(ctx->image, ctx->gen);
		gdk_window_invalidate_rect(drawing->window, &drawing->allocation, FALSE);
	}
	
	return FALSE;
}

int main(int argc, char *argv[]) {

	GtkWidget *window;
	GtkWidget *drawing;
	IPRTestContext *ctx;
	double dlx, dly, dlz;
	
	mapping_rotate(1.0, 0.0, 0.0, &dlx, &dly, &dlz, 0.0, 0.0);
	printf("omega=0.0, tilt=0.0   => image 'x' = { %f %f %f }\n", dlx, dly, dlz);
	mapping_rotate(0.0, 1.0, 0.0, &dlx, &dly, &dlz, 0.0, 0.0);
	printf("omega=0.0, tilt=0.0   => image 'y' = { %f %f %f }\n", dlx, dly, dlz);

	mapping_rotate(1.0, 0.0, 0.0, &dlx, &dly, &dlz, deg2rad(90.0), 0.0);
	printf("omega=90.0, tilt=0.0   => image 'x' = { %f %f %f }\n", dlx, dly, dlz);
	mapping_rotate(0.0, 1.0, 0.0, &dlx, &dly, &dlz, deg2rad(90.0), 0.0);
	printf("omega=90.0, tilt=0.0   => image 'y' = { %f %f %f }\n", dlx, dly, dlz);

	mapping_rotate(1.0, 0.0, 0.0, &dlx, &dly, &dlz, 0.0, deg2rad(90.0));
	printf("omega=0.0, tilt=90.0   => image 'x' = { %f %f %f }\n", dlx, dly, dlz);
	mapping_rotate(0.0, 1.0, 0.0, &dlx, &dly, &dlz, 0.0, deg2rad(90.0));
	printf("omega=0.0, tilt=90.0   => image 'y' = { %f %f %f }\n", dlx, dly, dlz);

	mapping_rotate(1.0, 0.0, 0.0, &dlx, &dly, &dlz, deg2rad(90.0), deg2rad(90.0));
	printf("omega=90.0, tilt=90.0   => image 'x' = { %f %f %f }\n", dlx, dly, dlz);
	mapping_rotate(0.0, 1.0, 0.0, &dlx, &dly, &dlz, deg2rad(90.0), deg2rad(90.0));
	printf("omega=90.0, tilt=90.0   => image 'y' = { %f %f %f }\n", dlx, dly, dlz);
	
	gtk_init(&argc, &argv);
	
	ctx = malloc(sizeof(IPRTestContext));
	
	ctx->image = malloc(sizeof(ImageRecord));
	ctx->image->image = NULL;
	ctx->image->tilt = deg2rad(90.0);
	ctx->image->omega = deg2rad(90.0);
	ctx->image->slop = deg2rad(0.0);
	ctx->image->fmode = FORMULATION_PIXELSIZE;
	ctx->image->pixel_size = 0.6e8;
	ctx->image->camera_len = 0.0;
	ctx->image->lambda = lambda(1000000);
	ctx->image->resolution = 1.0;
	ctx->image->width = 640;
	ctx->image->height = 640;
	ctx->image->x_centre = 320;
	ctx->image->y_centre = 320;
	ctx->image->features = NULL;
	ctx->image->rflist = NULL;
	
	ctx->cell = malloc(sizeof(Basis));
	/* The "true" cell */
	ctx->cell->a.x = 5.0e9;  ctx->cell->a.y = 0.0;  ctx->cell->a.z = 0.0;
	ctx->cell->b.x = 0.0;  ctx->cell->b.y = 5.0e9;  ctx->cell->b.z = 0.0;
	ctx->cell->c.x = 0.0;  ctx->cell->c.y = 0.0;  ctx->cell->c.z = 5.0e9;
	ctx->reflections = reflection_list_from_cell(ctx->cell);
	ctx->image->features = reproject_get_reflections(ctx->image, ctx->reflections);
	/* The "model" cell */
	ctx->cell->a.x = 5.2e9;  ctx->cell->a.y = 0.1e9;  ctx->cell->a.z = 0.0;
	ctx->cell->b.x = 0.1e9;  ctx->cell->b.y = 4.5e9;  ctx->cell->b.z = 0.0;
	ctx->cell->c.x = 0.0;  ctx->cell->c.y = 0.0;  ctx->cell->c.z = 5.0e9;
	ctx->gen = reflection_list_from_cell(ctx->cell);
	ctx->image->rflist = reproject_get_reflections(ctx->image, ctx->gen);
	
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window), "IPR Test");
	gtk_widget_set_size_request(GTK_WIDGET(window), 640, 640);
	g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(main_window_closed), ctx);
	gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
	
	drawing = gtk_drawing_area_new();
	gtk_container_add(GTK_CONTAINER(window), drawing);
	g_signal_connect(G_OBJECT(drawing), "expose_event", G_CALLBACK(main_window_expose), ctx);
	gtk_widget_add_events(drawing, GDK_BUTTON_PRESS_MASK);
	g_signal_connect(GTK_OBJECT(drawing), "button_press_event", G_CALLBACK(main_button_press), ctx);
	
	gtk_widget_show_all(window);
	
	gtk_main();
	
	return 0;
	
}

/* Dummy function stubs */
#include "displaywindow.h"
#include "gtk-valuegraph.h"
void displaywindow_update_imagestack(DisplayWindow *dw) { };
void displaywindow_enable_cell_functions(DisplayWindow *dw, gboolean g) { };
void displaywindow_update(DisplayWindow *dw) { };
void displaywindow_error(const char *msg, DisplayWindow *dw) { };
guint gtk_value_graph_get_type() { return 0; };
GtkWidget *gtk_value_graph_new() { return NULL; };
void gtk_value_graph_set_data(GtkValueGraph *vg, double *data, unsigned int n) { };