MeshLib C++ Docs
Loading...
Searching...
No Matches
MRObjectVoxels.h
1#pragma once
2#include "MRVoxelsFwd.h"
3
4#include "MRMesh/MRObjectMeshHolder.h"
5#include "MRMesh/MRProgressCallback.h"
6#include "MRMesh/MRHistogram.h"
7#include "MRMesh/MRVolumeIndexer.h"
8#include "MRVoxelsVolume.h"
9#include "MRMarchingCubes.h"
10
11namespace MR
12{
13
16class MRVOXELS_CLASS ObjectVoxels : public ObjectMeshHolder
17{
18public:
19 MRVOXELS_API ObjectVoxels();
20 ObjectVoxels& operator = ( ObjectVoxels&& ) noexcept = default;
21 ObjectVoxels( ObjectVoxels&& ) noexcept = default;
22 virtual ~ObjectVoxels() = default;
23
24 constexpr static const char* StaticTypeName() noexcept { return "ObjectVoxels"; }
25 virtual const char* typeName() const override { return StaticTypeName(); }
26
27 constexpr static const char* StaticClassName() noexcept { return "Voxel Volume"; }
28 virtual std::string className() const override { return StaticClassName(); }
29
30 constexpr static const char* StaticClassNameInPlural() noexcept { return "Voxel Volumes"; }
31 virtual std::string classNameInPlural() const override { return StaticClassNameInPlural(); }
32
33 MRVOXELS_API virtual void applyScale( float scaleFactor ) override;
34
36 const std::shared_ptr<Mesh>& surface() const { return data_.mesh; }
37
39 const VdbVolume& vdbVolume() const { return vdbVolume_; };
40 VdbVolume& varVdbVolume() { return vdbVolume_; }
41
43 const FloatGrid& grid() const { return vdbVolume_.data; }
44
45 [[nodiscard]] virtual bool hasModel() const override { return bool( vdbVolume_.data ); }
46
48 const Vector3i& dimensions() const
49 { return vdbVolume_.dims; }
51 float getIsoValue() const
52 { return isoValue_; }
54 const Histogram& histogram() const
55 { return histogram_; }
56
57 const Vector3f& voxelSize() const
58 { return vdbVolume_.voxelSize; }
59
60 MRVOXELS_API virtual std::vector<std::string> getInfoLines() const override;
61
66 MRVOXELS_API void construct( const SimpleVolume& simpleVolume, const std::optional<Vector2f> & minmax = {}, ProgressCallback cb = {}, bool normalPlusGrad = false );
67
71 MRVOXELS_API void construct( const SimpleVolumeMinMax& simpleVolumeMinMax, ProgressCallback cb = {}, bool normalPlusGrad = false );
72
75 MRVOXELS_API void construct( const FloatGrid& grid, const Vector3f& voxelSize, const std::optional<Vector2f> & minmax = {} );
76
78 MRVOXELS_API void construct( const VdbVolume& vdbVolume );
79
82 MRVOXELS_API void updateHistogramAndSurface( ProgressCallback cb = {} );
83
86 MRVOXELS_API virtual Expected<bool> setIsoValue( float iso, ProgressCallback cb = {}, bool updateSurface = true );
87
90 MRVOXELS_API std::shared_ptr<Mesh> updateIsoSurface( std::shared_ptr<Mesh> mesh );
93 MRVOXELS_API VdbVolume updateVdbVolume( VdbVolume vdbVolume );
96 MRVOXELS_API Histogram updateHistogram( Histogram histogram );
97
99 MRVOXELS_API Expected<std::shared_ptr<Mesh>> recalculateIsoSurface( float iso, ProgressCallback cb = {} ) const;
101 MRVOXELS_API Expected<std::shared_ptr<Mesh>> recalculateIsoSurface( const VdbVolume& volume, float iso, ProgressCallback cb = {} ) const;
103 MRVOXELS_API Histogram recalculateHistogram( std::optional<Vector2f> minmax, ProgressCallback cb = {} ) const;
105 bool getDualMarchingCubes() const { return dualMarchingCubes_; }
108 MRVOXELS_API virtual void setDualMarchingCubes( bool on, bool updateSurface = true, ProgressCallback cb = {} );
110 virtual void setVoxelPointPositioner( VoxelPointPositioner positioner ) { positioner_ = positioner; }
111
112
116 MRVOXELS_API virtual void setActiveBounds( const Box3i& activeBox, ProgressCallback cb = {}, bool updateSurface = true );
119 MRVOXELS_API const Box3i& getActiveBounds() const;
121 MRVOXELS_API virtual void invalidateActiveBoundsCaches();
122
123 const VoxelBitSet& getSelectedVoxels() const { return selectedVoxels_; }
124 void selectVoxels( const VoxelBitSet& selectedVoxels ) { selectedVoxels_ = selectedVoxels; }
125
127 const VoxelBitSet& getVolumeRenderActiveVoxels() const { return volumeRenderActiveVoxels_; }
129 MRVOXELS_API void setVolumeRenderActiveVoxels( const VoxelBitSet& activeVoxels );
130
134 MRVOXELS_API VoxelId getVoxelIdByCoordinate( const Vector3i& coord ) const;
135 MRVOXELS_API VoxelId getVoxelIdByPoint( const Vector3f& point ) const;
136 MRVOXELS_API Vector3i getCoordinateByVoxelId( VoxelId id ) const;
137
139 const VolumeIndexer& getVolumeIndexer() const { return indexer_; }
140
141 // prepare data for volume rendering
142 // returns false if canceled or voxel data is empty
143 MRVOXELS_API bool prepareDataForVolumeRendering( ProgressCallback cb = {} ) const;
144
145 bool isVolumeRenderingEnabled() const { return volumeRendering_; }
146 // this function should only be called from GUI thread because it changes rendering object,
147 // it can take some time to prepare data, so you can prepare data with progress callback
148 // by calling `prepareDataForVolumeRendering(cb)` function before calling this one
149 MRVOXELS_API void enableVolumeRendering( bool on );
150 // move volume rendering data to caller: basically used in RenderVolumeObject
151 [[nodiscard]] std::unique_ptr<SimpleVolume> getVolumeRenderingData() const { return std::move( volumeRenderingData_ ); }
152
153 // struct to control volume rendering texture
155 {
156 // volume texture smoothing
157 FilterType volumeFilterType{ FilterType::Linear };
158 // shading model
159 enum class ShadingType
160 {
161 None,
162 ValueGradient,
163 AlphaGradient
164 } shadingType{ ShadingType::None };
165 // coloring type
166 enum class LutType
167 {
168 GrayShades,
169 Rainbow,
170 OneColor
171 } lutType{ LutType::Rainbow };
172 // color that is used for OneColor mode
173 Color oneColor{ Color::white() };
174 // minimum colored value (voxels with lower values are transparent)
175 float min{ 0.0f };
176 // maximum colored value (voxels with higher values are transparent)
177 float max{ 0.0f };
178 // step to sample each ray with
179 // if <= 0 then default sampling is used
180 float samplingStep{ -1.0f };
181 // type of alpha function on texture
182 enum class AlphaType
183 {
184 Constant,
185 LinearIncreasing,
186 LinearDecreasing
187 } alphaType{ AlphaType::Constant };
188 uint8_t alphaLimit{ 10 };
189 bool operator==( const VolumeRenderingParams& )const = default;
190 };
191 const VolumeRenderingParams& getVolumeRenderingParams() const { return volumeRenderingParams_; }
192 MRVOXELS_API void setVolumeRenderingParams( const VolumeRenderingParams& params );
193
194 MRVOXELS_API virtual bool hasVisualRepresentation() const override;
195
197 MRVOXELS_API void setMaxSurfaceVertices( int maxVerts );
199 int getMaxSurfaceVertices() const { return maxSurfaceVertices_; }
200
201 MRVOXELS_API virtual std::shared_ptr<Object> clone() const override;
202 MRVOXELS_API virtual std::shared_ptr<Object> shallowClone() const override;
203
204 MRVOXELS_API virtual void setDirtyFlags( uint32_t mask, bool invalidateCaches = true ) override;
205
207 [[nodiscard]] MRVOXELS_API size_t activeVoxels() const;
208
210 ObjectVoxels( ProtectedStruct, const ObjectVoxels& obj ) : ObjectVoxels( obj ) {}
211
213 [[nodiscard]] MRVOXELS_API virtual size_t heapBytes() const override;
214
216 [[nodiscard]] const char * serializeFormat() const { return serializeFormat_; }
217
220 MRVOXELS_API void setSerializeFormat( const char * newFormat );
221
223 MRVOXELS_API void resetFrontColor() override;
224
226 using IsoSurfaceChangedSignal = Signal<void()>;
227 IsoSurfaceChangedSignal isoSurfaceChangedSignal;
228
230 using VoxelsChangedSignal = Signal<void()>;
231 VoxelsChangedSignal voxelsChangedSignal;
232
233private:
234 VolumeRenderingParams volumeRenderingParams_;
235 mutable UniquePtr<SimpleVolume> volumeRenderingData_;
236
237 int maxSurfaceVertices_{ 5'000'000 };
238 VdbVolume vdbVolume_;
239 float isoValue_{0.0f};
240 bool dualMarchingCubes_{true};
241 VoxelPointPositioner positioner_ = {};
242 Histogram histogram_;
243 mutable std::optional<Box3i> activeBounds_;
244 mutable std::optional<size_t> activeVoxels_;
245
246 const char * serializeFormat_ = nullptr; //means defaultSerializeVoxelsFormat()
247
249 VolumeIndexer indexer_ = VolumeIndexer( vdbVolume_.dims );
250 Vector3f reverseVoxelSize_;
251
252 void updateHistogram_( float min, float max, ProgressCallback cb = {} );
253
254
256 void setDefaultColors_();
257
259 void setDefaultSceneProperties_();
260
261protected:
262 VoxelBitSet selectedVoxels_;
263 VoxelBitSet volumeRenderActiveVoxels_;
264
265 ObjectVoxels( const ObjectVoxels& other ) = default;
266 bool volumeRendering_{ false };
267
269 MRVOXELS_API virtual void swapBase_( Object& other ) override;
272 MRVOXELS_API virtual void swapSignals_( Object& other ) override;
273
274 MRVOXELS_API virtual void serializeFields_( Json::Value& root ) const override;
275
276 MRVOXELS_API void deserializeFields_( const Json::Value& root ) override;
277
278 MRVOXELS_API Expected<void> deserializeModel_( const std::filesystem::path& path, ProgressCallback progressCb = {} ) override;
279
280 MRVOXELS_API virtual Expected<std::future<Expected<void>>> serializeModel_( const std::filesystem::path& path ) const override;
281};
282
285[[nodiscard]] MRVOXELS_API const std::string & defaultSerializeVoxelsFormat();
286
289MRVOXELS_API void setDefaultSerializeVoxelsFormat( std::string newFormat );
290
291} //namespace MR
static unsafe MR.Std.Const_String defaultSerializeVoxelsFormat()
static unsafe void setDefaultSerializeVoxelsFormat(string newFormat)
MRMESH_API bool operator==(const BitSet &a, const BitSet &b)
compare that two bit sets have the same set bits (they can be equal even if sizes are distinct but la...
size_t heapBytes(const BitSet &bs)
returns the amount of memory given BitSet occupies on heap
Definition MRMesh/MRBitSet.h:298
Definition MRMesh/MRColor.h:9
Definition MRObjectVoxels.h:155