MeshLib C++ Docs
Loading...
Searching...
No Matches
MRPickPointManager.h
Go to the documentation of this file.
1#pragma once
2
4#include "MRViewport.h"
6#include "MRHistoryStore.h"
8#include "MRMesh/MRMeshFwd.h"
10#include <MRMesh/MRphmap.h>
11#include <unordered_map>
12
13namespace MR
14{
15
18class MRVIEWER_CLASS PickPointManager : public MultiListener<
19 MouseDownListener,
20 MouseMoveListener>
21{
22public:
23 using PickerPointCallBack = std::function<void( std::shared_ptr<VisualObject> obj, int index )>;
24 using AllowCallBack = std::function<bool( const std::shared_ptr<VisualObject>& obj, int index )>;
25 using ChangeObjectCallBack = std::function<bool( const std::shared_ptr<VisualObject>& obj )>;
26
27 struct Params
28 {
30 int widgetContourCloseMod = getGlfwModPrimaryCtrl();
31
33 int widgetDeletePointMod = GLFW_MOD_SHIFT;
34
36 bool writeHistory = true;
37
39 std::string historyNameSuffix;
40
42 bool startDraggingJustAddedPoint = true;
43
47
49 Color ordinaryPointColor = Color::gray();
50
52 Color lastPointColor = Color::green();
53
55 Color closeContourPointColor = Color::transparent();
56
59
63
66
70
73
76
79
83
86
90
91 };
93
96
97 using SurfaceContour = std::vector<std::shared_ptr<SurfacePointWidget>>;
98 using SurfaceContours = std::unordered_map <std::shared_ptr<VisualObject>, SurfaceContour>;
99
101 MRVIEWER_API PickPointManager();
102
104 MRVIEWER_API ~PickPointManager();
105
107 [[nodiscard]] const SurfaceContour& getSurfaceContour( const std::shared_ptr<VisualObject>& obj ) { return pickedPoints_[obj]; }
108
110 [[nodiscard]] const SurfaceContours& getSurfaceContours() const { return pickedPoints_; }
111
113 [[nodiscard]] MRVIEWER_API bool isClosedContour( const std::shared_ptr<VisualObject>& obj ) const;
114
116 [[nodiscard]] MRVIEWER_API size_t numPickPoints( const std::shared_ptr<VisualObject>& obj ) const;
117
119 [[nodiscard]] MRVIEWER_API std::shared_ptr<SurfacePointWidget> getPointWidget( const std::shared_ptr<VisualObject>& obj, int index ) const;
120
122 [[nodiscard]] MRVIEWER_API int getPointIndex( const std::shared_ptr<VisualObject>& obj, SurfacePointWidget& pointWidget ) const;
123
125 [[nodiscard]] SurfacePointWidget* draggedPointWidget() const { return draggedPointWidget_; }
126
129 MRVIEWER_API bool appendPoint( const std::shared_ptr<VisualObject>& obj, const PickedPoint& triPoint, bool startDragging = false );
130
134 MRVIEWER_API bool insertPoint( const std::shared_ptr<VisualObject>& obj, int index, const PickedPoint& triPoint, bool startDragging = false );
135
137 MRVIEWER_API bool removePoint( const std::shared_ptr<VisualObject>& obj, int pickedIndex );
138
139 // if ( makeClosed ), and the contour is open add a special transparent point contour to the end of contour connected with given object.
140 // A coordinated of this special transparent point will be equal to the firs point in contour, which will means that contour is closed.
141 // if ( !makeClosed ), and the contour is closed remove last point of contour connected with given object.
142 MRVIEWER_API bool closeContour( const std::shared_ptr<VisualObject>& obj, bool makeClosed = true );
143
145 {
146 std::weak_ptr<VisualObject> objPtr;
147 std::vector<PickedPoint> pickedPoints;
148 };
149 using FullState = std::vector<ObjectState>;
150
152 MRVIEWER_API FullState getFullState() const;
153
155 MRVIEWER_API void clear();
156
158 MRVIEWER_API void setFullState( FullState s );
159
160private:
161 MRVIEWER_API bool onMouseDown_( MouseButton button, int modifier ) override;
162 MRVIEWER_API bool onMouseMove_( int mouse_x, int mouse_y ) override;
163
164 ObjAndPick pick_() const;
165
168 void colorLast2Points_( const std::shared_ptr<VisualObject>& obj );
169
170 // creates point widget for add to contour.
171 [[nodiscard]] std::shared_ptr<SurfacePointWidget> createPickWidget_( const std::shared_ptr<VisualObject>& obj, const PickedPoint& pt );
172
174 void clearNoHistory_();
175
178 void swapStateNoHistory_( FullState& s );
179
182 int insertPointNoHistory_( const std::shared_ptr<VisualObject>& obj, int index, const PickedPoint& point, bool startDragging );
183
185 PickedPoint removePointNoHistory_( const std::shared_ptr<VisualObject>& obj, int index );
186
188 template<class HistoryActionType, typename... Args>
189 void appendHistory_( Args&&... args );
190
192 void appendHistory_( std::shared_ptr<HistoryAction> action ) const;
193
195 void setHoveredPointWidget_( SurfacePointWidget* newHoveredPoint );
196
197 // whether the contour was closed before dragging of point #0, so we need to move the last point on end drag
198 bool moveClosedPoint_ = false;
199
200 // point widget currently under mouse and highlighted
201 SurfacePointWidget* hoveredPointWidget_ = nullptr;
202
203 // point widget currently dragged by mouse
204 SurfacePointWidget* draggedPointWidget_ = nullptr;
205
206 // data storage
207 SurfaceContours pickedPoints_;
208
209 // all spheres created in createPickWidget_ to quickly differentiate them from other features
210 HashSet<const VisualObject*> myPickSpheres_;
211
212 // for each object with pick points, holds connections to mesh/point changed event
213 struct ConnectionHolder
214 {
215 boost::signals2::scoped_connection onMeshChanged;
216 boost::signals2::scoped_connection onPointsChanged;
217 };
219
220 // History classes:
221 class SetStateHistoryAction;
222 class AddRemovePointHistoryAction;
223 class MovePointHistoryAction;
224};
225
226} //namespace MR
Definition MRHistoryAction.h:12
Definition MRPickPointManager.h:21
MRVIEWER_API bool insertPoint(const std::shared_ptr< VisualObject > &obj, int index, const PickedPoint &triPoint, bool startDragging=false)
const SurfaceContour & getSurfaceContour(const std::shared_ptr< VisualObject > &obj)
return contour for specific object (creating new one if necessary)
Definition MRPickPointManager.h:107
MRVIEWER_API FullState getFullState() const
returns the state of this
std::vector< ObjectState > FullState
Definition MRPickPointManager.h:149
MRVIEWER_API ~PickPointManager()
destroy this and remove the undo/redo actions referring this from the history.
std::unordered_map< std::shared_ptr< VisualObject >, SurfaceContour > SurfaceContours
Definition MRPickPointManager.h:98
MRVIEWER_API int getPointIndex(const std::shared_ptr< VisualObject > &obj, SurfacePointWidget &pointWidget) const
returns index of given point widget on given object or -1 if this widget is not from given object
SurfacePointWidget * draggedPointWidget() const
returns point widget currently dragged by mouse
Definition MRPickPointManager.h:125
std::vector< std::shared_ptr< SurfacePointWidget > > SurfaceContour
Definition MRPickPointManager.h:97
const SurfaceContours & getSurfaceContours() const
return all contours, i.e. per object unorderd_map of ordered surface points [vector].
Definition MRPickPointManager.h:110
MRVIEWER_API bool removePoint(const std::shared_ptr< VisualObject > &obj, int pickedIndex)
Remove point with pickedIndex index from contour connected with obj.
MRVIEWER_API void clear()
removes all points from all objects
MRVIEWER_API PickPointManager()
create an object and starts listening for mouse events
MRVIEWER_API bool isClosedContour(const std::shared_ptr< VisualObject > &obj) const
check whether the contour is closed for a particular object.
MRVIEWER_API void setFullState(FullState s)
removes all current points, then adds pick points on all objects as prescribed by given state
Params params
Definition MRPickPointManager.h:92
MRVIEWER_API size_t numPickPoints(const std::shared_ptr< VisualObject > &obj) const
returns the total number of pick points (including extra point if the contour is closed) on given obj...
std::function< bool(const std::shared_ptr< VisualObject > &obj, int index)> AllowCallBack
Definition MRPickPointManager.h:24
std::function< bool(const std::shared_ptr< VisualObject > &obj)> ChangeObjectCallBack
Definition MRPickPointManager.h:25
std::function< void(std::shared_ptr< VisualObject > obj, int index)> PickerPointCallBack
Definition MRPickPointManager.h:23
MRVIEWER_API bool appendPoint(const std::shared_ptr< VisualObject > &obj, const PickedPoint &triPoint, bool startDragging=false)
MRVIEWER_API std::shared_ptr< SurfacePointWidget > getPointWidget(const std::shared_ptr< VisualObject > &obj, int index) const
returns point widget by index from given object or nullptr if no such widget exists
MRVIEWER_API bool closeContour(const std::shared_ptr< VisualObject > &obj, bool makeClosed=true)
Definition MRSurfacePointPicker.h:18
std::function< bool(const VisualObject *, ViewportMask)> PickRenderObjectPredicate
Definition MRViewport.h:159
Definition MRCameraOrientationPlugin.h:8
MouseButton
Definition MRMouse.h:9
std::variant< std::monostate, MeshTriPoint, EdgePoint, VertId > PickedPoint
Definition MRPointOnObject.h:42
std::pair< std::shared_ptr< MR::VisualObject >, MR::PointOnObject > ObjAndPick
Definition MRViewerFwd.h:75
MRVIEWER_API int getGlfwModPrimaryCtrl()
phmap::flat_hash_map< K, V, Hash, Eq > HashMap
Definition MRMesh/MRMeshFwd.h:592
Definition MRMesh/MRColor.h:9
Definition MRViewerEventsListener.h:20
Definition MRViewerEventsListener.h:29
Definition MRPickPointManager.h:145
std::vector< PickedPoint > pickedPoints
Definition MRPickPointManager.h:147
std::weak_ptr< VisualObject > objPtr
Definition MRPickPointManager.h:146
Definition MRPickPointManager.h:28
AllowCallBack canRemovePoint
Definition MRPickPointManager.h:82
PickerPointCallBack onPointMoveFinish
This callback is invoked when point's dragging is completed.
Definition MRPickPointManager.h:78
PickerPointCallBack onPointRemove
This callback is invoked when a point is removed with its index before deletion.
Definition MRPickPointManager.h:85
ChangeObjectCallBack onUpdatePoints
Definition MRPickPointManager.h:89
SurfacePointWidget::Parameters surfacePointParams
Definition MRPickPointManager.h:46
PickerPointCallBack onPointAdd
This callback is invoked after a point is added with its index.
Definition MRPickPointManager.h:65
Viewport::PickRenderObjectPredicate pickPredicate
Predicate to additionally filter objects that should be treated as pickable.
Definition MRPickPointManager.h:58
PickerPointCallBack onPointMove
This callback is invoked every time after currently dragged point is moved (in between onPointMoveSta...
Definition MRPickPointManager.h:75
PickerPointCallBack onPointMoveStart
This callback is invoked when a point starts being dragged.
Definition MRPickPointManager.h:72
AllowCallBack canAddPoint
Definition MRPickPointManager.h:62
AllowCallBack canMovePoint
Definition MRPickPointManager.h:69
std::string historyNameSuffix
This is appended to the names of all undo/redo actions.
Definition MRPickPointManager.h:39
A common base class for all history actions of this widget.
Definition MRPickPointManager.h:95
Definition MRSurfacePointPicker.h:32