MeshLib C++ Docs
Loading...
Searching...
No Matches
MRChangeVoxelsAction.h
Go to the documentation of this file.
1#pragma once
2#include "MRVoxelsFwd.h"
3
5#include "MRObjectVoxels.h"
6#include "MRMesh/MRMesh.h"
8#include "MRFloatGrid.h"
9#include <memory>
10
11namespace MR
12{
13
16
18class ChangeIsoAction : public HistoryAction
19{
20public:
21 using Obj = ObjectVoxels;
23 ChangeIsoAction( std::string name, const std::shared_ptr<ObjectVoxels>& obj ) :
24 objVoxels_{ obj },
25 name_{ std::move( name ) }
26 {
27 if ( obj )
28 {
29 storedIso_ = obj->getIsoValue();
30 }
31 }
32
33 virtual std::string name() const override
34 {
35 return name_;
36 }
37
38 virtual void action( HistoryAction::Type ) override
39 {
40 if ( !objVoxels_ )
41 return;
42
43 float newIso = objVoxels_->getIsoValue();
44 (void)objVoxels_->setIsoValue( storedIso_, {}, false ); //TODO: process potential error
45 storedIso_ = newIso;
46 }
47
48 static void setObjectDirty( const std::shared_ptr<Obj>& )
49 {
50 }
51
52 [[nodiscard]] virtual size_t heapBytes() const override
53 {
54 return name_.capacity();
55 }
56
57private:
58 std::shared_ptr<ObjectVoxels> objVoxels_;
59 float storedIso_{ 0.0f };
60
61 std::string name_;
62};
63
65class ChangeDualMarchingCubesAction : public HistoryAction
66{
67public:
68 using Obj = ObjectVoxels;
70 ChangeDualMarchingCubesAction( std::string name, const std::shared_ptr<ObjectVoxels>& obj ) :
71 objVoxels_{ obj },
72 name_{ std::move( name ) }
73 {
74 if ( obj )
75 {
76 storedDual_ = obj->getDualMarchingCubes();
77 }
78 }
79
81 ChangeDualMarchingCubesAction( std::string name, const std::shared_ptr<ObjectVoxels>& obj, bool storeDual ) :
82 objVoxels_{ obj },
83 storedDual_( storeDual ),
84 name_{ std::move( name ) }
85 {
86 }
87
88 virtual std::string name() const override
89 {
90 return name_;
91 }
92
93 virtual void action( HistoryAction::Type ) override
94 {
95 if ( !objVoxels_ )
96 return;
97
98 auto newDual = objVoxels_->getDualMarchingCubes();
99 objVoxels_->setDualMarchingCubes( storedDual_, false );
100 storedDual_ = newDual;
101 }
102
103 static void setObjectDirty( const std::shared_ptr<Obj>& )
104 {
105 }
106
107 [[nodiscard]] virtual size_t heapBytes() const override
108 {
109 return name_.capacity();
110 }
111
112private:
113 std::shared_ptr<ObjectVoxels> objVoxels_;
114 bool storedDual_{ true };
115
116 std::string name_;
117};
118
119// Undo action for ObjectVoxels active bounds change
120class ChangeActiveBoxAction : public HistoryAction
121{
122public:
123 using Obj = ObjectVoxels;
125 ChangeActiveBoxAction( std::string name, const std::shared_ptr<ObjectVoxels>& obj ) :
126 objVoxels_{ obj },
127 name_{ std::move( name ) }
128 {
129 if ( obj )
130 {
131 activeBox_ = obj->getActiveBounds();
132 }
133 }
134
135 virtual std::string name() const override
136 {
137 return name_;
138 }
139
140 virtual void action( HistoryAction::Type ) override
141 {
142 if ( !objVoxels_ )
143 return;
144
145 auto box = objVoxels_->getActiveBounds();
146 objVoxels_->setActiveBounds( activeBox_, {}, false );
147 activeBox_ = box;
148 }
149
150 static void setObjectDirty( const std::shared_ptr<Obj>& )
151 {
152 }
153
154 [[nodiscard]] virtual size_t heapBytes() const override
155 {
156 return name_.capacity();
157 }
158
159private:
160 std::shared_ptr<ObjectVoxels> objVoxels_;
161 Box3i activeBox_;
162
163 std::string name_;
164};
165
166// Undo action for ObjectVoxels surface change (need for faster undo redo)
167class ChangeSurfaceAction : public HistoryAction
168{
169public:
170 using Obj = ObjectVoxels;
172 ChangeSurfaceAction( std::string name, const std::shared_ptr<ObjectVoxels>& obj ) :
173 objVoxels_{ obj },
174 name_{ std::move( name ) }
175 {
176 if ( obj )
177 {
178 if ( auto m = obj->mesh() )
179 cloneSurface_ = std::make_shared<Mesh>( *m );
180 }
181 }
182
183 virtual std::string name() const override
184 {
185 return name_;
186 }
187
188 virtual void action( HistoryAction::Type ) override
189 {
190 if ( !objVoxels_ )
191 return;
192
193 cloneSurface_ = objVoxels_->updateIsoSurface( cloneSurface_ );
194 }
195
196 static void setObjectDirty( const std::shared_ptr<Obj>& obj )
197 {
198 if ( obj )
199 obj->setDirtyFlags( DIRTY_ALL );
200 }
201
202 [[nodiscard]] virtual size_t heapBytes() const override
203 {
204 return name_.capacity() + MR::heapBytes( cloneSurface_ );
205 }
206
207private:
208 std::shared_ptr<ObjectVoxels> objVoxels_;
209 std::shared_ptr<Mesh> cloneSurface_;
210
211 std::string name_;
212};
213
214// Undo action for ObjectVoxels all data change (need for faster undo redo)
215class ChangeGridAction : public HistoryAction
216{
217public:
218 using Obj = ObjectVoxels;
220 ChangeGridAction( std::string name, const std::shared_ptr<ObjectVoxels>& obj ) :
221 objVoxels_{ obj },
222 changeIsoAction_( name, obj ),
223 changeSurfaceAction_(name, obj),
224 name_{ std::move( name ) }
225 {
226 if ( obj )
227 {
228 vdbVolume_ = obj->vdbVolume();
229 histogram_ = obj->histogram();
230 }
231 }
232
233 virtual std::string name() const override
234 {
235 return name_;
236 }
237
238 virtual void action( HistoryAction::Type obj ) override
239 {
240 if ( !objVoxels_ )
241 return;
242
243 vdbVolume_ = objVoxels_->updateVdbVolume( std::move( vdbVolume_ ) );
244 histogram_ = objVoxels_->updateHistogram( std::move( histogram_ ) );
245 changeIsoAction_.action( obj );
246 changeSurfaceAction_.action( obj );
247 }
248
249 static void setObjectDirty( const std::shared_ptr<Obj>& obj )
250 {
251 if ( obj )
252 obj->setDirtyFlags( DIRTY_ALL );
253 }
254
255 [[nodiscard]] virtual size_t heapBytes() const override
256 {
257 return name_.capacity() + histogram_.heapBytes() + changeIsoAction_.heapBytes() + changeSurfaceAction_.heapBytes() + MR::heapBytes( vdbVolume_.data );
258 }
259
260private:
261 std::shared_ptr<ObjectVoxels> objVoxels_;
262 VdbVolume vdbVolume_;
263 Histogram histogram_;
264 ChangeIsoAction changeIsoAction_;
265 ChangeSurfaceAction changeSurfaceAction_;
266
267 std::string name_;
268};
269
271
272}
unsafe ChangeIsoAction(MR._ByValue_ChangeIsoAction _other)
unsafe void action(MR.HistoryAction.Type _1)
size_t heapBytes(const BitSet &bs)
returns the amount of memory given BitSet occupies on heap
Definition MRMesh/MRBitSet.h:298
std::string name(const T &primitive)
Definition MRFeatures.h:309
Definition MRCameraOrientationPlugin.h:8
int heapBytes(FloatGrid grid)