-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitk_txt.rs
More file actions
284 lines (264 loc) · 9.36 KB
/
Copy pathitk_txt.rs
File metadata and controls
284 lines (264 loc) · 9.36 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! Reader for the **Insight Transform File V1.0** plain-text format used
//! by ITK and ANTs for affine transforms. Format is the same parameter
//! convention as `*Composite.h5`'s `AffineTransform_*_3_3` entries — 12
//! parameters (9 row-major matrix + 3 translation) plus 3 fixed
//! parameters (center of rotation) — but encoded as ASCII rather than
//! HDF5.
//!
//! The format is **affine-only by spec**; displacement-field components
//! are returned by [`read_itk_h5`](crate::read_itk_h5) only and surface
//! as [`XfmError::UnsupportedTransformType`] here.
//!
//! Example file:
//!
//! ```text
//! #Insight Transform File V1.0
//! #Transform 0
//! Transform: MatrixOffsetTransformBase_double_3_3
//! Parameters: 1.06 -0.0066 -0.0071 0.034 1.03 0.00005 0.0056 0.046 1.04 -0.23 -4.35 2.80
//! FixedParameters: 2.14 4.70 24.39
//! ```
//!
//! The file may declare multiple `#Transform N` sections; they're
//! returned in order as a multi-component [`TransformChain`], applied as
//! `chain(p) = c_N(c_{N-1}(... c_1(p)))` matching the same composition
//! semantics as the h5 reader.
use std::fs;
use std::path::Path;
use nalgebra::{Matrix3, Vector3};
use crate::affine::Affine3;
use crate::chain::TransformChain;
use crate::error::{Result, XfmError};
/// Read an Insight Transform File V1.0 (`.txt`) and return a
/// [`TransformChain`] in RAS+ mm.
pub fn read_itk_txt(path: &Path) -> Result<TransformChain> {
let body = fs::read_to_string(path).map_err(|e| XfmError::InvalidFile {
path: path.to_path_buf(),
reason: format!("read failed: {e}"),
})?;
parse(&body, path)
}
#[derive(Default)]
struct Section {
transform_type: Option<String>,
parameters: Option<Vec<f64>>,
fixed_parameters: Option<Vec<f64>>,
}
impl Section {
fn is_empty(&self) -> bool {
self.transform_type.is_none()
&& self.parameters.is_none()
&& self.fixed_parameters.is_none()
}
}
fn parse(body: &str, path: &Path) -> Result<TransformChain> {
// Verify the magic header.
let mut lines = body.lines();
match lines.next() {
Some(first) if first.trim_start().starts_with("#Insight Transform File") => {}
_ => {
return Err(XfmError::InvalidFile {
path: path.to_path_buf(),
reason: "missing '#Insight Transform File' magic".into(),
})
}
}
// Walk remaining lines, splitting on `#Transform N` boundaries.
let mut sections: Vec<Section> = Vec::new();
let mut current = Section::default();
for line in lines {
let trimmed = line.trim();
if trimmed.is_empty() {
continue;
}
if trimmed.starts_with("#Transform") {
if !current.is_empty() {
sections.push(std::mem::take(&mut current));
}
continue;
}
if let Some(rest) = trimmed.strip_prefix("Transform:") {
current.transform_type = Some(rest.trim().to_string());
} else if let Some(rest) = trimmed.strip_prefix("Parameters:") {
current.parameters = Some(parse_floats(rest));
} else if let Some(rest) = trimmed.strip_prefix("FixedParameters:") {
current.fixed_parameters = Some(parse_floats(rest));
} else if trimmed.starts_with('#') {
// Other comments are ignored.
}
}
if !current.is_empty() {
sections.push(current);
}
if sections.is_empty() {
return Err(XfmError::InvalidFile {
path: path.to_path_buf(),
reason: "no #Transform sections in file".into(),
});
}
// ITK Insight Transform File V1.0 stores `#Transform N` sections in
// queue-addition order, matching the h5 convention. ITK's
// CompositeTransform applies its queue last-first (rbegin → rend), so we
// push sections in *reverse* file order to align with our chain's
// stored-order apply semantics.
let mut chain = TransformChain::new();
for (idx, section) in sections.into_iter().enumerate().rev() {
let ttype = section
.transform_type
.ok_or_else(|| XfmError::InvalidFile {
path: path.to_path_buf(),
reason: format!("section {idx} is missing 'Transform:' line"),
})?;
if ttype.starts_with("AffineTransform")
|| ttype.starts_with("MatrixOffsetTransformBase")
|| ttype.starts_with("ScaleSkewVersor3DTransform")
|| ttype.starts_with("Rigid3DTransform")
|| ttype.starts_with("Similarity3DTransform")
|| ttype.starts_with("Euler3DTransform")
|| ttype.starts_with("VersorRigid3DTransform")
|| ttype.starts_with("ScaleVersor3DTransform")
|| ttype.starts_with("FixedCenterOfRotationAffineTransform")
{
let params = section.parameters.ok_or_else(|| {
XfmError::MalformedParameters(format!(
"section {idx} '{ttype}' missing 'Parameters:' line"
))
})?;
let fixed = section.fixed_parameters.unwrap_or_default();
chain.push_affine(build_affine(&ttype, ¶ms, &fixed)?);
} else {
return Err(XfmError::UnsupportedTransformType(ttype));
}
}
Ok(chain)
}
fn build_affine(ttype: &str, params: &[f64], fixed: &[f64]) -> Result<Affine3> {
if params.len() != 12 {
return Err(XfmError::MalformedParameters(format!(
"{ttype} expects 12 Parameters (9 matrix + 3 translation), got {}",
params.len()
)));
}
let center = match fixed.len() {
0 => Vector3::zeros(),
3 => Vector3::new(fixed[0], fixed[1], fixed[2]),
n => {
return Err(XfmError::MalformedParameters(format!(
"{ttype} FixedParameters must have 0 or 3 floats, got {n}"
)))
}
};
let m = Matrix3::new(
params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7],
params[8],
);
let t = Vector3::new(params[9], params[10], params[11]);
Ok(Affine3::from_itk_components(m, t, center))
}
fn parse_floats(s: &str) -> Vec<f64> {
s.split_whitespace()
.filter_map(|tok| tok.parse::<f64>().ok())
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
fn write_temp(body: &str) -> NamedTempFile {
let mut f = tempfile::Builder::new().suffix(".txt").tempfile().unwrap();
f.write_all(body.as_bytes()).unwrap();
f.flush().unwrap();
f
}
#[test]
fn parse_zero_center_identity() {
let body = "\
#Insight Transform File V1.0
#Transform 0
Transform: AffineTransform_double_3_3
Parameters: 1 0 0 0 1 0 0 0 1 0 0 0
FixedParameters: 0 0 0
";
let f = write_temp(body);
let chain = read_itk_txt(f.path()).unwrap();
assert_eq!(chain.components.len(), 1);
// Identity round-trips any point.
let p = chain.map_point([3.0, -1.0, 7.5]);
assert!((p[0] - 3.0).abs() < 1e-12);
assert!((p[1] + 1.0).abs() < 1e-12);
assert!((p[2] - 7.5).abs() < 1e-12);
}
#[test]
fn parse_translation_in_lps() {
// ITK translation [1, 2, 3] in LPS → RAS [-1, -2, 3].
let body = "\
#Insight Transform File V1.0
#Transform 0
Transform: AffineTransform_double_3_3
Parameters: 1 0 0 0 1 0 0 0 1 1 2 3
FixedParameters: 0 0 0
";
let f = write_temp(body);
let chain = read_itk_txt(f.path()).unwrap();
let p = chain.map_point([0.0, 0.0, 0.0]);
assert!((p[0] + 1.0).abs() < 1e-12);
assert!((p[1] + 2.0).abs() < 1e-12);
assert!((p[2] - 3.0).abs() < 1e-12);
}
#[test]
fn parse_two_sections_in_order() {
// Two translations that should compose: [1,0,0] then [0,2,0],
// both in LPS → [-1,-2,0] in RAS+ when applied to origin.
let body = "\
#Insight Transform File V1.0
#Transform 0
Transform: AffineTransform_double_3_3
Parameters: 1 0 0 0 1 0 0 0 1 1 0 0
FixedParameters: 0 0 0
#Transform 1
Transform: AffineTransform_double_3_3
Parameters: 1 0 0 0 1 0 0 0 1 0 2 0
FixedParameters: 0 0 0
";
let f = write_temp(body);
let chain = read_itk_txt(f.path()).unwrap();
assert_eq!(chain.components.len(), 2);
let p = chain.map_point([0.0, 0.0, 0.0]);
assert!((p[0] + 1.0).abs() < 1e-12);
assert!((p[1] + 2.0).abs() < 1e-12);
}
#[test]
fn rejects_displacement_field_type() {
let body = "\
#Insight Transform File V1.0
#Transform 0
Transform: DisplacementFieldTransform_double_3_3
Parameters: 1 0 0 0 1 0 0 0 1 0 0 0
FixedParameters: 0 0 0
";
let f = write_temp(body);
let err = read_itk_txt(f.path()).unwrap_err();
assert!(matches!(err, XfmError::UnsupportedTransformType(_)));
}
#[test]
fn rejects_missing_magic() {
let body = "Parameters: 1 0 0 0 1 0 0 0 1 0 0 0\n";
let f = write_temp(body);
let err = read_itk_txt(f.path()).unwrap_err();
assert!(matches!(err, XfmError::InvalidFile { .. }));
}
#[test]
fn rejects_wrong_parameter_count() {
let body = "\
#Insight Transform File V1.0
#Transform 0
Transform: AffineTransform_double_3_3
Parameters: 1 0 0 0 1 0 0 0 1 0 0
FixedParameters: 0 0 0
";
let f = write_temp(body);
let err = read_itk_txt(f.path()).unwrap_err();
assert!(matches!(err, XfmError::MalformedParameters(_)));
}
}