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
//! OpenIGTLink Protocol Implementation in Rust
//!
//! This library provides a Rust implementation of the OpenIGTLink protocol,
//! which is an open network protocol for image-guided therapy environments.
//!
//! # Features
//!
//! - **Type-safe message handling** - Leverages Rust's type system for protocol correctness
//! - **Comprehensive message types** - 20 message types fully implemented
//! - **Synchronous and asynchronous I/O** - Works with both sync and async Rust
//! - **Full protocol compliance** - OpenIGTLink Version 2 and 3
//! - **Memory safe** - No buffer overflows or memory leaks
//! - **Zero-copy parsing** - Efficient deserialization where possible
//!
//! # Quick Start
//!
//! ## Basic Client-Server Example
//!
//! **Server:**
//! ```no_run
//! use openigtlink_rust::io::IgtlServer;
//! use openigtlink_rust::protocol::types::StatusMessage;
//! use openigtlink_rust::protocol::message::IgtlMessage;
//!
//! let server = IgtlServer::bind("127.0.0.1:18944")?;
//! let mut client_conn = server.accept()?;
//!
//! let status = StatusMessage::ok("Server ready");
//! let msg = IgtlMessage::new(status, "Server")?;
//! client_conn.send(&msg)?;
//! # Ok::<(), openigtlink_rust::IgtlError>(())
//! ```
//!
//! **Client:**
//! ```no_run
//! use openigtlink_rust::io::ClientBuilder;
//! use openigtlink_rust::protocol::types::TransformMessage;
//! use openigtlink_rust::protocol::message::IgtlMessage;
//!
//! let mut client = ClientBuilder::new()
//! .tcp("127.0.0.1:18944")
//! .sync()
//! .build()?;
//!
//! let transform = TransformMessage::identity();
//! let msg = IgtlMessage::new(transform, "Device")?;
//! client.send(&msg)?;
//!
//! let response = client.receive::<TransformMessage>()?;
//! # Ok::<(), openigtlink_rust::IgtlError>(())
//! ```
//!
//! ## Sending Medical Images
//!
//! ```no_run
//! use openigtlink_rust::protocol::types::{ImageMessage, ImageScalarType};
//! use openigtlink_rust::protocol::message::IgtlMessage;
//! use openigtlink_rust::io::ClientBuilder;
//!
//! let mut client = ClientBuilder::new()
//! .tcp("127.0.0.1:18944")
//! .sync()
//! .build()?;
//!
//! let image = ImageMessage::new(
//! ImageScalarType::Uint8,
//! [512, 512, 1],
//! vec![0u8; 512 * 512]
//! )?;
//!
//! let msg = IgtlMessage::new(image, "Device")?;
//! client.send(&msg)?;
//! # Ok::<(), openigtlink_rust::IgtlError>(())
//! ```
//!
//! # Architecture
//!
//! The library is organized into three main modules:
//!
//! ## Module Structure
//!
//! - **`protocol`** - Core protocol implementation
//! - `header` - OpenIGTLink message header (58 bytes)
//! - `types` - All 20 message type implementations
//! - `crc` - CRC-64 checksum validation
//!
//! - **`io`** - Network I/O layer
//! - `ClientBuilder` - Type-state builder for configuring clients
//! - `SyncIgtlClient` / `AsyncIgtlClient` - TCP clients for sync/async workflows
//! - `IgtlServer` - TCP server for accepting connections
//!
//! - **`error`** - Error handling
//! - `IgtlError` - Unified error type for all operations
//! - `Result<T>` - Type alias for `Result<T, IgtlError>`
//!
//! ## Design Principles
//!
//! 1. **Type Safety**: Each message type is a distinct Rust type
//! 2. **Zero-cost Abstractions**: Minimal runtime overhead
//! 3. **Explicit Error Handling**: All network and parsing errors are explicit
//! 4. **Protocol Compliance**: Strict adherence to OpenIGTLink specification
//!
//! # Supported Message Types
//!
//! This implementation includes all major OpenIGTLink message types:
//!
//! - **Transform & Tracking**: TRANSFORM, POSITION, QTDATA, TDATA
//! - **Medical Imaging**: IMAGE, IMGMETA, LBMETA, COLORTABLE
//! - **Geometric Data**: POINT, POLYDATA, TRAJECTORY
//! - **Sensor Data**: SENSOR, NDARRAY
//! - **Communication**: STATUS, CAPABILITY, STRING, COMMAND, BIND
//! - **Video Streaming**: VIDEO, VIDEOMETA
//!
//! # Choosing Message Types
//!
//! ## Transform & Tracking
//!
//! - **TRANSFORM** - Single 4x4 homogeneous transformation matrix
//! - Use for: Robot end-effector pose, single tool tracking
//! - Example: Surgical instrument position
//!
//! - **TDATA** - Array of transformation matrices with names
//! - Use for: Multiple tracking tools simultaneously
//! - Example: Tracking 5 surgical instruments at once
//!
//! - **POSITION** - 3D position only (no orientation)
//! - Use for: Point cloud data, simplified tracking
//! - Example: Marker positions without rotation
//!
//! - **QTDATA** - Position + Quaternion (alternative to TRANSFORM)
//! - Use for: More compact rotation representation
//! - Example: IMU sensor orientation
//!
//! ## Medical Imaging
//!
//! - **IMAGE** - 2D/3D medical image with metadata
//! - Use for: CT, MRI, ultrasound images
//! - Supports: 8/16-bit grayscale, RGB, RGBA
//! - Example: Real-time ultrasound streaming
//!
//! - **VIDEO** - Real-time video frames
//! - Use for: Endoscopic video, webcam feeds
//! - Supports: MJPEG, H.264, raw formats
//! - Example: Laparoscopic camera feed
//!
//! - **IMGMETA** - Metadata for multiple images
//! - Use for: Image catalog, DICOM series info
//! - Example: List of available CT slices
//!
//! ## Sensor Data
//!
//! - **SENSOR** - Multi-channel sensor readings
//! - Use for: Force sensors, temperature arrays
//! - Example: 6-axis force/torque sensor
//!
//! - **NDARRAY** - N-dimensional array data
//! - Use for: Generic numerical data
//! - Example: Spectrogram, multi-dimensional measurements
//!
//! ## Geometric Data
//!
//! - **POINT** - Collection of 3D points with attributes
//! - Use for: Fiducial markers, anatomical landmarks
//! - Example: Registration points for navigation
//!
//! - **POLYDATA** - 3D mesh with vertices and polygons
//! - Use for: Organ surfaces, tumor boundaries
//! - Example: Liver segmentation mesh
//!
//! - **TRAJECTORY** - Planned surgical paths
//! - Use for: Needle insertion paths, biopsy planning
//! - Example: Brain biopsy trajectory
//!
//! ## Communication
//!
//! - **STATUS** - Status/error messages
//! - Use for: Operation success/failure notifications
//! - Example: "Registration completed successfully"
//!
//! - **STRING** - Text messages
//! - Use for: Logging, user messages
//! - Example: "Patient positioned, ready to scan"
//!
//! - **COMMAND** - Remote procedure calls
//! - Use for: Controlling remote devices
//! - Example: "StartScanning", "StopMotor"
//!
//! # Examples
//!
//! ## Example 1: Sending Transform Data
//!
//! ```no_run
//! use openigtlink_rust::io::ClientBuilder;
//! use openigtlink_rust::protocol::types::TransformMessage;
//! use openigtlink_rust::protocol::message::IgtlMessage;
//!
//! let mut client = ClientBuilder::new()
//! .tcp("192.168.1.100:18944")
//! .sync()
//! .build()?;
//!
//! // Create transformation matrix (4x4)
//! let mut transform = TransformMessage::identity();
//!
//! // Set translation (in mm)
//! transform.matrix[0][3] = 100.0; // X
//! transform.matrix[1][3] = 50.0; // Y
//! transform.matrix[2][3] = 200.0; // Z
//!
//! // Send to server
//! let msg = IgtlMessage::new(transform, "RobotArm")?;
//! client.send(&msg)?;
//! # Ok::<(), openigtlink_rust::IgtlError>(())
//! ```
//!
//! ## Example 2: Streaming Medical Images
//!
//! ```no_run
//! use openigtlink_rust::io::ClientBuilder;
//! use openigtlink_rust::protocol::types::{ImageMessage, ImageScalarType};
//! use openigtlink_rust::protocol::message::IgtlMessage;
//!
//! let mut client = ClientBuilder::new()
//! .tcp("localhost:18944")
//! .sync()
//! .build()?;
//!
//! // Simulate ultrasound image stream (640x480 grayscale)
//! for frame_num in 0..100 {
//! // Generate synthetic image data
//! let image_data = vec![((frame_num % 256) as u8); 640 * 480];
//!
//! let image = ImageMessage::new(
//! ImageScalarType::Uint8,
//! [640, 480, 1],
//! image_data
//! )?;
//!
//! let msg = IgtlMessage::new(image, "UltrasoundProbe")?;
//! client.send(&msg)?;
//! std::thread::sleep(std::time::Duration::from_millis(33)); // ~30 fps
//! }
//! # Ok::<(), openigtlink_rust::IgtlError>(())
//! ```
//!
//! ## Example 3: Multi-Channel Sensor Data
//!
//! ```no_run
//! use openigtlink_rust::io::ClientBuilder;
//! use openigtlink_rust::protocol::types::SensorMessage;
//! use openigtlink_rust::protocol::message::IgtlMessage;
//!
//! let mut client = ClientBuilder::new()
//! .tcp("localhost:18944")
//! .sync()
//! .build()?;
//!
//! // 6-axis force/torque sensor
//! // Forces (Fx, Fy, Fz) and Torques (Tx, Ty, Tz)
//! let readings = vec![1.2, -0.5, 3.8, 0.1, 0.05, -0.2];
//! let sensor = SensorMessage::with_unit(1, 0x0101, readings)?;
//!
//! let msg = IgtlMessage::new(sensor, "ForceSensor")?;
//! client.send(&msg)?;
//! # Ok::<(), openigtlink_rust::IgtlError>(())
//! ```
//!
//! ## Example 4: Status Message Handling
//!
//! ```no_run
//! use openigtlink_rust::io::IgtlServer;
//! use openigtlink_rust::protocol::types::StatusMessage;
//! use openigtlink_rust::protocol::message::IgtlMessage;
//!
//! let server = IgtlServer::bind("0.0.0.0:18944")?;
//! let mut client_conn = server.accept()?;
//!
//! // Receive message
//! let message = client_conn.receive::<StatusMessage>()?;
//!
//! // Send acknowledgment
//! let status = StatusMessage::ok("Data received successfully");
//! let msg = IgtlMessage::new(status, "Server")?;
//! client_conn.send(&msg)?;
//! # Ok::<(), openigtlink_rust::IgtlError>(())
//! ```
//!
//! ## Example 5: Error Handling and Recovery
//!
//! ```no_run
//! use openigtlink_rust::io::ClientBuilder;
//! use openigtlink_rust::protocol::types::TransformMessage;
//! use openigtlink_rust::protocol::message::IgtlMessage;
//! use openigtlink_rust::IgtlError;
//!
//! fn send_with_retry(addr: &str) -> Result<(), IgtlError> {
//! let max_retries = 3;
//!
//! for attempt in 0..max_retries {
//! match ClientBuilder::new().tcp(addr).sync().build() {
//! Ok(mut client) => {
//! let transform = TransformMessage::identity();
//! let msg = IgtlMessage::new(transform, "Device")?;
//! return client.send(&msg);
//! }
//! Err(e) if attempt < max_retries - 1 => {
//! eprintln!("Connection failed (attempt {}): {}", attempt + 1, e);
//! std::thread::sleep(std::time::Duration::from_secs(1));
//! continue;
//! }
//! Err(e) => return Err(e),
//! }
//! }
//! unreachable!()
//! }
//! # Ok::<(), openigtlink_rust::IgtlError>(())
//! ```
//!
//! # Error Handling
//!
//! All operations return `Result<T, IgtlError>`. Common error types:
//!
//! - **IoError** - Network communication failures
//! - **InvalidHeader** - Malformed message headers
//! - **CrcMismatch** - Data corruption detected
//! - **UnsupportedMessage** - Unknown message type
//! - **InvalidData** - Invalid message body
//!
//! ```no_run
//! use openigtlink_rust::io::ClientBuilder;
//! use openigtlink_rust::IgtlError;
//!
//! match ClientBuilder::new().tcp("localhost:18944").sync().build() {
//! Ok(client) => println!("Connected"),
//! Err(IgtlError::Io(e)) => eprintln!("Network error: {}", e),
//! Err(e) => eprintln!("Other error: {}", e),
//! }
//! ```
// Re-export commonly used types
pub use ;