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