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