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