Skip to content

Commit 0b6e58a

Browse files
Make panel constructor more flexible (#38)
1 parent c692d03 commit 0b6e58a

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

dx2/detector.cxx

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,54 @@ void Panel::update(Matrix3d d) {
7474
}
7575

7676
Panel::Panel(json panel_data) {
77+
// We can either get an explicit origin or beam centre and distance
78+
std::vector<std::string> required_keys = {
79+
"fast_axis", "slow_axis", "pixel_size", "image_size",
80+
"trusted_range", "type", "name", "thickness",
81+
"raw_image_offset", "gain", "pedestal", "px_mm_strategy"};
82+
for (const auto &key : required_keys) {
83+
if (panel_data.find(key) == panel_data.end()) {
84+
throw std::invalid_argument(
85+
"Key " + key + " is missing from the input detector panel JSON");
86+
}
87+
}
7788
Vector3d fast{{panel_data["fast_axis"][0], panel_data["fast_axis"][1],
7889
panel_data["fast_axis"][2]}};
7990
Vector3d slow{{panel_data["slow_axis"][0], panel_data["slow_axis"][1],
8091
panel_data["slow_axis"][2]}};
81-
Vector3d origin{{panel_data["origin"][0], panel_data["origin"][1],
82-
panel_data["origin"][2]}};
83-
Matrix3d d_matrix{{fast[0], slow[0], origin[0]},
84-
{fast[1], slow[1], origin[1]},
85-
{fast[2], slow[2], origin[2]}};
86-
origin_ = origin;
92+
fast.normalize();
93+
slow.normalize();
8794
fast_axis_ = fast;
8895
slow_axis_ = slow;
89-
normal_ = fast_axis_.cross(slow_axis_);
90-
d_ = d_matrix;
91-
D_ = d_.inverse();
9296
pixel_size_ = {{panel_data["pixel_size"][0], panel_data["pixel_size"][1]}};
9397
image_size_ = {{panel_data["image_size"][0], panel_data["image_size"][1]}};
98+
Matrix3d d_matrix;
99+
100+
if (panel_data.contains("beam_center") && panel_data.contains("distance")) {
101+
double distance = panel_data["distance"];
102+
std::array<double, 2> beam_center = panel_data["beam_center"];
103+
origin_ = {0., 0., -1.0 * distance};
104+
origin_ -= beam_center[0] * pixel_size_[0] * fast_axis_;
105+
origin_ -= beam_center[1] * pixel_size_[1] * slow_axis_;
106+
normal_ = fast_axis_.cross(slow_axis_);
107+
d_matrix << fast_axis_[0], slow_axis_[0], origin_[0], fast_axis_[1],
108+
slow_axis_[1], origin_[1], fast_axis_[2], slow_axis_[2], origin_[2];
109+
} else {
110+
if (!panel_data.contains("origin")) {
111+
throw std::invalid_argument("Detector panel JSON must contain either "
112+
"origin or beam_center + distance");
113+
}
114+
origin_ = Vector3d{{panel_data["origin"][0], panel_data["origin"][1],
115+
panel_data["origin"][2]}};
116+
d_matrix << fast[0], slow[0], origin_[0], fast[1], slow[1], origin_[1],
117+
fast[2], slow[2], origin_[2];
118+
// origin_ = origin;
119+
normal_ = fast_axis_.cross(slow_axis_);
120+
}
121+
122+
d_ = d_matrix;
123+
D_ = d_.inverse();
124+
94125
image_size_mm_ = {
95126
{image_size_[0] * pixel_size_[0], image_size_[1] * pixel_size_[1]}};
96127
trusted_range_ = {
@@ -200,10 +231,12 @@ const std::map<std::string, Vector3d> axis_map = {
200231
{"-x", Vector3d(-1.0, 0.0, 0.0)},
201232
{"y", Vector3d(0.0, 1.0, 0.0)},
202233
{"-y", Vector3d(0.0, -1.0, 0.0)}};
203-
Panel::Panel(double distance, std::array<double, 2> beam_center,
204-
std::array<double, 2> pixel_size, std::array<int, 2> image_size,
205-
const std::string &fast_axis, const std::string &slow_axis,
206-
double thickness, double mu)
234+
235+
Panel::Panel(double distance, // units mm
236+
std::array<double, 2> beam_center, // units px
237+
std::array<double, 2> pixel_size, // units mm
238+
std::array<int, 2> image_size, const std::string &fast_axis,
239+
const std::string &slow_axis, double thickness, double mu)
207240
: pixel_size_(pixel_size), image_size_(image_size), thickness_(thickness),
208241
mu_(mu) {
209242
if (valid_axes.find(fast_axis) == valid_axes.end()) {

include/dx2/detector.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ class Panel {
7070

7171
protected:
7272
// panel_frame items
73-
Vector3d origin_{{0.0, 0.0, 100.0}}; // needs to be set
73+
Vector3d origin_{{0.0, 0.0, 100.0}}; // needs to be set // units mm
7474
Vector3d fast_axis_{{1.0, 0.0, 0.0}};
7575
Vector3d slow_axis_{{0.0, 1.0, 0.0}};
7676
Vector3d normal_{{0.0, 0.0, 1.0}};
7777
Matrix3d d_{{1, 0, 0}, {0, 1, 0}, {0, 0, 100.0}};
7878
Matrix3d D_{{1, 0, 0}, {0, 1, 0}, {0, 0, 0.01}};
7979
// panel data
80-
std::array<double, 2> pixel_size_{{0.075, 0.075}};
80+
std::array<double, 2> pixel_size_{{0.075, 0.075}}; // units mm
8181
std::array<int, 2> image_size_{{0, 0}};
8282
std::array<double, 2> image_size_mm_{{0, 0}};
8383
std::array<double, 2> trusted_range_{0.0, 65536.0};

0 commit comments

Comments
 (0)