openigtlink-rust 0.4.1

Rust implementation of the OpenIGTLink protocol for image-guided therapy
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! POLYDATA message type implementation
//!
//! The POLYDATA message is used to transfer 3D polygon/mesh data for surgical navigation,
//! visualization of anatomical structures, or surgical planning.

use crate::error::{IgtlError, Result};
use crate::protocol::message::Message;
use bytes::{Buf, BufMut};

/// Attribute type for polygon data
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttributeType {
    Point = 0,
    Cell = 1,
}

impl AttributeType {
    /// Create from type value
    pub fn from_u8(value: u8) -> Result<Self> {
        match value {
            0 => Ok(AttributeType::Point),
            1 => Ok(AttributeType::Cell),
            _ => Err(IgtlError::InvalidSize {
                expected: 0,
                actual: value as usize,
            }),
        }
    }
}

/// Attribute data for points or cells
#[derive(Debug, Clone, PartialEq)]
pub struct Attribute {
    /// Attribute type (point or cell)
    pub attr_type: AttributeType,
    /// Number of components per attribute
    pub num_components: u8,
    /// Attribute name (max 64 chars)
    pub name: String,
    /// Attribute data (length = n_points/cells * num_components)
    pub data: Vec<f32>,
}

impl Attribute {
    /// Create a new attribute
    pub fn new(
        attr_type: AttributeType,
        num_components: u8,
        name: impl Into<String>,
        data: Vec<f32>,
    ) -> Self {
        Attribute {
            attr_type,
            num_components,
            name: name.into(),
            data,
        }
    }
}

/// POLYDATA message for 3D polygon/mesh data
///
/// # OpenIGTLink Specification
/// - Message type: "POLYDATA"
/// - Format: Points + Vertices + Lines + Polygons + Triangle Strips + Attributes
/// - Complex variable-length structure
#[derive(Debug, Clone, PartialEq)]
pub struct PolyDataMessage {
    /// 3D points (x, y, z)
    pub points: Vec<[f32; 3]>,
    /// Vertex indices
    pub vertices: Vec<u32>,
    /// Line connectivity (first element = count, followed by indices)
    pub lines: Vec<u32>,
    /// Polygon connectivity (first element = count, followed by indices)
    pub polygons: Vec<u32>,
    /// Triangle strip connectivity
    pub triangle_strips: Vec<u32>,
    /// Attribute data
    pub attributes: Vec<Attribute>,
}

impl PolyDataMessage {
    /// Create a new POLYDATA message
    pub fn new(points: Vec<[f32; 3]>) -> Self {
        PolyDataMessage {
            points,
            vertices: Vec::new(),
            lines: Vec::new(),
            polygons: Vec::new(),
            triangle_strips: Vec::new(),
            attributes: Vec::new(),
        }
    }

    /// Add vertices
    pub fn with_vertices(mut self, vertices: Vec<u32>) -> Self {
        self.vertices = vertices;
        self
    }

    /// Add lines
    pub fn with_lines(mut self, lines: Vec<u32>) -> Self {
        self.lines = lines;
        self
    }

    /// Add polygons
    pub fn with_polygons(mut self, polygons: Vec<u32>) -> Self {
        self.polygons = polygons;
        self
    }

    /// Add triangle strips
    pub fn with_triangle_strips(mut self, strips: Vec<u32>) -> Self {
        self.triangle_strips = strips;
        self
    }

    /// Add attribute
    pub fn add_attribute(&mut self, attr: Attribute) {
        self.attributes.push(attr);
    }

    /// Get number of points
    pub fn num_points(&self) -> usize {
        self.points.len()
    }
}

