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