MeshLib
 
Loading...
Searching...
No Matches
MRMeshBoundarySelectionWidget.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRViewer.h"
5#include "MRMesh/MRMeshFwd.h"
7#include "MRHistoryStore.h"
9#include <unordered_map>
13#include "MRMesh/MRMesh.h"
14
15namespace MR
16{
17
18// A widget that allows you to find, highlight and select boundaries (holes) in the mesh.
19// To provide feedback during creation, it is necessary to specify a callback that will be called if a specific hole is selected.
20class MRVIEWER_CLASS BoundarySelectionWidget : public MultiListener<
21 MouseDownListener,
22 MouseMoveListener>
23{
24public:
25
27
28 MR::Color ordinaryColor = MR::Color::gray();
29 float ordinaryLineWidth = 3;
30
31 MR::Color hoveredColor = MR::Color::green();
32 float hoveredLineWidth = 4;
33
34 MR::Color selectedColor = MR::Color::purple();
35 float selectedLineWidth = 3;
36
37 };
38
39 using BoundarySelectionWidgetCallBack = std::function<void( std::shared_ptr<const MR::ObjectMeshHolder> )>;
40 using BoundarySelectionWidgetChecker = std::function<bool( std::shared_ptr<const MR::ObjectMeshHolder> )>;
41
42 using HolesOnObject = std::vector<MR::EdgeId>;
43 using PerObjectHoles = std::unordered_map <std::shared_ptr<MR::ObjectMeshHolder>, HolesOnObject>;
44 using PerObjectHolesPolylines = std::unordered_map <std::shared_ptr<MR::ObjectMeshHolder>, std::vector<AncillaryLines>>;
45 using PerObjectMeshChangedSignals = std::unordered_map < std::shared_ptr<MR::ObjectMeshHolder>, boost::signals2::scoped_connection>;
46
47
48 // enable or disable widget
49 MRVIEWER_API void enable( bool isEnabled );
50
51 // create a widget and connect it.
52 // To create a widget, you need to provide 1 callbacks and one function that determines whether this object can be used to detect and select boundaries ( holes ).
53 // All callback takes a shared pointer to an MR::ObjectMeshHolder as an argument.
54 // onBoundarySelected: This callback is invoked when a boundary is selected.
55 // isObjectValidToPick : Must return true or false. This callback is used to determine whether an object is valid for picking.
56 MRVIEWER_API void create(
57 BoundarySelectionWidgetCallBack onBoundarySelected,
58 BoundarySelectionWidgetChecker isObjectValidToPick
59 );
60
61 // meshChangedSignal processor
63
64 // reset widget, clear internal variables and detach from signals.
65 MRVIEWER_API void reset();
66
67 // select one of the holes. Return true on success.
68 MRVIEWER_API bool selectHole( std::shared_ptr<MR::ObjectMeshHolder> object, int index );
69
70 // clear selection
71 MRVIEWER_API void clear();
72
73 // returns pair of selected hole ( in Edge representations) and objects on which particular hole is present
74 MRVIEWER_API std::pair< std::shared_ptr<MR::ObjectMeshHolder>, EdgeId > getSelectHole() const;
75
76 // collect and return vector of points ( verts coord ) for all edges in selected mesh boundary
77 MRVIEWER_API std::vector<MR::Vector3f> getPointsForSelectedHole() const;
78
79 // configuration params
81private:
82
83 float mouseAccuracy_{ 5.5f };
84
85 bool isSelectorActive_ = false;
86
87 PerObjectHoles holes_;
88 PerObjectHolesPolylines holeLines_;
89 PerObjectMeshChangedSignals onMeshChangedSignals_;
90
91 MRVIEWER_API bool onMouseDown_( Viewer::MouseButton button, int modifier ) override;
92 MRVIEWER_API bool onMouseMove_( int mouse_x, int mouse_y ) override;
93
94 // create an ancillaryLines object (polyline) for given mesh hole, for visually preview it
95 AncillaryLines createAncillaryLines_( std::shared_ptr<ObjectMeshHolder>& obj, MR::EdgeId hole );
96
97 // For given object and hole ( edge representation ), return a polyline around the hole boundary.
98 std::shared_ptr<MR::Polyline3> getHoleBorder_( const std::shared_ptr<ObjectMeshHolder> obj, EdgeId initEdge );
99
100 // Currently it returns the first hole it comes across.
101 // Those. if the condition is met for several holes( including holes on different objects ), then the first one available will be selected.
102 std::pair<std::shared_ptr<MR::ObjectMeshHolder>, HoleEdgePoint> getHoverdHole_();
103
104 // select hole
105 bool selectHole_( std::shared_ptr<ObjectMeshHolder> object, int index, bool writeHistory = true );
106
107 // update color for one of the polylines
108 bool updateHole_( std::shared_ptr<MR::ObjectMeshHolder> object, int index, MR::Color color, float lineWidth );
109
110
111 enum class ActionType {
112 SelectHole,
113 HoverHole
114 };
115
116 // pick processor, for both mouseDown and MouseMove events.
117 bool actionByPick_( ActionType actionType );
118
119 // CallBack functions
120 BoundarySelectionWidgetCallBack onBoundarySelected_;
121 BoundarySelectionWidgetChecker isObjectValidToPick_;
122
123 // selected hole
124 std::shared_ptr<MR::ObjectMeshHolder> selectedHoleObject_;
125 int selectedHoleIndex_;
126
127 // hovered hole
128 std::shared_ptr<MR::ObjectMeshHolder> hoveredHoleObject_;
129 int hoveredHoleIndex_;
130
131 // return is selected hole hovered at this time or not.
132 bool isSelectedAndHoveredTheSame_();
133
134 // hover particular hole/
135 bool hoverHole_( std::shared_ptr<MR::ObjectMeshHolder> object, int index );
136
137 // calculate and store all holes on meshes which allowed by isObjectValidToPick_ callback
138 void calculateHoles_();
139
141};
142
144{
145public:
146 ChangeBoundarySelectionHistoryAction( std::string name, BoundarySelectionWidget& widget, std::shared_ptr<ObjectMeshHolder> object, int index );
147
148public:
149 // HistoryAction
150 [[nodiscard]] std::string name() const override { return name_; }
151
152 void action( Type type ) override;
153
154 [[nodiscard]] size_t heapBytes() const override;
155
156private:
157 std::string name_;
159 std::shared_ptr<ObjectMeshHolder> prevSelectedHoleObject_;
160 std::shared_ptr<ObjectMeshHolder> nextSelectedHoleObject_;
161 int prevSelectedHoleIndex_;
162 int nextSelectedHoleIndex_;
163};
164
165} // namespace MR
int EdgeId
Definition MRDotNet/MRMeshFwd.h:52
Definition MRMeshBoundarySelectionWidget.h:23
MRVIEWER_API void enable(bool isEnabled)
MRVIEWER_API void create(BoundarySelectionWidgetCallBack onBoundarySelected, BoundarySelectionWidgetChecker isObjectValidToPick)
std::function< bool(std::shared_ptr< const MR::ObjectMeshHolder >)> BoundarySelectionWidgetChecker
Definition MRMeshBoundarySelectionWidget.h:40
std::unordered_map< std::shared_ptr< MR::ObjectMeshHolder >, boost::signals2::scoped_connection > PerObjectMeshChangedSignals
Definition MRMeshBoundarySelectionWidget.h:45
std::unordered_map< std::shared_ptr< MR::ObjectMeshHolder >, std::vector< AncillaryLines > > PerObjectHolesPolylines
Definition MRMeshBoundarySelectionWidget.h:44
MRVIEWER_API void clear()
MRVIEWER_API std::vector< MR::Vector3f > getPointsForSelectedHole() const
MRVIEWER_API std::pair< std::shared_ptr< MR::ObjectMeshHolder >, EdgeId > getSelectHole() const
BoundarySelectionWidgetParams params
Definition MRMeshBoundarySelectionWidget.h:80
std::function< void(std::shared_ptr< const MR::ObjectMeshHolder >)> BoundarySelectionWidgetCallBack
Definition MRMeshBoundarySelectionWidget.h:39
std::vector< MR::EdgeId > HolesOnObject
Definition MRMeshBoundarySelectionWidget.h:42
MRVIEWER_API void reset()
std::unordered_map< std::shared_ptr< MR::ObjectMeshHolder >, HolesOnObject > PerObjectHoles
Definition MRMeshBoundarySelectionWidget.h:43
MRVIEWER_API bool selectHole(std::shared_ptr< MR::ObjectMeshHolder > object, int index)
Definition MRMeshBoundarySelectionWidget.h:144
void action(Type type) override
This function is called on history action (undo, redo, etc.)
std::string name() const override
Definition MRMeshBoundarySelectionWidget.h:150
ChangeBoundarySelectionHistoryAction(std::string name, BoundarySelectionWidget &widget, std::shared_ptr< ObjectMeshHolder > object, int index)
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 MRCameraOrientationPlugin.h:7
MouseButton
Definition MRMouse.h:9
Helper class to manage ancillary visual lines used by plugins.
Definition MRAncillaryLines.h:13
Definition MRMeshBoundarySelectionWidget.h:26
Definition MRColor.h:9
static constexpr Color green() noexcept
Definition MRColor.h:31
static constexpr Color purple() noexcept
Definition MRColor.h:35
static constexpr Color gray() noexcept
Definition MRColor.h:29
Definition MRPickHoleBorderElement.h:11
Definition MRViewerEventsListener.h:29