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