impl Message for PolyDataMessage {
    fn message_type() -> &'static str {
        "POLYDATA"
    }

    fn encode_content(&self) -> Result<Vec<u8>> {
        let mut buf = Vec::new();

        // Encode number of points (uint32)
        buf.put_u32(self.points.len() as u32);

        // Encode points
        for point in &self.points {
            for &coord in point {
                buf.put_f32(coord);
            }
        }

        // Encode vertices (uint32 count + data)
        buf.put_u32(self.vertices.len() as u32);
        for &v in &self.vertices {
            buf.put_u32(v);
        }

        // Encode lines
        buf.put_u32(self.lines.len() as u32);
        for &l in &self.lines {
            buf.put_u32(l);
        }

        // Encode polygons
        buf.put_u32(self.polygons.len() as u32);
        for &p in &self.polygons {
            buf.put_u32(p);
        }

        // Encode triangle strips
        buf.put_u32(self.triangle_strips.len() as u32);
        for &t in &self.triangle_strips {
            buf.put_u32(t);
        }

        // Encode number of attributes
        buf.put_u32(self.attributes.len() as u32);

        // Encode each attribute
        for attr in &self.attributes {
            // Attribute type (uint8)
            buf.put_u8(attr.attr_type as u8);

            // Number of components (uint8)
            buf.put_u8(attr.num_components);

            // Name (char[64])
            let mut name_bytes = [0u8; 64];
            let name_str = attr.name.as_bytes();
            let copy_len = name_str.len().min(63);
            name_bytes[..copy_len].copy_from_slice(&name_str[..copy_len]);
            buf.extend_from_slice(&name_bytes);

            // Data size (uint32)
            buf.put_u32(attr.data.len() as u32);

            // Data (float32[])
            for &val in &attr.data {
                buf.put_f32(val);
            }
        }

        Ok(buf)
    }

    fn decode_content(mut data: &[u8]) -> Result<Self> {
        if data.len() < 4 {
            return Err(IgtlError::InvalidSize {
                expected: 4,
                actual: data.len(),
            });
        }

        // Decode number of points
        let num_points = data.get_u32() as usize;

        // Decode points
        let mut points = Vec::with_capacity(num_points);
        for _ in 0..num_points {
            if data.remaining() < 12 {
                return Err(IgtlError::InvalidSize {
                    expected: 12,
                    actual: data.remaining(),
                });
            }
            points.push([data.get_f32(), data.get_f32(), data.get_f32()]);
        }

        // Decode vertices
        let num_vertices = data.get_u32() as usize;
        let mut vertices = Vec::with_capacity(num_vertices);
        for _ in 0..num_vertices {
            vertices.push(data.get_u32());
        }

        // Decode lines
        let num_lines = data.get_u32() as usize;
        let mut lines = Vec::with_capacity(num_lines);
        for _ in 0..num_lines {
            lines.push(data.get_u32());
        }

        // Decode polygons
        let num_polygons = data.get_u32() as usize;
        let mut polygons = Vec::with_capacity(num_polygons);
        for _ in 0..num_polygons {
            polygons.push(data.get_u32());
        }

        // Decode triangle strips
        let num_strips = data.get_u32() as usize;
        let mut triangle_strips = Vec::with_capacity(num_strips);
        for _ in 0..num_strips {
            triangle_strips.push(data.get_u32());
        }

        // Decode attributes
        let num_attributes = data.get_u32() as usize;
        let mut attributes = Vec::with_capacity(num_attributes);

        for _ in 0..num_attributes {
            // Attribute type
            let attr_type = AttributeType::from_u8(data.get_u8())?;

            // Number of components
            let num_components = data.get_u8();

            // Name (char[64])
            let name_bytes = &data[..64];
            data.advance(64);
            let name_len = name_bytes.iter().position(|&b| b == 0).unwrap_or(64);
            let name = String::from_utf8(name_bytes[..name_len].to_vec())?;

            // Data size
            let data_size = data.get_u32() as usize;

            // Data
            let mut attr_data = Vec::with_capacity(data_size);
            for _ in 0..data_size {
                attr_data.push(data.get_f32());
            }

            attributes.push(Attribute {
                attr_type,
                num_components,
                name,
                data: attr_data,
            });
        }

        if !data.is_empty() {
            return Err(IgtlError::InvalidSize {
                expected: 0,
                actual: data.remaining(),
            });
        }

        Ok(PolyDataMessage {
            points,
            vertices,
            lines,
            polygons,
            triangle_strips,
            attributes,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_message_type() {
        assert_eq!(PolyDataMessage::message_type(), "POLYDATA");
    }

    #[test]
    fn test_new() {
        let points = vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        let poly = PolyDataMessage::new(points.clone());
        assert_eq!(poly.num_points(), 3);
        assert_eq!(poly.points, points);
    }

    #[test]
    fn test_with_polygons() {
        let points = vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        let poly = PolyDataMessage::new(points).with_polygons(vec![3, 0, 1, 2]); // Triangle with 3 vertices

        assert_eq!(poly.polygons, vec![3, 0, 1, 2]);
    }

    #[test]
    fn test_add_attribute() {
        let points = vec![[0.0, 0.0, 0.0]];
        let mut poly = PolyDataMessage::new(points);

        let attr = Attribute::new(AttributeType::Point, 3, "Normals", vec![0.0, 0.0, 1.0]);
        poly.add_attribute(attr);

        assert_eq!(poly.attributes.len(), 1);
    }

    #[test]
    fn test_encode_simple() {
        let points = vec![[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]];
        let poly = PolyDataMessage::new(points);
        let encoded = poly.encode_content().unwrap();

        // Should contain: point count (4) + points (24) + 5 counts (20) = 48 bytes minimum
        assert!(encoded.len() >= 48);
    }

    #[test]
    fn test_roundtrip_points_only() {
        let original = PolyDataMessage::new(vec![
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [0.0, 1.0, 0.0],
            [0.0, 0.0, 1.0],
        ]);

        let encoded = original.encode_content().unwrap();
        let decoded = PolyDataMessage::decode_content(&encoded).unwrap();

        assert_eq!(decoded.num_points(), 4);
        assert_eq!(decoded.points, original.points);
    }

    #[test]
    fn test_roundtrip_with_polygons() {
        let original =
            PolyDataMessage::new(vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]])
                .with_polygons(vec![3, 0, 1, 2]);

        let encoded = original.encode_content().unwrap();
        let decoded = PolyDataMessage::decode_content(&encoded).unwrap();

        assert_eq!(decoded.polygons, vec![3, 0, 1, 2]);
    }

    #[test]
    fn test_roundtrip_with_attribute() {
        let mut original = PolyDataMessage::new(vec![[0.0, 0.0, 0.0]]);

        original.add_attribute(Attribute::new(
            AttributeType::Point,
            3,
            "Normals",
            vec![0.0, 0.0, 1.0],
        ));

        let encoded = original.encode_content().unwrap();
        let decoded = PolyDataMessage::decode_content(&encoded).unwrap();

        assert_eq!(decoded.attributes.len(), 1);
        assert_eq!(decoded.attributes[0].name, "Normals");
        assert_eq!(decoded.attributes[0].num_components, 3);
        assert_eq!(decoded.attributes[0].data, vec![0.0, 0.0, 1.0]);
    }

    #[test]
    fn test_roundtrip_complex() {
        let mut original = PolyDataMessage::new(vec![
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [1.0, 1.0, 0.0],
            [0.0, 1.0, 0.0],
        ])
        .with_polygons(vec![4, 0, 1, 2, 3])
        .with_lines(vec![2, 0, 1]);

        original.add_attribute(Attribute::new(
            AttributeType::Cell,
            1,
            "Quality",
            vec![0.95],
        ));

        let encoded = original.encode_content().unwrap();
        let decoded = PolyDataMessage::decode_content(&encoded).unwrap();

        assert_eq!(decoded.num_points(), 4);
        assert_eq!(decoded.polygons, vec![4, 0, 1, 2, 3]);
        assert_eq!(decoded.lines, vec![2, 0, 1]);
        assert_eq!(decoded.attributes.len(), 1);
    }

    #[test]
    fn test_decode_invalid() {
        let data = vec![0u8; 2]; // Too short
        let result = PolyDataMessage::decode_content(&data);
        assert!(result.is_err());
    }
}