MeshLib C++ Docs
Loading...
Searching...
No Matches
MRIOFormatsRegistry.h
Go to the documentation of this file.
1#pragma once
2#include "MRMeshFwd.h"
3#ifndef MR_PARSING_FOR_PB11_BINDINGS
4#include "MRDistanceMap.h"
5#include "MRExpected.h"
6#include "MRIOFilters.h"
8#include "MROnInit.h"
10#include "MRSaveSettings.h"
11#include "MRLoadedObjects.h"
12
13#include <filesystem>
14#include <map>
15
16#define MR_FORMAT_REGISTRY_EXTERNAL_DECL( API_ATTR, ProcName ) \
17API_ATTR ProcName MR_CONCAT( get, ProcName )( const IOFilter& filter ); \
18API_ATTR ProcName MR_CONCAT( get, ProcName )( const std::string& extension ); \
19API_ATTR void MR_CONCAT( set, ProcName )( const IOFilter& filter, ProcName proc, int8_t priorityScore = 0 ); \
20API_ATTR const IOFilters& getFilters();
21
22#define MR_FORMAT_REGISTRY_DECL( ProcName ) MR_FORMAT_REGISTRY_EXTERNAL_DECL( MRMESH_API, ProcName )
23
24#define MR_FORMAT_REGISTRY_IMPL( ProcName ) \
25ProcName MR_CONCAT( get, ProcName )( const IOFilter& filter ) \
26{ \
27 return FormatRegistry<ProcName>::getProcessor( filter ); \
28} \
29ProcName MR_CONCAT( get, ProcName )( const std::string& extension ) \
30{ \
31 return FormatRegistry<ProcName>::getProcessor( extension ); \
32} \
33void MR_CONCAT( set, ProcName )( const IOFilter& filter, ProcName processor, int8_t priorityScore ) \
34{ \
35 FormatRegistry<ProcName>::setProcessor( filter, processor, priorityScore ); \
36} \
37const IOFilters& getFilters() \
38{ \
39 return FormatRegistry<ProcName>::getFilters(); \
40}
41
42namespace MR
43{
44
45MRMESH_API extern const IOFilters AllFilter;
46
50template <typename Processor>
52{
53public:
54 // get all registered filters
55 static const IOFilters& getFilters()
56 {
57 return get_().filters_;
58 }
59
60 // get a registered loader for the filter
61 static Processor getProcessor( const IOFilter& filter )
62 {
63 const auto& processors = get_().processors_;
64 auto it = processors.find( filter );
65 if ( it != processors.end() )
66 return it->second;
67 else
68 return {};
69 }
70
71 // get a registered loader for the extension
72 static Processor getProcessor( const std::string& extension )
73 {
74 const auto& processors = get_().processors_;
75 // TODO: extension cache
76 auto it = std::find_if( processors.begin(), processors.end(), [&extension] ( auto&& item )
77 {
78 const auto& [filter, _] = item;
79 return filter.isSupportedExtension( extension );
80 } );
81 if ( it != processors.end() )
82 return it->second;
83 else
84 return {};
85 }
86
87 // register or update a loader for the filter
88 static void setProcessor( const IOFilter& filter, Processor processor, int8_t priorityScore = 0 )
89 {
90 auto& processors = get_().processors_;
91 auto it = processors.find( filter );
92 if ( it != processors.end() )
93 {
94 it->second = processor;
95 }
96 else
97 {
98 processors.emplace( filter, processor );
99
100 auto& filters = get_().filterPriorityQueue_;
101 filters.emplace( priorityScore, filter );
102 get_().updateFilterList_();
103 }
104 }
105
106private:
107 FormatRegistry() = default;
108 ~FormatRegistry() = default;
109
110 static FormatRegistry<Processor>& get_()
111 {
112 static FormatRegistry<Processor> instance;
113 return instance;
114 }
115
116 void updateFilterList_()
117 {
118 filters_.clear();
119 filters_.reserve( filterPriorityQueue_.size() );
120 for ( const auto& [_, filter] : filterPriorityQueue_ )
121 filters_.emplace_back( filter );
122 }
123
124 std::map<IOFilter, Processor> processors_;
125 std::multimap<int8_t, IOFilter> filterPriorityQueue_;
126 std::vector<IOFilter> filters_;
127};
128
129namespace MeshLoad
130{
131
135
136using MeshFileLoader = Expected<MR::Mesh>( * )( const std::filesystem::path&, const MeshLoadSettings& );
137using MeshStreamLoader = Expected<MR::Mesh>( * )( std::istream&, const MeshLoadSettings& );
138
140{
143};
144
145MR_FORMAT_REGISTRY_DECL( MeshLoader )
146
147
153#define MR_ADD_MESH_LOADER( filter, loader ) \
154MR_ON_INIT { using namespace MR::MeshLoad; setMeshLoader( filter, { static_cast<MeshFileLoader>( loader ), static_cast<MeshStreamLoader>( loader ) } ); };
155
156#define MR_ADD_MESH_LOADER_WITH_PRIORITY( filter, loader, priority ) \
157MR_ON_INIT { using namespace MR::MeshLoad; setMeshLoader( filter, { static_cast<MeshFileLoader>( loader ), static_cast<MeshStreamLoader>( loader ) }, priority ); };
158
160
161} // namespace MeshLoad
162
163namespace MeshSave
164{
165
166using MeshFileSaver = Expected<void>( * )( const Mesh&, const std::filesystem::path&, const SaveSettings& );
167using MeshStreamSaver = Expected<void>( * )( const Mesh&, std::ostream&, const SaveSettings& );
168
170{
173};
174
175MR_FORMAT_REGISTRY_DECL( MeshSaver )
176
177#define MR_ADD_MESH_SAVER( filter, saver ) \
178MR_ON_INIT { using namespace MR::MeshSave; setMeshSaver( filter, { static_cast<MeshFileSaver>( saver ), static_cast<MeshStreamSaver>( saver ) } ); };
179
180#define MR_ADD_MESH_SAVER_WITH_PRIORITY( filter, saver, priority ) \
181MR_ON_INIT { using namespace MR::MeshSave; setMeshSaver( filter, { static_cast<MeshFileSaver>( saver ), static_cast<MeshStreamSaver>( saver ) }, priority ); };
182
183} // namespace MeshSave
184
185namespace LinesLoad
186{
187
188using LinesFileLoader = Expected<Polyline3>( * )( const std::filesystem::path&, ProgressCallback );
190
196
197MR_FORMAT_REGISTRY_DECL( LinesLoader )
198
199#define MR_ADD_LINES_LOADER( filter, loader ) \
200MR_ON_INIT { using namespace MR::LinesLoad; setLinesLoader( filter, { static_cast<LinesFileLoader>( loader ), static_cast<LinesStreamLoader>( loader ) } ); };
201
202#define MR_ADD_LINES_LOADER_WITH_PRIORITY( filter, loader, priority ) \
203MR_ON_INIT { using namespace MR::LinesLoad; setLinesLoader( filter, { static_cast<LinesFileLoader>( loader ), static_cast<LinesStreamLoader>( loader ) }, priority ); };
204
205} // namespace LinesLoad
206
207namespace LinesSave
208{
209
210using LinesFileSaver = Expected<void>( * )( const Polyline3&, const std::filesystem::path&, const SaveSettings& );
211using LinesStreamSaver = Expected<void>( * )( const Polyline3&, std::ostream&, const SaveSettings& );
212
214{
217};
218
219MR_FORMAT_REGISTRY_DECL( LinesSaver )
220
221#define MR_ADD_LINES_SAVER( filter, saver ) \
222MR_ON_INIT { using namespace MR::LinesSave; setLinesSaver( filter, { static_cast<LinesFileSaver>( saver ), static_cast<LinesStreamSaver>( saver ) } ); };
223
224#define MR_ADD_LINES_SAVER_WITH_PRIORITY( filter, saver, priority ) \
225MR_ON_INIT { using namespace MR::LinesSave; setLinesSaver( filter, { static_cast<LinesFileSaver>( saver ), static_cast<LinesStreamSaver>( saver ) }, priority ); };
226
227} // namespace LinesSave
228
229namespace PointsLoad
230{
231
232using PointsFileLoader = Expected<PointCloud>( * )( const std::filesystem::path&, const PointsLoadSettings& );
233using PointsStreamLoader = Expected<PointCloud>( * )( std::istream&, const PointsLoadSettings& );
234
240
241MR_FORMAT_REGISTRY_DECL( PointsLoader )
242
243#define MR_ADD_POINTS_LOADER( filter, loader ) \
244MR_ON_INIT { using namespace MR::PointsLoad; setPointsLoader( filter, { static_cast<PointsFileLoader>( loader ), static_cast<PointsStreamLoader>( loader ) } ); };
245
246} // namespace PointsLoad
247
248namespace PointsSave
249{
250
251using PointsFileSaver = Expected<void>( * )( const PointCloud&, const std::filesystem::path&, const SaveSettings& );
252using PointsStreamSaver = Expected<void>( * )( const PointCloud&, std::ostream&, const SaveSettings& );
253
259
260MR_FORMAT_REGISTRY_DECL( PointsSaver )
261
262#define MR_ADD_POINTS_SAVER( filter, saver ) \
263MR_ON_INIT { using namespace MR::PointsSave; setPointsSaver( filter, { static_cast<PointsFileSaver>( saver ), static_cast<PointsStreamSaver>( saver ) } ); };
264
265} // namespace PointsSave
266
267namespace ImageLoad
268{
269
270using ImageLoader = Expected<Image>( * )( const std::filesystem::path& );
271
273
274#define MR_ADD_IMAGE_LOADER( filter, loader ) \
275MR_ON_INIT { using namespace MR::ImageLoad; setImageLoader( filter, loader ); };
276
277#define MR_ADD_IMAGE_LOADER_WITH_PRIORITY( filter, loader, priority ) \
278MR_ON_INIT { using namespace MR::ImageLoad; setImageLoader( filter, loader, priority ); };
279
280} // namespace ImageLoad
281
282namespace ImageSave
283{
284
285using ImageSaver = Expected<void>( * )( const Image&, const std::filesystem::path& );
286
288
289#define MR_ADD_IMAGE_SAVER( filter, saver ) \
290MR_ON_INIT { using namespace MR::ImageSave; setImageSaver( filter, saver ); };
291
292#define MR_ADD_IMAGE_SAVER_WITH_PRIORITY( filter, saver, priority ) \
293MR_ON_INIT { using namespace MR::ImageSave; setImageSaver( filter, saver, priority ); };
294
295} // namespace ImageSave
296
297namespace ObjectLoad
298{
299
300using ObjectLoader = Expected<LoadedObjects>( * )( const std::filesystem::path&, const ProgressCallback& );
301
303
304#define MR_ADD_OBJECT_LOADER( filter, loader ) \
305MR_ON_INIT { using namespace MR::ObjectLoad; setObjectLoader( filter, loader ); };
306
307} // namespace ObjectLoad
308
309namespace ObjectSave
310{
311
312using ObjectSaver = Expected<void>( * )( const Object&, const std::filesystem::path&, const ProgressCallback& );
313
315
316#define MR_ADD_OBJECT_SAVER( filter, saver ) \
317MR_ON_INIT { using namespace MR::ObjectSave; setObjectSaver( filter, saver ); };
318
319} // namespace ObjectSave
320
321namespace AsyncObjectLoad
322{
323
324using PostLoadCallback = std::function<void ( Expected<LoadedObjects> )>;
325using ObjectLoader = void( * )( const std::filesystem::path&, PostLoadCallback, const ProgressCallback& );
326
328
329} // namespace AsyncObjectLoad
330
331namespace SceneLoad
332{
333
334using SceneLoader = Expected<LoadedObject>( * )( const std::filesystem::path&, const ProgressCallback& );
335
337
338#define MR_ADD_SCENE_LOADER( filter, loader ) \
339MR_ON_INIT { using namespace MR::SceneLoad; setSceneLoader( filter, loader ); };
340
341#define MR_ADD_SCENE_LOADER_WITH_PRIORITY( filter, loader, priority ) \
342MR_ON_INIT { using namespace MR::SceneLoad; setSceneLoader( filter, loader, priority ); };
343
344} // namespace SceneLoad
345
346namespace SceneSave
347{
348
349using SceneSaver = Expected<void>( * )( const Object&, const std::filesystem::path&, ProgressCallback );
350
352
353#define MR_ADD_SCENE_SAVER( filter, saver ) \
354MR_ON_INIT { using namespace MR::SceneSave; setSceneSaver( filter, saver ); };
355
356#define MR_ADD_SCENE_SAVER_WITH_PRIORITY( filter, saver, priority ) \
357MR_ON_INIT { using namespace MR::SceneSave; setSceneSaver( filter, saver, priority ); };
358
359} // namespace SceneSave
360
361namespace DistanceMapLoad
362{
363
364using DistanceMapLoader = Expected<DistanceMap>( * )( const std::filesystem::path& path, const DistanceMapLoadSettings& settings );
365
367
368#define MR_ADD_DISTANCE_MAP_LOADER( filter, loader ) \
369MR_ON_INIT { using namespace MR::DistanceMapLoad; setDistanceMapLoader( filter, loader ); };
370
371#define MR_ADD_DISTANCE_MAP_LOADER_WITH_PRIORITY( filter, loader, priority ) \
372MR_ON_INIT { using namespace MR::DistanceMapLoad; setDistanceMapLoader( filter, loader, priority ); };
373
374} // namespace DistanceMapLoad
375
376namespace DistanceMapSave
377{
378
379using DistanceMapSaver = Expected<void>( * )( const DistanceMap& distanceMap, const std::filesystem::path& path, const DistanceMapSaveSettings& settings );
380
382
383#define MR_ADD_DISTANCE_MAP_SAVER( filter, saver ) \
384MR_ON_INIT { using namespace MR::DistanceMapSave; setDistanceMapSaver( filter, saver ); };
385
386#define MR_ADD_DISTANCE_MAP_SAVER_WITH_PRIORITY( filter, saver, priority ) \
387MR_ON_INIT { using namespace MR::DistanceMapSave; setDistanceMapSaver( filter, saver, priority ); };
388
389} // namespace DistanceMapSave
390
391} // namespace MR
392#endif
#define MR_FORMAT_REGISTRY_DECL(ProcName)
Definition MRIOFormatsRegistry.h:22
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:79
Definition MRDistanceMap.h:24
Definition MRIOFormatsRegistry.h:52
static const IOFilters & getFilters()
Definition MRIOFormatsRegistry.h:55
static void setProcessor(const IOFilter &filter, Processor processor, int8_t priorityScore=0)
Definition MRIOFormatsRegistry.h:88
static Processor getProcessor(const IOFilter &filter)
Definition MRIOFormatsRegistry.h:61
static Processor getProcessor(const std::string &extension)
Definition MRIOFormatsRegistry.h:72
named object in the data model
Definition MRObject.h:60
std::function< bool(float)> ProgressCallback
Definition MRMesh/MRMeshFwd.h:641
std::vector< IOFilter > IOFilters
Definition MRIOFilters.h:32
Expected< MR::Mesh >(*)(std::istream &, const MeshLoadSettings &) MeshStreamLoader
Definition MRIOFormatsRegistry.h:137
Expected< MR::Mesh >(*)(const std::filesystem::path &, const MeshLoadSettings &) MeshFileLoader
Definition MRIOFormatsRegistry.h:136
std::function< void(Expected< LoadedObjects >)> PostLoadCallback
Definition MRIOFormatsRegistry.h:324
void(*)(const std::filesystem::path &, PostLoadCallback, const ProgressCallback &) ObjectLoader
Definition MRIOFormatsRegistry.h:325
Expected< DistanceMap >(*)(const std::filesystem::path &path, const DistanceMapLoadSettings &settings) DistanceMapLoader
Definition MRIOFormatsRegistry.h:364
Expected< void >(*)(const DistanceMap &distanceMap, const std::filesystem::path &path, const DistanceMapSaveSettings &settings) DistanceMapSaver
Definition MRIOFormatsRegistry.h:379
Expected< Image >(*)(const std::filesystem::path &) ImageLoader
Definition MRIOFormatsRegistry.h:270
Expected< void >(*)(const Image &, const std::filesystem::path &) ImageSaver
Definition MRIOFormatsRegistry.h:285
Expected< Polyline3 >(*)(std::istream &, ProgressCallback) LinesStreamLoader
Definition MRIOFormatsRegistry.h:189
Expected< Polyline3 >(*)(const std::filesystem::path &, ProgressCallback) LinesFileLoader
Definition MRIOFormatsRegistry.h:188
Expected< void >(*)(const Polyline3 &, std::ostream &, const SaveSettings &) LinesStreamSaver
Definition MRIOFormatsRegistry.h:211
Expected< void >(*)(const Polyline3 &, const std::filesystem::path &, const SaveSettings &) LinesFileSaver
Definition MRIOFormatsRegistry.h:210
Expected< void >(*)(const Mesh &, const std::filesystem::path &, const SaveSettings &) MeshFileSaver
Definition MRIOFormatsRegistry.h:166
Expected< void >(*)(const Mesh &, std::ostream &, const SaveSettings &) MeshStreamSaver
Definition MRIOFormatsRegistry.h:167
Expected< LoadedObjects >(*)(const std::filesystem::path &, const ProgressCallback &) ObjectLoader
Definition MRIOFormatsRegistry.h:300
Expected< void >(*)(const Object &, const std::filesystem::path &, const ProgressCallback &) ObjectSaver
Definition MRIOFormatsRegistry.h:312
Expected< PointCloud >(*)(std::istream &, const PointsLoadSettings &) PointsStreamLoader
Definition MRIOFormatsRegistry.h:233
Expected< PointCloud >(*)(const std::filesystem::path &, const PointsLoadSettings &) PointsFileLoader
Definition MRIOFormatsRegistry.h:232
Expected< void >(*)(const PointCloud &, std::ostream &, const SaveSettings &) PointsStreamSaver
Definition MRIOFormatsRegistry.h:252
Expected< void >(*)(const PointCloud &, const std::filesystem::path &, const SaveSettings &) PointsFileSaver
Definition MRIOFormatsRegistry.h:251
Expected< LoadedObject >(*)(const std::filesystem::path &, const ProgressCallback &) SceneLoader
Definition MRIOFormatsRegistry.h:334
Expected< void >(*)(const Object &, const std::filesystem::path &, ProgressCallback) SceneSaver
Definition MRIOFormatsRegistry.h:349
tl::expected< T, E > Expected
Definition MRExpected.h:59
MRMESH_API const IOFilters AllFilter
settings for loading distance maps from external formats
Definition MRDistanceMapParams.h:189
determines how to save distance maps
Definition MRDistanceMapParams.h:198
Definition MRIOFilters.h:17
Definition MRImage.h:15
Definition MRIOFormatsRegistry.h:192
LinesFileLoader fileLoad
Definition MRIOFormatsRegistry.h:193
LinesStreamLoader streamLoad
Definition MRIOFormatsRegistry.h:194
Definition MRIOFormatsRegistry.h:214
LinesStreamSaver streamSave
Definition MRIOFormatsRegistry.h:216
LinesFileSaver fileSave
Definition MRIOFormatsRegistry.h:215
setting for mesh loading from external format, and locations of optional output data
Definition MRMeshLoadSettings.h:10
Definition MRIOFormatsRegistry.h:140
MeshFileLoader fileLoad
Definition MRIOFormatsRegistry.h:141
MeshStreamLoader streamLoad
Definition MRIOFormatsRegistry.h:142
Definition MRIOFormatsRegistry.h:170
MeshStreamSaver streamSave
Definition MRIOFormatsRegistry.h:172
MeshFileSaver fileSave
Definition MRIOFormatsRegistry.h:171
Definition MRMesh/MRMesh.h:22
Definition MRMesh/MRPointCloud.h:16
Definition MRMesh/MRPointsLoadSettings.h:10
Definition MRIOFormatsRegistry.h:236
PointsFileLoader fileLoad
Definition MRIOFormatsRegistry.h:237
PointsStreamLoader streamLoad
Definition MRIOFormatsRegistry.h:238
Definition MRIOFormatsRegistry.h:255
PointsFileSaver fileSave
Definition MRIOFormatsRegistry.h:256
PointsStreamSaver streamSave
Definition MRIOFormatsRegistry.h:257
determines how to save points/lines/mesh
Definition MRMesh/MRSaveSettings.h:14