MeshLib C++ Docs
Loading...
Searching...
No Matches
MRSurfacePointPicker.h
Go to the documentation of this file.
1#pragma once
2
4
5#include "MRMesh/MRMeshFwd.h"
6#include "MRMesh/MRVector3.h"
8#include <MRMesh/MRColor.h>
10
11#include <functional>
12
13namespace MR
14{
15
16// Widget for controlling point on surface with mouse
17class MRVIEWER_CLASS SurfacePointWidget : public MultiListener<PreDrawListener, MouseDownListener, MouseMoveListener, MouseUpListener>
18{
19public:
20 MRVIEWER_API ~SurfacePointWidget();
21
22 enum class PositionType
23 {
24 Faces, // point can be in any place of surface
25 FaceCenters, // point can be only in face center
26 Edges, // point can be only on edges
27 EdgeCenters, // point can be only in edge center
28 Verts // point can be only in vertex
29 };
30
32 {
33 enum class PointSizeType {
34 Metrical, // point size in mm
35 Pixel // point size in pixels (will automatically be multipled by the UI scale)
36 };
37 // type of point positioning, look at PositionType comments for more info
38 PositionType positionType{ PositionType::Faces };
39 // basic color of control sphere
40 Color baseColor{ Color::gray() };
41 // color of control sphere when it is hovered by mouse
42 Color hoveredColor{ Color::red() };
43 // color of control sphere when it is in move
44 Color activeColor{ { Color::red() } };
45 // how to set the size of the dots in mm or in pixels.
46 PointSizeType radiusSizeType{ PointSizeType::Pixel };
47 // Radius of control sphere. If <= 0.0f, uses a default value (a certain fraction of the AABB diagnoal of the target object for `radiusSizeType == Metrical`, or a fixed pixel size for `radiusSizeType == Pixel`).
48 // When `radiusSizeType == Pixel`, this is automatically multiplied by the current UI scale, you don't need to do that yourself.
49 float radius{ 0.0f };
50 // Typically, the widget does not respond to actions with a modifier.
51 // 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.
52 int customModifiers = 0; // GLFW modifier bitmask
53 // pick_render_object parameters. Allow to use object in which pick exactly fell, instead of closer object in pick radius.
54 bool pickInBackFaceObject = true;
55 };
56
57 // creates control sphere in start pos
58 // returns updated pos if it was moved according to PositionType
59 MRVIEWER_API const PickedPoint& create( const std::shared_ptr<VisualObject>& surface, const PointOnObject& startPos );
60 MRVIEWER_API const PickedPoint& create( const std::shared_ptr<VisualObject>& surface, const PickedPoint& startPos );
61
62 // resets whole widget
63 MRVIEWER_API void reset();
64 // returns object of control sphere
65 const std::shared_ptr<SphereObject>& getPickSphere() const
66 {
67 return pickSphere_;
68 }
69 // get current setup of this widget
71 {
72 return params_;
73 }
74
75 // set parameters for this widget
76 MRVIEWER_API void setParameters( const Parameters& params );
77
78 // set baseColor parameter for this widget
79 MRVIEWER_API void setBaseColor( const Color& color );
80
83 MRVIEWER_API void updateParameters( const std::function<void ( Parameters& )>& visitor );
84
85 // if auto hover is enabled, pick_render_object() is used
86 // !note: disabling it is useful if there are many widgets, not to call `pick_render_object()` for each of them separately
87 bool getAutoHover()const
88 {
89 return autoHover_;
90 }
91 void setAutoHover( bool on )
92 {
93 autoHover_ = on;
94 }
95 // function for manual enable and disable hover mode
96 // use it if auto hover is disabled
97 MRVIEWER_API void setHovered( bool on );
98
99 // returns stored position of this widget
101 {
102 return currentPos_;
103 }
104
106 MRVIEWER_API Vector3f getCoords() const;
107 [[deprecated]] Vector3f toVector3f() const { return getCoords(); }
108
110 std::optional<Vector3f> findCoords() const { return getPickedPointPosition( *baseObject_, currentPos_ ); }
111
113 std::optional<Vector3f> findNormal() const { return getPickedPointNormal( *baseObject_, currentPos_ ); }
114
117 {
118 if ( const MeshTriPoint* triPoint = std::get_if<MeshTriPoint>( &currentPos_ ) )
119 return *triPoint;
120 else
121 return {};
122 }
123
125 MRVIEWER_API void setCurrentPosition( const PointOnObject& pos );
126
128 MRVIEWER_API void setCurrentPosition( const PickedPoint& pos );
129
131 MRVIEWER_API void swapCurrentPosition( PickedPoint& pos );
132
133 // this callback is called when modification starts if it is set
134 void setStartMoveCallback( std::function<void( SurfacePointWidget &, const PickedPoint& )> startMove )
135 {
136 startMove_ = startMove;
137 }
138 // this callback is called on modification if it is set
139 void setOnMoveCallback( std::function<void( SurfacePointWidget &, const PickedPoint& )> onMove )
140 {
141 onMove_ = onMove;
142 }
143 // this callback is called when modification ends if it is set
144 void setEndMoveCallback( std::function<void( SurfacePointWidget &, const PickedPoint& )> endMove )
145 {
146 endMove_ = endMove;
147 }
148
149 std::shared_ptr<VisualObject>& getBaseSurface()
150 {
151 return baseObject_;
152 }
153
154 // returns whether is the widget moving
155 [[nodiscard]] bool isOnMove() const { return isOnMove_; }
156
157 // Checks whether the current peak is a peak in the invisible (reverse) side of the mesh or cloud point.
158 [[nodiscard]] static bool isPickIntoBackFace( const std::shared_ptr<MR::VisualObject>& obj, const MR::PointOnObject& pick, const Vector3f& cameraEye );
159
164
165private:
166 MRVIEWER_API virtual bool onMouseDown_( MouseButton button, int modifier ) override;
167 MRVIEWER_API virtual bool onMouseUp_( MouseButton button, int modifier ) override;
168 MRVIEWER_API virtual bool onMouseMove_( int mouse_x, int mouse_y ) override;
169
170 void updatePositionAndRadius_();
171 void updatePositionAndRadiusMesh_( MeshTriPoint mtp );
172
173 Parameters params_;
174
175 bool autoHover_{ true };
176 bool isOnMove_{ false };
177 bool isHovered_{ false };
178 MRVIEWER_API void preDraw_() override;
179
180 PickedPoint currentPos_;
181
182 std::shared_ptr<SphereObject> pickSphere_;
183 std::shared_ptr<VisualObject> baseObject_;
184
185 boost::signals2::scoped_connection onBaseObjectWorldXfChanged_;
186
187 std::function<void( SurfacePointWidget &, const PickedPoint& )> startMove_;
188 std::function<void( SurfacePointWidget &, const PickedPoint& )> onMove_;
189 std::function<void( SurfacePointWidget &, const PickedPoint& )> endMove_;
190
191 // Depending on the type of selected size, sets the point size
192 void setPointRadius_();
193
194 // set color of pick sphere from the current state and parameters
195 void setSphereColor_();
196};
197
198} //namespace MR
Faces
Definition MRObjectMeshHolder.h:13
Edges
Definition MRObjectMeshHolder.h:15
Definition MRSurfacePointPicker.h:18
void setOnMoveCallback(std::function< void(SurfacePointWidget &, const PickedPoint &)> onMove)
Definition MRSurfacePointPicker.h:139
const std::shared_ptr< SphereObject > & getPickSphere() const
Definition MRSurfacePointPicker.h:65
std::optional< Vector3f > findNormal() const
return the normal in local object's coordinates at the current position, or std::nullopt if it is inv...
Definition MRSurfacePointPicker.h:113
void setEndMoveCallback(std::function< void(SurfacePointWidget &, const PickedPoint &)> endMove)
Definition MRSurfacePointPicker.h:144
MRVIEWER_API void setCurrentPosition(const PickedPoint &pos)
sets new position for the widget
MRVIEWER_API void setCurrentPosition(const PointOnObject &pos)
sets new position for the widget
MRVIEWER_API void swapCurrentPosition(PickedPoint &pos)
sets new position for the widget and returns previous position in the argument
MRVIEWER_API void setHovered(bool on)
std::optional< Vector3f > findCoords() const
return local object's coordinates at the current position, or std::nullopt if it is invalid
Definition MRSurfacePointPicker.h:110
MRVIEWER_API void setBaseColor(const Color &color)
MeshTriPoint getCurrentPositionMeshTriPoint() const
returns stored position as MeshTriPoint, otherwise returns invalid (default) MeshTriPoint
Definition MRSurfacePointPicker.h:116
MRVIEWER_API void setParameters(const Parameters &params)
bool getAutoHover() const
Definition MRSurfacePointPicker.h:87
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:155
std::shared_ptr< VisualObject > & getBaseSurface()
Definition MRSurfacePointPicker.h:149
static bool isPickIntoBackFace(const std::shared_ptr< MR::VisualObject > &obj, const MR::PointOnObject &pick, const Vector3f &cameraEye)
const Parameters & getParameters() const
Definition MRSurfacePointPicker.h:70
MRVIEWER_API const PickedPoint & create(const std::shared_ptr< VisualObject > &surface, const PickedPoint &startPos)
MRVIEWER_API Vector3f getCoords() const
return local object's coordinates at the current position where the center of sphere is located
void setStartMoveCallback(std::function< void(SurfacePointWidget &, const PickedPoint &)> startMove)
Definition MRSurfacePointPicker.h:134
Vector3f toVector3f() const
Definition MRSurfacePointPicker.h:107
PositionType
Definition MRSurfacePointPicker.h:23
MRVIEWER_API void reset()
const PickedPoint & getCurrentPosition() const
Definition MRSurfacePointPicker.h:100
void setAutoHover(bool on)
Definition MRSurfacePointPicker.h:91
Definition MRCameraOrientationPlugin.h:8
MouseButton
Definition MRMouse.h:9
std::variant< std::monostate, MeshTriPoint, EdgePoint, VertId > PickedPoint
Definition MRPointOnObject.h:41
Definition MRMesh/MRColor.h:9
Definition MRMesh/MRMeshTriPoint.h:23
Definition MRViewerEventsListener.h:29
Definition MRPointOnObject.h:17
Definition MRSurfacePointPicker.h:32
PointSizeType
Definition MRSurfacePointPicker.h:33