ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecv2DViewportLabel.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 "ecv2DViewportLabel.h"
9 
10 #include "ecvDisplayTools.h"
11 
12 // CV_CORE_LIB
13 #include <CVConst.h>
14 
15 // Qt
16 #include <QDataStream>
17 #include <QFontMetrics>
18 
19 // system
20 #include <string.h>
21 
24  // label rectangle
25  memset(m_roi, 0, sizeof(float) * 4);
26  setVisible(false);
27 }
28 
29 void cc2DViewportLabel::setRoi(const float* roi) {
30  memcpy(m_roi, roi, sizeof(float) * 4);
31 }
32 
33 bool cc2DViewportLabel::toFile_MeOnly(QFile& out, short dataVersion) const {
34  assert(out.isOpen() && (out.openMode() & QIODevice::WriteOnly));
35  if (dataVersion < 21) {
36  assert(false);
37  return false;
38  }
39 
40  if (!cc2DViewportObject::toFile_MeOnly(out, dataVersion)) return false;
41 
42  // ROI (dataVersion>=21)
43  QDataStream outStream(&out);
44  for (int i = 0; i < 4; ++i) outStream << m_roi[i];
45 
46  return true;
47 }
48 
50  return std::max(static_cast<short>(21),
52 }
53 
55  short dataVersion,
56  int flags,
57  LoadedIDMap& oldToNewIDMap) {
58  if (!cc2DViewportObject::fromFile_MeOnly(in, dataVersion, flags,
59  oldToNewIDMap))
60  return false;
61 
62  if (dataVersion < 21) return false;
63 
64  // ROI (dataVersion>=21)
65  QDataStream inStream(&in);
66  for (int i = 0; i < 4; ++i) inStream >> m_roi[i];
67 
68  return true;
69 }
70 
72  if (!ecvDisplayTools::GetCurrentScreen()) return;
73 
76 }
77 
82 }
83 
85  bool updateScreen /* = true */) {
86  context.drawingFlags = CC_DRAW_2D | CC_DRAW_FOREGROUND;
88  if (updateScreen) {
90  }
91 }
92 
94  // 2D foreground only
95  if (!MACRO_Foreground(context) || !MACRO_Draw2D(context)) return;
96 
97  if (!ecvDisplayTools::GetCurrentScreen()) return;
98 
99  // clear history
100  clear2Dviews();
101  if (!isVisible() || !isEnabled()) {
102  return;
103  }
104 
105  // test viewport parameters
106  const ecvViewportParameters& params =
108 
109  // general parameters
110  if (params.perspectiveView != m_params.perspectiveView ||
112  params.pixelSize != m_params.pixelSize) {
113  return;
114  }
115 
116  // test base view matrix
117  for (unsigned i = 0; i < 12; ++i) {
118  if (cloudViewer::GreaterThanEpsilon(fabs(params.viewMat.data()[i] -
119  m_params.viewMat.data()[i]))) {
120  return;
121  }
122  }
123 
125  if (params.fov_deg != m_params.fov_deg ||
127  return;
128 
130  (params.getPivotPoint() - m_params.getPivotPoint())
131  .norm()) ||
134  .norm())) {
135  return;
136  }
137  }
138 
139  float relativeZoom = 1.0f;
140  float dx = 0, dy = 0;
141  if (!m_params.perspectiveView) // ortho mode
142  {
143  // Screen pan & pivot compensation
144  float totalZoom = m_params.zoom / m_params.pixelSize;
145  float winTotalZoom = params.zoom / params.pixelSize;
146  relativeZoom = winTotalZoom / totalZoom;
147 
149 
152 
153  dx = static_cast<float>(dC.x + P.x);
154  dy = static_cast<float>(dC.y + P.y);
155 
156  dx *= winTotalZoom;
157  dy *= winTotalZoom;
158  }
159 
160  const ecvColor::Rgb* defaultColor =
161  m_selected ? &ecvColor::red : &context.textDefaultCol;
162 
164  this->getViewId());
165  const ecvColor::Rgbf& tempColor = ecvColor::FromRgb(*defaultColor);
166  param.color.r = tempColor.r;
167  param.color.g = tempColor.g;
168  param.color.b = tempColor.b;
169  param.color.a = 1.0f;
170  param.p1 =
171  QPoint(dx + m_roi[0] * relativeZoom, dy + m_roi[1] * relativeZoom);
172  param.p2 =
173  QPoint(dx + m_roi[2] * relativeZoom, dy + m_roi[1] * relativeZoom);
174  param.p3 =
175  QPoint(dx + m_roi[2] * relativeZoom, dy + m_roi[3] * relativeZoom);
176  param.p4 =
177  QPoint(dx + m_roi[0] * relativeZoom, dy + m_roi[3] * relativeZoom);
178  ecvDisplayTools::DrawWidgets(param, false);
179 
180  // title
181  QString title(getName());
182  if (!title.isEmpty()) {
183  // takes rendering zoom into account!
184  QFont titleFont(ecvDisplayTools::GetTextDisplayFont());
185  titleFont.setBold(true);
186  QFontMetrics titleFontMetrics(titleFont);
187  int titleHeight = titleFontMetrics.height();
188 
189  int xStart = static_cast<int>(dx + std::min<float>(m_roi[0], m_roi[2]) *
190  relativeZoom);
191  int yStart = static_cast<int>(dy + std::min<float>(m_roi[1], m_roi[3]) *
192  relativeZoom);
193 
194  ecvDisplayTools::DisplayText(title, xStart, yStart - 5 - titleHeight,
196  defaultColor->rgb, &titleFont,
197  this->getViewId());
198  }
199 }
std::string name
Type y
Definition: CVGeom.h:137
Type x
Definition: CVGeom.h:137
void setRoi(const float *roi)
Sets ROI (relative to screen)
float m_roi[4]
label ROI
bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads own object data.
bool toFile_MeOnly(QFile &out, short dataVersion) const override
Save own object data.
cc2DViewportLabel(QString name=QString())
Default constructor.
void update2DLabelView(CC_DRAW_CONTEXT &context, bool updateScreen=true)
const float * roi() const
Returns ROI (relative to screen)
short minimumFileVersion_MeOnly() const override
virtual void drawMeOnly(CC_DRAW_CONTEXT &context) override
Draws the entity only (not its children)
2D viewport object
ecvViewportParameters m_params
Viewport parameters.
short minimumFileVersion_MeOnly() const override
bool toFile_MeOnly(QFile &out, short dataVersion) const override
Save own object data.
bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads own object data.
virtual bool isVisible() const
Returns whether entity is visible or not.
virtual void setVisible(bool state)
Sets entity visibility.
bool m_selected
Specifies whether the object is selected or not.
T * data()
Returns a pointer to internal data.
void apply(float vec[3]) const
Applies transformation to a 3D vector (in place) - float version.
QString getViewId() const
Definition: ecvHObject.h:225
virtual QString getName() const
Returns object name.
Definition: ecvObject.h:72
virtual bool isEnabled() const
Returns whether the object is enabled or not.
Definition: ecvObject.h:97
QMultiMap< unsigned, unsigned > LoadedIDMap
Map of loaded unique IDs (old ID --> new ID)
RGB color structure.
Definition: ecvColorTypes.h:49
static void RemoveWidgets(const WIDGETS_PARAMETER &param, bool update=false)
static QFont GetTextDisplayFont()
static QWidget * GetCurrentScreen()
static void DrawWidgets(const WIDGETS_PARAMETER &param, bool update=false)
static void DisplayText(const QString &text, int x, int y, unsigned char align=ALIGN_DEFAULT, float bkgAlpha=0.0f, const unsigned char *rgbColor=nullptr, const QFont *font=nullptr, const QString &id="")
Displays a string at a given 2D position.
static void UpdateScreen()
static void GetContext(CC_DRAW_CONTEXT &CONTEXT)
Returns context information.
static const ecvViewportParameters & GetViewportParameters()
Standard parameters for GL displays/viewports.
bool perspectiveView
Perspective view state.
float fov_deg
Camera F.O.V. (field of view) in degrees.
float pixelSize
Current pixel size (in 'current unit'/pixel)
float cameraAspectRatio
Camera aspect ratio.
const CCVector3d & getCameraCenter() const
Returns the camera center.
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)
@ CC_DRAW_2D
@ CC_DRAW_FOREGROUND
@ WIDGET_TRIANGLE_2D
ImGuiContext * context
Definition: Window.cpp:76
bool GreaterThanEpsilon(float x)
Test a floating point number against our epsilon (a very small number).
Definition: CVMath.h:37
constexpr Rgbub defaultColor(MAX, MAX, MAX)
constexpr Rgb red(MAX, 0, 0)
Rgbf FromRgb(const Rgb &color)
ecvColor::Rgbaf color
Display context.