MCPcopy Create free account
hub / github.com/MeshInspector/MeshLib / fromRaw

Function fromRaw

source/MRMesh/MRDistanceMapLoad.cpp:20–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18{
19
20Expected<DistanceMap> fromRaw( const std::filesystem::path& path, const DistanceMapLoadSettings& settings )
21{
22 MR_TIMER;
23
24 if ( path.empty() )
25 return unexpected( "Path is empty" );
26
27 auto ext = utf8string( path.extension() );
28 for ( auto& c : ext )
29 c = ( char )tolower( c );
30
31 if ( ext != ".raw" )
32 {
33 std::stringstream ss;
34 ss << "Extension is not correct, expected \".raw\" current \"" << ext << "\"" << std::endl;
35 return unexpected( ss.str() );
36 }
37
38 std::error_code ec;
39 if ( !std::filesystem::exists( path, ec ) )
40 return unexpected( "File " + utf8string( path ) + " does not exist" );
41
42 std::ifstream inFile( path, std::ios::binary );
43 const std::string readError = "Cannot read file: " + utf8string( path );
44 if ( !inFile )
45 return unexpected( readError );
46
47 uint64_t resolution[2] = {};
48 if ( !inFile.read( ( char* )resolution, sizeof( resolution ) ) )
49 return unexpected( readError );
50 const size_t size = size_t( resolution[0] ) * size_t( resolution[1] );
51 const size_t fileSize = std::filesystem::file_size( path, ec );
52
53 if ( size != ( fileSize - 2 * sizeof( uint64_t ) ) / sizeof( float ) )
54 return unexpected( "File does not hold a distance map" );
55
56 DistanceMap dmap( resolution[0], resolution[1] );
57 std::vector<float> buffer( size );
58
59 if ( !readByBlocks( inFile, ( char* )buffer.data(), buffer.size() * sizeof( float ), settings.progress ) )
60 return unexpectedOperationCanceled();
61
62 if ( !inFile )
63 return unexpected( readError );
64
65 for ( size_t i = 0; i < size; ++i )
66 dmap.set( int( i ), buffer[i] );
67
68 return dmap;
69}
70
71Expected<DistanceMap> fromMrDistanceMap( const std::filesystem::path& path, const DistanceMapLoadSettings& settings )
72{

Callers 1

Calls 9

utf8stringFunction · 0.85
readByBlocksFunction · 0.85
unexpectedFunction · 0.70
emptyMethod · 0.45
readMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected