aboutsummaryrefslogtreecommitdiff
path: root/src/refine.c
blob: 45aec8ecea63a6c23d7a1e142502e0b68312bc5c (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
/*
 * refine.c
 *
 * Refine the reconstruction
 *
 * (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 <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

#include "control.h"
#include "displaywindow.h"
#include "image.h"
#include "reproject.h"
#include "mapping.h"

/* A simplex is an array of ten of these */
typedef struct {
	double dax;	double dbx;	double dcx;
	double day;	double dby;	double dcy;
	double daz;	double dbz;	double dcz;
} SimplexVertex;

typedef struct {
	signed int h;	signed int k;	signed int l;
	double dx;	double dy;	double dz;
} Deviation;

void refine_do_sequence(ControlContext *ctx) {

	
}

void refine_do_cell(ControlContext *ctx) {

	SimplexVertex s[10];
	Deviation *d;
	const double delta = 0.1e9;
	int i, nf, f, v_worst;
	double fom_worst;
	
	if ( !ctx->cell_lattice ) {
		displaywindow_error("No reciprocal unit cell has been found.", ctx->dw);
		return;
	}
	
	if ( ctx->images->n_images == 0 ) {
		displaywindow_error("There are no images to refine against.", ctx->dw);
		return;
	}
	
	/* Initialise the simplex */
	s[0].dax = 0.0;	s[0].dbx = 0.0;	s[0].dcx = 0.0;
	s[0].day = 0.0;	s[0].dby = 0.0;	s[0].dcy = 0.0;
	s[0].daz = 0.0;	s[0].dbz = 0.0;	s[0].dcz = 0.0;
	memcpy(&s[1], &s[0], sizeof(SimplexVertex));	s[1].dax = delta;
	memcpy(&s[2], &s[0], sizeof(SimplexVertex));	s[2].dbx = delta;
	memcpy(&s[3], &s[0], sizeof(SimplexVertex));	s[3].dcx = delta;
	memcpy(&s[4], &s[0], sizeof(SimplexVertex));	s[4].day = delta;
	memcpy(&s[5], &s[0], sizeof(SimplexVertex));	s[5].dby = delta;
	memcpy(&s[6], &s[0], sizeof(SimplexVertex));	s[6].dcy = delta;
	memcpy(&s[7], &s[0], sizeof(SimplexVertex));	s[7].daz = delta;
	memcpy(&s[8], &s[0], sizeof(SimplexVertex));	s[8].dbz = delta;
	memcpy(&s[9], &s[0], sizeof(SimplexVertex));	s[9].dcz = delta;
	
	/* Create the table of indicies and deviations */
	nf = 0;
	for ( i=0; i<ctx->images->n_images; i++ ) {
		int j;
		
		if ( !ctx->images->images[i].rflist ) {
			ctx->images->images[i].rflist = reproject_get_reflections(&ctx->images->images[i], ctx->cell_lattice);
		}
		
		for ( j=0; j<ctx->images->images[i].rflist->n_features; j++ ) {
			if ( ctx->images->images[i].rflist->features[j].partner != NULL ) nf++;
		}
		
	}
	printf("RF: There are %i partnered features in total\n", nf);
	
	d = malloc(nf*sizeof(Deviation));
	f = 0;
	for ( i=0; i<ctx->images->n_images; i++ ) {
		
		ImageRecord *image;
		int j;
		
		image = &ctx->images->images[i];
		
		for ( j=0; j<ctx->images->images[i].features->n_features; j++ ) {
		
			ImageFeature *rf;
			double dix, diy, dx, dy;
			double dlx, dly, dlz;
			double old_x, old_y;
			
			rf = &image->rflist->features[j];
			if ( !rf->partner ) continue;
			
			d[f].h = rf->reflection->h;
			d[f].k = rf->reflection->k;
			d[f].l = rf->reflection->l;
			
			/* Determine the difference vector */
			dix = rf->partner->x - rf->x;
			diy = rf->partner->y - rf->y;
			printf("RF: Feature %3i: %3i %3i %3i dev = %+9.5f %+9.5f px ", j, d[f].h, d[f].k, d[f].l, dix, diy);
			
			old_x = rf->partner->x;
			old_y = rf->partner->y;
			rf->partner->x = dix + rf->partner->parent->x_centre;
			rf->partner->y = diy + rf->partner->parent->y_centre;
			mapping_scale(rf->partner, &dx, &dy);
			mapping_rotate(dx, dy, 0.0, &dlx, &dly, &dlz, image->omega, image->tilt);
			rf->partner->x = old_x;
			rf->partner->y = old_y;
			printf("=> %+10.5f %+10.5f %+10.5f nm^-1\n", dlx/1e9, dly/1e9, dlz/1e9);
			
			d[f].dx = dlx;
			d[f].dy = dly;
			d[f].dz = dlz;
			
			f++;
		
		}
		
	}
	assert( f == nf );
	
	/* Find the least favourable vertex of the simplex */
	v_worst = 0;
	fom_worst = 0;
	for ( i=0; i<10; i++ ) {
	
		double fom = 0;
		int j;
		
		for ( j=0; j<nf; j++ ) {
		
			double xdev, ydev, zdev;
			
			xdev = 0;
			ydev = 0;
			zdev = 0;
			
			fom += sqrt(xdev*xdev + ydev*ydev + zdev*zdev);
			
		}
		
	}
	
	ctx->images->images[ctx->dw->cur_image].rflist = NULL;
	reproject_lattice_changed(ctx);
	displaywindow_update(ctx->dw);
		
}