openigtlink-rust 0.1.0

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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# OpenIGTLink Rust

[![Crates.io](https://img.shields.io/crates/v/openigtlink-rust)](https://crates.io/crates/openigtlink-rust)
[![Documentation](https://docs.rs/openigtlink-rust/badge.svg)](https://docs.rs/openigtlink-rust)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

A **high-performance**, **type-safe** Rust implementation of the [OpenIGTLink](http://openigtlink.org/) protocol for real-time communication in image-guided therapy and surgical navigation systems.

> **OpenIGTLink** is the industry-standard open network protocol used in medical applications like **3D Slicer**, **PLUS Toolkit**, and numerous surgical navigation systems worldwide.

## Why OpenIGTLink Rust?

- 🦀 **Memory Safety** - Rust's ownership system eliminates memory leaks and buffer overflows common in medical software
- 🚀 **High Performance** - Zero-copy parsing and efficient serialization for real-time surgical applications
-**100% Compatible** - Binary-compatible with the official C++ library - works with all existing OpenIGTLink software
- 🔒 **Production Ready** - 363 comprehensive tests, extensive documentation, and real-world examples
- 🏗️ **Type-Safe Builder** - Compile-time guarantees prevent invalid client configurations

## Quick Start

```rust
use openigtlink_rust::io::builder::ClientBuilder;
use openigtlink_rust::protocol::message::IgtlMessage;
use openigtlink_rust::protocol::types::TransformMessage;

// Build a TCP client with the Builder pattern
let mut client = ClientBuilder::new()
    .tcp("127.0.0.1:18944")
    .async_mode()
    .build()
    .await?;

// Send surgical tool position
let transform = TransformMessage::identity();
let msg = IgtlMessage::new(transform, "SurgicalTool")?;
client.send(&msg).await?;

// Receive tracking data
let response: IgtlMessage<TransformMessage> = client.receive().await?;
```

Run your first example in 30 seconds:
```bash
# Terminal 1: Start server
cargo run --example server

# Terminal 2: Send/receive messages
cargo run --example client
```

## Key Features

### 🏗️ Flexible Client Builder

Create clients with exactly the features you need using the **type-state builder pattern**:

```rust
// Simple TCP client
let client = ClientBuilder::new()
    .tcp("127.0.0.1:18944")
    .async_mode()
    .build()
    .await?;

// TLS-encrypted client
let client = ClientBuilder::new()
    .tcp("hospital-server.local:18944")
    .async_mode()
    .with_tls(tls_config)
    .build()
    .await?;

// Auto-reconnecting client
let client = ClientBuilder::new()
    .tcp("127.0.0.1:18944")
    .async_mode()
    .with_reconnect(ReconnectConfig::with_max_attempts(10))
    .build()
    .await?;

// TLS + Auto-reconnect (previously impossible!)
let client = ClientBuilder::new()
    .tcp("hospital-server.local:18944")
    .async_mode()
    .with_tls(tls_config)
    .with_reconnect(reconnect_config)
    .build()
    .await?;

// UDP for low-latency tracking
let client = ClientBuilder::new()
    .udp("127.0.0.1:18944")
    .build()?;
```

**Compile-time safety**: Unsupported combinations (like UDP + TLS) are prevented at compile time!

### 🏥 Medical Imaging & Tracking
- **20/20 Message Types** - Complete implementation of all OpenIGTLink messages
  - Medical images (CT/MRI/Ultrasound) with compression
  - Real-time video streaming (H.264, VP9, MJPEG)
  - Surgical tool tracking (60-120 Hz)
  - Sensor data (force sensors, IMU)
  - 3D visualization (meshes, point clouds)

### 🌐 Networking & I/O
- **Flexible Builder API** - Type-safe client construction with compile-time validation
- **Async/Sync I/O** - Choose between blocking or Tokio async for your use case
- **UDP Support** - Low-latency tracking data transmission (120+ Hz)
- **TLS/SSL Encryption** - Secure medical data transfer with certificate validation
- **Auto-reconnection** - Robust network error handling with exponential backoff
- **Multi-client Server** - Built-in session management for concurrent connections

### ⚡ Performance & Reliability
- **Zero-copy Parsing** - Minimal overhead for real-time applications
- **Image Compression** - 98-99% reduction for medical images (Deflate/Gzip)
- **Message Queuing** - Backpressure control for high-throughput scenarios
- **CRC-64 Validation** - Optional integrity checking
- **Structured Logging** - Production-ready tracing integration

## Installation

Add to your `Cargo.toml`:
```toml
[dependencies]
openigtlink-rust = "0.1.0"
```

Or install from source:
```bash
git clone https://github.com/gongfour/openigtlink-rust.git
cd openigtlink-rust
cargo build --release
```

## Architecture

### Builder Pattern Design

The library uses a **type-state builder pattern** to ensure compile-time safety:

```rust
ClientBuilder::new()
    .tcp(addr)           // Or .udp(addr)
    .async_mode()        // Or .sync() for blocking I/O
    .with_tls(config)    // Optional: Add TLS encryption
    .with_reconnect(cfg) // Optional: Enable auto-reconnection
    .verify_crc(true)    // Optional: CRC verification
    .build()             // Returns Result<Client>
```

**Key Design Decisions:**

1. **No Variant Explosion**: Instead of creating separate types for every feature combination (TcpAsync, TcpAsyncTls, TcpAsyncReconnect, TcpAsyncTlsReconnect...), we use a single `UnifiedAsyncClient` with optional features.

2. **Type-Safe States**: The builder uses Rust's type system to prevent invalid configurations at compile time. For example, you cannot call `.with_tls()` on a UDP client.

3. **Zero Runtime Cost**: The `PhantomData` markers used for type states have zero size and are optimized away at compile time.

### UnifiedAsyncClient Architecture

```rust
pub struct UnifiedAsyncClient {
    // Internal transport (Plain TCP or TLS)
    transport: Option<Transport>,

    // Optional auto-reconnection
    reconnect_config: Option<ReconnectConfig>,

    // Connection parameters
    conn_params: ConnectionParams,

    // CRC verification
    verify_crc: bool,
}

enum Transport {
    Plain(TcpStream),
    Tls(TlsStream<TcpStream>),
}
```

This design:
- ✅ Scales to any number of features without combinatorial explosion
- ✅ Maintains type safety and compile-time guarantees
- ✅ Enables previously impossible combinations (TLS + Reconnect)
- ✅ Easy to extend with new features (compression, authentication, etc.)

### Client Types

| Builder | Result Type | Best For | Key Features |
|---------|-------------|----------|-------------|
| `.tcp().sync()` | `SyncIgtlClient` | Simple applications | Blocking I/O, easy to use |
| `.tcp().async_mode()` | `UnifiedAsyncClient` | High concurrency | Tokio async, 100+ clients |
| `.tcp().async_mode().with_tls()` | `UnifiedAsyncClient` | Secure networks | Certificate-based encryption |
| `.tcp().async_mode().with_reconnect()` | `UnifiedAsyncClient` | Unreliable networks | Auto-reconnect with backoff |
| `.udp()` | `UdpClient` | Real-time tracking | Low latency (120+ Hz) |

## Use Cases

### 🔬 Surgical Navigation
```rust
// Track surgical tools in real-time with UDP
let mut client = ClientBuilder::new()
    .udp("127.0.0.1:18944")
    .build()?;

loop {
    let transform = get_tool_position();
    let msg = IgtlMessage::new(transform, "Scalpel")?;
    client.send(&msg)?;
    tokio::time::sleep(Duration::from_millis(8)).await; // 120 Hz
}
```

### 🏥 Medical Imaging Pipeline
```rust
use openigtlink_rust::protocol::types::ImageMessage;

// Stream CT/MRI scans with compression
let image = ImageMessage::new(
    ImageScalarType::Uint16,
    [512, 512, 100],
    image_data
)?;
let msg = IgtlMessage::new(image, "CTScan")?;
client.send(&msg).await?;
```

### 🔐 Secure Hospital Network
```rust
use openigtlink_rust::io::tls_client::insecure_tls_config;
use std::sync::Arc;

// TLS-encrypted communication with auto-reconnection
let tls_config = Arc::new(insecure_tls_config());
let reconnect_config = ReconnectConfig::with_max_attempts(10);

let mut client = ClientBuilder::new()
    .tcp("hospital-server.local:18944")
    .async_mode()
    .with_tls(tls_config)
    .with_reconnect(reconnect_config)
    .build()
    .await?;

client.send(&patient_data).await?;
```

### 🔄 Robust Production System
```rust
// Production-ready client with all features
let client = ClientBuilder::new()
    .tcp("production-server:18944")
    .async_mode()
    .with_tls(load_production_certs()?)
    .with_reconnect(
        ReconnectConfig::with_max_attempts(100)
    )
    .verify_crc(true)
    .build()
    .await?;
```

## Supported Message Types

✅ **20/20 message types fully implemented** - Complete OpenIGTLink protocol coverage

<details>
<summary><b>📡 Tracking & Position (6 types)</b></summary>

- **TRANSFORM** - 4x4 transformation matrices
- **POSITION** - Position + quaternion orientation
- **QTDATA** - Quaternion tracking for surgical tools
- **TDATA** - Transform tracking data (3x4 matrices)
- **TRAJECTORY** - 3D surgical trajectories
- **POINT** - Fiducial points for navigation
</details>

<details>
<summary><b>🏥 Medical Imaging (4 types)</b></summary>

- **IMAGE** - 2D/3D medical images (CT/MRI/Ultrasound)
- **VIDEO** - Video streaming (H.264/VP9/MJPEG/Raw)
- **IMGMETA** - Image metadata (patient info, modality)
- **VIDEOMETA** - Video metadata (codec, resolution, bitrate)
</details>

<details>
<summary><b>📊 Data & Sensors (2 types)</b></summary>

- **SENSOR** - Sensor arrays (force, IMU, etc.)
- **NDARRAY** - N-dimensional numerical arrays
</details>

<details>
<summary><b>🎨 Visualization (3 types)</b></summary>

- **POLYDATA** - 3D meshes and polygons
- **LBMETA** - Segmentation labels
- **COLORTABLE** - Color lookup tables
</details>

<details>
<summary><b>💬 Communication (5 types)</b></summary>

- **STRING** - Text messages
- **COMMAND** - XML commands
- **STATUS** - Device status
- **CAPABILITY** - Protocol negotiation
- **BIND** - Message grouping

**Plus 22 query/control messages:** GET_*, STT_*, STP_*, RTS_*
</details>

## Examples

📝 **27 ready-to-run examples** covering all features - [Browse all examples](./examples/)

### 🚀 Getting Started (2 min)
```bash
# Terminal 1: Start server
cargo run --example server

# Terminal 2: Send/receive messages
cargo run --example client
```

### 🏥 Medical Applications

<details>
<summary><b>Medical Imaging</b></summary>

```bash
# CT/MRI/Ultrasound streaming
cargo run --example image_streaming ct
cargo run --example image_streaming ultrasound

# Real-time video
cargo run --example video_streaming h264
```
</details>

<details>
<summary><b>Surgical Navigation</b></summary>

```bash
# Tool tracking at 60-120 Hz
cargo run --example tracking_server
cargo run --example udp_tracking

# Fiducial registration
cargo run --example point_navigation
```
</details>

<details>
<summary><b>Sensor Integration</b></summary>

```bash
# Force/torque sensors
cargo run --example sensor_logger force

# IMU data
cargo run --example sensor_logger imu
```
</details>

### 🔧 Advanced Features

<details>
<summary><b>Security & Networking</b></summary>

```bash
# TLS encryption
./examples/generate_test_certs.sh
cargo run --example tls_communication

# Auto-reconnection
cargo run --example reconnect

# Multi-client server
cargo run --example session_manager
```
</details>

<details>
<summary><b>Performance Optimization</b></summary>

```bash
# Image compression (98-99% ratio)
cargo run --example compression

# UDP low-latency
cargo run --example udp_tracking compare

# Message queuing
cargo run --example message_queue
```
</details>

<details>
<summary><b>3D Slicer Integration</b></summary>

```bash
# Query & streaming control
cargo run --example query_streaming

# Connect to remote Slicer
cargo run --example query_streaming -- 192.168.1.100:18944
```
</details>

### 📊 Testing & Benchmarks
```bash
cargo test           # 363 tests
cargo bench          # Performance benchmarks
RUST_LOG=debug cargo run --example logging
```

## Performance

Real-world benchmarks on Apple M1:

| Operation | Performance | Details |
|-----------|------------|---------|
| **Message Throughput** | 10,000+ msg/sec | TRANSFORM, STATUS messages |
| **Image Encoding** | ~50ms | 512×512×100 CT scan (16-bit) |
| **Compression** | 98-99% | Medical images (Deflate) |
| **UDP Latency** | <1ms RTT | TRANSFORM messages |
| **Concurrency** | 100+ clients | Single async thread |

Run benchmarks:
```bash
cargo bench
```

## Compatibility

### 🔗 Interoperability
- **OpenIGTLink C++ 3.0+** - 100% binary compatible
-**[3D Slicer]https://www.slicer.org/** - Medical imaging platform
-**[PLUS Toolkit]https://plustoolkit.github.io/** - Image-guided intervention

### 🦀 Rust Support
- **MSRV**: Rust 1.70+
- **Platforms**: Linux, macOS, Windows

## Documentation & Resources

- 📚 **[API Docs]https://docs.rs/openigtlink-rust** - Complete API reference
- 📖 **[Examples]./examples/** - 27 practical examples
- 🔍 **[Query Guide]./docs/query_usage.md** - Streaming control protocol
- 📊 **[Benchmarks]./BENCHMARKS.md** - Performance analysis
- 🌐 **[OpenIGTLink Protocol]http://openigtlink.org/** - Official specification

## Contributing

Contributions welcome! Feel free to:
- 🐛 Report bugs via [issues]https://github.com/gongfour/openigtlink-rust/issues
- 💡 Suggest features or improvements
- 🔧 Submit pull requests

## Project Status

✅ **Production-ready** - Used in real surgical navigation systems

| Metric | Status |
|--------|--------|
| Message Types | 20/20 ✅ |
| Query/Control | 22/22 ✅ |
| Tests | 363 passing ✅ |
| C++ Compatibility | 100% ✅ |
| Documentation | Complete ✅ |

## License

MIT License - See [LICENSE](LICENSE) for details

---

<div align="center">

**[⭐ Star on GitHub](https://github.com/gongfour/openigtlink-rust)** • **[📦 View on crates.io](https://crates.io/crates/openigtlink-rust)** • **[📚 Read the Docs](https://docs.rs/openigtlink-rust)**

Built with ❤️ for the medical robotics community

</div>