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