aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: a89fdf933a95e5afb41cc3349ee6058bf78530d8 (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
/*
 * main.c
 *
 * The Top Level Source File
 *
 * (c) 2007 Thomas White <taw27@cam.ac.uk>
 *  dtr - Diffraction Tomography Reconstruction
 *
 */

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

#define _GNU_SOURCE
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <math.h>
#include <gdk/gdkgl.h>
#include <gtk/gtkgl.h>
#include <sys/stat.h>

#include "displaywindow.h"
#include "reflections.h"
#include "mrc.h"
#include "qdrp.h"
#include "ipr.h"

static gint main_method_window_response(GtkWidget *method_window, gint response, ControlContext *ctx) {

	if ( response == GTK_RESPONSE_OK ) {
	
		int val = -1;
		
		switch ( gtk_combo_box_get_active(GTK_COMBO_BOX(ctx->combo_algorithm)) ) {
			case 0 : ctx->rmode = RECONSTRUCTION_MAPPING; break;
			case 1 : ctx->rmode = RECONSTRUCTION_PREDICTION; break;
			default: abort();
		}
		
		switch ( gtk_combo_box_get_active(GTK_COMBO_BOX(ctx->combo_peaksearch)) ) {
			case 0 : ctx->psmode = PEAKSEARCH_THRESHOLD; break;
			case 1 : ctx->psmode = PEAKSEARCH_ADAPTIVE_THRESHOLD; break;
			case 2 : ctx->psmode = PEAKSEARCH_LSQ; break;
			case 3 : ctx->psmode = PEAKSEARCH_ZAEFFERER; break;
			case 4 : ctx->psmode = PEAKSEARCH_ITERATIVE; break;
			default: abort();
		}
		
		gtk_widget_destroy(method_window);
		while ( gtk_events_pending() ) gtk_main_iteration();

		if ( ctx->inputfiletype == INPUT_QDRP ) {
			val = qdrp_read(ctx);
		} else if ( ctx->inputfiletype == INPUT_MRC ) {
			val = mrc_read(ctx);
		}
		
		if ( ctx->rmode == RECONSTRUCTION_PREDICTION ) {
			ipr_reduce(ctx);
		}
		
		if ( val == 0 ) {
			displaywindow_open(ctx);
		} else {
			fprintf(stderr, "Reconstruction failed.\n");
			gtk_exit(0);
		}
		
	} else {
		gtk_exit(0);
	}
	
	return 0;

}

void main_method_dialog_open(ControlContext *ctx) {

	GtkWidget *method_window;
	GtkWidget *vbox;
	GtkWidget *hbox;
	GtkWidget *table;
	GtkWidget *method_label;
	GtkWidget *peaksearch_label;
	
	method_window = gtk_dialog_new_with_buttons("Reconstruction Parameters", NULL,
		GTK_DIALOG_DESTROY_WITH_PARENT,	GTK_STOCK_CANCEL, GTK_RESPONSE_CLOSE, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
	
	vbox = gtk_vbox_new(FALSE, 0);
	hbox = gtk_hbox_new(TRUE, 0);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(method_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(3, 2, FALSE);

	method_label = gtk_label_new("Reconstruction Algorithm: ");
	gtk_table_attach_defaults(GTK_TABLE(table), method_label, 1, 2, 1, 2);
	gtk_misc_set_alignment(GTK_MISC(method_label), 1, 0.5);
	ctx->combo_algorithm = gtk_combo_box_new_text();
	gtk_combo_box_append_text(GTK_COMBO_BOX(ctx->combo_algorithm), "Feature Detection and Mapping");
	gtk_combo_box_append_text(GTK_COMBO_BOX(ctx->combo_algorithm), "Iterative Prediction and Refinement");
	gtk_combo_box_set_active(GTK_COMBO_BOX(ctx->combo_algorithm), 1);
	gtk_table_attach_defaults(GTK_TABLE(table), ctx->combo_algorithm, 2, 3, 1, 2);
	gtk_table_set_row_spacing(GTK_TABLE(table), 1, 5);
	
	peaksearch_label = gtk_label_new("Peak Search: ");
	gtk_table_attach_defaults(GTK_TABLE(table), peaksearch_label, 1, 2, 2, 3);
	gtk_misc_set_alignment(GTK_MISC(peaksearch_label), 1, 0.5);
	ctx->combo_peaksearch = gtk_combo_box_new_text();
	gtk_combo_box_append_text(GTK_COMBO_BOX(ctx->combo_peaksearch), "Simple Thresholding");
	gtk_combo_box_append_text(GTK_COMBO_BOX(ctx->combo_peaksearch), "Adaptive Thresholding");
	gtk_combo_box_append_text(GTK_COMBO_BOX(ctx->combo_peaksearch), "Least-Squares Fit");
	gtk_combo_box_append_text(GTK_COMBO_BOX(ctx->combo_peaksearch), "Zaefferer Gradient Search");
	gtk_combo_box_append_text(GTK_COMBO_BOX(ctx->combo_peaksearch), "Iterative Statistical");
	gtk_combo_box_set_active(GTK_COMBO_BOX(ctx->combo_peaksearch), 3);
	gtk_table_attach_defaults(GTK_TABLE(table), ctx->combo_peaksearch, 2, 3, 2, 3);

	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(table), TRUE, TRUE, 5);
	
	g_signal_connect(G_OBJECT(method_window), "response", G_CALLBACK(main_method_window_response), ctx);
	
	gtk_widget_show_all(method_window);

}


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

	char *filename;
	ControlContext *ctx;
	InputFileType type;
	struct stat stat_buffer;

	gtk_init(&argc, &argv);
	
	if ( gtk_gl_init_check(&argc, &argv) == FALSE ) {
		fprintf(stderr, "Could not initialise gtkglext\n");
		return 1;
	}
	
	if ( gdk_gl_query_extension() == FALSE ) {
		fprintf(stderr, "OpenGL not supported\n");
		return 1;
	}
	
	if ( argc != 2 ) {
		fprintf(stderr, "Syntax: %s [<MRC file> | qdrp.rc]\n", argv[0]);
		return 1;
	}
	
	filename = basename(argv[1]);
	ctx = malloc(sizeof(ControlContext));
	type = INPUT_NONE;
	
	if ( strcmp(filename, "qdrp.rc") == 0 ) {
		printf("QDRP input file detected.\n");
		ctx->inputfiletype = INPUT_QDRP;
	} else if ( strcmp(filename+(strlen(filename)-4), ".mrc") == 0 ) {
		printf("MRC tomography file detected.\n");
		ctx->inputfiletype = INPUT_MRC;
	} else {
		fprintf(stderr, "Input file must either be an MRC tomography file or a QDRP-style control file.\n");
		return 1;
	}
	ctx->filename = strdup(argv[1]);
	ctx->max_d = 0;
	
	if ( stat(argv[1], &stat_buffer) == -1 ) {
		fprintf(stderr, "File '%s' not found\n", argv[1]);
		return 1;
	}
	
	main_method_dialog_open(ctx);
		
	gtk_main();
	
	free(ctx->filename);
	free(ctx);
	
	return 0;
	
}