| 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 |
enum _IndexType{ INT , U_INT , LONG_LONG }; |
| 30 |
|
| 31 |
inline std::string _FullFileName( std::string dir , std::string fileName ) |
| 32 |
{ |
| 33 |
if( dir.back()!=FileSeparator ) dir += std::string(1,FileSeparator); |
| 34 |
return dir + fileName; |
| 35 |
} |
| 36 |
|
| 37 |
inline void _Copy( FILE *target , FILE *source , size_t sz , size_t bufferSize=1<<20 ) |
| 38 |
{ |
| 39 |
Pointer( unsigned char ) buffer = NewPointer< unsigned char >( bufferSize ); |
| 40 |
if( sz==-1 ) |
| 41 |
{ |
| 42 |
size_t ioBytes; |
| 43 |
while( ( ioBytes=fread( buffer , sizeof(unsigned char) , bufferSize , source ) ) ) fwrite( buffer , sizeof(unsigned char) , ioBytes , target ); |
| 44 |
} |
| 45 |
else |
| 46 |
{ |
| 47 |
while( sz ) |
| 48 |
{ |
| 49 |
size_t ioBytes = std::min< size_t >( bufferSize , sz ); |
| 50 |
if( ioBytes!=fread( buffer , sizeof(unsigned char) , ioBytes , source ) ) MK_THROW( "Failed to read from source: " , ioBytes ); |
| 51 |
if( ioBytes!=fwrite( buffer , sizeof(unsigned char) , ioBytes , target ) ) MK_THROW( "Failed to write to target: " , ioBytes ); |
| 52 |
sz -= ioBytes; |
| 53 |
} |
| 54 |
} |
| 55 |
DeletePointer( buffer ); |
| 56 |
} |
| 57 |
|
| 58 |
template< typename Factory > |
| 59 |
size_t SizeOnDisk( const Factory &factory ) |
| 60 |
{ |
| 61 |
size_t sizeOnDisk = 0; |
| 62 |
if constexpr( Factory::IsStaticallyAllocated() ) for( unsigned int i=0 ; i<factory.plyWriteNum() ; i++ ) sizeOnDisk += ply_type_size[ factory.plyStaticWriteProperty(i).external_type ]; |
| 63 |
else for( unsigned int i=0 ; i<factory.plyWriteNum() ; i++ ) sizeOnDisk += ply_type_size[ factory.plyWriteProperty(i).external_type ]; |
| 64 |
return sizeOnDisk; |
| 65 |
} |
| 66 |
|
| 67 |
template< typename Index , typename Factory > |
| 68 |
void _OffsetPolygons( const Factory &factory , std::string in , std::string out , size_t offset , Profiler &profiler ) |
| 69 |
{ |
| 70 |
|
| 71 |
int ft; |
| 72 |
std::vector< std::string > comments; |
| 73 |
|
| 74 |
std::vector< std::tuple< std::string , size_t , std::vector< PlyProperty > > > _elems; |
| 75 |
PlyFile *inPly = PLY::ReadHeader( in , ft , _elems ); |
| 76 |
|
| 77 |
size_t sizeOnDisk = SizeOnDisk( factory ); |
| 78 |
|
| 79 |
// Skip the vertices |
| 80 |
{ |
| 81 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 82 |
_fseeki64( inPly->fp , sizeOnDisk * std::get<1>( _elems[0] ) , SEEK_CUR ); |
| 83 |
#else // !_WIN32 && !_WIN64 |
| 84 |
fseek( inPly->fp , sizeOnDisk * std::get<1>( _elems[0] ) , SEEK_CUR ); |
| 85 |
#endif // _WIN32 || _WIN64 |
| 86 |
} |
| 87 |
|
| 88 |
std::vector< std::tuple< std::string , size_t , std::vector< PlyProperty > > > elems(1); |
| 89 |
std::get<0>( elems[0] ) = std::string( "face" ); |
| 90 |
std::get<1>( elems[0] ) = std::get<1>( _elems[1] ); |
| 91 |
std::get<2>( elems[0] ).resize(1); |
| 92 |
std::get<2>( elems[0] )[0] = PLY::Face< Index >::Properties[0]; |
| 93 |
|
| 94 |
PlyFile *outPly = PLY::WriteHeader( out , ft , elems , comments ); |
| 95 |
|
| 96 |
// Read and offset the polygons |
| 97 |
{ |
| 98 |
int maxIndices = 64; |
| 99 |
Index *faceIndices = (Index*)malloc( sizeof(Index) * maxIndices ); |
| 100 |
|
| 101 |
auto ReadPolygon = [&]( FILE *fp ) |
| 102 |
{ |
| 103 |
int n; |
| 104 |
if( fread( &n , sizeof(int) , 1 , fp )!=1 ) MK_THROW( "Failed to read polygon size" ); |
| 105 |
if( n>maxIndices ) |
| 106 |
{ |
| 107 |
maxIndices = n; |
| 108 |
faceIndices = (Index*)realloc( faceIndices , sizeof(Index) * maxIndices ); |
| 109 |
} |
| 110 |
if( fread( faceIndices , sizeof(Index) , n , fp )!=n ) MK_THROW( "Failed to read polygon indices" ); |
| 111 |
return n; |
| 112 |
}; |
| 113 |
|
| 114 |
auto WritePolygon = [&]( FILE *fp , int n ) |
| 115 |
{ |
| 116 |
fwrite( &n , sizeof(int) , 1 , fp ); |
| 117 |
fwrite( faceIndices , sizeof(Index) , n , fp ); |
| 118 |
}; |
| 119 |
|
| 120 |
for( unsigned int j=0 ; j<std::get<2>( _elems[1] ).size() ; j++ ) inPly->get_property( std::get<0>( _elems[1] ) , &std::get<2>( _elems[1] )[j] ); |
| 121 |
outPly->put_element_setup( std::string( "face" ) ); |
| 122 |
|
| 123 |
for( size_t i=0 ; i<std::get<1>( _elems[1] ) ; i++ ) |
| 124 |
{ |
| 125 |
int n = ReadPolygon( inPly->fp ); |
| 126 |
for( int j=0 ; j<n ; j++ ) faceIndices[j] += (Index)offset; |
| 127 |
WritePolygon( outPly->fp , n ); |
| 128 |
} |
| 129 |
|
| 130 |
profiler.update(); |
| 131 |
|
| 132 |
delete[] faceIndices; |
| 133 |
} |
| 134 |
delete inPly; |
| 135 |
delete outPly; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
//////////// |
| 140 |
// Server // |
| 141 |
//////////// |
| 142 |
|
| 143 |
template< typename Real , unsigned int Dim , typename Factory > |
| 144 |
void _RunServer |
| 145 |
( |
| 146 |
std::string inDir , |
| 147 |
std::string tempDir , |
| 148 |
std::string header , |
| 149 |
std::string out , |
| 150 |
std::vector< Socket > &clientSockets , |
| 151 |
const std::vector< unsigned int > &sharedVertexCounts , |
| 152 |
ClientMergePlyInfo clientMergePlyInfo , |
| 153 |
const Factory &factory , |
| 154 |
unsigned int sampleMS , |
| 155 |
std::function< std::vector< std::string > (unsigned int) > commentFunctor |
| 156 |
) |
| 157 |
{ |
| 158 |
Profiler profiler(sampleMS); |
| 159 |
typedef typename Factory::VertexType Vertex; |
| 160 |
|
| 161 |
auto ClientFile = []( std::string dir , std::string header , unsigned int idx ) |
| 162 |
{ |
| 163 |
std::stringstream ss; |
| 164 |
ss << _FullFileName( dir , header ) << "." << idx << ".ply"; |
| 165 |
return ss.str(); |
| 166 |
}; |
| 167 |
|
| 168 |
auto InFile = [&]( unsigned int idx ){ return ClientFile( inDir , header , idx ); }; |
| 169 |
auto TempPolygonFile = [&]( unsigned int idx ){ return ClientFile( tempDir , header + std::string( ".polygons" ) , idx ); }; |
| 170 |
|
| 171 |
profiler.reset(); |
| 172 |
|
| 173 |
// Get the number of vertices and polygons per file |
| 174 |
std::vector< size_t > vNum( sharedVertexCounts.size()+1 ) , fNum( sharedVertexCounts.size()+1 ) , offsets( sharedVertexCounts.size()+1 ); |
| 175 |
{ |
| 176 |
for( unsigned int i=0 ; i<=sharedVertexCounts.size() ; i++ ) |
| 177 |
{ |
| 178 |
std::string inFile = InFile(i); |
| 179 |
int ft; |
| 180 |
std::vector< std::tuple< std::string , size_t , std::vector< PlyProperty > > > elems; |
| 181 |
PlyFile *ply = PLY::ReadHeader( inFile , ft , elems ); |
| 182 |
delete ply; |
| 183 |
bool foundVertices = false , foundFaces = false; |
| 184 |
for( unsigned int j=0 ; j<elems.size() ; j++ ) |
| 185 |
if ( std::get<0>( elems[j] )==std::string( "vertex" ) ) foundVertices = true , vNum[i] = std::get<1>( elems[j] ); |
| 186 |
else if( std::get<0>( elems[j] )==std::string( "face" ) ) foundFaces = true , fNum[i] = std::get<1>( elems[j] ); |
| 187 |
if( !foundVertices ) MK_THROW( "Could not find vertices" ); |
| 188 |
if( !foundFaces ) MK_THROW( "Could not find faces" ); |
| 189 |
profiler.update(); |
| 190 |
} |
| 191 |
offsets[0] = 0; |
| 192 |
for( unsigned int i=1 ; i<=sharedVertexCounts.size() ; i++ ) offsets[i] = clientMergePlyInfo.keepSeparate ? 0 : offsets[i-1] + vNum[i-1] - sharedVertexCounts[i-1]; |
| 193 |
} |
| 194 |
|
| 195 |
// Strip out the polygons (though perhaps we should just have the reconstruction code do this). |
| 196 |
#ifdef SHOW_WARNINGS |
| 197 |
MK_WARN( "Should split the mesh during reconstruction" ); |
| 198 |
#endif // SHOW_WARNINGS |
| 199 |
|
| 200 |
_IndexType idxType; |
| 201 |
{ |
| 202 |
size_t maxV; |
| 203 |
if( clientMergePlyInfo.keepSeparate ) |
| 204 |
{ |
| 205 |
maxV = 0; |
| 206 |
for( unsigned int i=0 ; i<vNum.size() ; i++ ) maxV = std::max< size_t >( maxV , vNum[i] ); |
| 207 |
} |
| 208 |
else maxV = offsets.back()+vNum.back(); |
| 209 |
|
| 210 |
if( maxV>std::numeric_limits< int >::max() ) |
| 211 |
{ |
| 212 |
if( maxV>std::numeric_limits< unsigned int >::max () ) idxType = LONG_LONG; |
| 213 |
else idxType = U_INT; |
| 214 |
} |
| 215 |
else idxType = INT; |
| 216 |
} |
| 217 |
|
| 218 |
if( clientMergePlyInfo.verbose ) std::cout << "Got mesh info: " << profiler(true) << std::endl; |
| 219 |
|
| 220 |
|
| 221 |
for( unsigned int i=0 ; i<clientSockets.size() ; i++ ) |
| 222 |
{ |
| 223 |
SocketStream clientStream( clientSockets[i] ); |
| 224 |
clientStream.write( InFile(i) ); |
| 225 |
clientStream.write( TempPolygonFile(i) ); |
| 226 |
clientStream.write( idxType ); |
| 227 |
clientStream.write( offsets[i] ); |
| 228 |
|
| 229 |
} |
| 230 |
for( unsigned int i=0 ; i<clientSockets.size() ; i++ ) |
| 231 |
{ |
| 232 |
unsigned char done; |
| 233 |
SocketStream clientStream( clientSockets[i] ); |
| 234 |
clientStream.read( done ); |
| 235 |
} |
| 236 |
|
| 237 |
profiler.reset(); |
| 238 |
std::vector< std::tuple< std::string , size_t , std::vector< PlyProperty > > > elems(2); |
| 239 |
{ |
| 240 |
std::get<0>( elems[0] ) = std::string( "vertex" ); |
| 241 |
std::get<1>( elems[0] ) = 0; |
| 242 |
for( unsigned int i=0 ; i<vNum.size() ; i++ ) std::get<1>( elems[0] ) += vNum[i]; |
| 243 |
for( unsigned int i=0 ; i<sharedVertexCounts.size() ; i++ ) std::get<1>( elems[0] ) -= sharedVertexCounts[i]; |
| 244 |
std::get<2>( elems[0] ).resize( factory.plyWriteNum() ); |
| 245 |
for( unsigned int i=0 ; i<factory.plyWriteNum() ; i++ ) |
| 246 |
if constexpr( Factory::IsStaticallyAllocated() ) std::get<2>( elems[0] )[i] = factory.plyStaticWriteProperty(i); |
| 247 |
else std::get<2>( elems[0] )[i] = factory.plyWriteProperty(i); |
| 248 |
|
| 249 |
std::get<0>( elems[1] ) = std::string( "face" ); |
| 250 |
std::get<1>( elems[1] ) = 0; |
| 251 |
for( unsigned int i=0 ; i<fNum.size() ; i++ ) std::get<1>( elems[1] ) += fNum[i]; |
| 252 |
std::get<2>( elems[1] ).resize(1); |
| 253 |
switch( idxType ) |
| 254 |
{ |
| 255 |
case INT: std::get<2>( elems[1] )[0] = PLY::Face< int >::Properties[0] ; break; |
| 256 |
case U_INT: std::get<2>( elems[1] )[0] = PLY::Face< unsigned int >::Properties[0] ; break; |
| 257 |
case LONG_LONG: std::get<2>( elems[1] )[0] = PLY::Face< long long >::Properties[0] ; break; |
| 258 |
default: MK_THROW( "Unrecognized output type" ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
std::vector< PlyFile * > outPly( clientMergePlyInfo.keepSeparate ? sharedVertexCounts.size()+1 : 1 , NULL ); |
| 263 |
if( clientMergePlyInfo.keepSeparate ) |
| 264 |
for( unsigned int i=0 ; i<=sharedVertexCounts.size() ; i++ ) |
| 265 |
{ |
| 266 |
std::string fileName = out + std::string( "." ) + std::to_string(i) + std::string( ".ply" ); |
| 267 |
std::get<1>( elems[0] ) = vNum[i]; |
| 268 |
std::get<1>( elems[1] ) = fNum[i]; |
| 269 |
std::pair< unsigned , unsigned int > shared; |
| 270 |
shared.first = i>0 ? sharedVertexCounts[i-1] : 0; |
| 271 |
shared.second = i<sharedVertexCounts.size() ? sharedVertexCounts[i ] : 0; |
| 272 |
std::vector< std::string > _comments = commentFunctor( i ); |
| 273 |
std::vector< std::string > comments( 1+_comments.size() ); |
| 274 |
comments[0] = std::string( "Shared: " ) + std::to_string( shared.first ) + std::string( "|" ) + std::to_string( shared.second ); |
| 275 |
for( unsigned int j=0 ; j<_comments.size() ; j++ ) comments[j+1] = _comments[j]; |
| 276 |
outPly[i] = PLY::WriteHeader( fileName , PLY_BINARY_NATIVE , elems , comments ); |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
std::vector< std::string > comments = commentFunctor(-1); |
| 281 |
outPly[0] = PLY::WriteHeader( out , PLY_BINARY_NATIVE , elems , comments ); |
| 282 |
} |
| 283 |
|
| 284 |
// Write out the (merged) vertices |
| 285 |
{ |
| 286 |
Pointer( char ) vBuffer = NewPointer< char >( factory.bufferSize() ); |
| 287 |
|
| 288 |
for( unsigned int i=0 ; i<outPly.size() ; i++ ) outPly[i]->put_element_setup( std::string( "vertex" ) ); |
| 289 |
|
| 290 |
size_t sizeOnDisk = SizeOnDisk( factory ); |
| 291 |
|
| 292 |
std::vector< Vertex > sharedVertices; |
| 293 |
for( unsigned int i=0 ; i<=sharedVertexCounts.size() ; i++ ) |
| 294 |
{ |
| 295 |
std::vector< std::tuple< std::string , size_t , std::vector< PlyProperty > > > _elems; |
| 296 |
int ft; |
| 297 |
PlyFile *inPly = PLY::ReadHeader( InFile(i) , ft , _elems ); |
| 298 |
|
| 299 |
for( unsigned int j=0 ; j<std::get<2>( elems[0] ).size() ; j++ ) inPly->get_property( std::get<0>( elems[0] ) , &std::get<2>( elems[0] )[j] ); |
| 300 |
|
| 301 |
// Merge the start vertices with the end vertices from the previous client |
| 302 |
Vertex v = factory(); |
| 303 |
for( unsigned int j=0 ; j<sharedVertices.size() ; j++ ) |
| 304 |
{ |
| 305 |
if constexpr( Factory::IsStaticallyAllocated() ) |
| 306 |
{ |
| 307 |
inPly->get_element( &v ); |
| 308 |
sharedVertices[j] = ( sharedVertices[j] + v ) / (Real)2.; |
| 309 |
if( clientMergePlyInfo.keepSeparate ) |
| 310 |
{ |
| 311 |
// Write the merged vertices into the end of the previous |
| 312 |
if( i ) outPly[i-1]->put_element( (void*)&sharedVertices[j] ); |
| 313 |
// Write the merged vertices into the beginning of the current |
| 314 |
outPly[i]->put_element( (void*)&sharedVertices[j] ); |
| 315 |
} |
| 316 |
else outPly[0]->put_element( (void*)&sharedVertices[j] ); |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
inPly->get_element( PointerAddress( vBuffer ) ); |
| 321 |
factory.fromBuffer( vBuffer , v ); |
| 322 |
sharedVertices[j] = ( sharedVertices[j] + v ) / (Real)2.; |
| 323 |
factory.toBuffer( sharedVertices[j] , vBuffer ); |
| 324 |
if( clientMergePlyInfo.keepSeparate ) |
| 325 |
{ |
| 326 |
// Write the merged vertices into the end of the previous |
| 327 |
if( i ) outPly[i-1]->put_element( PointerAddress( vBuffer ) ); |
| 328 |
// Write the merged vertices into the beginning of the current |
| 329 |
outPly[i]->put_element( PointerAddress( vBuffer ) ); |
| 330 |
} |
| 331 |
else outPly[0]->put_element( PointerAddress( vBuffer ) ); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
// Copy the interior vertices |
| 336 |
{ |
| 337 |
size_t vNum = std::get<1>( _elems[0] ) - sharedVertices.size(); |
| 338 |
if( i<sharedVertexCounts.size() ) vNum -= sharedVertexCounts[i]; |
| 339 |
_Copy( outPly[ clientMergePlyInfo.keepSeparate ? i : 0 ]->fp , inPly->fp , vNum*sizeOnDisk , clientMergePlyInfo.bufferSize ); |
| 340 |
} |
| 341 |
|
| 342 |
// Buffer the end vertices |
| 343 |
if( i<sharedVertexCounts.size() ) |
| 344 |
{ |
| 345 |
sharedVertices.resize( sharedVertexCounts[i] , factory() ); |
| 346 |
|
| 347 |
for( unsigned int j=0 ; j<sharedVertices.size() ; j++ ) |
| 348 |
{ |
| 349 |
if constexpr( Factory::IsStaticallyAllocated() ) inPly->get_element( (void*)&sharedVertices[j] ); |
| 350 |
else |
| 351 |
{ |
| 352 |
inPly->get_element( PointerAddress( vBuffer ) ); |
| 353 |
factory.fromBuffer( vBuffer , sharedVertices[j] ); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
else sharedVertices.resize(0); |
| 358 |
profiler.update(); |
| 359 |
|
| 360 |
delete inPly; |
| 361 |
} |
| 362 |
DeletePointer( vBuffer ); |
| 363 |
if( clientMergePlyInfo.verbose ) std::cout << "Merged vertices: " << profiler(true) << std::endl; |
| 364 |
} |
| 365 |
|
| 366 |
profiler.reset(); |
| 367 |
// Write out the polygons |
| 368 |
{ |
| 369 |
for( unsigned int i=0 ; i<outPly.size() ; i++ ) outPly[i]->put_element_setup( std::string( "face" ) ); |
| 370 |
|
| 371 |
for( unsigned int i=0 ; i<=sharedVertexCounts.size() ; i++ ) |
| 372 |
{ |
| 373 |
std::vector< std::tuple< std::string , size_t , std::vector< PlyProperty > > > _elems; |
| 374 |
int ft; |
| 375 |
PlyFile *inPly = PLY::ReadHeader( TempPolygonFile(i) , ft , _elems ); |
| 376 |
|
| 377 |
_Copy( outPly[ clientMergePlyInfo.keepSeparate ? i : 0 ]->fp , inPly->fp , -1 , clientMergePlyInfo.bufferSize ); |
| 378 |
profiler.update(); |
| 379 |
delete inPly; |
| 380 |
} |
| 381 |
if( clientMergePlyInfo.verbose ) std::cout << "Merged polygons: " << profiler(true) << std::endl; |
| 382 |
} |
| 383 |
for( unsigned int i=0 ; i<outPly.size() ; i++ ) delete outPly[i]; |
| 384 |
|
| 385 |
|
| 386 |
for( unsigned int i=0 ; i<=sharedVertexCounts.size() ; i++ ) |
| 387 |
{ |
| 388 |
std::string fileName = TempPolygonFile(i); |
| 389 |
std::remove( fileName.c_str() ); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
template< typename Real , unsigned int Dim > |
| 394 |
void RunServer |
| 395 |
( |
| 396 |
std::string inDir , |
| 397 |
std::string tempDir , |
| 398 |
std::string header , |
| 399 |
std::string out , |
| 400 |
std::vector< Socket > &clientSockets , |
| 401 |
const std::vector< unsigned int > &sharedVertexCounts , |
| 402 |
ClientMergePlyInfo clientMergePlyInfo , |
| 403 |
unsigned int sampleMS , |
| 404 |
std::function< std::vector< std::string > (unsigned int) > commentFunctor |
| 405 |
) |
| 406 |
{ |
| 407 |
if( clientSockets.size()!=sharedVertexCounts.size()+1 ) MK_THROW( "Socket num and shared vertex count don't match: " , clientSockets.size() , " / " , sharedVertexCounts.size() ); |
| 408 |
|
| 409 |
for( unsigned int i=0 ; i<clientSockets.size() ; i++ ) |
| 410 |
{ |
| 411 |
SocketStream clientStream( clientSockets[i] ); |
| 412 |
clientMergePlyInfo.write( clientStream ); |
| 413 |
} |
| 414 |
|
| 415 |
if( clientMergePlyInfo.auxProperties.size() ) |
| 416 |
{ |
| 417 |
typedef VertexFactory::Factory< Real , VertexFactory::PositionFactory< Real , Dim > , VertexFactory::DynamicFactory< Real > > Factory; |
| 418 |
VertexFactory::PositionFactory< Real , Dim > vFactory; |
| 419 |
VertexFactory::DynamicFactory< Real > dFactory( clientMergePlyInfo.auxProperties ); |
| 420 |
Factory factory( vFactory , dFactory ); |
| 421 |
_RunServer< Real , Dim >( inDir , tempDir , header , out , clientSockets , sharedVertexCounts , clientMergePlyInfo , factory , sampleMS , commentFunctor ); |
| 422 |
} |
| 423 |
else |
| 424 |
{ |
| 425 |
typedef VertexFactory::Factory< Real , VertexFactory::PositionFactory< Real , Dim > > Factory; |
| 426 |
Factory factory; |
| 427 |
_RunServer< Real , Dim >( inDir , tempDir , header , out , clientSockets , sharedVertexCounts , clientMergePlyInfo , factory , sampleMS , commentFunctor ); |
| 428 |
} |
| 429 |
|
| 430 |
} |
| 431 |
|
| 432 |
//////////// |
| 433 |
// Client // |
| 434 |
//////////// |
| 435 |
template< typename Real , unsigned int Dim , typename Factory > |
| 436 |
void _RunClients |
| 437 |
( |
| 438 |
ClientMergePlyInfo clientMergePlyInfo , |
| 439 |
const Factory &factory , |
| 440 |
std::vector< Socket > &serverSockets , |
| 441 |
unsigned int sampleMS |
| 442 |
) |
| 443 |
{ |
| 444 |
Profiler profiler( sampleMS ); |
| 445 |
|
| 446 |
for( unsigned int c=0 ; c<serverSockets.size() ; c++ ) |
| 447 |
{ |
| 448 |
SocketStream socketStream( serverSockets[c] ); |
| 449 |
std::string in , out; |
| 450 |
_IndexType idxType; |
| 451 |
size_t offset; |
| 452 |
socketStream.read( in ); |
| 453 |
socketStream.read( out ); |
| 454 |
socketStream.read( idxType ); |
| 455 |
socketStream.read( offset ); |
| 456 |
|
| 457 |
switch( idxType ) |
| 458 |
{ |
| 459 |
case INT: _OffsetPolygons< int >( factory , in , out , offset , profiler ) ; break; |
| 460 |
case U_INT: _OffsetPolygons< unsigned int >( factory , in , out , offset , profiler ) ; break; |
| 461 |
case LONG_LONG: _OffsetPolygons< long long >( factory , in , out , offset , profiler ) ; break; |
| 462 |
default: MK_THROW( "Unrecognized output index type" ); |
| 463 |
} |
| 464 |
char done = 1; |
| 465 |
socketStream.write( done ); |
| 466 |
} |
| 467 |
if( clientMergePlyInfo.verbose ) std::cout << "Offset polygons: " << profiler(true) << std::endl; |
| 468 |
} |
| 469 |
|
| 470 |
template< typename Real , unsigned int Dim > |
| 471 |
void RunClients( std::vector< Socket > &serverSockets , unsigned int sampleMS ) |
| 472 |
{ |
| 473 |
|
| 474 |
ClientMergePlyInfo clientMergePlyInfo; |
| 475 |
|
| 476 |
for( unsigned int i=0 ; i<serverSockets.size() ; i++ ) |
| 477 |
{ |
| 478 |
SocketStream serverStream( serverSockets[i] ); |
| 479 |
clientMergePlyInfo = ClientMergePlyInfo( serverStream ); |
| 480 |
} |
| 481 |
|
| 482 |
if( clientMergePlyInfo.auxProperties.size() ) |
| 483 |
{ |
| 484 |
typedef VertexFactory::Factory< Real , VertexFactory::PositionFactory< Real , Dim > , VertexFactory::DynamicFactory< Real > > Factory; |
| 485 |
VertexFactory::PositionFactory< Real , Dim > vFactory; |
| 486 |
VertexFactory::DynamicFactory< Real > dFactory( clientMergePlyInfo.auxProperties ); |
| 487 |
Factory factory( vFactory , dFactory ); |
| 488 |
_RunClients< Real , Dim >( clientMergePlyInfo , factory , serverSockets , sampleMS ); |
| 489 |
} |
| 490 |
else |
| 491 |
{ |
| 492 |
typedef VertexFactory::Factory< Real , VertexFactory::PositionFactory< Real , Dim > > Factory; |
| 493 |
Factory factory; |
| 494 |
_RunClients< Real , Dim >( clientMergePlyInfo , factory , serverSockets , sampleMS ); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
///////////////////////// |
| 499 |
// ClientPartitionInfo // |
| 500 |
///////////////////////// |
| 501 |
ClientMergePlyInfo::ClientMergePlyInfo( void ){} |
| 502 |
|
| 503 |
ClientMergePlyInfo::ClientMergePlyInfo( BinaryStream &stream ) |
| 504 |
{ |
| 505 |
auto ReadBool = [&]( bool &b ) |
| 506 |
{ |
| 507 |
char _b; |
| 508 |
if( !stream.read( _b ) ) return false; |
| 509 |
b = _b!=0; |
| 510 |
return true; |
| 511 |
}; |
| 512 |
|
| 513 |
if( !stream.read( bufferSize ) ) MK_THROW( "Failed to read buffer size" ); |
| 514 |
{ |
| 515 |
size_t sz; |
| 516 |
if( !stream.read( sz ) ) MK_THROW( "Failed to read number of auxiliary properties" ); |
| 517 |
auxProperties.resize(sz); |
| 518 |
for( size_t i=0 ; i<sz ; i++ ) auxProperties[i].read( stream ); |
| 519 |
} |
| 520 |
if( !ReadBool( keepSeparate ) ) MK_THROW( "Failed to read keep-separate flag" ); |
| 521 |
if( !ReadBool( verbose ) ) MK_THROW( "Failed to read verbose flag" ); |
| 522 |
} |
| 523 |
|
| 524 |
void ClientMergePlyInfo::write( BinaryStream &stream ) const |
| 525 |
{ |
| 526 |
auto WriteBool = [&]( bool b ) |
| 527 |
{ |
| 528 |
char _b = b ? 1 : 0; |
| 529 |
stream.write( _b ); |
| 530 |
}; |
| 531 |
|
| 532 |
stream.write( bufferSize ); |
| 533 |
{ |
| 534 |
size_t sz = auxProperties.size(); |
| 535 |
stream.write( sz ); |
| 536 |
for( size_t j=0 ; j<sz ; j++ ) auxProperties[j].write( stream ); |
| 537 |
} |
| 538 |
WriteBool( keepSeparate ); |
| 539 |
WriteBool( verbose ); |
| 540 |
} |