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<MR::VisualObject> obj, int index )>;
24 using AllowCallBack = std::function<bool( const std::shared_ptr<MR::VisualObject>& obj, int index )>;
25
26 struct Params
27 {
29 int widgetContourCloseMod = GLFW_MOD_CONTROL;
30
32 int widgetDeletePointMod = GLFW_MOD_SHIFT;
33
35 bool writeHistory = true;
36
38 std::string historyNameSuffix;
39
43
46 MR::Color ordinaryPointColor = Color::gray();
47
50 MR::Color lastPointColor = Color::green();
51
54 MR::Color closeContourPointColor = Color::transparent();
55
58
62
65
68
71
74
78
81 };
83
86
87 using SurfaceContour = std::vector<std::shared_ptr<SurfacePointWidget>>;
88 using SurfaceContours = std::unordered_map <std::shared_ptr<MR::VisualObject>, SurfaceContour>;
89
91 MRVIEWER_API PickPointManager();
92
94 MRVIEWER_API ~PickPointManager();
95
97 [[nodiscard]] const SurfaceContour& getSurfaceContour( const std::shared_ptr<MR::VisualObject>& obj ) { return pickedPoints_[obj]; }
98
100 [[nodiscard]] const SurfaceContours& getSurfaceContours() const { return pickedPoints_; }
101
103 [[nodiscard]] MRVIEWER_API bool isClosedCountour( const std::shared_ptr<VisualObject>& obj ) const;
104
106 [[nodiscard]] MRVIEWER_API std::shared_ptr<SurfacePointWidget> getPointWidget( const std::shared_ptr<VisualObject>& obj, int index ) const;
107
109 [[nodiscard]] SurfacePointWidget* draggedPointWidget() const { return draggedPointWidget_; }
110
112 MRVIEWER_API bool appendPoint( const std::shared_ptr<VisualObject>& obj, const PickedPoint& triPoint );
113
115 MRVIEWER_API bool removePoint( const std::shared_ptr<VisualObject>& obj, int pickedIndex );
116
117 // if ( makeClosed ), and the contour is open add a special transparent point contour to the end of contour connected with given object.
118 // A coordinated of this special transparent point will be equal to the firs point in contour, which will means that contour is closed.
119 // if ( !makeClosed ), and the contour is closed remove last point of contour connected with given object.
120 MRVIEWER_API bool closeContour( const std::shared_ptr<VisualObject>& obj, bool makeClosed = true );
121
123 {
124 std::weak_ptr<VisualObject> objPtr;
125 std::vector<PickedPoint> pickedPoints;
126 };
127 using FullState = std::vector<ObjectState>;
128
130 MRVIEWER_API FullState getFullState() const;
131
133 MRVIEWER_API void clear();
134
136 MRVIEWER_API void setFullState( FullState s );
137
138private:
139 MRVIEWER_API bool onMouseDown_( MouseButton button, int modifier ) override;
140 MRVIEWER_API bool onMouseMove_( int mouse_x, int mouse_y ) override;
141
142 ObjAndPick pick_() const;
143
146 void colorLast2Points_( const std::shared_ptr<VisualObject>& obj );
147
148 // creates point widget for add to contour.
149 [[nodiscard]] std::shared_ptr<SurfacePointWidget> createPickWidget_( const std::shared_ptr<MR::VisualObject>& obj, const PickedPoint& pt );
150
152 void clearNoHistory_();
153
156 void swapStateNoHistory_( FullState& s );
157
160 int insertPointNoHistory_( const std::shared_ptr<VisualObject>& obj, int index, const PickedPoint& point );
161
163 PickedPoint removePointNoHistory_( const std::shared_ptr<VisualObject>& obj, int index );
164
166 template<class HistoryActionType, typename... Args>
167 void appendHistory_( Args&&... args );
168
170 void appendHistory_( std::shared_ptr<HistoryAction> action ) const;
171
172 // whether the contour was closed before dragging of point #0, so we need to move the last point on end drag
173 bool moveClosedPoint_ = false;
174
175 // point widget currently under mouse and highlighted
176 SurfacePointWidget* hoveredPointWidget_ = nullptr;
177
178 // point widget currently dragged by mouse
179 SurfacePointWidget* draggedPointWidget_ = nullptr;
180
181 // data storage
182 SurfaceContours pickedPoints_;
183
184 // all spheres created in createPickWidget_ to quickly differentiate them from other features
185 HashSet<const VisualObject*> myPickSpheres_;
186
187 // for each object with pick points, holds connections to mesh/point changed event
188 struct ConnectionHolder
189 {
190 boost::signals2::scoped_connection onMeshChanged;
191 boost::signals2::scoped_connection onPointsChanged;
192 };
194
195 // History classes:
196 class SetStateHistoryAction;
197 class AddRemovePointHistoryAction;
198 class MovePointHistoryAction;
199};
200
201} //namespace MR
std::pair< std::shared_ptr< MR::VisualObject >, MR::PointOnObject > ObjAndPick
Definition MRViewport.h:21
Definition MRHistoryAction.h:12
Definition MRPickPointManager.h:21
MRVIEWER_API FullState getFullState() const
returns the state of this
std::vector< ObjectState > FullState
Definition MRPickPointManager.h:127
MRVIEWER_API ~PickPointManager()
destroy this and remove the undo/redo actions referring this from the history.
std::unordered_map< std::shared_ptr< MR::VisualObject >, SurfaceContour > SurfaceContours
Definition MRPickPointManager.h:88
SurfacePointWidget * draggedPointWidget() const
returns point widget currently dragged by mouse
Definition MRPickPointManager.h:109
std::vector< std::shared_ptr< SurfacePointWidget > > SurfaceContour
Definition MRPickPointManager.h:87
MRVIEWER_API bool appendPoint(const std::shared_ptr< VisualObject > &obj, const PickedPoint &triPoint)
Add a point to the end of non closed contour connected with obj.
const SurfaceContours & getSurfaceContours() const
return all contours, i.e. per object unorderd_map of ordered surface points [vector].
Definition MRPickPointManager.h:100
std::function< bool(const std::shared_ptr< MR::VisualObject > &obj, int index)> AllowCallBack
Definition MRPickPointManager.h:24
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 void setFullState(FullState s)
removes all current points, then adds pick points on all objects as prescribed by given state
std::function< void(std::shared_ptr< MR::VisualObject > obj, int index)> PickerPointCallBack
Definition MRPickPointManager.h:23
Params params
Definition MRPickPointManager.h:82
MRVIEWER_API bool isClosedCountour(const std::shared_ptr< VisualObject > &obj) const
check whether the contour is closed for a particular object.
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)
const SurfaceContour & getSurfaceContour(const std::shared_ptr< MR::VisualObject > &obj)
return contour for specific object (creating new one if necessary)
Definition MRPickPointManager.h:97
Definition MRSurfacePointPicker.h:18
std::function< bool(const VisualObject *, ViewportMask)> PickRenderObjectPredicate
Definition MRViewport.h:162
MouseButton
Definition MRMouse.h:9
std::variant< MeshTriPoint, EdgePoint, VertId, int > PickedPoint
Definition MRPointOnObject.h:40
phmap::flat_hash_map< K, V, Hash, Eq > HashMap
Definition MRMesh/MRMeshFwd.h:505
Definition MRMesh/MRColor.h:9
Definition MRViewerEventsListener.h:20
Definition MRViewerEventsListener.h:29
Definition MRPickPointManager.h:123
std::vector< PickedPoint > pickedPoints
Definition MRPickPointManager.h:125
std::weak_ptr< VisualObject > objPtr
Definition MRPickPointManager.h:124
Definition MRPickPointManager.h:27
AllowCallBack canRemovePoint
Definition MRPickPointManager.h:77
PickerPointCallBack onPointMoveFinish
This callback is invoked when point's dragging is completed.
Definition MRPickPointManager.h:73
PickerPointCallBack onPointRemove
This callback is invoked when a point is removed with its index before deletion.
Definition MRPickPointManager.h:80
SurfacePointWidget::Parameters surfacePointParams
Definition MRPickPointManager.h:42
PickerPointCallBack onPointAdd
This callback is invoked after a point is added with its index.
Definition MRPickPointManager.h:64
Viewport::PickRenderObjectPredicate pickPredicate
Predicate to additionally filter objects that should be treated as pickable.
Definition MRPickPointManager.h:57
PickerPointCallBack onPointMove
This callback is invoked every time after currently dragged point is moved (in between onPointMoveSta...
Definition MRPickPointManager.h:70
PickerPointCallBack onPointMoveStart
This callback is invoked when a point starts being dragged.
Definition MRPickPointManager.h:67
AllowCallBack canAddPoint
Definition MRPickPointManager.h:61
std::string historyNameSuffix
This is appended to the names of all undo/redo actions.
Definition MRPickPointManager.h:38
A common base class for all history actions of this widget.
Definition MRPickPointManager.h:85
Definition MRSurfacePointPicker.h:32