| 20 | |
| 21 | |
| 22 | Expected<LoadedObject> makeObjectTreeFromFolder( const std::filesystem::path & folder, bool dicomOnly, const ProgressCallback& callback ) |
| 23 | { |
| 24 | MR_TIMER; |
| 25 | |
| 26 | if ( callback && !callback( 0.f ) ) |
| 27 | return unexpected( getCancelMessage( folder ) ); |
| 28 | |
| 29 | ParallelProgressReporter cb( callback ); |
| 30 | |
| 31 | struct FilePathNode |
| 32 | { |
| 33 | std::filesystem::path path; |
| 34 | std::vector<FilePathNode> subfolders; |
| 35 | std::vector<FilePathNode> files; |
| 36 | bool dicomFolder = false; |
| 37 | #if !defined( MESHLIB_NO_VOXELS ) && !defined( MRVOXELS_NO_DICOM ) |
| 38 | VoxelsLoad::DicomStatus dicomStatus = VoxelsLoad::DicomStatusEnum::Invalid; |
| 39 | #endif |
| 40 | |
| 41 | bool empty() const |
| 42 | { |
| 43 | return files.empty() && subfolders.empty() && !dicomFolder; |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | std::function<FilePathNode( const std::filesystem::path& )> getFilePathNode; |
| 48 | getFilePathNode = [&getFilePathNode, filters = getAllFilters()] ( const std::filesystem::path& folder ) |
| 49 | { |
| 50 | FilePathNode node{ .path = folder }; |
| 51 | std::error_code ec; |
| 52 | for ( auto entry : Directory{ folder, ec } ) |
| 53 | { |
| 54 | auto path = entry.path(); |
| 55 | if ( entry.is_directory( ec ) ) |
| 56 | { |
| 57 | node.subfolders.push_back( getFilePathNode( path ) ); |
| 58 | } |
| 59 | else if ( !node.dicomFolder && ( entry.is_regular_file( ec ) || entry.is_symlink( ec ) ) ) |
| 60 | { |
| 61 | auto ext = utf8string( path.extension() ); |
| 62 | for ( auto& c : ext ) |
| 63 | c = ( char )tolower( c ); |
| 64 | |
| 65 | #if !defined( MESHLIB_NO_VOXELS ) && !defined( MRVOXELS_NO_DICOM ) |
| 66 | if ( auto dicomStatus = VoxelsLoad::isDicomFile( path ); dicomStatus != VoxelsLoad::DicomStatusEnum::Invalid ) // unsupported will be reported later |
| 67 | { |
| 68 | node.dicomStatus = dicomStatus; |
| 69 | node.dicomFolder = true; |
| 70 | node.files.clear(); |
| 71 | } |
| 72 | else |
| 73 | #endif |
| 74 | if ( findFilter( filters, ext ) ) |
| 75 | node.files.push_back( { .path = path } ); |
| 76 | } |
| 77 | } |
| 78 | return node; |
| 79 | }; |
no test coverage detected