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
87 MRVIEWER_API void clear( bool writeHistory = true );
88
89 // reset widget, clear internal variables and detach from signals.
90 MRVIEWER_API void reset();
91
92 // return contour for specific object, i.e. ordered vector of surface points
93 [[nodiscard]] const SurfaceContour& getSurfaceContour( const std::shared_ptr<MR::VisualObject>& obj )
94 {
95 return pickedPoints_[obj];
96 }
97
98 // return all contours, i.e. per object umap of ordered surface points [vestor].
99 [[nodiscard]] const SurfaceContours& getSurfaceContours() const
100 {
101 return pickedPoints_;
102 }
103
104 // chech is contour closed for particular object.
105 [[nodiscard]] bool isClosedCountour( const std::shared_ptr<VisualObject>& obj );
106
107 // Correctly selects the last point in the contours.
108 // If obj == nullptr then the check will be in all circuits.
109 // If specified, then only in the contour on specyfied object
110 void highlightLastPoint( const std::shared_ptr<VisualObject>& obj );
111
112 // shared variables. which need getters and setters.
113 MRVIEWER_API std::pair <std::shared_ptr<MR::VisualObject>, int > getActivePoint() const;
114 MRVIEWER_API void setActivePoint( std::shared_ptr<MR::VisualObject> obj, int index );
115
117 MRVIEWER_API std::shared_ptr<SurfacePointWidget> getActiveSurfacePoint() const;
118
119 // Add a point to the end of non closed contour connected with obj.
120 // With carefull it is possile to use it in CallBack.
121 MRVIEWER_API bool appendPoint( const std::shared_ptr<VisualObject>& obj, const PickedPoint& triPoint );
122
123 // Remove point with pickedIndex index from contour connected with obj.
124 // With carefull it is possile to use it in CallBack.
125 MRVIEWER_API bool removePoint( const std::shared_ptr<VisualObject>& obj, int pickedIndex );
126
127 // Add a special transperent point contour to the end of contour connected with objectToCloseCoutour.
128 // A coordinated of this special transperent point will be equal to the firs point in contour,
129 // which will means that contour is closed.
130 MRVIEWER_API bool closeContour( const std::shared_ptr<VisualObject>& objectToCloseCoutour );
131
132 // configuration params
134private:
135
136 MRVIEWER_API bool onMouseDown_( Viewer::MouseButton button, int modifier ) override;
137 MRVIEWER_API bool onMouseMove_( int mouse_x, int mouse_y ) override;
138
139 // creates point widget for add to contour.
140 [[nodiscard]] std::shared_ptr<SurfacePointWidget> createPickWidget_( const std::shared_ptr<MR::VisualObject>& obj, const PickedPoint& pt );
141
142 // SurfaceContoursWidget interlal variables
143 bool moveClosedPoint_ = false;
144 bool activeChange_ = false;
145 bool isPickerActive_ = false;
146
147 // active point
148 int activeIndex_{ 0 };
149 std::shared_ptr<MR::VisualObject> activeObject_ = nullptr;
150
151 // data storage
152 SurfaceContours pickedPoints_;
153
154 // picked points' cache
155 std::unordered_set<const VisualObject*> surfacePointWidgetCache_;
156
157 // connection storage
158 struct SurfaceConnectionHolder
159 {
160 boost::signals2::scoped_connection onMeshChanged;
161 boost::signals2::scoped_connection onPointsChanged;
162 };
163 std::unordered_map<std::shared_ptr<VisualObject>, SurfaceConnectionHolder> surfaceConnectionHolders_;
164
165 // CallBack functions
166 PickerPointCallBack onPointAdd_;
167 PickerPointCallBack onPointMove_;
168 PickerPointCallBack onPointMoveFinish_;
169 PickerPointCallBack onPointRemove_;
170 PickerPointObjectChecker isObjectValidToPick_;
171
176};
177
178
179// History classes;
181{
182public:
183 AddPointActionPickerPoint( SurfaceContoursWidget& widget, const std::shared_ptr<MR::VisualObject>& obj, const PickedPoint& point ) :
184 widget_{ widget },
185 obj_{ obj },
186 point_{ point }
187 {};
188
189 virtual std::string name() const override;
190 virtual void action( Type actionType ) override;
191 [[nodiscard]] virtual size_t heapBytes() const override;
192private:
193 SurfaceContoursWidget& widget_;
194 const std::shared_ptr<MR::VisualObject> obj_;
195 PickedPoint point_;
196};
197
199{
200public:
201 RemovePointActionPickerPoint( SurfaceContoursWidget& widget, const std::shared_ptr<MR::VisualObject>& obj, const PickedPoint& point, int index ) :
202 widget_{ widget },
203 obj_{ obj },
204 point_{ point },
205 index_{ index }
206 {};
207
208 virtual std::string name() const override;
209 virtual void action( Type actionType ) override;
210 [[nodiscard]] virtual size_t heapBytes() const override;
211private:
212 SurfaceContoursWidget& widget_;
213 const std::shared_ptr<MR::VisualObject> obj_;
214 PickedPoint point_;
215 int index_;
216};
217
219{
220public:
221 ChangePointActionPickerPoint( SurfaceContoursWidget& widget, const std::shared_ptr<MR::VisualObject>& obj, const PickedPoint& point, int index ) :
222 widget_{ widget },
223 obj_{ obj },
224 point_{ point },
225 index_{ index }
226 {};
227
228 virtual std::string name() const override;
229 virtual void action( Type ) override;
230 [[nodiscard]] virtual size_t heapBytes() const override;
231private:
232 SurfaceContoursWidget& widget_;
233 const std::shared_ptr<MR::VisualObject> obj_;
234 PickedPoint point_;
235 int index_;
236};
237
239{
240public:
242
243public:
244 // HistoryAction
245 [[nodiscard]] std::string name() const override { return name_; }
246
247 void action( Type type ) override;
248
249 [[nodiscard]] size_t heapBytes() const override;
250
251private:
252 std::string name_;
253 SurfaceContoursWidget& widget_;
254
255 struct ObjectState
256 {
257 std::weak_ptr<VisualObject> objPtr;
258 std::vector<PickedPoint> pickedPoints;
259 };
260 std::vector<ObjectState> states_;
261 std::weak_ptr<VisualObject> activeObject_;
262 int activeIndex_;
263};
264
265}
Definition MRSurfaceContoursWidget.h:181
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:183
Definition MRSurfaceContoursWidget.h:219
ChangePointActionPickerPoint(SurfaceContoursWidget &widget, const std::shared_ptr< MR::VisualObject > &obj, const PickedPoint &point, int index)
Definition MRSurfaceContoursWidget.h:221
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:199
RemovePointActionPickerPoint(SurfaceContoursWidget &widget, const std::shared_ptr< MR::VisualObject > &obj, const PickedPoint &point, int index)
Definition MRSurfaceContoursWidget.h:201
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:239
size_t heapBytes() const override
returns the amount of memory this object occupies on heap
std::string name() const override
Definition MRSurfaceContoursWidget.h:245
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:93
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:133
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: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:99
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: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:24
SurfacePointWidget::Parameters surfacePointParams
Definition MRSurfaceContoursWidget.h:42
Viewport::PickRenderObjectPredicate pickPredicate
Definition MRSurfaceContoursWidget.h:57
Definition MRSurfacePointPicker.h:34