MeshLib Documentation
Loading...
Searching...
No Matches
MRViewport.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRViewportGL.h"
4#include "MRFitData.h"
6#include <MRMesh/MRVector3.h>
7#include <MRMesh/MRPlane3.h>
10#include <MRMesh/MRQuaternion.h>
11#include <MRMesh/MRMatrix4.h>
12#include <MRMesh/MRColor.h>
13#include <MRMesh/MRBox.h>
14#include "imgui.h"
15#include <memory>
16#include <functional>
17#include <unordered_map>
18#include <optional>
19#include <span>
20
21using ObjAndPick = std::pair<std::shared_ptr<MR::VisualObject>, MR::PointOnObject>;
22using ConstObjAndPick = std::pair<std::shared_ptr<const MR::VisualObject>, MR::PointOnObject>;
23
24namespace MR
25{
26
27inline ImVec2 position( const ViewportRectangle& rect )
28{
29 return { rect.min.x, rect.min.y };
30}
31
32inline ImVec2 size( const ViewportRectangle& rect )
33{
34 return { width( rect ), height( rect ) };
35}
36
37// Conversion to Vector4 type, where:
38// x, y: min.x and min.y
39// z, w: width and height
40template<typename T>
41inline Vector4<T> toVec4( const ViewportRectangle& rect )
42{
43 return Vector4<T>{T( rect.min.x ), T( rect.min.y ), T( MR::width( rect ) ), T( MR::height( rect ) )};
44}
45
46// Viewport is a rectangular area, in which the objects of interest are going to be rendered.
47// An application can have a number of viewports each with its own ID.
49{
50public:
52
55 MRVIEWER_API static Viewport& get( ViewportId viewportId = {} );
56
57 MRVIEWER_API Viewport();
58 MRVIEWER_API ~Viewport();
59
60private: //hide copying from the user to avoid silly mistakes (copies are necessary only inside this project)
61 Viewport( const Viewport & ) = default;
62 Viewport & operator = ( const Viewport & ) = default;
63public:
64 [[nodiscard]] Viewport clone() const { return Viewport( *this ); }
65 Viewport( Viewport && ) noexcept = default;
66 Viewport & operator = ( Viewport && ) noexcept = default;
67
68 // Initialization
69 MRVIEWER_API void init();
70
71 // positive pixelXoffset -> offset from the left border
72 // negative pixelXoffset -> offset from the right border
73 // positive pixelYoffset -> offset from the top border
74 // negative pixelYoffset -> offset from the bottom border
75 // axisPixSize -> length of each axes arrows in pixels
76 // For example: default values -> right bottom corner with 100 offsets for the base point and 80 pixels each axis length
77 MRVIEWER_API void setAxesPos( const int pixelXoffset = -100, const int pixelYoffset = -100 );
78 MRVIEWER_API void setAxesSize( const int axisPixSize = 80 );
79
80 // Shutdown
81 MRVIEWER_API void shut();
82
83 // ------------------- Drawing functions
84
85 // Clear the frame buffers
86 MRVIEWER_API void clearFramebuffers();
87
90 MRVIEWER_API bool draw( const VisualObject& obj,
91 DepthFunction depthFunc = DepthFunction::Default, RenderModelPassMask pass = RenderModelPassMask::All, bool allowAlphaSort = false ) const;
92
95 MRVIEWER_API bool draw( const VisualObject& obj, const AffineXf3f& xf,
96 DepthFunction depthFunc = DepthFunction::Default, RenderModelPassMask pass = RenderModelPassMask::All, bool allowAlphaSort = false ) const;
97
100 MRVIEWER_API bool draw( const VisualObject& obj, const AffineXf3f& xf, const Matrix4f & projM,
101 DepthFunction depthFunc = DepthFunction::Default, RenderModelPassMask pass = RenderModelPassMask::All, bool allowAlphaSort = false ) const;
102
105 {
106 float width{1.0f};
107 bool depthTest{ true };
108 };
109
111 MRVIEWER_API void drawLines( const std::vector<LineSegm3f>& lines, const std::vector<SegmEndColors>& colors, const LinePointImmediateRenderParams & params );
112 void drawLines( const std::vector<LineSegm3f>& lines, const std::vector<SegmEndColors>& colors, float width = 1, bool depthTest = true )
113 { drawLines( lines, colors, { getBaseRenderParams(), width, depthTest } ); }
114
116 MRVIEWER_API void drawPoints( const std::vector<Vector3f>& points, const std::vector<Vector4f>& colors, const LinePointImmediateRenderParams & params );
117 void drawPoints( const std::vector<Vector3f>& points, const std::vector<Vector4f>& colors, float width = 1, bool depthTest = true )
118 { drawPoints( points, colors, { getBaseRenderParams(), width, depthTest } ); }
119
121 {
122 Vector4f a, b, c;
123 };
124
126 MRVIEWER_API void drawTris( const std::vector<Triangle3f>& tris, const std::vector<TriCornerColors>& colors, const ModelRenderParams& params, bool depthTest = true );
127 MRVIEWER_API void drawTris( const std::vector<Triangle3f>& tris, const std::vector<TriCornerColors>& colors, const Matrix4f& modelM = {}, bool depthTest = true );
128
130 [[nodiscard]] BaseRenderParams getBaseRenderParams() const { return getBaseRenderParams( projM_ ); }
131
133 [[nodiscard]] BaseRenderParams getBaseRenderParams( const Matrix4f & projM ) const
134 { return { viewM_, projM, id, toVec4<int>( viewportRect_ ) }; }
135
138 const Matrix4f & modelM,
139 Matrix4f * normM,
142 bool allowAlphaSort = false
143 ) const
144 { return getModelRenderParams( modelM, projM_, normM, depthFunc, pass, allowAlphaSort ); }
145
147 [[nodiscard]] MRVIEWER_API ModelRenderParams getModelRenderParams( const Matrix4f & modelM, const Matrix4f & projM,
148 Matrix4f * normM,
151 bool allowAlphaSort = false
152 ) const;
153
154 // Predicate to additionally filter objects that should be treated as pickable.
155 using PickRenderObjectPredicate = std::function<bool ( const VisualObject*, ViewportMask )>;
156 // Point picking parameters.
158 {
159 // If specified, this is the target screen point. Otherwise use the mouse pos in viewport coordinates.
160 std::optional<Vector2f> point;
161
162 // Predicate to additionally filter objects that should be treated as pickable.
164
165 // Radius (in pixels) of a picking area.
166 // <0 defaults to `getViewerInstance().glPickRadius`.
167 int pickRadius = -1;
168 // Usually, from several objects that fall into the peak, the closest one along the ray is selected. However,
169 // if exactPickFirst = true, then the object in which the pick exactly fell (for example, a point in point cloud)
170 // will be returned as the result, even if there are others within the radius, including closer objects.
171 bool exactPickFirst = true;
172
173 // This will always return `{}`. We need the functions because `= {}`
174 // can't be used directly inside default arguments in the same class.
175 // You don't have to use this function.
177 {
178 return {};
179 }
180 };
181 // This function allows to pick point in scene by GL with given parameters.
182 // This overload uses all objects in the scene (possibly filtered by a predicate).
184 // This overload uses objects from the list (possibly filtered by a predicate).
185 MRVIEWER_API ObjAndPick pickRenderObject( std::span<VisualObject* const> objects, const PickRenderObjectParams& params = PickRenderObjectParams::defaults() ) const;
186 // This overload uses const objects from the list (possibly filtered by a predicate). Sadly need a different name to avoid over resolution issues.
187 MRVIEWER_API ConstObjAndPick pickRenderObjectConst( std::span<const VisualObject* const> objects, const PickRenderObjectParams& params = PickRenderObjectParams::defaults() ) const;
188
189 // This function allows to pick point in scene by GL
190 // use default pick radius
191 // comfortable usage:
192 // auto [obj,pick] = pick_render_object();
193 // pick all visible and pickable objects
194 // picks objects from current mouse pose by default
195 // [[deprecated("Use `pickRenderObject()`")]] // Should eventually deprecate this?
196 MRVIEWER_API ObjAndPick pick_render_object() const;
197 [[deprecated("Use `pickRenderObject()`")]]
198 MRVIEWER_API ObjAndPick pick_render_object( uint16_t pickRadius ) const;
199 // This function allows to pick point in scene by GL
200 // use default pick radius
201 // comfortable usage:
202 // auto [obj,pick] = pick_render_object( objects );
203 // pick objects from input
204 [[deprecated("Use `pickRenderObject( objects } )`")]] // NOTE! If your list is hardcoded, use `.objects = std::array{ a, b, c }`.
205 MRVIEWER_API ObjAndPick pick_render_object( const std::vector<VisualObject*>& objects ) const;
206 // This function allows to pick point in scene by GL with a given peak radius.
207 // usually, from several objects that fall into the peak, the closest one along the ray is selected.However
208 // if exactPickFirst = true, then the object in which the pick exactly fell( for example, a point in point cloud )
209 // will be returned as the result, even if there are others within the radius, including closer objects.
210 [[deprecated("Use `pickRenderObject( ... )`")]]
211 MRVIEWER_API ObjAndPick pick_render_object( const std::vector<VisualObject*>& objects, uint16_t pickRadius, bool exactPickFirst = true ) const;
212 // This function allows to pick point in scene by GL with default pick radius, but with specified exactPickFirst parameter (see description upper).
213 [[deprecated("Use `pickRenderObject( { .exactPickFirst = ... } )`")]]
214 MRVIEWER_API ObjAndPick pick_render_object( bool exactPickFirst ) const;
215 // This function allows to pick point in scene by GL
216 // comfortable usage:
217 // auto [obj,pick] = pick_render_object( objects );
218 // pick all visible and pickable objects
219 // picks objects from custom viewport point
220 [[deprecated("Use `pickRenderObject( { .point = ... } )`")]]
221 MRVIEWER_API ObjAndPick pick_render_object( const Vector2f& viewportPoint ) const;
222 // This function allows to pick point in scene by GL
223 // comfortable usage:
224 // auto [obj,pick] = pick_render_object( objects );
225 // picks objects from custom viewport point
226 [[deprecated("Use `multiPickObjects( objects, { .point = ... } )`")]]
227 MRVIEWER_API ObjAndPick pick_render_object( const std::vector<VisualObject*>& objects, const Vector2f& viewportPoint ) const;
228
229 // This function allows to pick several custom viewport space points by GL
230 // returns vector of pairs [obj,pick]
231 // To hardcode the list of `objects`, use `{{ a, b, c }}`.
232 MRVIEWER_API std::vector<ObjAndPick> multiPickObjects( std::span<VisualObject* const> objects, const std::vector<Vector2f>& viewportPoints ) const;
233
234 // This function finds all visible objects in given rect (max excluded) in viewport space,
235 // maxRenderResolutionSide - this parameter limits render resolution to improve performance
236 // if it is too small, little objects can be lost
237 // viewport space: X [0,viewport_width], Y [0,viewport_height] - (0,0) is upper left of viewport
238 MRVIEWER_API std::vector<std::shared_ptr<VisualObject>> findObjectsInRect( const Box2i& rect,
239 int maxRenderResolutionSide = 512 ) const;
240
241 // This functions finds all visible faces in given includePixBs in viewport space,
242 // maxRenderResolutionSide - this parameter limits render resolution to improve performance
243 // if it is too small, little faces can be lost
244 // viewport space: X [0,viewport_width], Y [0,viewport_height] - (0,0) is upper left of viewport
245 MRVIEWER_API std::unordered_map<std::shared_ptr<ObjectMesh>, FaceBitSet> findVisibleFaces( const BitSet& includePixBs,
246 int maxRenderResolutionSide = 512 ) const;
247
248 // This function allows to pick point in scene by GL
249 // comfortable usage:
250 // const auto [obj,pick] = pick_render_object();
251 // pick all visible and pickable objects
252 // picks objects from current mouse pose by default
253 [[deprecated("Use `pickRenderObject()`.")]]
255 // This function allows to pick point in scene by GL
256 // comfortable usage:
257 // const auto [obj,pick] = pick_render_object( objects );
258 // pick objects from input
259 [[deprecated("Use `pickRenderObject( objects )`.")]]
260 MRVIEWER_API ConstObjAndPick const_pick_render_object( const std::vector<const VisualObject*>& objects ) const;
261 // This function allows to pick several custom viewport space points by GL
262 // returns vector of pairs [obj,pick]
263 MRVIEWER_API std::vector<ConstObjAndPick> constMultiPickObjects( const std::vector<const VisualObject*>& objects, const std::vector<Vector2f>& viewportPoints ) const;
264
265 // multiplies view-matrix on given transformation from the _right_;
266 // so if you at the same time multiplies model transformation from the _left_ on xf.inverse()
267 // and call transformView( xf ), then the user will see exactly the same picture
268 MRVIEWER_API void transformView( const AffineXf3f & xf );
269
270 bool getRedrawFlag() const { return needRedraw_; }
271 void resetRedrawFlag() { needRedraw_ = false; }
272
273 // Unique identifier
275
277 {
278 Color backgroundColor = Color( Vector3f{0.3f, 0.3f, 0.5f} );
279 Vector3f lightPosition{0.0f, 0.3f, 0.0f};// x y z - position, w - factor
280
283 float cameraZoom{1.0f};
284 float cameraViewAngle{45.0f};
285 float cameraDnear{1.0f};
286 float cameraDfar{100.0f};
287
288 bool depthTest{true};
289 bool orthographic{true};
290
292 {
293 Auto, // uses current scene size
294 Fixed // uses global basis object internal size (one can change it with globalBasisAxes->setXf( AffineXf3f::linear( Matrix3f::scale( size ) ) ) )
295 } globalBasisScaleMode{ GlobalBasisScaleMode::Auto };
296
297 // Caches the two-norm between the min/max point of the bounding box
298 float objectScale{1.0f};
299
301
302 std::string label;
303
304 Plane3f clippingPlane{Vector3f::plusX(), 0.0f};
305
307 {
308 Static, // scene is always rotated around its center
309 DynamicStatic, // scene is rotated around picked point on object, or around center, if miss pick
310 Dynamic // scene is rotated around picked point on object, or around last rotation pivot, if miss pick
312
313 // this flag allows viewport to be selected by user
314 bool selectable{true};
315
316 bool operator==( const Viewport::Parameters& other ) const = default;
317 };
318
319 // Starts or stop rotation
320 MRVIEWER_API void setRotation( bool state );
321
322 // Note, Y is up for this box.
323 MRVIEWER_API const ViewportRectangle& getViewportRect() const;
324
325 // Finds length between near pixels on zNear plane. Only good in the orthographic projection.
326 MRVIEWER_API float getPixelSize() const;
327
328 // Finds the pixel scale at a specific world point. This works in both perspective and orthographic projection.
329 MRVIEWER_API float getPixelSizeAtPoint( const Vector3f& worldPoint ) const;
330
331 // Sets position and size of viewport:
332 // rect is given as OpenGL coordinates: (0,0) is lower left corner
333 MRVIEWER_API void setViewportRect( const ViewportRectangle& rect );
334
335private:
336 // Save the OpenGL transformation matrices used for the previous rendering pass
337 Matrix4f viewM_;
338 Matrix4f projM_;
339
340public:
342 [[nodiscard]] MRVIEWER_API AffineXf3f getUnscaledViewXf() const;
343
345 [[nodiscard]] AffineXf3f getViewXf() const { return AffineXf3f( viewM_ ); }
346
348 [[nodiscard]] Vector3f getUpDirection() const { return Vector3f( viewM_.y.x, viewM_.y.y, viewM_.y.z ).normalized(); } // assume that viewM is orthogonal and inverse=transpose
349
351 [[nodiscard]] Vector3f getRightDirection() const { return Vector3f( viewM_.x.x, viewM_.x.y, viewM_.x.z ).normalized(); } // assume that viewM is orthogonal and inverse=transpose
352
354 [[nodiscard]] Vector3f getBackwardDirection() const { return Vector3f( viewM_.z.x, viewM_.z.y, viewM_.z.z ).normalized(); } // assume that viewM is orthogonal and inverse=transpose
355
356 // returns the line from Dnear to Dfar planes for the current pixel
357 // viewport space: X [0,viewport_width], Y [0,viewport_height] - (0,0) is upper left of viewport
358 // Z [0.f,1.f] - 0.f is Dnear, 1.f is Dfar
359 MRVIEWER_API Line3f unprojectPixelRay( const Vector2f& viewportPoint ) const;
360
361 // convert point(s) to camera space by applying view matrix
362 MRVIEWER_API Vector3f worldToCameraSpace( const Vector3f& p ) const;
363 MRVIEWER_API std::vector<Vector3f> worldToCameraSpace( const std::vector<Vector3f>& p ) const;
364
365 // projects point(s) to clip space. (rather, to normalized device coordinates, as it includes perspective division)
366 // clip space: XYZ [-1.f, 1.f], X axis from left(-1.f) to right(1.f), Y axis from bottom(-1.f) to top(1.f),
367 // Z axis from Dnear(-1.f) to Dfar(1.f)
368 MRVIEWER_API Vector3f projectToClipSpace( const Vector3f& worldPoint ) const;
369 MRVIEWER_API std::vector<Vector3f> projectToClipSpace( const std::vector<Vector3f>& worldPoints ) const;
370 MRVIEWER_API Vector3f unprojectFromClipSpace( const Vector3f& clipPoint ) const;
371 MRVIEWER_API std::vector<Vector3f> unprojectFromClipSpace( const std::vector<Vector3f>& clipPoints ) const;
372
373 // project point and convert coordinates to viewport space
374 // viewport space: X [0,viewport_width], Y [0,viewport_height] - (0,0) is upper left of viewport
375 // Z [0.f,1.f] - 0.f is Dnear, 1.f is Dfar
376 MRVIEWER_API Vector3f projectToViewportSpace( const Vector3f& worldPoint ) const;
377 MRVIEWER_API std::vector<Vector3f> projectToViewportSpace( const std::vector<Vector3f>& worldPoints ) const;
378 // unproject coordinates from viewport space
379 // viewport space: X [0,viewport_width], Y [0,viewport_height] - (0,0) is upper left of viewport
380 // Z [0.f,1.f] - 0.f is Dnear, 1.f is Dfar
381 MRVIEWER_API Vector3f unprojectFromViewportSpace( const Vector3f& viewportPoint ) const;
382 MRVIEWER_API std::vector<Vector3f> unprojectFromViewportSpace( const std::vector<Vector3f>& viewportPoints ) const;
383
384 // conversations between clip space and viewport space
385 // clip space: XYZ [-1.f, 1.f], X axis from left(-1.f) to right(1.f), Y axis from bottom(-1.f) to top(1.f),
386 // Z axis from Dnear(-1.f) to Dfar(1.f)
387 // viewport space: X [0,viewport_width], Y [0,viewport_height] - (0,0) is upper left of viewport
388 // Z [0.f,1.f] - 0.f is Dnear, 1.f is Dfar
389 MRVIEWER_API Vector3f clipSpaceToViewportSpace( const Vector3f& p ) const;
390 MRVIEWER_API std::vector<Vector3f> clipSpaceToViewportSpace( const std::vector<Vector3f>& p ) const;
391 MRVIEWER_API Vector3f viewportSpaceToClipSpace( const Vector3f& p ) const;
392 MRVIEWER_API std::vector<Vector3f> viewportSpaceToClipSpace( const std::vector<Vector3f>& p ) const;
393
394 // updates view and projection matrices due to camera parameters (called each frame)
395 void setupView();
396 // draws viewport primitives:
397 // lines: if depth test is on
398 // points: if depth test is on
399 // rotation center
400 // global basis
401 void preDraw();
402 // draws viewport primitives:
403 // lines: if depth test is off
404 // points: if depth test is off
405 // viewport border
406 // overlay basis
407 void postDraw() const;
408
409 // fit camera to the scene box (note: scene box does not include ancillary objects)
410 // fill = 1.0 parameter means that scene will be approximately 0.5 of screen
411 // snapView - to snap camera angle to closest canonical quaternion
412 MRVIEWER_API void fitData( float fill = 1.0f, bool snapView = true );
413
414 // set scene box by given one and fit camera to it
415 // fill = 1.0 parameter means that box diagonal will be approximately 0.5 of the viewport
416 // snapView - to snap camera angle to closest canonical quaternion
417 MRVIEWER_API void fitBox( const Box3f& newSceneBox, float fill = 1.0f, bool snapView = true );
418
423
424 // fit view and proj matrices to match the screen size with given box
425 MRVIEWER_API void preciseFitBoxToScreenBorder( const FitBoxParams& params );
426 // fit view and proj matrices to match the screen size with given objects
427 MRVIEWER_API void preciseFitDataToScreenBorder( const FitDataParams& params = {} );
428
429 // returns viewport width/height ratio
430 MRVIEWER_API float getRatio() const;
431
432 // returns true if all models are fully projected inside the viewport rectangle
433 MRVIEWER_API bool allModelsInsideViewportRectangle() const;
434
435 MRVIEWER_API const Box3f& getSceneBox() const;
436
437 const Parameters& getParameters() const { return params_; }
438
439 // returns camera world location for the current view
440 MRVIEWER_API Vector3f getCameraPoint() const;
441 // sets camera world location for the current view
442 MRVIEWER_API void setCameraPoint( const Vector3f& cameraWorldPos );
443
444 MRVIEWER_API void setCameraTrackballAngle( const Quaternionf& rot );
445
446 MRVIEWER_API void setCameraTranslation( const Vector3f& translation );
447
448 MRVIEWER_API void setCameraViewAngle( float newViewAngle );
449
450 MRVIEWER_API void setCameraZoom( float zoom );
451
452 MRVIEWER_API void setOrthographic( bool orthographic );
453
454 MRVIEWER_API void setBackgroundColor( const Color& color );
455
456 MRVIEWER_API void setClippingPlane( const Plane3f& plane );
457
458 MRVIEWER_API void setLabel( std::string s );
459
460 void setSelectable( bool on ) { params_.selectable = on; }
461
462 MRVIEWER_API void showAxes( bool on );
463 MRVIEWER_API void showClippingPlane( bool on );
464 MRVIEWER_API void showRotationCenter( bool on );
465 MRVIEWER_API void showGlobalBasis( bool on );
467
468 MRVIEWER_API void setParameters( const Viewport::Parameters& params );
469
470 // Set camera look direction and up direction (they should be perpendicular)
471 // this function changes camera position and do not change camera spot (0,0,0) by default
472 // to change camera position use setCameraTranslation after this function
473 MRVIEWER_API void cameraLookAlong( const Vector3f& dir, const Vector3f& up );
474
475 // Rotates camera around axis +direction applied to axis point
476 // note: this can make camera clip objects (as far as distance to scene center is not fixed)
477 MRVIEWER_API void cameraRotateAround( const Line3f& axis, float angle );
478
479 // Get current rotation pivot in world space
480 MRVIEWER_API Vector3f getRotationPivot() const;
481
482private:
483 // initializes view matrix based on camera position
484 void setupViewMatrix_();
485 // returns world space to camera space transformation
486 AffineXf3f getViewXf_() const;
487
488 // initializes proj matrix based on camera angle and viewport rectangle size
489 void setupProjMatrix_();
490 // initializes proj matrix for static view objects (like corner axes)
491 void setupStaticProjMatrix_();
492
493 // use this matrix to convert world 3d point to clip point
494 // clip space: XYZ [-1.f, 1.f], X axis from left(-1.f) to right(1.f), X axis from bottom(-1.f) to top(1.f),
495 // Z axis from Dnear(-1.f) to Dfar(1.f)
496 Matrix4f getFullViewportMatrix() const { return projM_ * viewM_; }
497 Matrix4f getFullViewportInversedMatrix() const;
498
499 ViewportRectangle viewportRect_;
500
501 ViewportGL viewportGL_;
502
503 bool previewLinesDepthTest_ = false;
504 bool previewPointsDepthTest_ = false;
505
506 void draw_border() const;
507 void draw_rotation_center() const;
508 void draw_clipping_plane() const;
509 void draw_global_basis() const;
510
511 // init basis axis in the corner
512 void initBaseAxes();
513 // Drawing basis axes in the corner
514 void drawAxes() const;
515 // This matrix should be used for a static objects
516 // For example, basis axes in the corner
517 Matrix4f staticProj_;
518 Vector3f relPoseBase;
519 Vector3f relPoseSide;
520
521 // basis axis params
522 int pixelXoffset_{ -100 };
523 int pixelYoffset_{ -100 };
524 int axisPixSize_{ 70 };
525
526 // Receives point in scene coordinates, that should appear static on a screen, while rotation
527 void setRotationPivot_( const Vector3f& point );
528 void updateSceneBox_();
529 void rotateView_();
530
531 enum class Space
532 {
533 World, // (x, y, z) in world space
534 CameraOrthographic, // (x, y, z) in camera space
535 CameraPerspective // (x/z, y/z, z), where (x, y, z) in camera space
536 };
537
542 Box3f calcBox_( const std::vector<std::shared_ptr<VisualObject>>& objs, Space space, bool selectedPrimitives = false ) const;
543
551 std::pair<float, bool> getZoomFOVtoScreen_( std::function<Box3f()> getBoxFn, Vector3f* cameraShift = nullptr ) const;
552 // fit view and proj matrices to match the screen size with boxes returned by getBoxFn
553 // getBoxFn( true ) - always camera space (respecting projection)
554 // getBoxFn( false ) - if orthographic - camera space, otherwise - world space
555 // getBoxFn/globalBasis - if true then getBoxFn should return box of global basis object (separetely, not to interfere with actual scene size)
556 void preciseFitToScreenBorder_( std::function<Box3f( bool zoomFOV, bool globalBasis )> getBoxFn, const BaseFitParams& params );
557
558 bool rotation_{ false };
559 Vector3f rotationPivot_;
560 Vector3f static_point_;
561 Vector2f static_viewport_point;
562 float distToSceneCenter_;
563
564 bool needRedraw_{false};
565
566 // world bounding box of scene objects visible in this viewport
567 Box3f sceneBox_;
568
569 Parameters params_;
570};
571
572} //namespace MR
std::pair< std::shared_ptr< const MR::VisualObject >, MR::PointOnObject > ConstObjAndPick
Definition MRViewport.h:22
std::pair< std::shared_ptr< MR::VisualObject >, MR::PointOnObject > ObjAndPick
Definition MRViewport.h:21
container of bits
Definition MRMesh/MRBitSet.h:27
Definition MRViewportGL.h:53
Definition MRViewportId.h:16
stores mask of viewport unique identifiers
Definition MRViewportId.h:38
Definition MRViewport.h:49
MRVIEWER_API Vector3f projectToClipSpace(const Vector3f &worldPoint) const
MRVIEWER_API void showRotationCenter(bool on)
MRVIEWER_API ConstObjAndPick const_pick_render_object() const
MRVIEWER_API Vector3f projectToViewportSpace(const Vector3f &worldPoint) const
MRVIEWER_API void setCameraViewAngle(float newViewAngle)
MRVIEWER_API void setCameraZoom(float zoom)
MRVIEWER_API ObjAndPick pick_render_object(const std::vector< VisualObject * > &objects, uint16_t pickRadius, bool exactPickFirst=true) const
MRVIEWER_API void shut()
MR::BaseFitParams BaseFitParams
Definition MRViewport.h:420
MRVIEWER_API ObjAndPick pick_render_object(const std::vector< VisualObject * > &objects, const Vector2f &viewportPoint) const
Vector3f getRightDirection() const
returns unit vector in world space corresponding to right-direction in camera space
Definition MRViewport.h:351
ModelRenderParams getModelRenderParams(const Matrix4f &modelM, Matrix4f *normM, DepthFunction depthFunc=DepthFunction::Default, RenderModelPassMask pass=RenderModelPassMask::All, bool allowAlphaSort=false) const
Prepares rendering parameters to draw a model with given transformation in this viewport.
Definition MRViewport.h:137
MRVIEWER_API ~Viewport()
MRVIEWER_API std::vector< Vector3f > unprojectFromViewportSpace(const std::vector< Vector3f > &viewportPoints) const
MRVIEWER_API void cameraLookAlong(const Vector3f &dir, const Vector3f &up)
MRVIEWER_API Vector3f unprojectFromClipSpace(const Vector3f &clipPoint) const
MRVIEWER_API ObjAndPick pick_render_object(uint16_t pickRadius) const
MRVIEWER_API ConstObjAndPick pickRenderObjectConst(std::span< const VisualObject *const > objects, const PickRenderObjectParams &params=PickRenderObjectParams::defaults()) const
MRVIEWER_API void showGlobalBasis(bool on)
std::function< bool(const VisualObject *, ViewportMask)> PickRenderObjectPredicate
Definition MRViewport.h:155
MRVIEWER_API void cameraRotateAround(const Line3f &axis, float angle)
MRVIEWER_API ModelRenderParams getModelRenderParams(const Matrix4f &modelM, const Matrix4f &projM, Matrix4f *normM, DepthFunction depthFunc=DepthFunction::Default, RenderModelPassMask pass=RenderModelPassMask::All, bool allowAlphaSort=false) const
Prepares rendering parameters to draw a model with given transformation in this viewport with custom ...
MRVIEWER_API void setCameraTranslation(const Vector3f &translation)
MRVIEWER_API ObjAndPick pickRenderObject(const PickRenderObjectParams &params=PickRenderObjectParams::defaults()) const
bool getRedrawFlag() const
Definition MRViewport.h:270
BaseRenderParams getBaseRenderParams(const Matrix4f &projM) const
Prepares base rendering parameters for this viewport with custom projection matrix.
Definition MRViewport.h:133
MRVIEWER_API float getRatio() const
void setSelectable(bool on)
Definition MRViewport.h:460
MRVIEWER_API float getPixelSize() const
MRVIEWER_API ConstObjAndPick const_pick_render_object(const std::vector< const VisualObject * > &objects) const
MRVIEWER_API Vector3f unprojectFromViewportSpace(const Vector3f &viewportPoint) const
void drawLines(const std::vector< LineSegm3f > &lines, const std::vector< SegmEndColors > &colors, float width=1, bool depthTest=true)
Definition MRViewport.h:112
MRVIEWER_API void init()
MRVIEWER_API float getPixelSizeAtPoint(const Vector3f &worldPoint) const
void resetRedrawFlag()
Definition MRViewport.h:271
MRVIEWER_API void preciseFitDataToScreenBorder(const FitDataParams &params={})
MRVIEWER_API Vector3f viewportSpaceToClipSpace(const Vector3f &p) const
MR::ViewportRectangle ViewportRectangle
Definition MRViewport.h:51
MRVIEWER_API void setRotation(bool state)
MRVIEWER_API void setParameters(const Viewport::Parameters &params)
MRVIEWER_API std::vector< Vector3f > clipSpaceToViewportSpace(const std::vector< Vector3f > &p) const
MRVIEWER_API void setOrthographic(bool orthographic)
AffineXf3f getViewXf() const
converts directly from the view matrix
Definition MRViewport.h:345
void preDraw()
MRVIEWER_API void showClippingPlane(bool on)
MRVIEWER_API const ViewportRectangle & getViewportRect() const
MRVIEWER_API void fitBox(const Box3f &newSceneBox, float fill=1.0f, bool snapView=true)
MRVIEWER_API void showAxes(bool on)
MRVIEWER_API std::vector< Vector3f > worldToCameraSpace(const std::vector< Vector3f > &p) const
MRVIEWER_API Vector3f getCameraPoint() const
MRVIEWER_API void drawTris(const std::vector< Triangle3f > &tris, const std::vector< TriCornerColors > &colors, const Matrix4f &modelM={}, bool depthTest=true)
MRVIEWER_API void drawTris(const std::vector< Triangle3f > &tris, const std::vector< TriCornerColors > &colors, const ModelRenderParams &params, bool depthTest=true)
Draw triangles immediately (flat shaded)
MRVIEWER_API void setCameraTrackballAngle(const Quaternionf &rot)
MRVIEWER_API ObjAndPick pick_render_object(const std::vector< VisualObject * > &objects) const
Vector3f getBackwardDirection() const
returns unit vector in world space corresponding to direction toward camera in camera space
Definition MRViewport.h:354
MRVIEWER_API Line3f unprojectPixelRay(const Vector2f &viewportPoint) const
MRVIEWER_API std::vector< Vector3f > unprojectFromClipSpace(const std::vector< Vector3f > &clipPoints) const
Viewport clone() const
Definition MRViewport.h:64
MRVIEWER_API void setLabel(std::string s)
MRVIEWER_API std::unordered_map< std::shared_ptr< ObjectMesh >, FaceBitSet > findVisibleFaces(const BitSet &includePixBs, int maxRenderResolutionSide=512) const
const Parameters & getParameters() const
Definition MRViewport.h:437
MRVIEWER_API std::vector< Vector3f > viewportSpaceToClipSpace(const std::vector< Vector3f > &p) const
MRVIEWER_API std::vector< Vector3f > projectToViewportSpace(const std::vector< Vector3f > &worldPoints) const
MRVIEWER_API void fitData(float fill=1.0f, bool snapView=true)
MRVIEWER_API void setAxesSize(const int axisPixSize=80)
MRVIEWER_API void drawPoints(const std::vector< Vector3f > &points, const std::vector< Vector4f > &colors, const LinePointImmediateRenderParams &params)
Draw points immediately.
MRVIEWER_API const Box3f & getSceneBox() const
static MRVIEWER_API Viewport & get(ViewportId viewportId={})
MRVIEWER_API std::vector< ObjAndPick > multiPickObjects(std::span< VisualObject *const > objects, const std::vector< Vector2f > &viewportPoints) const
void setupView()
MRVIEWER_API ObjAndPick pickRenderObject(std::span< VisualObject *const > objects, const PickRenderObjectParams &params=PickRenderObjectParams::defaults()) const
MRVIEWER_API void setViewportRect(const ViewportRectangle &rect)
Viewport(Viewport &&) noexcept=default
MRVIEWER_API void transformView(const AffineXf3f &xf)
MRVIEWER_API Vector3f worldToCameraSpace(const Vector3f &p) const
MRVIEWER_API ObjAndPick pick_render_object(bool exactPickFirst) const
MRVIEWER_API std::vector< ConstObjAndPick > constMultiPickObjects(const std::vector< const VisualObject * > &objects, const std::vector< Vector2f > &viewportPoints) const
MRVIEWER_API bool draw(const VisualObject &obj, DepthFunction depthFunc=DepthFunction::Default, RenderModelPassMask pass=RenderModelPassMask::All, bool allowAlphaSort=false) const
MRVIEWER_API AffineXf3f getUnscaledViewXf() const
returns orthonormal matrix with translation
MRVIEWER_API void setClippingPlane(const Plane3f &plane)
MRVIEWER_API void preciseFitBoxToScreenBorder(const FitBoxParams &params)
MRVIEWER_API bool allModelsInsideViewportRectangle() const
void postDraw() const
BaseRenderParams getBaseRenderParams() const
Prepares base rendering parameters for this viewport.
Definition MRViewport.h:130
MRVIEWER_API void drawLines(const std::vector< LineSegm3f > &lines, const std::vector< SegmEndColors > &colors, const LinePointImmediateRenderParams &params)
Draw lines immediately.
void drawPoints(const std::vector< Vector3f > &points, const std::vector< Vector4f > &colors, float width=1, bool depthTest=true)
Definition MRViewport.h:117
MRVIEWER_API Viewport()
MRVIEWER_API void setBackgroundColor(const Color &color)
MRVIEWER_API Vector3f getRotationPivot() const
MRVIEWER_API std::vector< std::shared_ptr< VisualObject > > findObjectsInRect(const Box2i &rect, int maxRenderResolutionSide=512) const
MRVIEWER_API void setAxesPos(const int pixelXoffset=-100, const int pixelYoffset=-100)
MRVIEWER_API void clearFramebuffers()
MRVIEWER_API ObjAndPick pick_render_object(const Vector2f &viewportPoint) const
MRVIEWER_API void setCameraPoint(const Vector3f &cameraWorldPos)
MRVIEWER_API Vector3f clipSpaceToViewportSpace(const Vector3f &p) const
ViewportId id
Definition MRViewport.h:274
MRVIEWER_API std::vector< Vector3f > projectToClipSpace(const std::vector< Vector3f > &worldPoints) const
MRVIEWER_API void rotationCenterMode(Parameters::RotationCenterMode mode)
Vector3f getUpDirection() const
returns unit vector in world space corresponding to up-direction in camera space
Definition MRViewport.h:348
MRVIEWER_API ObjAndPick pick_render_object() const
Visual Object.
Definition MRVisualObject.h:131
auto width(const Box< V > &box)
returns size along x axis
Definition MRMesh/MRBox.h:247
auto height(const Box< V > &box)
returns size along y axis
Definition MRMesh/MRBox.h:254
Definition MRCameraOrientationPlugin.h:8
FitMode
Definition MRFitData.h:11
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
RenderModelPassMask
Various passes of the 3D rendering.
Definition MRRenderModelParameters.h:10
Vector4< T > toVec4(const ViewportRectangle &rect)
Definition MRViewport.h:41
DepthFunction
Definition MRIRenderObject.h:18
Box2f ViewportRectangle
Viewport size.
Definition MRViewerFwd.h:12
Definition MRFitData.h:19
Common rendering parameters for meshes and UI.
Definition MRIRenderObject.h:33
Definition MRColor.h:9
Definition MRFitData.h:41
Definition MRFitData.h:28
Mesh rendering parameters for primary rendering (as opposed to the picker).
Definition MRIRenderObject.h:51
Definition MRPointOnObject.h:16
Definition MRVector4.h:13
Rendering parameters for immediate drawing of lines and points.
Definition MRViewport.h:105
Definition MRViewport.h:277
RotationCenterMode
Definition MRViewport.h:307
float cameraViewAngle
Definition MRViewport.h:284
Vector3f cameraTranslation
Definition MRViewport.h:282
Color backgroundColor
Definition MRViewport.h:278
bool orthographic
Definition MRViewport.h:289
float cameraDfar
Definition MRViewport.h:286
Color borderColor
Definition MRViewport.h:300
bool depthTest
Definition MRViewport.h:288
Plane3f clippingPlane
Definition MRViewport.h:304
std::string label
Definition MRViewport.h:302
float cameraZoom
Definition MRViewport.h:283
Quaternionf cameraTrackballAngle
Definition MRViewport.h:281
Vector3f lightPosition
Definition MRViewport.h:279
enum MR::Viewport::Parameters::GlobalBasisScaleMode Auto
bool selectable
Definition MRViewport.h:314
GlobalBasisScaleMode
Definition MRViewport.h:292
enum MR::Viewport::Parameters::RotationCenterMode Dynamic
bool operator==(const Viewport::Parameters &other) const =default
float cameraDnear
Definition MRViewport.h:285
float objectScale
Definition MRViewport.h:298
Definition MRViewport.h:158
int pickRadius
Definition MRViewport.h:167
PickRenderObjectPredicate predicate
Definition MRViewport.h:163
bool exactPickFirst
Definition MRViewport.h:171
static PickRenderObjectParams defaults()
Definition MRViewport.h:176
std::optional< Vector2f > point
Definition MRViewport.h:160
Definition MRViewport.h:121
Vector4f c
Definition MRViewport.h:122
Vector4f b
Definition MRViewport.h:122
Vector4f a
Definition MRViewport.h:122