LibCarna Version 3.4.0
Loading...
Searching...
No Matches
HUIO.hpp
1/*
2 * Copyright (C) 2010 - 2016 Leonid Kostrykin
3 *
4 * Chair of Medical Engineering (mediTEC)
5 * RWTH Aachen University
6 * Pauwelsstr. 20
7 * 52074 Aachen
8 * Germany
9 *
10 *
11 * Copyright (C) 2021 - 2025 Leonid Kostrykin
12 *
13 */
14
15#pragma once
16
17#include <LibCarna/LibCarna.hpp>
18#include <ostream>
19#include <istream>
20#include <queue>
21#include <cstdint>
22
23namespace LibCarna
24{
25
26namespace testing
27{
28
29namespace HUIO
30{
31
32
33
34// ----------------------------------------------------------------------------------
35// Types & Constants
36// ----------------------------------------------------------------------------------
37
38typedef uint16_t shifted_huv_t;
39
40struct buffer_t
41{
42 unsigned int first : 12;
43 unsigned int second : 12;
44};
45
46const std::streamsize BUFFER_LENGTH = 3;
47
48
49
50// ----------------------------------------------------------------------------------
51// Writer
52// ----------------------------------------------------------------------------------
53
54struct Writer
55{
56
57 Writer( std::ostream& out )
58 : out( out )
59 {
60 }
61
62 ~Writer()
63 {
64 flush();
65 }
66
67 void write( signed short huv )
68 {
69 const signed short shifted_huv = huv + 1024;
70 buffered_shifted_huv.push( static_cast< shifted_huv_t >( shifted_huv ) );
71 if( buffered_shifted_huv.size() % 2 == 0 )
72 {
73 flush();
74 }
75 }
76
77private:
78
79 std::ostream& out;
80
81 std::queue< shifted_huv_t > buffered_shifted_huv;
82 buffer_t buffer;
83
84 void flush()
85 {
86 if( buffered_shifted_huv.empty() )
87 {
88 return;
89 }
90
91 buffer.first = buffered_shifted_huv.front();
92 buffered_shifted_huv.pop();
93
94 if( buffered_shifted_huv.empty() )
95 {
96 buffer.second = 0; // padding
97 }
98 else
99 {
100 buffer.second = buffered_shifted_huv.front();
101 buffered_shifted_huv.pop();
102 }
103
104 out.write( reinterpret_cast< const char* >( &buffer ), BUFFER_LENGTH );
105 }
106
107}; // HUIO :: Writer
108
109
110
111// ----------------------------------------------------------------------------------
112// Reader
113// ----------------------------------------------------------------------------------
114
115struct Reader
116{
117
118 Reader( std::istream& in )
119 : in( in )
120 {
121 }
122
123 signed short read()
124 {
125 if( read_values.empty() )
126 {
127 readAhead();
128 }
129 const shifted_huv_t shifted_huv = read_values.front();
130 read_values.pop();
131 const signed short huv = static_cast< signed short >( shifted_huv ) - 1024;
132 return huv;
133 }
134
135private:
136
137 std::istream& in;
138
139 buffer_t buffer;
140 std::queue< shifted_huv_t > read_values;
141
142 void readAhead()
143 {
144 in.read( reinterpret_cast< char* >( &buffer ), BUFFER_LENGTH );
145 read_values.push( buffer.first );
146 read_values.push( buffer.second );
147 }
148
149}; // HUIO :: Reader
150
151
152
153} // namespace HUIO
154
155} // namespace testing
156
157} // namespace LibCarna
Contains forward-declarations.