MeshLib
 
Loading...
Searching...
No Matches
MRSurfacePointPicker.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRViewer.h"
5
6#include "MRMesh/MRMeshFwd.h"
7#include "MRMesh/MRVector3.h"
9#include <MRMesh/MRColor.h>
11
12#include <functional>
13#include <optional>
14
15namespace MR
16{
17
18// Widget for controlling point on surface with mouse
19class MRVIEWER_CLASS SurfacePointWidget : public MultiListener<PreDrawListener, MouseDownListener, MouseMoveListener, MouseUpListener>
20{
21public:
22 MRVIEWER_API ~SurfacePointWidget();
23
24 enum class PositionType
25 {
26 Faces, // point can be in any place of surface
27 FaceCenters, // point can be only in face center
28 Edges, // point can be only on edges
29 EdgeCeneters, // point can be only in edge center
30 Verts // point can be only in vertex
31 };
32
34 {
35 enum class PointSizeType {
36 Metrical, // point size in mm
37 Pixel // point size in pixels
38 };
39 // type of point positioning, look at PositionType comments for more info
40 PositionType positionType{ PositionType::Faces };
41 // basic color of control sphere
42 Color baseColor{ Color::gray() };
43 // color of control sphere when it is hovered by mouse
44 Color hoveredColor{ Color::red() };
45 // color of control sphere when it is in move
46 Color activeColor{ { Color::red() } };
47 // how to set the size of the dots in mm or in pixels.
48 PointSizeType radiusSizeType{ PointSizeType::Metrical };
49 // radius of control sphere, if <= 0.0f it is equal to 5e-3*box.diagonal()
50 float radius{ 0.0f };
51 // Typically, the widget does not respond to actions with a modifier.
52 // If the parameter is set, then custom modifiers located in this GLFW bitmask will be ignored and the widget will work with them as usual.
53 int customModifiers = 0; // GLFW modifier bitmask
54 // pick_render_object parameters. Allow to use object in which pick exactly fell, instead of closer object in pick radius.
55 bool pickInBackFaceObject = true;
56 };
57
58 // creates control sphere in start pos
59 // returns updated pos if it was moved according to PositionType
60 MRVIEWER_API const PickedPoint& create( const std::shared_ptr<VisualObject>& surface, const PointOnObject& startPos );
61 MRVIEWER_API const PickedPoint& create( const std::shared_ptr<VisualObject>& surface, const PickedPoint& startPos );
62
63 // resets whole widget
64 MRVIEWER_API void reset();
65 // returns object of control sphere
66 std::shared_ptr<SphereObject> getPickSphere() const
67 {
68 return pickSphere_;
69 }
70 // get current setup of this widget
72 {
73 return params_;
74 }
75 // set parameters for this widget
76 MRVIEWER_API void setParameters( const Parameters& params );
79 MRVIEWER_API void updateParameters( const std::function<void ( Parameters& )>& visitor );
80
81 // if auto hover is enabled, pick_render_object() is used
82 // !note: disabling it is useful if there are many widgets, not to call `pick_render_object()` for each of them separately
83 bool getAutoHover()const
84 {
85 return autoHover_;
86 }
87 void setAutoHover( bool on )
88 {
89 autoHover_ = on;
90 }
91 // function for manual enable and disable hover mode
92 // use it if auto hover is disabled
93 MRVIEWER_API void setHovered( bool on );
94
95 // returns stored position of this widget
97 {
98 return currentPos_;
99 }
100
101 // return current position transformed to Vector3f
102 MRVIEWER_API Vector3f toVector3f() const;
103
104 // returns stored position in MeshTriPointFormat if it is possible
105 std::optional<MeshTriPoint> getCurrentPositionMeshTriPoint() const
106 {
107 if ( const MeshTriPoint* triPoint = std::get_if<MeshTriPoint>( &currentPos_ ) )
108 return *triPoint;
109 else
110 return std::nullopt;
111 }
112
113 MRVIEWER_API void updateCurrentPosition( const PointOnObject& pos );
114 MRVIEWER_API void updateCurrentPosition( const PickedPoint& pos );
115
116 // this callback is called when modification starts if it is set
117 void setStartMoveCallback( std::function<void( const PickedPoint& )> startMove )
118 {
119 startMove_ = startMove;
120 }
121 // this callback is called on modification if it is set
122 void setOnMoveCallback( std::function<void( const PickedPoint& )> onMove )
123 {
124 onMove_ = onMove;
125 }
126 // this callback is called when modification ends if it is set
127 void setEndMoveCallback( std::function<void( const PickedPoint& )> endMove )
128 {
129 endMove_ = endMove;
130 }
131
132 std::shared_ptr<VisualObject>& getBaseSurface()
133 {
134 return baseObject_;
135 }
136
137 // returns whether is the widget moving
138 [[nodiscard]] bool isOnMove() const { return isOnMove_; }
139
140 // Checks whether the current peak is a peak in the invisible (reverse) side of the mesh or cloud point.
141 [[nodiscard]] static bool isPickIntoBackFace( const std::shared_ptr<MR::VisualObject>& obj, const MR::PointOnObject& pick, const Vector3f& cameraEye );
142
143private:
144 MRVIEWER_API virtual bool onMouseDown_( Viewer::MouseButton button, int modifier ) override;
145 MRVIEWER_API virtual bool onMouseUp_( Viewer::MouseButton button, int modifier ) override;
146 MRVIEWER_API virtual bool onMouseMove_( int mouse_x, int mouse_y ) override;
147
148 void updatePositionAndRadius_();
149 void updatePositionAndRadiusMesh_( MeshTriPoint mtp );
150 void updatePositionAndRadiusPoints_( const VertId& v );
151 void updatePositionAndRadiusLines_( const EdgePoint& ep );
152
153 Parameters params_;
154
155 bool autoHover_{ true };
156 bool isOnMove_{ false };
157 bool isHovered_{ false };
158 MRVIEWER_API void preDraw_() override;
159
160 PickedPoint currentPos_;
161
162 std::shared_ptr<SphereObject> pickSphere_;
163 std::shared_ptr<VisualObject> baseObject_;
164
165 boost::signals2::scoped_connection onBaseObjectWorldXfChanged_;
166
167 std::function<void( const PickedPoint& )> startMove_;
168 std::function<void( const PickedPoint& )> onMove_;
169 std::function<void( const PickedPoint& )> endMove_;
170
171 // Depending on the type of selected size, sets the point size
172 void setPointRadius_();
173
174};
175
176
177
178}
int VertId
Definition MRDotNet/MRMeshFwd.h:51
Faces
Definition MRObjectMeshHolder.h:12
Edges
Definition MRObjectMeshHolder.h:14
Definition MRSurfacePointPicker.h:20
void setOnMoveCallback(std::function< void(const PickedPoint &)> onMove)
Definition MRSurfacePointPicker.h:122
MRVIEWER_API void setHovered(bool on)
void setEndMoveCallback(std::function< void(const PickedPoint &)> endMove)
Definition MRSurfacePointPicker.h:127
MRVIEWER_API void updateCurrentPosition(const PickedPoint &pos)
std::optional< MeshTriPoint > getCurrentPositionMeshTriPoint() const
Definition MRSurfacePointPicker.h:105
void setStartMoveCallback(std::function< void(const PickedPoint &)> startMove)
Definition MRSurfacePointPicker.h:117
MRVIEWER_API void setParameters(const Parameters &params)
bool getAutoHover() const
Definition MRSurfacePointPicker.h:83
MRVIEWER_API ~SurfacePointWidget()
MRVIEWER_API const PickedPoint & create(const std::shared_ptr< VisualObject > &surface, const PointOnObject &startPos)
MRVIEWER_API void updateParameters(const std::function< void(Parameters &)> &visitor)
bool isOnMove() const
Definition MRSurfacePointPicker.h:138
std::shared_ptr< VisualObject > & getBaseSurface()
Definition MRSurfacePointPicker.h:132
static bool isPickIntoBackFace(const std::shared_ptr< MR::VisualObject > &obj, const MR::PointOnObject &pick, const Vector3f &cameraEye)
std::shared_ptr< SphereObject > getPickSphere() const
Definition MRSurfacePointPicker.h:66
const Parameters & getParameters() const
Definition MRSurfacePointPicker.h:71
MRVIEWER_API void updateCurrentPosition(const PointOnObject &pos)
MRVIEWER_API const PickedPoint & create(const std::shared_ptr< VisualObject > &surface, const PickedPoint &startPos)
PositionType
Definition MRSurfacePointPicker.h:25
MRVIEWER_API void reset()
const PickedPoint & getCurrentPosition() const
Definition MRSurfacePointPicker.h:96
void setAutoHover(bool on)
Definition MRSurfacePointPicker.h:87
MRVIEWER_API Vector3f toVector3f() const
represents a 3-dimentional float-typed vector
Definition MRDotNet/MRVector3.h:8
Definition MRCameraOrientationPlugin.h:7
MouseButton
Definition MRMouse.h:9
std::variant< MeshTriPoint, EdgePoint, VertId, int > PickedPoint
Definition MRPointOnObject.h:40
Definition MRColor.h:9
encodes a point on an edge of mesh or of polyline
Definition MREdgePoint.h:11
Definition MRMesh/MRMeshTriPoint.h:23
Definition MRViewerEventsListener.h:29
Definition MRPointOnObject.h:16
Definition MRSurfacePointPicker.h:34
PointSizeType
Definition MRSurfacePointPicker.h:35