MeshLib Documentation
Loading...
Searching...
No Matches
MRSurfaceContoursWidget.h
Go to the documentation of this file.
1#pragma once
2
4#include "MRViewport.h"
5#include "MRMesh/MRMeshFwd.h"
8#include "MRHistoryStore.h"
10
11#include <unordered_map>
12#include <unordered_set>
13
14namespace MR
15{
16
17class MRVIEWER_CLASS SurfaceContoursWidget : public MultiListener<
18 MouseDownListener,
19 MouseMoveListener>
20{
21public:
23 {
24 // Modifier key for closing a contour (ordered vector of points) using the widget
25 int widgetContourCloseMod = GLFW_MOD_CONTROL;
26
27 // Modifier key for deleting a point using the widget
28 int widgetDeletePointMod = GLFW_MOD_SHIFT;
29
30 // Indicates whether to write history of the contours
31 bool writeHistory = true;
32
33 // This is appended to the names of all undo/redo actions.
34 std::string historyNameSuffix;
35
36 // Indicates whether to flash history on reset call
37 bool filterHistoryonReset = true;
38
39 // Parameters for configuring the surface point widget
40 // Parameters affect to future points only
42
43 // Color for ordinary points in the contour
44 // Parameters affect to future points only
45 MR::Color ordinaryPointColor = Color::gray();
46
47 // Color for the last modified point in the contour
48 // Parameters affect to future points only
49 MR::Color lastPoitColor = Color::green();
50
51 // Color for the special point used to close a contour. Better do not change it.
52 // Parameters affect to future points only
53 MR::Color closeContourPointColor = Color::transparent();
54
55 // Predicate to additionally filter objects that should be treated as pickable.
57 };
58
59 // A common base class for all history actions of this widget.
61
62 using PickerPointCallBack = std::function<void( std::shared_ptr<MR::VisualObject> )>;
63 using PickerPointObjectChecker = std::function<bool( std::shared_ptr<MR::VisualObject> )>;
64
65 using SurfaceContour = std::vector<std::shared_ptr<SurfacePointWidget>>;
66 using SurfaceContours = std::unordered_map <std::shared_ptr<MR::VisualObject>, SurfaceContour>;
67
68 // enable or disable widget
69 MRVIEWER_API void enable( bool isEnabled );
70
71 // create a widget and connect it.
72 // To create a widget, you need to provide 4 callbacks and one function that determines whether this object can be used to place points.
73 // All callback takes a shared pointer to an MR::VisualObject as an argument.
74 // onPointAdd: This callback is invoked when a point is added.
75 // onPointMove : This callback is triggered when a point is being start moved or dragge.
76 // onPointMoveFinish : This callback is called when the movement of a point is completed.
77 // onPointRemove : This callback is executed when a point is removed.
78 // isObjectValidToPick : Must returh true or false. This callback is used to determine whether an object is valid for picking.
79 MRVIEWER_API void create(
80 PickerPointCallBack onPointAdd,
81 PickerPointCallBack onPointMove,
82 PickerPointCallBack onPointMoveFinish,
83 PickerPointCallBack onPointRemove,
84 PickerPointObjectChecker isObjectValidToPick
85 );
86
89 MRVIEWER_API void clear( bool writeHistory = true );
90
91 // Reset widget, clear internal variables and detach from signals.
92 // Also remove the undo/redo actions from the history.
93 MRVIEWER_API void reset();
94
95 // return contour for specific object, i.e. ordered vector of surface points
96 [[nodiscard]] const SurfaceContour& getSurfaceContour( const std::shared_ptr<MR::VisualObject>& obj )
97 {
98 return pickedPoints_[obj];
99 }
100
101 // return all contours, i.e. per object umap of ordered surface points [vestor].
102 [[nodiscard]] const SurfaceContours& getSurfaceContours() const
103 {
104 return pickedPoints_;
105 }
106
107 // chech is contour closed for particular object.
108 [[nodiscard]] bool isClosedCountour( const std::shared_ptr<VisualObject>& obj );
109
110 // Correctly selects the last point in the contours.
111 // If obj == nullptr then the check will be in all circuits.
112 // If specified, then only in the contour on specyfied object
113 void highlightLastPoint( const std::shared_ptr<VisualObject>& obj );
114
115 // shared variables. which need getters and setters.
116 MRVIEWER_API std::pair <std::shared_ptr<MR::VisualObject>, int > getActivePoint() const;
117 MRVIEWER_API void setActivePoint( std::shared_ptr<MR::VisualObject> obj, int index );
118
120 MRVIEWER_API std::shared_ptr<SurfacePointWidget> getActiveSurfacePoint() const;
121
122 // Add a point to the end of non closed contour connected with obj.
123 // With carefull it is possile to use it in CallBack.
124 MRVIEWER_API bool appendPoint( const std::shared_ptr<VisualObject>& obj, const PickedPoint& triPoint );
125
126 // Remove point with pickedIndex index from contour connected with obj.
127 // With carefull it is possile to use it in CallBack.
128 MRVIEWER_API bool removePoint( const std::shared_ptr<VisualObject>& obj, int pickedIndex );
129
130 // Add a special transperent point contour to the end of contour connected with objectToCloseCoutour.
131 // A coordinated of this special transperent point will be equal to the firs point in contour,
132 // which will means that contour is closed.
133 MRVIEWER_API bool closeContour( const std::shared_ptr<VisualObject>& objectToCloseCoutour );
134
135 // configuration params
137private:
138
139 MRVIEWER_API bool onMouseDown_( MouseButton button, int modifier ) override;
140 MRVIEWER_API bool onMouseMove_( int mouse_x, int mouse_y ) override;
141
142 // creates point widget for add to contour.
143 [[nodiscard]] std::shared_ptr<SurfacePointWidget> createPickWidget_( const std::shared_ptr<MR::VisualObject>& obj, const PickedPoint& pt );
144
145 // SurfaceContoursWidget interlal variables
146 bool moveClosedPoint_ = false;
147 bool activeChange_ = false;
148 bool isPickerActive_ = false;
149
150 // active point
151 int activeIndex_{ 0 };
152 std::shared_ptr<MR::VisualObject> activeObject_ = nullptr;
153
154 // data storage
155 SurfaceContours pickedPoints_;
156
157 // picked points' cache
158 std::unordered_set<const VisualObject*> surfacePointWidgetCache_;
159
160 // connection storage
161 struct SurfaceConnectionHolder
162 {
163 boost::signals2::scoped_connection onMeshChanged;
164 boost::signals2::scoped_connection onPointsChanged;
165 };
166 std::unordered_map<std::shared_ptr<VisualObject>, SurfaceConnectionHolder> surfaceConnectionHolders_;
167
168 // CallBack functions
169 PickerPointCallBack onPointAdd_;
170 PickerPointCallBack onPointMove_;
171 PickerPointCallBack onPointMoveFinish_;
172 PickerPointCallBack onPointRemove_;
173 PickerPointObjectChecker isObjectValidToPick_;
174
175 // undo/redo flag; used by the history action classes to force disable internal checks
176 bool undoRedoMode_{ false };
177
178 // History classes:
179 class AddPointActionPickerPoint : public WidgetHistoryAction
180 {
181 public:
182 AddPointActionPickerPoint( SurfaceContoursWidget& widget, const std::shared_ptr<MR::VisualObject>& obj, const PickedPoint& point ) :
183 widget_{ widget },
184 obj_{ obj },
185 point_{ point }
186 {};
187
188 virtual std::string name() const override;
189 virtual void action( Type actionType ) override;
190 [[nodiscard]] virtual size_t heapBytes() const override;
191 private:
192 SurfaceContoursWidget& widget_;
193 const std::shared_ptr<MR::VisualObject> obj_;
194 PickedPoint point_;
195 };
196
197 class RemovePointActionPickerPoint : public WidgetHistoryAction
198 {
199 public:
200 RemovePointActionPickerPoint( SurfaceContoursWidget& widget, const std::shared_ptr<MR::VisualObject>& obj, const PickedPoint& point, int index ) :
201 widget_{ widget },
202 obj_{ obj },
203 point_{ point },
204 index_{ index }
205 {};
206
207 virtual std::string name() const override;
208 virtual void action( Type actionType ) override;
209 [[nodiscard]] virtual size_t heapBytes() const override;
210 private:
211 SurfaceContoursWidget& widget_;
212 const std::shared_ptr<MR::VisualObject> obj_;
213 PickedPoint point_;
214 int index_;
215 };
216
217 class ChangePointActionPickerPoint : public WidgetHistoryAction
218 {
219 public:
220 ChangePointActionPickerPoint( SurfaceContoursWidget& widget, const std::shared_ptr<MR::VisualObject>& obj, const PickedPoint& point, int index ) :
221 widget_{ widget },
222 obj_{ obj },
223 point_{ point },
224 index_{ index }
225 {};
226
227 virtual std::string name() const override;
228 virtual void action( Type ) override;
229 [[nodiscard]] virtual size_t heapBytes() const override;
230 private:
231 SurfaceContoursWidget& widget_;
232 const std::shared_ptr<MR::VisualObject> obj_;
233 PickedPoint point_;
234 int index_;
235 };
236
237 class SurfaceContoursWidgetClearAction : public WidgetHistoryAction
238 {
239 public:
240 SurfaceContoursWidgetClearAction( std::string name, SurfaceContoursWidget& widget );
241
242 public:
243 [[nodiscard]] std::string name() const override { return name_; }
244
245 void action( Type type ) override;
246
247 [[nodiscard]] size_t heapBytes() const override;
248
249 private:
250 std::string name_;
251 SurfaceContoursWidget& widget_;
252
253 struct ObjectState
254 {
255 std::weak_ptr<VisualObject> objPtr;
256 std::vector<PickedPoint> pickedPoints;
257 };
258 std::vector<ObjectState> states_;
259 std::weak_ptr<VisualObject> activeObject_;
260 int activeIndex_;
261 };
262};
263
264}
Definition MRHistoryAction.h:12
Definition MRSurfaceContoursWidget.h:20
std::unordered_map< std::shared_ptr< MR::VisualObject >, SurfaceContour > SurfaceContours
Definition MRSurfaceContoursWidget.h:66
std::function< bool(std::shared_ptr< MR::VisualObject >)> PickerPointObjectChecker
Definition MRSurfaceContoursWidget.h:63
const SurfaceContour & getSurfaceContour(const std::shared_ptr< MR::VisualObject > &obj)
Definition MRSurfaceContoursWidget.h:96
MRVIEWER_API std::shared_ptr< SurfacePointWidget > getActiveSurfacePoint() const
Get the active (the latest picked/moved) surface point widget.
MRVIEWER_API void reset()
SurfaceContoursWidgetParams params
Definition MRSurfaceContoursWidget.h:136
MRVIEWER_API void clear(bool writeHistory=true)
MRVIEWER_API void setActivePoint(std::shared_ptr< MR::VisualObject > obj, int index)
std::vector< std::shared_ptr< SurfacePointWidget > > SurfaceContour
Definition MRSurfaceContoursWidget.h:65
MRVIEWER_API bool appendPoint(const std::shared_ptr< VisualObject > &obj, const PickedPoint &triPoint)
void highlightLastPoint(const std::shared_ptr< VisualObject > &obj)
MRVIEWER_API bool closeContour(const std::shared_ptr< VisualObject > &objectToCloseCoutour)
const SurfaceContours & getSurfaceContours() const
Definition MRSurfaceContoursWidget.h:102
std::function< void(std::shared_ptr< MR::VisualObject >)> PickerPointCallBack
Definition MRSurfaceContoursWidget.h:62
MRVIEWER_API std::pair< std::shared_ptr< MR::VisualObject >, int > getActivePoint() const
MRVIEWER_API void create(PickerPointCallBack onPointAdd, PickerPointCallBack onPointMove, PickerPointCallBack onPointMoveFinish, PickerPointCallBack onPointRemove, PickerPointObjectChecker isObjectValidToPick)
MRVIEWER_API void enable(bool isEnabled)
MRVIEWER_API bool removePoint(const std::shared_ptr< VisualObject > &obj, int pickedIndex)
bool isClosedCountour(const std::shared_ptr< VisualObject > &obj)
std::function< bool(const VisualObject *, ViewportMask)> PickRenderObjectPredicate
Definition MRViewport.h:155
size_t heapBytes(const std::vector< T > &vec)
returns the amount of memory given vector occupies on heap
Definition MRHeapBytes.h:15
std::string name(const T &primitive)
Definition MRFeatures.h:297
MRVIEWER_API void point(Element elem, float menuScaling, const Params &params, ImVec2 point)
Definition MRCameraOrientationPlugin.h:8
MouseButton
Definition MRMouse.h:9
std::variant< MeshTriPoint, EdgePoint, VertId, int > PickedPoint
Definition MRPointOnObject.h:40
Definition MRColor.h:9
Definition MRViewerEventsListener.h:29
Definition MRSurfaceContoursWidget.h:23
SurfacePointWidget::Parameters surfacePointParams
Definition MRSurfaceContoursWidget.h:41
Viewport::PickRenderObjectPredicate pickPredicate
Definition MRSurfaceContoursWidget.h:56
std::string historyNameSuffix
Definition MRSurfaceContoursWidget.h:34
Definition MRSurfaceContoursWidget.h:60
Definition MRSurfacePointPicker.h:33