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