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
25 struct Params
26 {
28 int widgetContourCloseMod = GLFW_MOD_CONTROL;
29
31 int widgetDeletePointMod = GLFW_MOD_SHIFT;
32
34 std::string historyNameSuffix;
35
39
42 MR::Color ordinaryPointColor = Color::gray();
43
46 MR::Color lastPointColor = Color::green();
47
50 MR::Color closeContourPointColor = Color::transparent();
51
54
57
60
63
66 };
68
71
72 using SurfaceContour = std::vector<std::shared_ptr<SurfacePointWidget>>;
73 using SurfaceContours = std::unordered_map <std::shared_ptr<MR::VisualObject>, SurfaceContour>;
74
76 MRVIEWER_API PickPointManager();
77
79 MRVIEWER_API ~PickPointManager();
80
82 [[nodiscard]] const SurfaceContour& getSurfaceContour( const std::shared_ptr<MR::VisualObject>& obj ) { return pickedPoints_[obj]; }
83
85 [[nodiscard]] const SurfaceContours& getSurfaceContours() const { return pickedPoints_; }
86
88 [[nodiscard]] MRVIEWER_API bool isClosedCountour( const std::shared_ptr<VisualObject>& obj ) const;
89
91 [[nodiscard]] MRVIEWER_API std::shared_ptr<SurfacePointWidget> getPointWidget( const std::shared_ptr<VisualObject>& obj, int index ) const;
92
94 [[nodiscard]] SurfacePointWidget* draggedPointWidget() const { return draggedPointWidget_; }
95
97 MRVIEWER_API bool appendPoint( const std::shared_ptr<VisualObject>& obj, const PickedPoint& triPoint );
98
100 MRVIEWER_API bool removePoint( const std::shared_ptr<VisualObject>& obj, int pickedIndex );
101
102 // if ( makeClosed ), and the contour is open add a special transparent point contour to the end of contour connected with given object.
103 // A coordinated of this special transparent point will be equal to the firs point in contour, which will means that contour is closed.
104 // if ( !makeClosed ), and the contour is closed remove last point of contour connected with given object.
105 MRVIEWER_API bool closeContour( const std::shared_ptr<VisualObject>& obj, bool makeClosed = true );
106
108 {
109 std::weak_ptr<VisualObject> objPtr;
110 std::vector<PickedPoint> pickedPoints;
111 };
112 using FullState = std::vector<ObjectState>;
113
115 MRVIEWER_API FullState getFullState() const;
116
118 MRVIEWER_API void clear();
119
121 MRVIEWER_API void setFullState( FullState s );
122
123private:
124 MRVIEWER_API bool onMouseDown_( MouseButton button, int modifier ) override;
125 MRVIEWER_API bool onMouseMove_( int mouse_x, int mouse_y ) override;
126
127 ObjAndPick pick_() const;
128
131 void colorLast2Points_( const std::shared_ptr<VisualObject>& obj );
132
133 // creates point widget for add to contour.
134 [[nodiscard]] std::shared_ptr<SurfacePointWidget> createPickWidget_( const std::shared_ptr<MR::VisualObject>& obj, const PickedPoint& pt );
135
137 void clearNoHistory_();
138
141 void swapStateNoHistory_( FullState& s );
142
145 int insertPointNoHistory_( const std::shared_ptr<VisualObject>& obj, int index, const PickedPoint& point );
146
148 PickedPoint removePointNoHistory_( const std::shared_ptr<VisualObject>& obj, int index );
149
150 // whether the contour was closed before dragging of point #0, so we need to move the last point on end drag
151 bool moveClosedPoint_ = false;
152
153 // point widget currently under mouse and highlighted
154 SurfacePointWidget* hoveredPointWidget_ = nullptr;
155
156 // point widget currently dragged by mouse
157 SurfacePointWidget* draggedPointWidget_ = nullptr;
158
159 // data storage
160 SurfaceContours pickedPoints_;
161
162 // all spheres created in createPickWidget_ to quickly differentiate them from other features
163 HashSet<const VisualObject*> myPickSpheres_;
164
165 // for each object with pick points, holds connections to mesh/point changed event
166 struct ConnectionHolder
167 {
168 boost::signals2::scoped_connection onMeshChanged;
169 boost::signals2::scoped_connection onPointsChanged;
170 };
172
173 // History classes:
174 class SetStateHistoryAction;
175 class AddRemovePointHistoryAction;
176 class MovePointHistoryAction;
177};
178
179} //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:112
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:73
SurfacePointWidget * draggedPointWidget() const
returns point widget currently dragged by mouse
Definition MRPickPointManager.h:94
std::vector< std::shared_ptr< SurfacePointWidget > > SurfaceContour
Definition MRPickPointManager.h:72
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:85
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:67
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:82
Definition MRSurfacePointPicker.h:18
std::function< bool(const VisualObject *, ViewportMask)> PickRenderObjectPredicate
Definition MRViewport.h:155
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:482
Definition MRMesh/MRColor.h:9
Definition MRViewerEventsListener.h:20
Definition MRViewerEventsListener.h:29
Definition MRPickPointManager.h:108
std::vector< PickedPoint > pickedPoints
Definition MRPickPointManager.h:110
std::weak_ptr< VisualObject > objPtr
Definition MRPickPointManager.h:109
Definition MRPickPointManager.h:26
PickerPointCallBack onPointMoveFinish
This callback is invoked when point's dragging is completed.
Definition MRPickPointManager.h:62
PickerPointCallBack onPointRemove
This callback is invoked when a point is removed with its index before deletion.
Definition MRPickPointManager.h:65
SurfacePointWidget::Parameters surfacePointParams
Definition MRPickPointManager.h:38
PickerPointCallBack onPointAdd
This callback is invoked after a point is added with its index.
Definition MRPickPointManager.h:56
Viewport::PickRenderObjectPredicate pickPredicate
Predicate to additionally filter objects that should be treated as pickable.
Definition MRPickPointManager.h:53
PickerPointCallBack onPointMoveStart
This callback is invoked when a point starts being dragged.
Definition MRPickPointManager.h:59
std::string historyNameSuffix
This is appended to the names of all undo/redo actions.
Definition MRPickPointManager.h:34
A common base class for all history actions of this widget.
Definition MRPickPointManager.h:70
Definition MRSurfacePointPicker.h:32