-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDetector.cpp
167 lines (133 loc) · 3.35 KB
/
Detector.cpp
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
// Mandexing: a manual indexing program for crystallographic data.
// Copyright (C) 2017-2018 Helen Ginn
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Please email: vagabond @ hginn.co.uk for more details.
#include "Crystal.h"
#include "Detector.h"
#include "defaults.h"
#include <iostream>
#include "float.h"
Detector::Detector()
{
_beamCentre = make_vec3(-1, -1, STARTING_DISTANCE);
_wavelength = STARTING_WAVELENGTH;
_lookupTree = NULL;
}
void Detector::calculatePositions()
{
vec3 samplePos = make_vec3(0, 0, - 1 / _wavelength);
for (size_t i = 0; i < _xtal->millerCount(); i++)
{
vec3 miller = _xtal->miller(i);
vec3 diff = vec3_subtract_vec3(miller, samplePos);
double mult = _beamCentre.z / diff.z;
vec3_mult(&diff, mult);
_xtal->setPositionForMiller(i, diff);
}
}
double Detector::distToMiller(int i, int x, int y)
{
vec3 pos = _xtal->position(i);
vec3 clickPos = make_vec3(x, y, pos.z);
vec3 diff = vec3_subtract_vec3(clickPos, pos);
return vec3_length(diff);
}
bool Detector::nearMiller(int i, int x, int y)
{
bool onImage = _xtal->shouldDisplayMiller(i);
if (!onImage) return false;
vec3 pos = _xtal->position(i);
if (x >= pos.x - CLOSENESS && x <= pos.x + CLOSENESS &&
y >= pos.y - CLOSENESS && y <= pos.y + CLOSENESS)
{
return true;
}
return false;
}
int Detector::positionNearCoord(int x, int y)
{
x -= _beamCentre.x;
y -= _beamCentre.y;
if (!_lookupTree)
{
for (size_t i = 0; i < _xtal->millerCount(); i++)
{
if (nearMiller(i, x, y))
{
return i;
}
}
return -1;
}
Node *node = _lookupTree;
int drills = 0;
/* Some non-exit condition and I'm too lazy to fix properly */
int max_drills = 50;
while (true && drills < max_drills)
{
for (int i = 0; i < 4; i++)
{
Node *trial = node->nextNodes[i];
if (!trial)
{
goto finished_node_search;
}
if (x > trial->xMin && x < trial->xMax &&
y > trial->yMin && y < trial->yMax)
{
node = trial;
break;
}
}
drills++;
}
finished_node_search:
std::cout << "Drilled down " << drills << " nodes." << std::endl;
int best_n = -1;
double distance = FLT_MAX;
for (size_t i = 0; i < node->nRefls; i++)
{
int n = node->reflPtrs[i];
if (nearMiller(n, x, y))
{
double thisDist = distToMiller(n, x, y);
if (thisDist < distance)
{
distance = thisDist;
best_n = n;
}
}
}
return best_n;
}
void Detector::prepareLookupTable()
{
delete_node(_lookupTree);
_lookupTree = NULL;
size_t count = _xtal->millerCount();
if (!count) return;
Reflection *start = _xtal->refl(0);
_lookupTree = make_node();
prepare_node(_lookupTree, start, count);
recursive_split_node(_lookupTree);
}
Detector::~Detector()
{
if (_lookupTree)
{
delete_node(_lookupTree);
}
}