MeshLib Documentation
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 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 virtual std::string getClassName() const override { return "Voxels"; }
56
61 MRVOXELS_API void construct( const SimpleVolume& simpleVolume, const std::optional<Vector2f> & minmax = {}, ProgressCallback cb = {}, bool normalPlusGrad = false );
62
66 MRVOXELS_API void construct( const SimpleVolumeMinMax& simpleVolumeMinMax, ProgressCallback cb = {}, bool normalPlusGrad = false );
67
70 MRVOXELS_API void construct( const FloatGrid& grid, const Vector3f& voxelSize, const std::optional<Vector2f> & minmax = {} );
71
73 MRVOXELS_API void construct( const VdbVolume& vdbVolume );
74
78
81 MRVOXELS_API virtual Expected<bool> setIsoValue( float iso, ProgressCallback cb = {}, bool updateSurface = true );
82
85 MRVOXELS_API std::shared_ptr<Mesh> updateIsoSurface( std::shared_ptr<Mesh> mesh );
88 MRVOXELS_API VdbVolume updateVdbVolume( VdbVolume vdbVolume );
92
96 MRVOXELS_API Expected<std::shared_ptr<Mesh>> recalculateIsoSurface( const VdbVolume& volume, float iso, ProgressCallback cb = {} ) const;
98 MRVOXELS_API Histogram recalculateHistogram( std::optional<Vector2f> minmax, ProgressCallback cb = {} ) const;
100 bool getDualMarchingCubes() const { return dualMarchingCubes_; }
103 MRVOXELS_API virtual void setDualMarchingCubes( bool on, bool updateSurface = true, ProgressCallback cb = {} );
105 virtual void setVoxelPointPositioner( VoxelPointPositioner positioner ) { positioner_ = positioner; }
106
107
111 MRVOXELS_API virtual void setActiveBounds( const Box3i& activeBox, ProgressCallback cb = {}, bool updateSurface = true );
114 MRVOXELS_API const Box3i& getActiveBounds() const;
117
118 const VoxelBitSet& getSelectedVoxels() const { return selectedVoxels_; }
119 void selectVoxels( const VoxelBitSet& selectedVoxels ) { selectedVoxels_ = selectedVoxels; }
120
122 const VoxelBitSet& getVolumeRenderActiveVoxels() const { return volumeRenderActiveVoxels_; }
124 MRVOXELS_API void setVolumeRenderActiveVoxels( const VoxelBitSet& activeVoxels );
125
129 MRVOXELS_API VoxelId getVoxelIdByCoordinate( const Vector3i& coord ) const;
130 MRVOXELS_API VoxelId getVoxelIdByPoint( const Vector3f& point ) const;
131 MRVOXELS_API Vector3i getCoordinateByVoxelId( VoxelId id ) const;
132
134 const VolumeIndexer& getVolumeIndexer() const { return indexer_; }
135
136 // prepare data for volume rendering
137 // returns false if canceled or voxel data is empty
139
140 bool isVolumeRenderingEnabled() const { return volumeRendering_; }
141 // this function should only be called from GUI thread because it changes rendering object,
142 // it can take some time to prepare data, so you can prepare data with progress callback
143 // by calling `prepareDataForVolumeRendering(cb)` function before calling this one
145 // move volume rendering data to caller: basically used in RenderVolumeObject
146 [[nodiscard]] std::unique_ptr<SimpleVolume> getVolumeRenderingData() const { return std::move( volumeRenderingData_ ); }
147
148 // struct to control volume rendering texture
150 {
151 // volume texture smoothing
152 FilterType volumeFilterType{ FilterType::Linear };
153 // shading model
154 enum class ShadingType
155 {
156 None,
157 ValueGradient,
158 AlphaGradient
159 } shadingType{ ShadingType::None };
160 // coloring type
161 enum class LutType
162 {
163 GrayShades,
164 Rainbow,
165 OneColor
166 } lutType{ LutType::Rainbow };
167 // color that is used for OneColor mode
168 Color oneColor{ Color::white() };
169 // minimum colored value (voxels with lower values are transparent)
170 float min{ 0.0f };
171 // maximum colored value (voxels with higher values are transparent)
172 float max{ 0.0f };
173 // type of alpha function on texture
174 enum class AlphaType
175 {
176 Constant,
177 LinearIncreasing,
178 LinearDecreasing
179 } alphaType{ AlphaType::Constant };
180 uint8_t alphaLimit{ 10 };
181 bool operator==( const VolumeRenderingParams& )const = default;
182 };
183 const VolumeRenderingParams& getVolumeRenderingParams() const { return volumeRenderingParams_; }
185
186 MRVOXELS_API virtual bool hasVisualRepresentation() const override;
187
191 int getMaxSurfaceVertices() const { return maxSurfaceVertices_; }
192
193 MRVOXELS_API virtual std::shared_ptr<Object> clone() const override;
194 MRVOXELS_API virtual std::shared_ptr<Object> shallowClone() const override;
195
196 MRVOXELS_API virtual void setDirtyFlags( uint32_t mask, bool invalidateCaches = true ) override;
197
199 [[nodiscard]] MRVOXELS_API size_t activeVoxels() const;
200
203
205 [[nodiscard]] MRVOXELS_API virtual size_t heapBytes() const override;
206
208 [[nodiscard]] const char * serializeFormat() const { return serializeFormat_; }
209
211 MRVOXELS_API void setSerializeFormat( const char * newFormat );
212
216
217private:
218 VolumeRenderingParams volumeRenderingParams_;
219 mutable UniquePtr<SimpleVolume> volumeRenderingData_;
220
221 int maxSurfaceVertices_{ 5'000'000 };
222 VdbVolume vdbVolume_;
223 float isoValue_{0.0f};
224 bool dualMarchingCubes_{true};
225 VoxelPointPositioner positioner_ = {};
226 Histogram histogram_;
227 mutable std::optional<Box3i> activeBounds_;
228 mutable std::optional<size_t> activeVoxels_;
229
230 const char * serializeFormat_ = ".raw";
231
233 VolumeIndexer indexer_ = VolumeIndexer( vdbVolume_.dims );
234 Vector3f reverseVoxelSize_;
235
236 void updateHistogram_( float min, float max, ProgressCallback cb = {} );
237
238
240 void setDefaultColors_();
241
243 void setDefaultSceneProperties_();
244
245protected:
246 VoxelBitSet selectedVoxels_;
248
249 ObjectVoxels( const ObjectVoxels& other ) = default;
250 bool volumeRendering_{ false };
251
253 MRVOXELS_API virtual void swapBase_( Object& other ) override;
256 MRVOXELS_API virtual void swapSignals_( Object& other ) override;
257
258 MRVOXELS_API virtual void serializeFields_( Json::Value& root ) const override;
259
260 MRVOXELS_API void deserializeFields_( const Json::Value& root ) override;
261
262 MRVOXELS_API Expected<void> deserializeModel_( const std::filesystem::path& path, ProgressCallback progressCb = {} ) override;
263
264 MRVOXELS_API virtual Expected<std::future<Expected<void>>> serializeModel_( const std::filesystem::path& path ) const override;
265};
266
267
268}
#define MRVOXELS_API
Definition MRVoxelsFwd.h:13
#define MRVOXELS_CLASS
Definition MRVoxelsFwd.h:14
Definition MRHistogram.h:10
Definition MRObjectMeshHolder.h:30
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:146
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:122
MRVOXELS_API Vector3i getCoordinateByVoxelId(VoxelId id) const
const char * serializeFormat() const
returns file extension used to serialize the voxels
Definition MRObjectVoxels.h:208
bool isVolumeRenderingEnabled() const
Definition MRObjectVoxels.h:140
const VolumeRenderingParams & getVolumeRenderingParams() const
Definition MRObjectVoxels.h:183
virtual MRVOXELS_API std::shared_ptr< Object > clone() const override
const VoxelBitSet & getSelectedVoxels() const
Definition MRObjectVoxels.h:118
bool getDualMarchingCubes() const
returns true if the iso-surface is built using Dual Marching Cubes algorithm or false if using Standa...
Definition MRObjectVoxels.h:100
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:215
virtual MRVOXELS_API void setDirtyFlags(uint32_t mask, bool invalidateCaches=true) override
const VdbVolume & vdbVolume() const
Return VdbVolume.
Definition MRObjectVoxels.h:33
void selectVoxels(const VoxelBitSet &selectedVoxels)
Definition MRObjectVoxels.h:119
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:247
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,...
virtual std::string getClassName() const override
return human readable name of subclass
Definition MRObjectVoxels.h:55
const VolumeIndexer & getVolumeIndexer() const
Returns indexer with more options.
Definition MRObjectVoxels.h:134
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:191
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:202
virtual MRVOXELS_API void swapBase_(Object &other) override
swaps this object with other
VoxelBitSet selectedVoxels_
Definition MRObjectVoxels.h:246
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:105
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)
sets file extension used to serialize the voxels: must be not null and must start from '....
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:576
@ None
special value not to limit path in one slice
Definition MRVoxelPath.h:33
Definition MRCameraOrientationPlugin.h:8
std::shared_ptr< OpenVdbFloatGrid > FloatGrid
Definition MRVoxelsFwd.h:25
FilterType
Definition MRMesh/MRMeshFwd.h:579
tl::expected< T, E > Expected
Definition MRExpected.h:58
std::function< Vector3f(const Vector3f &, const Vector3f &, float, float, float)> VoxelPointPositioner
Definition MRMarchingCubes.h:16
Definition MRColor.h:9
Definition MRObjectVoxels.h:150
ShadingType
Definition MRObjectVoxels.h:155
bool operator==(const VolumeRenderingParams &) const =default
LutType
Definition MRObjectVoxels.h:162
AlphaType
Definition MRObjectVoxels.h:175
Definition MRObject.h:246
Definition MRUniquePtr.h:12