Debian Codegraph @

src/thirdparty/PoissonRecon/DataStream.h

raw
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28
29 #ifndef DATA_STREAM_INCLUDED
30 #define DATA_STREAM_INCLUDED
31
32 #include <mutex>
33 #include <vector>
34 #include <atomic>
35 #include "Geometry.h"
36
37 namespace PoissonRecon
38 {
39 // The input/output stream types
40 template< typename ... Data > struct InputDataStream;
41 template< typename ... Data > struct OutputDataStream;
42
43 // Input/output streams, internally represented by an std::vector< Input/OutputDataStream< Data ... > >
44 template< typename ... Data > struct MultiInputDataStream;
45 template< typename ... Data > struct MultiOutputDataStream;
46
47 // Class for de-interleaving output
48 template< typename ... Data > using OutputIndexedDataStream = OutputDataStream< size_t , Data ... >;
49 template< typename ... Data > using MultiOutputIndexedDataStream = MultiOutputDataStream< size_t , Data ... >;
50 template< typename ... Data > struct DeInterleavedMultiOutputIndexedDataStream;
51
52 // Classes for re-interleaving input
53 template< typename ... Data > using InputIndexedDataStream = InputDataStream< size_t , Data ... >;
54 template< typename ... Data > using MultiInputIndexedDataStream = MultiInputDataStream< size_t , Data ... >;
55 template< typename ... Data > struct InterleavedMultiInputIndexedDataStream;
56
57 // Classes for converting one stream type to another
58 template< typename IDataTuple , typename XDataTuple > struct InputDataStreamConverter;
59 template< typename IDataTuple , typename XDataTuple > struct OutputDataStreamConverter;
60
61 template< typename ... Data >
62 struct TupleConverter
63 {
64 static void FromTuple( const std::tuple< Data ... > &dTuple , Data& ... d );
65 static void ToTuple( std::tuple< Data ... > &dTuple , const Data& ... d );
66 protected:
67 template< unsigned int I , typename _Data , typename ... _Datas >
68 static void _FromTuple( const std::tuple< Data ... > &dTuple , _Data &d , _Datas& ... ds );
69 template< unsigned int I , typename _Data , typename ... _Datas >
70 static void _ToTuple( std::tuple< Data ... > &dTuple , const _Data &d , const _Datas& ... ds );
71 };
72
73 template< typename Real , typename ... Data >
74 struct DirectSumConverter
75 {
76 static void FromDirectSum( const DirectSum< Real , Data ... > &dSum , Data& ... d );
77 static void ToDirectSum( DirectSum< Real , Data ... > &dSum , const Data& ... d );
78 protected:
79 template< unsigned int I , typename _Data , typename ... _Datas >
80 static void _FromDirectSum( const DirectSum<Real , Data ... > &dSum , _Data &d , _Datas& ... ds );
81 template< unsigned int I , typename _Data , typename ... _Datas >
82 static void _ToDirectSum( DirectSum< Real , Data ... > &dSum , const _Data &d , const _Datas& ... ds );
83 };
84
85 ////////////////////////
86 // Input data streams //
87 ////////////////////////
88
89 // An input stream containing "Data" types
90 // Supporting:
91 // -- Resetting the stream to the start
92 // -- Trying to read the next element from the stream
93 template< typename ... Data >
94 struct InputDataStream
95 {
96 friend struct MultiInputDataStream< Data... >;
97
98 virtual ~InputDataStream( void ){}
99
100 // Reset to the start of the stream
101 virtual void reset( void ) = 0;
102
103 // Read the next datum from the stream
104 virtual bool read( Data& ... d ) = 0;
105 virtual bool read( unsigned int thread , Data& ... d );
106
107 bool read( std::tuple< Data ... > &data );
108 bool read( unsigned int thread , std::tuple< Data ... > &data );
109
110 template< typename Real >
111 bool read( DirectSum< Real , Data ... > &data );
112 template< typename Real >
113 bool read( unsigned int thread , DirectSum< Real , Data ... > &data );
114
115 protected:
116 std::mutex _insertionMutex;
117
118 template< typename ... _Data >
119 bool _read( std::tuple< Data ... > &data , _Data& ... _data );
120 template< typename ... _Data >
121 bool _read( unsigned int thread , std::tuple< Data ... > &data , _Data& ... _data );
122 template< typename Real , typename ... _Data >
123 bool _read( DirectSum< Real , Data ... > &data , _Data& ... _data );
124 template< typename Real , typename ... _Data >
125 bool _read( unsigned int thread , DirectSum< Real , Data ... > &data , _Data& ... _data );
126 };
127
128
129 /////////////////////////
130 // Output data streams //
131 /////////////////////////
132 //
133 // An output stream containing "Data" types
134 // Supporting:
135 // -- Writing the next element to the stream
136 // -- Pushing the next element to the stream (and getting the count of elements pushed so far)
137 template< typename ... Data >
138 struct OutputDataStream
139 {
140 friend struct MultiOutputDataStream< Data ... >;
141
142 OutputDataStream( void ){}
143 virtual ~OutputDataStream( void ){}
144
145 virtual size_t size( void ) const = 0;
146
147 virtual size_t write( const Data& ... d ) = 0;
148 virtual size_t write( unsigned int thread , const Data& ... d );
149
150 size_t write( const std::tuple< Data ... > &data );
151 size_t write( unsigned int thread , const std::tuple< Data ... > &data );
152
153 template< typename Real > size_t write( const DirectSum< Real , Data ... > &data );
154 template< typename Real > size_t write( unsigned int thread , const DirectSum< Real , Data ... > &data );
155
156 protected:
157 std::mutex _insertionMutex;
158
159 template< typename ... _Data > size_t _write( const std::tuple< Data ... > &data , const _Data& ... _data );
160 template< typename ... _Data > size_t _write( unsigned int thread , const std::tuple< Data ... > &data , const _Data& ... _data );
161
162 template< typename Real , typename ... _Data > size_t _write( const DirectSum< Real , Data ... > &data , const _Data& ... _data );
163 template< typename Real , typename ... _Data > size_t _write( unsigned int thread , const DirectSum< Real , Data ... > &data , const _Data& ... _data );
164 };
165
166 ////////////////////////////////
167 // Multiple input data stream //
168 ////////////////////////////////
169 template< typename ...Data >
170 struct MultiInputDataStream : public InputDataStream< Data ... >
171 {
172 using InputDataStream< Data ... >::read;
173
174 MultiInputDataStream( InputDataStream< Data ... > **streams , size_t N );
175 MultiInputDataStream( const std::vector< InputDataStream< Data ... > * > &streams );
176
177 void reset( void );
178
179 unsigned int numStreams( void ) const;
180
181 bool read( unsigned int t , Data& ... d );
182 bool read( Data& ... d );
183
184 protected:
185 std::vector< InputDataStream< Data ... > * > _streams;
186 unsigned int _current;
187 };
188
189
190 /////////////////////////////////
191 // Multiple output data stream //
192 /////////////////////////////////
193 template< typename ... Data >
194 struct MultiOutputDataStream : public OutputDataStream< Data ... >
195 {
196 using OutputDataStream< Data ... >::write;
197
198 MultiOutputDataStream( OutputDataStream< Data ... > **streams , size_t N );
199 MultiOutputDataStream( const std::vector< OutputDataStream< Data ... > * > &streams );
200
201 unsigned int numStreams( void ) const;
202
203 size_t size( void ) const { return _size; }
204
205 size_t write( const Data& ... d );
206 size_t write( unsigned int t , const Data& ... d );
207
208 protected:
209 std::vector< OutputDataStream< Data ... > * > _streams;
210 std::atomic< size_t > _size;
211 };
212
213 /////////////////////////////////
214 // Input data stream converter //
215 /////////////////////////////////
216 template< typename ... IData , typename ... XData >
217 struct InputDataStreamConverter< std::tuple< IData ... > , std::tuple< XData ... > > : public InputDataStream< XData ... >
218 {
219 using InputDataStream< XData ... >::read;
220
221 InputDataStreamConverter( InputDataStream< IData ... > &stream , std::function< std::tuple< XData ... > ( const std::tuple< IData ... >& ) > converter , IData ... zero );
222 void reset( void );
223 bool read( XData& ... xData );
224 bool read( unsigned int thread , XData& ... xData );
225
226 protected:
227 InputDataStream< IData ... > &_stream;
228 std::function< std::tuple< XData ... > ( const std::tuple< IData ... >& ) > _converter;
229 std::tuple< IData ... > _iZero;
230 };
231
232 //////////////////////////////////
233 // Output data stream converter //
234 //////////////////////////////////
235 template< typename ... IData , typename ... XData >
236 struct OutputDataStreamConverter< std::tuple< IData ... > , std::tuple< XData ... > > : public OutputDataStream< XData ... >
237 {
238 using OutputDataStream< XData ... >::write;
239
240 OutputDataStreamConverter( OutputDataStream< IData ... > &stream , std::function< std::tuple< IData ... > ( const std::tuple< XData ... >& ) > converter );
241
242 size_t size( void ) const { return _stream.size(); }
243
244 size_t write( const XData& ... xData );
245 size_t write( unsigned int thread , const XData& ... xData );
246 protected:
247 OutputDataStream< IData ... > &_stream;
248 std::function< std::tuple< IData ... > ( const std::tuple< XData ... >& ) > _converter;
249 };
250
251
252 ////////////////////////////////////////////////
253 // De-interleaved multiple output data stream //
254 ////////////////////////////////////////////////
255 template< typename ... Data >
256 struct DeInterleavedMultiOutputIndexedDataStream : public OutputDataStream< Data ... >
257 {
258 using OutputDataStream< Data ... >::write;
259
260 DeInterleavedMultiOutputIndexedDataStream( OutputIndexedDataStream< Data ... > **streams , size_t N );
261 DeInterleavedMultiOutputIndexedDataStream( const std::vector< OutputIndexedDataStream< Data ... > * > &streams );
262
263 unsigned int numStreams( void ) const;
264
265 size_t size( void ) const { return _size; }
266
267 size_t write( const Data& ... d );
268 size_t write( unsigned int thread , const Data& ... d );
269
270 protected:
271 MultiOutputIndexedDataStream< Data ... > _multiStream;
272 std::atomic< size_t > _size;
273 };
274
275
276 ////////////////////////////////////////////
277 // Interleaved multiple input data stream //
278 ////////////////////////////////////////////
279
280 template< typename ... Data >
281 struct InterleavedMultiInputIndexedDataStream : public InputDataStream< Data ... >
282 {
283 using InputDataStream< Data... >::read;
284
285 InterleavedMultiInputIndexedDataStream( InputIndexedDataStream< Data ... > **streams , size_t N );
286 InterleavedMultiInputIndexedDataStream( const std::vector< InputIndexedDataStream< Data ... > * > &streams );
287 void reset( void );
288 bool read( Data& ... d );
289
290 protected:
291
292 struct _NextValue
293 {
294 std::tuple< size_t , Data ... > data;
295 unsigned int streamIndex;
296 bool validData;
297
298 // Returns true if v1>v2 so that it's a min-heap
299 static bool Compare( const _NextValue &v1 , const _NextValue &v2 );
300 };
301
302 MultiInputIndexedDataStream< Data... > _multiStream;
303 std::vector< _NextValue > _nextValues;
304 bool _firstTime;
305
306 void _init( const std::tuple< Data ... > &data );
307 };
308 #include "DataStream.inl"
309 }
310
311 #endif // DATA_STREAM_INCLUDED