MeshLib C++ Docs
Loading...
Searching...
No Matches
MRObjectVoxels.h
Go to the documentation of this file.
1#pragma once
2#include "MRVoxelsFwd.h"
3
8#include "MRVoxelsVolume.h"
9#include "MRMarchingCubes.h"
10
11namespace MR
12{
13
17{
18public:
20 ObjectVoxels& operator = ( ObjectVoxels&& ) noexcept = default;
21 ObjectVoxels( ObjectVoxels&& ) noexcept = default;
22 virtual ~ObjectVoxels() = default;
23
24 constexpr static const char* TypeName() noexcept { return "ObjectVoxels"; }
25 virtual const char* typeName() const override { return TypeName(); }
26
27 MRVOXELS_API virtual void applyScale( float scaleFactor ) override;
28
30 const std::shared_ptr<Mesh>& surface() const { return data_.mesh; }
31
33 const VdbVolume& vdbVolume() const { return vdbVolume_; };
34 VdbVolume& varVdbVolume() { return vdbVolume_; }
35
37 const FloatGrid& grid() const { return vdbVolume_.data; }
38
39 [[nodiscard]] virtual bool hasModel() const override { return bool( vdbVolume_.data ); }
40
42 const Vector3i& dimensions() const
43 { return vdbVolume_.dims; }
45 float getIsoValue() const
46 { return isoValue_; }
48 const Histogram& histogram() const
49 { return histogram_; }
50
51 const Vector3f& voxelSize() const
52 { return vdbVolume_.voxelSize; }
53
54 MRVOXELS_API virtual std::vector<std::string> getInfoLines() const override;
55
56 std::string getClassName() const override { return "Voxels"; }
57 std::string getClassNameInPlural() const override { return "Voxels"; }
58
63 MRVOXELS_API void construct( const SimpleVolume& simpleVolume, const std::optional<Vector2f> & minmax = {}, ProgressCallback cb = {}, bool normalPlusGrad = false );
64
68 MRVOXELS_API void construct( const SimpleVolumeMinMax& simpleVolumeMinMax, ProgressCallback cb = {}, bool normalPlusGrad = false );
69
72 MRVOXELS_API void construct( const FloatGrid& grid, const Vector3f& voxelSize, const std::optional<Vector2f> & minmax = {} );
73
75 MRVOXELS_API void construct( const VdbVolume& vdbVolume );
76
80
83 MRVOXELS_API virtual Expected<bool> setIsoValue( float iso, ProgressCallback cb = {}, bool updateSurface = true );
84
87 MRVOXELS_API std::shared_ptr<Mesh> updateIsoSurface( std::shared_ptr<Mesh> mesh );
90 MRVOXELS_API VdbVolume updateVdbVolume( VdbVolume vdbVolume );
94
98 MRVOXELS_API Expected<std::shared_ptr<Mesh>> recalculateIsoSurface( const VdbVolume& volume, float iso, ProgressCallback cb = {} ) const;
100 MRVOXELS_API Histogram recalculateHistogram( std::optional<Vector2f> minmax, ProgressCallback cb = {} ) const;
102 bool getDualMarchingCubes() const { return dualMarchingCubes_; }
105 MRVOXELS_API virtual void setDualMarchingCubes( bool on, bool updateSurface = true, ProgressCallback cb = {} );
107 virtual void setVoxelPointPositioner( VoxelPointPositioner positioner ) { positioner_ = positioner; }
108
109
113 MRVOXELS_API virtual void setActiveBounds( const Box3i& activeBox, ProgressCallback cb = {}, bool updateSurface = true );
116 MRVOXELS_API const Box3i& getActiveBounds() const;
119
120 const VoxelBitSet& getSelectedVoxels() const { return selectedVoxels_; }
121 void selectVoxels( const VoxelBitSet& selectedVoxels ) { selectedVoxels_ = selectedVoxels; }
122
124 const VoxelBitSet& getVolumeRenderActiveVoxels() const { return volumeRenderActiveVoxels_; }
126 MRVOXELS_API void setVolumeRenderActiveVoxels( const VoxelBitSet& activeVoxels );
127
131 MRVOXELS_API VoxelId getVoxelIdByCoordinate( const Vector3i& coord ) const;
132 MRVOXELS_API VoxelId getVoxelIdByPoint( const Vector3f& point ) const;
133 MRVOXELS_API Vector3i getCoordinateByVoxelId( VoxelId id ) const;
134
136 const VolumeIndexer& getVolumeIndexer() const { return indexer_; }
137
138 // prepare data for volume rendering
139 // returns false if canceled or voxel data is empty
141
142 bool isVolumeRenderingEnabled() const { return volumeRendering_; }
143 // this function should only be called from GUI thread because it changes rendering object,
144 // it can take some time to prepare data, so you can prepare data with progress callback
145 // by calling `prepareDataForVolumeRendering(cb)` function before calling this one
147 // move volume rendering data to caller: basically used in RenderVolumeObject
148 [[nodiscard]] std::unique_ptr<SimpleVolume> getVolumeRenderingData() const { return std::move( volumeRenderingData_ ); }
149
150 // struct to control volume rendering texture
152 {
153 // volume texture smoothing
154 FilterType volumeFilterType{ FilterType::Linear };
155 // shading model
156 enum class ShadingType
157 {
158 None,
159 ValueGradient,
160 AlphaGradient
161 } shadingType{ ShadingType::None };
162 // coloring type
163 enum class LutType
164 {
165 GrayShades,
166 Rainbow,
167 OneColor
168 } lutType{ LutType::Rainbow };
169 // color that is used for OneColor mode
170 Color oneColor{ Color::white() };
171 // minimum colored value (voxels with lower values are transparent)
172 float min{ 0.0f };
173 // maximum colored value (voxels with higher values are transparent)
174 float max{ 0.0f };
175 // type of alpha function on texture
176 enum class AlphaType
177 {
178 Constant,
179 LinearIncreasing,
180 LinearDecreasing
181 } alphaType{ AlphaType::Constant };
182 uint8_t alphaLimit{ 10 };
183 bool operator==( const VolumeRenderingParams& )const = default;
184 };
185 const VolumeRenderingParams& getVolumeRenderingParams() const { return volumeRenderingParams_; }
187
188 MRVOXELS_API virtual bool hasVisualRepresentation() const override;
189
193 int getMaxSurfaceVertices() const { return maxSurfaceVertices_; }
194
195 MRVOXELS_API virtual std::shared_ptr<Object> clone() const override;
196 MRVOXELS_API virtual std::shared_ptr<Object> shallowClone() const override;
197
198 MRVOXELS_API virtual void setDirtyFlags( uint32_t mask, bool invalidateCaches = true ) override;
199
201 [[nodiscard]] MRVOXELS_API size_t activeVoxels() const;
202
205
207 [[nodiscard]] MRVOXELS_API virtual size_t heapBytes() const override;
208
210 [[nodiscard]] const char * serializeFormat() const { return serializeFormat_; }
211
214 MRVOXELS_API void setSerializeFormat( const char * newFormat );
215
219
220private:
221 VolumeRenderingParams volumeRenderingParams_;
222 mutable UniquePtr<SimpleVolume> volumeRenderingData_;
223
224 int maxSurfaceVertices_{ 5'000'000 };
225 VdbVolume vdbVolume_;
226 float isoValue_{0.0f};
227 bool dualMarchingCubes_{true};
228 VoxelPointPositioner positioner_ = {};
229 Histogram histogram_;
230 mutable std::optional<Box3i> activeBounds_;
231 mutable std::optional<size_t> activeVoxels_;
232
233 const char * serializeFormat_ = nullptr; //means defaultSerializeVoxelsFormat()
234
236 VolumeIndexer indexer_ = VolumeIndexer( vdbVolume_.dims );
237 Vector3f reverseVoxelSize_;
238
239 void updateHistogram_( float min, float max, ProgressCallback cb = {} );
240
241
243 void setDefaultColors_();
244
246 void setDefaultSceneProperties_();
247
248protected:
249 VoxelBitSet selectedVoxels_;
251
252 ObjectVoxels( const ObjectVoxels& other ) = default;
253 bool volumeRendering_{ false };
254
256 MRVOXELS_API virtual void swapBase_( Object& other ) override;
259 MRVOXELS_API virtual void swapSignals_( Object& other ) override;
260
261 MRVOXELS_API virtual void serializeFields_( Json::Value& root ) const override;
262
263 MRVOXELS_API void deserializeFields_( const Json::Value& root ) override;
264
265 MRVOXELS_API Expected<void> deserializeModel_( const std::filesystem::path& path, ProgressCallback progressCb = {} ) override;
266
267 MRVOXELS_API virtual Expected<std::future<Expected<void>>> serializeModel_( const std::filesystem::path& path ) const override;
268};
269
272[[nodiscard]] MRVOXELS_API const std::string & defaultSerializeVoxelsFormat();
273
276MRVOXELS_API void setDefaultSerializeVoxelsFormat( std::string newFormat );
277
278} //namespace MR
#define MRVOXELS_API
Definition MRVoxels/MRVoxelsFwd.h:13
#define MRVOXELS_CLASS
Definition MRVoxels/MRVoxelsFwd.h:14
Definition MRHistogram.h:10
Definition MRObjectMeshHolder.h:31
Definition MRObjectVoxels.h:17
virtual MRVOXELS_API Expected< std::future< Expected< void > > > serializeModel_(const std::filesystem::path &path) const override
virtual MRVOXELS_API void setDualMarchingCubes(bool on, bool updateSurface=true, ProgressCallback cb={})
MRVOXELS_API Expected< std::shared_ptr< Mesh > > recalculateIsoSurface(float iso, ProgressCallback cb={}) const
Calculates and return new mesh or error message.
const Vector3i & dimensions() const
Returns dimensions of voxel objects.
Definition MRObjectVoxels.h:42
VdbVolume & varVdbVolume()
Definition MRObjectVoxels.h:34
std::unique_ptr< SimpleVolume > getVolumeRenderingData() const
Definition MRObjectVoxels.h:148
const Vector3f & voxelSize() const
Definition MRObjectVoxels.h:51
MRVOXELS_API std::shared_ptr< Mesh > updateIsoSurface(std::shared_ptr< Mesh > mesh)
const VoxelBitSet & getVolumeRenderActiveVoxels() const
get active (visible) voxels
Definition MRObjectVoxels.h:124
MRVOXELS_API Vector3i getCoordinateByVoxelId(VoxelId id) const
const char * serializeFormat() const
returns overriden file extension used to serialize voxels inside this object, nullptr means defaultSe...
Definition MRObjectVoxels.h:210
bool isVolumeRenderingEnabled() const
Definition MRObjectVoxels.h:142
const VolumeRenderingParams & getVolumeRenderingParams() const
Definition MRObjectVoxels.h:185
virtual MRVOXELS_API std::shared_ptr< Object > clone() const override
const VoxelBitSet & getSelectedVoxels() const
Definition MRObjectVoxels.h:120
bool getDualMarchingCubes() const
returns true if the iso-surface is built using Dual Marching Cubes algorithm or false if using Standa...
Definition MRObjectVoxels.h:102
virtual MRVOXELS_API void invalidateActiveBoundsCaches()
Call this function in main thread post processing if you call setActiveBounds from progress bar threa...
float getIsoValue() const
Returns current iso value.
Definition MRObjectVoxels.h:45
MRVOXELS_API void deserializeFields_(const Json::Value &root) override
const std::shared_ptr< Mesh > & surface() const
Returns iso surface, empty if iso value is not set.
Definition MRObjectVoxels.h:30
MRVOXELS_API Histogram updateHistogram(Histogram histogram)
IsoSurfaceChangedSignal isoSurfaceChangedSignal
Definition MRObjectVoxels.h:218
virtual MRVOXELS_API void setDirtyFlags(uint32_t mask, bool invalidateCaches=true) override
std::string getClassNameInPlural() const override
return human readable name of subclass in plural form
Definition MRObjectVoxels.h:57
const VdbVolume & vdbVolume() const
Return VdbVolume.
Definition MRObjectVoxels.h:33
void selectVoxels(const VoxelBitSet &selectedVoxels)
Definition MRObjectVoxels.h:121
MRVOXELS_API Expected< std::shared_ptr< Mesh > > recalculateIsoSurface(const VdbVolume &volume, float iso, ProgressCallback cb={}) const
Same as above, but takes external volume.
const FloatGrid & grid() const
Returns Float grid which contains voxels data, see more on openvdb::FloatGrid.
Definition MRObjectVoxels.h:37
VoxelBitSet volumeRenderActiveVoxels_
Definition MRObjectVoxels.h:250
MRVOXELS_API void construct(const FloatGrid &grid, const Vector3f &voxelSize, const std::optional< Vector2f > &minmax={})
MRVOXELS_API void setVolumeRenderingParams(const VolumeRenderingParams &params)
MRVOXELS_API VoxelId getVoxelIdByCoordinate(const Vector3i &coord) const
MRVOXELS_API void construct(const VdbVolume &vdbVolume)
Clears all internal data and then creates grid and calculates histogram (surface is not built,...
const VolumeIndexer & getVolumeIndexer() const
Returns indexer with more options.
Definition MRObjectVoxels.h:136
MRVOXELS_API void setVolumeRenderActiveVoxels(const VoxelBitSet &activeVoxels)
set active (visible) voxels (using only in Volume Rendering mode)
MRVOXELS_API void updateHistogramAndSurface(ProgressCallback cb={})
MRVOXELS_API ObjectVoxels()
MRVOXELS_API void enableVolumeRendering(bool on)
virtual MRVOXELS_API void serializeFields_(Json::Value &root) const override
MRVOXELS_API void construct(const SimpleVolume &simpleVolume, const std::optional< Vector2f > &minmax={}, ProgressCallback cb={}, bool normalPlusGrad=false)
MRVOXELS_API VdbVolume updateVdbVolume(VdbVolume vdbVolume)
virtual MRVOXELS_API Expected< bool > setIsoValue(float iso, ProgressCallback cb={}, bool updateSurface=true)
int getMaxSurfaceVertices() const
gets top limit on the number of vertices in the iso-surface
Definition MRObjectVoxels.h:193
MRVOXELS_API bool prepareDataForVolumeRendering(ProgressCallback cb={}) const
virtual MRVOXELS_API void setActiveBounds(const Box3i &activeBox, ProgressCallback cb={}, bool updateSurface=true)
MRVOXELS_API const Box3i & getActiveBounds() const
ObjectVoxels(ProtectedStruct, const ObjectVoxels &obj)
Definition MRObjectVoxels.h:204
virtual MRVOXELS_API void swapBase_(Object &other) override
swaps this object with other
std::string getClassName() const override
return human readable name of subclass
Definition MRObjectVoxels.h:56
VoxelBitSet selectedVoxels_
Definition MRObjectVoxels.h:249
MRVOXELS_API void setMaxSurfaceVertices(int maxVerts)
sets top limit on the number of vertices in the iso-surface
virtual MRVOXELS_API void swapSignals_(Object &other) override
virtual const char * typeName() const override
Definition MRObjectVoxels.h:25
MRVOXELS_API size_t activeVoxels() const
returns cached information about the number of active voxels
virtual MRVOXELS_API size_t heapBytes() const override
returns the amount of memory this object occupies on heap
virtual MRVOXELS_API bool hasVisualRepresentation() const override
mesh object can be seen if the mesh has at least one edge
virtual MRVOXELS_API std::vector< std::string > getInfoLines() const override
return several info lines that can better describe the object in the UI
virtual void setVoxelPointPositioner(VoxelPointPositioner positioner)
set voxel point positioner for Marching Cubes (only for Standard Marching Cubes)
Definition MRObjectVoxels.h:107
MRVOXELS_API VoxelId getVoxelIdByPoint(const Vector3f &point) const
ObjectVoxels(const ObjectVoxels &other)=default
MRVOXELS_API Expected< void > deserializeModel_(const std::filesystem::path &path, ProgressCallback progressCb={}) override
Reads model from file.
const Histogram & histogram() const
Returns histogram.
Definition MRObjectVoxels.h:48
MRVOXELS_API Histogram recalculateHistogram(std::optional< Vector2f > minmax, ProgressCallback cb={}) const
Calculates and returns new histogram.
MRVOXELS_API void setSerializeFormat(const char *newFormat)
virtual MRVOXELS_API std::shared_ptr< Object > shallowClone() const override
virtual MRVOXELS_API void applyScale(float scaleFactor) override
scale object size (all point positions)
virtual bool hasModel() const override
Definition MRObjectVoxels.h:39
MRVOXELS_API void construct(const SimpleVolumeMinMax &simpleVolumeMinMax, ProgressCallback cb={}, bool normalPlusGrad=false)
named object in the data model
Definition MRObject.h:60
Definition MRVolumeIndexer.h:65
std::function< bool(float)> ProgressCallback
Definition MRMesh/MRMeshFwd.h:636
@ None
special value not to limit path in one slice
Definition MRVoxelPath.h:33
std::shared_ptr< OpenVdbFloatGrid > FloatGrid
Definition MRVoxels/MRVoxelsFwd.h:25
FilterType
Definition MRMesh/MRMeshFwd.h:639
tl::expected< T, E > Expected
Definition MRExpected.h:59
MRVOXELS_API const std::string & defaultSerializeVoxelsFormat()
MRVOXELS_API void setDefaultSerializeVoxelsFormat(std::string newFormat)
std::function< Vector3f(const Vector3f &, const Vector3f &, float, float, float)> VoxelPointPositioner
Definition MRMarchingCubes.h:16
Definition MRMesh/MRColor.h:9
Definition MRObjectVoxels.h:152
ShadingType
Definition MRObjectVoxels.h:157
bool operator==(const VolumeRenderingParams &) const =default
LutType
Definition MRObjectVoxels.h:164
AlphaType
Definition MRObjectVoxels.h:177
Definition MRObject.h:253
Definition MRUniquePtr.h:12