Debian Codegraph @

src/thirdparty/PoissonRecon/PoissonReconClientServer.inl

raw
1 /*
2 Copyright (c) 2023, Michael Kazhdan
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 template< typename Real >
30 using AuxDataType = typename VertexFactory::DynamicFactory< Real >::VertexType;
31
32 template< typename Real >
33 struct AuxDataTypeSerializer : public Serializer< AuxDataType< Real > >
34 {
35 AuxDataTypeSerializer( const std::vector< PlyProperty > &properties ) : _factory( properties ){}
36
37 size_t size( void ) const { return _factory.bufferSize(); }
38
39 void serialize( const AuxDataType< Real > &data , Pointer( char )buffer ) const
40 {
41 _factory.toBuffer( data , buffer );
42 }
43 void deserialize( ConstPointer( char ) buffer , AuxDataType< Real > &data ) const
44 {
45 data = _factory();
46 _factory.fromBuffer( buffer , data );
47 }
48 protected:
49 VertexFactory::DynamicFactory< Real > _factory;
50 };
51
52 template< typename Real >
53 struct ProjectiveAuxDataTypeSerializer : public Serializer< ProjectiveData< AuxDataType< Real > , Real > >
54 {
55 using Data = ProjectiveData< AuxDataType< Real > , Real >;
56
57 ProjectiveAuxDataTypeSerializer( const std::vector< PlyProperty > &properties ) : _factory( properties ){}
58
59 size_t size( void ) const { return sizeof( Real ) + _factory.bufferSize(); }
60
61 void serialize( const Data &data , Pointer( char )buffer ) const
62 {
63 memcpy( buffer , &data.weight , sizeof(Real) );
64 _factory.toBuffer( data.data , buffer+sizeof(Real) );
65 }
66 void deserialize( ConstPointer( char ) buffer , Data &data ) const
67 {
68 data.data = _factory();
69 memcpy( &data.weight , buffer , sizeof(Real) );
70 _factory.fromBuffer( buffer+sizeof(Real) , data.data );
71 }
72 protected:
73 VertexFactory::DynamicFactory< Real > _factory;
74 };
75
76 template< typename Real , unsigned int Dim >
77 using SampleDataType = DirectSum< Real , typename VertexFactory::NormalFactory< Real , Dim >::VertexType , typename VertexFactory::DynamicFactory< Real >::VertexType >;
78
79 template< typename Real , unsigned int Dim >
80 struct SampleDataTypeSerializer : public Serializer< SampleDataType< Real , Dim > >
81 {
82 using Normal = typename VertexFactory::NormalFactory< Real , Dim >::VertexType;
83 using AuxData = typename VertexFactory::DynamicFactory< Real >::VertexType;
84 using Data = DirectSum< Real , Normal , AuxData >;
85
86 SampleDataTypeSerializer( const std::vector< PlyProperty > &properties ) : _factory( properties ){}
87
88 size_t size( void ) const { return sizeof(Normal) + _factory.bufferSize(); }
89
90 void serialize( const Data &data , Pointer( char )buffer ) const
91 {
92 memcpy( buffer , &data.template get<0>() , sizeof(Normal) );
93 _factory.toBuffer( data.template get<1>() , buffer+sizeof(Normal) );
94 }
95 void deserialize( ConstPointer( char ) buffer , Data &data ) const
96 {
97 Point< Real > p = _factory();
98 memcpy( &data.template get<0>() , buffer , sizeof(Normal) );
99 _factory.fromBuffer( buffer+sizeof(Normal) , p );
100 data.template get<1>() = p;
101 }
102 protected:
103 VertexFactory::DynamicFactory< Real > _factory;
104 };
105
106 template< typename Real , unsigned int Dim >
107 struct ProjectiveSampleDataTypeSerializer : public Serializer< ProjectiveData< SampleDataType< Real , Dim > , Real > >
108 {
109 using Normal = typename VertexFactory::NormalFactory< Real , Dim >::VertexType;
110 using AuxData = typename VertexFactory::DynamicFactory< Real >::VertexType;
111 using Data = ProjectiveData< DirectSum< Real , Normal , AuxData > , Real >;
112
113 ProjectiveSampleDataTypeSerializer( const std::vector< PlyProperty > &properties ) : _factory( properties ){}
114
115 size_t size( void ) const { return sizeof( Real ) + sizeof(Normal) + _factory.bufferSize(); }
116
117 void serialize( const Data &data , Pointer( char )buffer ) const
118 {
119 memcpy( buffer , &data.weight , sizeof(Real) );
120 memcpy( buffer + sizeof(Real) , &data.data.template get<0>() , sizeof(Normal) );
121 _factory.toBuffer( data.data.template get<1>() , buffer+sizeof(Real)+sizeof(Normal) );
122 }
123 void deserialize( ConstPointer( char ) buffer , Data &data ) const
124 {
125 Point< Real > p = _factory();
126 memcpy( &data.weight , buffer , sizeof(Real) );
127 memcpy( &data.data.template get<0>() , buffer+sizeof(Real) , sizeof(Normal) );
128 _factory.fromBuffer( buffer+sizeof(Real)+sizeof(Normal) , p );
129 data.data.template get<1>() = p;
130 }
131 protected:
132 VertexFactory::DynamicFactory< Real > _factory;
133 };
134
135 std::string SendDataString( unsigned int phase , size_t ioBytes )
136 {
137 std::stringstream ss;
138 ss << "[SEND " << phase << "] ";
139 size_t sz;
140 std::string type;
141 if ( ioBytes<(1<<10) ) sz = (ioBytes>> 0) , type = " B";
142 else if( ioBytes<(1<<20) ) sz = (ioBytes>>10) , type = "KiB";
143 else if( ioBytes<(1<<30) ) sz = (ioBytes>>20) , type = "MiB";
144 else sz = (ioBytes>>30) , type = "GiB";
145 if ( sz<10 ) ss << " ";
146 else if( sz<100 ) ss << " ";
147 else if( sz<1000 ) ss << " ";
148 ss << sz << " " << type << ": ";
149 return ss.str();
150 }
151 std::string ReceiveDataString( unsigned int phase , size_t ioBytes )
152 {
153 std::stringstream ss;
154 ss << "[RECEIVE " << phase << "] ";
155 size_t sz;
156 std::string type;
157 if ( ioBytes<(1<<10) ) sz = (ioBytes>> 0) , type = " B";
158 else if( ioBytes<(1<<20) ) sz = (ioBytes>>10) , type = "KiB";
159 else if( ioBytes<(1<<30) ) sz = (ioBytes>>20) , type = "MiB";
160 else sz = (ioBytes>>30) , type = "GiB";
161 if ( sz<10 ) ss << " ";
162 else if( sz<100 ) ss << " ";
163 else if( sz<1000 ) ss << " ";
164 ss << sz << " " << type << ": ";
165 return ss.str();
166 }
167
168 template< unsigned int Dim >
169 void TreeAddressesToIndices( Pointer( RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > ) nodes , size_t nodeCount )
170 {
171 typedef RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > FEMTreeNode;
172
173 for( size_t i=0 ; i<nodeCount ; i++ )
174 {
175 if( nodes[i].parent ) nodes[i].parent = (FEMTreeNode*)( nodes[i].parent - PointerAddress( nodes ) );
176 else nodes[i].parent = (FEMTreeNode*)-1;
177 if( nodes[i].children ) nodes[i].children = (FEMTreeNode*)( nodes[i].children - PointerAddress( nodes ) );
178 else nodes[i].children = (FEMTreeNode*)-1;
179 }
180 }
181
182 template< unsigned int Dim >
183 void TreeIndicesToAddresses( Pointer( RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > ) nodes , size_t nodeCount )
184 {
185 typedef RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > FEMTreeNode;
186
187 for( size_t i=0 ; i<nodeCount ; i++ )
188 {
189 if( (size_t)nodes[i].parent!=-1 ) nodes[i].parent = PointerAddress( nodes ) + (size_t)nodes[i].parent;
190 else nodes[i].parent = NULL;
191 if( (size_t)nodes[i].children!=-1 ) nodes[i].children = PointerAddress( nodes ) + (size_t)nodes[i].children;
192 else nodes[i].children = NULL;
193 }
194 }
195
196 template< bool ReadFromFile >
197 struct ClientServerStream : BinaryStream
198 {
199 template< typename Real , unsigned int Dim >
200 static void Reset( unsigned int idx , const ClientReconstructionInfo< Real , Dim > &clientReconInfo )
201 {
202 auto CleanUp =[]( std::string fileName )
203 {
204 std::ifstream fs;
205 fs.open( fileName , std::ios::in | std::ios::binary );
206 if( fs.is_open() )
207 {
208 fs.close();
209 remove( fileName.c_str() );
210 }
211 };
212 {
213 CleanUp( clientReconInfo.sharedFile( idx , ClientReconstructionInfo< Real , Dim >::ShareType::BACK ) );
214 CleanUp( clientReconInfo.sharedFile( idx , ClientReconstructionInfo< Real , Dim >::ShareType::CENTER ) );
215 CleanUp( clientReconInfo.sharedFile( idx , ClientReconstructionInfo< Real , Dim >::ShareType::FRONT ) );
216 }
217 }
218
219 ClientServerStream( ClientServerStream &&css ) : _socket( std::move( css )._socket ) , _fs( std::move( css )._fs ) , _fileName( std::move( css )._fileName ) {}
220
221 template< typename Real , unsigned int Dim >
222 ClientServerStream( SocketStream &socket , unsigned int idx , const ClientReconstructionInfo< Real , Dim > &clientReconInfo , typename ClientReconstructionInfo< Real , Dim >::ShareType shareType = ClientReconstructionInfo< Real , Dim >::CENTER , unsigned int maxTries=-1 )
223 : _socket(socket)
224 {
225 _fileName = clientReconInfo.sharedFile( idx , shareType );
226 if constexpr( ReadFromFile )
227 {
228 // Check that the file exists and is the right size
229 auto validFile = []( std::string fileName , size_t sz )
230 {
231 std::ifstream fs;
232 fs.open( fileName , std::ios::in | std::ios::binary );
233 if( !fs.is_open() ) return false;
234
235 fs.seekg ( 0 , fs.end );
236 if( fs.tellg()!=sz )
237 {
238 std::cout << fs.tellg() << " / " << sz << std::endl;
239 return false;
240 }
241 else return true;
242 };
243 size_t sz;
244 _socket.read( sz );
245 unsigned int tries=0;
246 while( !validFile( _fileName , sz ) && tries<maxTries )
247 {
248 std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
249 tries++;
250 }
251 _fs.open( _fileName , std::ios::in | std::ios::binary );
252 if( !_fs.is_open() ) MK_THROW( "Failed to open file for reading: " , _fileName );
253 }
254 else
255 {
256 // Check that the file does not exist
257 auto validFile = []( std::string fileName )
258 {
259 std::ifstream fs;
260 fs.open( fileName , std::ios::in | std::ios::binary );
261 return !fs.is_open();
262 };
263 unsigned int tries=0;
264 while( !validFile( _fileName ) && tries<maxTries )
265 {
266 std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
267 tries++;
268 }
269 if( !validFile( _fileName ) ) MK_THROW( "File exists: " , _fileName , " , " , tries , " / " , maxTries );
270
271 _fs.open( _fileName , std::ios::out | std::ios::binary );
272 if( !_fs.is_open() ) MK_THROW( "Failed to open file for writing: " , _fileName );
273 }
274 }
275 ~ClientServerStream( void )
276 {
277 _fs.close();
278 if( ReadFromFile ) std::remove( _fileName.c_str() );
279 else _socket.write( ioBytes );
280 }
281
282 protected:
283 std::conditional_t< ReadFromFile , std::ifstream , std::ofstream > _fs;
284 SocketStream &_socket;
285 std::string _fileName;
286
287 bool _read( Pointer( unsigned char ) ptr , size_t sz )
288 {
289 if constexpr( ReadFromFile ) return (bool)_fs.read ( ( char * )PointerAddress( ptr ) , sz );
290 else return false;
291 }
292 bool _write( ConstPointer( unsigned char ) ptr , size_t sz )
293 {
294 if constexpr( !ReadFromFile ) return (bool)_fs.write( ( const char * )PointerAddress( ptr ) , sz );
295 else return false;
296 }
297 };
298
299 struct PhaseInfo
300 {
301 double processTime , readTime , writeTime;
302 size_t readBytes , writeBytes;
303
304 PhaseInfo( void ) : processTime(0) , readTime(0) , writeTime(0) , readBytes(0) , writeBytes(0) {}
305
306 PhaseInfo &operator += ( const PhaseInfo &pi )
307 {
308 processTime += pi.processTime;
309 readTime += pi.readTime;
310 readBytes += pi.readBytes;
311 writeBytes += pi.writeBytes;
312 return *this;
313 }
314 };
315
316 //////////////////////////////
317 // ClientReconstructionInfo //
318 //////////////////////////////
319 template< typename Real , unsigned int Dim >
320 ClientReconstructionInfo< Real , Dim >::ClientReconstructionInfo( void )
321 {
322 distributionDepth = 0;
323 baseDepth = 5;
324 iters = 8;
325 confidence = false;
326 samplesPerNode = (Real)1.5;
327 dataX = (Real)32.;
328 density = false;
329 linearFit = false;
330 mergeType = MergeType::TOPOLOGY_AND_FUNCTION;
331 bufferSize = BUFFER_IO;
332 filesPerDir = -1;
333 outputSolution = false;
334 targetValue = (Real)0.5;
335 gridCoordinates = false;
336 }
337
338 template< typename Real , unsigned int Dim >
339 ClientReconstructionInfo< Real , Dim >::ClientReconstructionInfo( BinaryStream &stream )
340 {
341 auto ReadBool = [&]( bool &b )
342 {
343 char _b;
344 if( !stream.read( _b ) ) return false;
345 b = _b!=0;
346 return true;
347 };
348 if( !stream.read( inDir ) ) MK_THROW( "Failed to read in dir" );
349 if( !stream.read( tempDir ) ) MK_THROW( "Failed to read temp dir" );
350 if( !stream.read( outDir ) ) MK_THROW( "Failed to read out dir" );
351 if( !stream.read( header ) ) MK_THROW( "Failed to read header" );
352 if( !stream.read( bufferSize ) ) MK_THROW( "Failed to read buffer size" );
353 if( !stream.read( filesPerDir ) ) MK_THROW( "Failed to read files per dir" );
354 if( !stream.read( reconstructionDepth ) ) MK_THROW( "Failed to read reconstruction depth" );
355 if( !stream.read( sharedDepth ) ) MK_THROW( "Failed to read shared depth" );
356 if( !stream.read( distributionDepth ) ) MK_THROW( "Failed to read distribution depth" );
357 if( !stream.read( baseDepth ) ) MK_THROW( "Failed to read base depth" );
358 if( !stream.read( kernelDepth ) ) MK_THROW( "Failed to read kernel depth" );
359 if( !stream.read( solveDepth ) ) MK_THROW( "Failed to read solveDepth depth" );
360 if( !stream.read( iters ) ) MK_THROW( "Failed to read iters" );
361 if( !stream.read( cgSolverAccuracy ) ) MK_THROW( "Failed to read CG-solver-accuracy" );
362 if( !stream.read( targetValue ) ) MK_THROW( "Failed to read target-value" );
363 if( !stream.read( pointWeight ) ) MK_THROW( "Failed to read point-weight" );
364 if( !stream.read( samplesPerNode ) ) MK_THROW( "Failed to read samples-per-node" );
365 if( !stream.read( dataX ) ) MK_THROW( "Failed to read data-multiplier" );
366 if( !stream.read( padSize ) ) MK_THROW( "Failed to read padSize" );
367 if( !stream.read( verbose ) ) MK_THROW( "Failed to read verbose" );
368 if( !stream.read( mergeType ) ) MK_THROW( "Failed to read merge-type" );
369 if( !ReadBool( density ) ) MK_THROW( "Failed to read density flag" );
370 if( !ReadBool( linearFit ) ) MK_THROW( "Failed to read linear-fit flag" );
371 if( !ReadBool( outputSolution ) ) MK_THROW( "Failed to read output-solution flag" );
372 if( !ReadBool( gridCoordinates ) ) MK_THROW( "Failed to read grid-coordinates flag" );
373 if( !ReadBool( ouputVoxelGrid ) ) MK_THROW( "Failed to read output-voxel-grid flag" );
374 if( !ReadBool( confidence ) ) MK_THROW( "Failed to read confidence flag" );
375 {
376 size_t sz;
377 if( !stream.read( sz ) ) MK_THROW( "Failed to read number of auxiliary properties" );
378 auxProperties.resize(sz);
379 for( size_t i=0 ; i<sz ; i++ ) auxProperties[i].read( stream );
380 }
381 }
382
383 template< typename Real , unsigned int Dim >
384 void ClientReconstructionInfo< Real , Dim >::write( BinaryStream &stream ) const
385 {
386 auto WriteBool = [&]( bool b )
387 {
388 char _b = b ? 1 : 0;
389 stream.write( _b );
390 };
391 stream.write( inDir );
392 stream.write( tempDir );
393 stream.write( outDir );
394 stream.write( header );
395 stream.write( bufferSize );
396 stream.write( filesPerDir );
397 stream.write( reconstructionDepth );
398 stream.write( sharedDepth );
399 stream.write( distributionDepth );
400 stream.write( baseDepth );
401 stream.write( kernelDepth );
402 stream.write( solveDepth );
403 stream.write( iters );
404 stream.write( cgSolverAccuracy );
405 stream.write( targetValue );
406 stream.write( pointWeight );
407 stream.write( samplesPerNode );
408 stream.write( dataX );
409 stream.write( padSize );
410 stream.write( verbose );
411 stream.write( mergeType );
412 WriteBool( density );
413 WriteBool( linearFit );
414 WriteBool( outputSolution );
415 WriteBool( gridCoordinates );
416 WriteBool( ouputVoxelGrid );
417 WriteBool( confidence );
418 {
419 size_t sz = auxProperties.size();
420 stream.write( sz );
421 for( size_t j=0 ; j<sz ; j++ ) auxProperties[j].write( stream );
422 }
423 }
424
425 template< typename Real , unsigned int Dim >
426 std::string ClientReconstructionInfo< Real , Dim >::sharedFile( unsigned int idx , ShareType shareType ) const
427 {
428 std::stringstream sStream;
429 switch( shareType )
430 {
431 case BACK: sStream << PointPartition::FileDir( tempDir , header ) << "." << idx << ".back.shared" ; break;
432 case CENTER: sStream << PointPartition::FileDir( tempDir , header ) << "." << idx << ".shared" ; break;
433 case FRONT: sStream << PointPartition::FileDir( tempDir , header ) << "." << idx << ".front.shared" ; break;
434 default: MK_THROW( "Unrecognized share type: " , shareType );
435 }
436 return sStream.str();
437 }
438