ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ccMouseCircle.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "ccMouseCircle.h"
9 
10 #include <ecvDisplayTools.h>
11 
12 #include <cmath>
13 
14 ccMouseCircle::ccMouseCircle(QWidget* owner, QString name)
15  : cc2DViewportObject(name.isEmpty() ? "label" : name) {
16  setVisible(true);
17  setEnabled(false);
18 
19  // setup unit circle
20  for (int n = 0; n < ccMouseCircle::RESOLUTION; n++) {
21  float heading =
22  n * (2 * M_PI /
23  (float)ccMouseCircle::RESOLUTION); // heading in radians
24  ccMouseCircle::UNIT_CIRCLE[n][0] = std::cos(heading);
25  ccMouseCircle::UNIT_CIRCLE[n][1] = std::sin(heading);
26  }
27 
28  // attach to owner
29  assert(owner); // check valid pointer
30  ccMouseCircle::m_owner = owner;
31  m_owner->installEventFilter(this);
32  ecvDisplayTools::AddToOwnDB(this, true);
33 }
34 
36  // cleanup event listner
37  if (m_owner) {
38  m_owner->removeEventFilter(this);
40  }
41 }
42 
43 // get the circle radius in px
45 
46 // get the circle radius in world coordinates
47 float ccMouseCircle::getRadiusWorld() { return getRadiusPx() / m_winTotalZoom; }
48 
49 // override draw function
51  // only draw when visible
52  if (!ccMouseCircle::isVisible()) return;
53 
54  // only draw in 2D foreground mode
55  if (!MACRO_Foreground(context) || !MACRO_Draw2D(context)) return;
56 
57  if (ecvDisplayTools::GetCurrentScreen() == nullptr) return;
58 
59  // test viewport parameters
62  // glFunc->glPushAttrib(GL_LINE_BIT);
63 
64  float dx = 0.0f;
65  float dy = 0.0f;
66  if (!m_params.perspectiveView) // ortho mode
67  {
68  // Screen pan & pivot compensation
69  m_winTotalZoom = params.zoom / params.pixelSize;
70 
71  // CCVector3d dC = m_params.cameraCenter - params.cameraCenter;
72  CCVector3d P = m_params.getPivotPoint() - params.getPivotPoint();
74 
75  dx *= m_winTotalZoom;
76  dy *= m_winTotalZoom;
77  }
78 
79  // thick dotted line
80  // glFunc->glLineWidth(2);
81  // glFunc->glLineStipple(1, 0xAAAA);
82  // glFunc->glEnable(GL_LINE_STIPPLE);
83 
84  // glFunc->glColor3ubv(ecvColor::red.rgb);
85 
86  // get height & width
87  int halfW = static_cast<int>(context.glW / 2.0f);
88  int halfH = static_cast<int>(context.glH / 2.0f);
89 
90  // get mouse position
91  QPoint p = m_owner->mapFromGlobal(QCursor::pos());
92  int mx = p.x(); // mouse x-coord
93  int my = 2 * halfH - p.y(); // mouse y-coord in OpenGL coordinates (origin
94  // at bottom left, not top left)
95 
96  // calculate circle location
97  int cx = dx + mx - halfW;
98  int cy = dy + my - halfH;
99 
100  // draw circle
101  // glFunc->glBegin(GL_LINE_LOOP);
102  // for (int n = 0; n < ccMouseCircle::RESOLUTION; n++)
103  //{
104  // glFunc->glVertex2f(ccMouseCircle::UNIT_CIRCLE[n][0] *
105  // ccMouseCircle::RADIUS + cx, ccMouseCircle::UNIT_CIRCLE[n][1] *
106  // ccMouseCircle::RADIUS + cy);
107  // }
108  // glFunc->glEnd();
109  // glFunc->glPopAttrib();
110 }
111 
112 // get mouse move events
113 bool ccMouseCircle::eventFilter(QObject* obj, QEvent* event) {
114  // only process events when visible
115  if (!ccMouseCircle::isVisible()) return false;
116 
117  if (event->type() == QEvent::MouseMove) {
118  if (m_owner) {
119  ecvDisplayTools::RedrawDisplay(true, true); // redraw 2D graphics
120  }
121  }
122 
123  if (event->type() == QEvent::Wheel) {
124  QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
125 
126  // is control down
127  if (wheelEvent->modifiers().testFlag(Qt::ControlModifier)) {
128  // adjust radius
129  double delta = qtCompatWheelEventDelta(wheelEvent);
131  ccMouseCircle::RADIUS_STEP * (delta / 100.0);
132 
133  // avoid really small radius
136  }
137  // repaint
138  ecvDisplayTools::RedrawDisplay(true, true);
139  }
140  }
141  return false; // pass event to other listeners
142 }
MouseEvent event
constexpr double M_PI
Pi.
Definition: CVConst.h:19
std::string name
double qtCompatWheelEventDelta(const QWheelEvent *event) noexcept
Definition: QtCompat.h:751
cmdLineReadable * params[]
2D viewport object
ecvViewportParameters m_params
Viewport parameters.
virtual bool isVisible() const
Returns whether entity is visible or not.
virtual void setVisible(bool state)
Sets entity visibility.
void apply(float vec[3]) const
Applies transformation to a 3D vector (in place) - float version.
float UNIT_CIRCLE[RESOLUTION][2]
Definition: ccMouseCircle.h:57
void draw(CC_DRAW_CONTEXT &context) override
Draws entity and its children.
ccMouseCircle(ecvMainAppInterface *appInterface, QWidget *owner, QString name=QString("MouseCircle"))
static const int RESOLUTION
Definition: ccMouseCircle.h:54
float getRadiusWorld()
int getRadiusPx() const
Definition: ccMouseCircle.h:35
virtual void setEnabled(bool state)
Sets the "enabled" property.
Definition: ecvObject.h:102
static void RemoveFromOwnDB(ccHObject *obj)
Removes an entity from window own DB.
static void AddToOwnDB(ccHObject *obj, bool noDependency=true)
Adds an entity to window own DB.
static const ecvViewportParameters & GetViewportParameters()
static QWidget * GetCurrentScreen()
static void RedrawDisplay(bool only2D=false, bool forceRedraw=true)
Standard parameters for GL displays/viewports.
bool perspectiveView
Perspective view state.
const CCVector3d & getPivotPoint() const
Returns the pivot point (for object-centered view mode)
ccGLMatrixd viewMat
Visualization matrix (rotation only)
#define MACRO_Draw2D(context)
#define MACRO_Foreground(context)
ImGuiContext * context
Definition: Window.cpp:76
Display context.