Skip to content

Commit 931c73e

Browse files
committed
ChangeOnePointInCloudAction and ChangeOneNormalInCloudAction
1 parent 8f18e83 commit 931c73e

File tree

2 files changed

+142
-2
lines changed

2 files changed

+142
-2
lines changed

source/MRMesh/MRChangePointCloudAction.h

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,74 @@ class ChangePointCloudPointsAction : public HistoryAction
117117
std::string name_;
118118
};
119119

120-
}
120+
/// Undo action that modifies one point's coordinates inside ObjectPoints
121+
/// \ingroup HistoryGroup
122+
class ChangeOnePointInCloudAction : public HistoryAction
123+
{
124+
public:
125+
using Obj = ObjectPoints;
126+
127+
/// use this constructor to remember point's coordinates before making any changes in it
128+
ChangeOnePointInCloudAction( std::string name, const std::shared_ptr<ObjectPoints>& obj, VertId pointId ) :
129+
objPoints_{ obj },
130+
pointId_{ pointId },
131+
name_{ std::move( name ) }
132+
{
133+
if ( obj )
134+
{
135+
if ( auto m = obj->pointCloud() )
136+
if ( m->points.size() > pointId_ )
137+
safeCoords_ = m->points[pointId_];
138+
}
139+
}
140+
141+
/// use this constructor to remember point's coordinates and immediate set new coordinates
142+
ChangeOnePointInCloudAction( std::string name, const std::shared_ptr<ObjectPoints>& obj, VertId pointId, const Vector3f & newCoords ) :
143+
objPoints_{ obj },
144+
pointId_{ pointId },
145+
safeCoords_{ newCoords },
146+
name_{ std::move( name ) }
147+
{
148+
action( HistoryAction::Type::Redo );
149+
}
150+
151+
virtual std::string name() const override
152+
{
153+
return name_;
154+
}
155+
156+
virtual void action( HistoryAction::Type ) override
157+
{
158+
if ( !objPoints_ )
159+
return;
160+
161+
if ( auto m = objPoints_->varPointCloud() )
162+
{
163+
if ( m->points.size() > pointId_ )
164+
{
165+
std::swap( safeCoords_, m->points[pointId_] );
166+
objPoints_->setDirtyFlags( DIRTY_POSITION );
167+
}
168+
}
169+
}
170+
171+
static void setObjectDirty( const std::shared_ptr<ObjectPoints>& obj )
172+
{
173+
if ( obj )
174+
obj->setDirtyFlags( DIRTY_POSITION );
175+
}
176+
177+
[[nodiscard]] virtual size_t heapBytes() const override
178+
{
179+
return name_.capacity();
180+
}
181+
182+
private:
183+
std::shared_ptr<ObjectPoints> objPoints_;
184+
VertId pointId_;
185+
Vector3f safeCoords_;
186+
187+
std::string name_;
188+
};
189+
190+
} //namespace MR

source/MRMesh/MRChangePointCloudNormalsAction.h

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,74 @@ class ChangePointCloudNormalsAction : public HistoryAction
6969
std::string name_;
7070
};
7171

72-
}
72+
/// Undo action that modifies one point's normal inside ObjectPoints
73+
/// \ingroup HistoryGroup
74+
class ChangeOneNormalInCloudAction : public HistoryAction
75+
{
76+
public:
77+
using Obj = ObjectPoints;
78+
79+
/// use this constructor to remember point's normal before making any changes in it
80+
ChangeOneNormalInCloudAction( std::string name, const std::shared_ptr<ObjectPoints>& obj, VertId pointId ) :
81+
objPoints_{ obj },
82+
pointId_{ pointId },
83+
name_{ std::move( name ) }
84+
{
85+
if ( obj )
86+
{
87+
if ( auto m = obj->pointCloud() )
88+
if ( m->normals.size() > pointId_ )
89+
safeNormal_ = m->normals[pointId_];
90+
}
91+
}
92+
93+
/// use this constructor to remember point's normal and immediate set new normal
94+
ChangeOneNormalInCloudAction( std::string name, const std::shared_ptr<ObjectPoints>& obj, VertId pointId, const Vector3f & newNormal ) :
95+
objPoints_{ obj },
96+
pointId_{ pointId },
97+
safeNormal_{ newNormal },
98+
name_{ std::move( name ) }
99+
{
100+
action( HistoryAction::Type::Redo );
101+
}
102+
103+
virtual std::string name() const override
104+
{
105+
return name_;
106+
}
107+
108+
virtual void action( HistoryAction::Type ) override
109+
{
110+
if ( !objPoints_ )
111+
return;
112+
113+
if ( auto m = objPoints_->varPointCloud() )
114+
{
115+
if ( m->normals.size() > pointId_ )
116+
{
117+
std::swap( safeNormal_, m->normals[pointId_] );
118+
objPoints_->setDirtyFlags( DIRTY_RENDER_NORMALS );
119+
}
120+
}
121+
}
122+
123+
static void setObjectDirty( const std::shared_ptr<ObjectPoints>& obj )
124+
{
125+
if ( obj )
126+
obj->setDirtyFlags( DIRTY_RENDER_NORMALS );
127+
}
128+
129+
[[nodiscard]] virtual size_t heapBytes() const override
130+
{
131+
return name_.capacity();
132+
}
133+
134+
private:
135+
std::shared_ptr<ObjectPoints> objPoints_;
136+
VertId pointId_;
137+
Vector3f safeNormal_;
138+
139+
std::string name_;
140+
};
141+
142+
} //namespace MR

0 commit comments

Comments
 (0)