ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ccSymbolCloud.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 "ccSymbolCloud.h"
9 
10 // CV_DB_LIB
11 #include <ecvDisplayTools.h>
12 
13 ccSymbolCloud::ccSymbolCloud(QString name /*=QString()*/)
14  : ccPointCloud(name),
15  m_symbolSize(10.0),
16  m_fontSize(12),
17  m_showSymbols(true),
18  m_showLabels(true),
19  m_labelAlignFlags(ecvDisplayTools::ALIGN_HMIDDLE |
20  ecvDisplayTools::ALIGN_VBOTTOM) {}
21 
22 bool ccSymbolCloud::reserve(unsigned numberOfPoints) {
23  if (!ccPointCloud::reserve(numberOfPoints)) return false;
24 
25  if (m_labels.size()) return reserveLabelArray(numberOfPoints);
26 
27  return true;
28 }
29 
30 bool ccSymbolCloud::resize(unsigned numberOfPoints) {
31  if (!ccPointCloud::resize(numberOfPoints)) return false;
32 
33  if (m_labels.size()) return resizeLabelArray(numberOfPoints);
34 
35  return true;
36 }
37 
39  try {
40  m_labels.reserve(count);
41  } catch (const std::bad_alloc&) {
42  // not enough memory
43  return false;
44  }
45  return true;
46 }
47 
48 void ccSymbolCloud::addLabel(QString label) {
49  try {
50  m_labels.push_back(label);
51  } catch (const std::bad_alloc&) {
52  // not enough memory
53  // TODO?
54  }
55 }
56 
58  try {
59  m_labels.resize(count);
60  } catch (const std::bad_alloc&) {
61  // not enough memory
62  return false;
63  }
64  return true;
65 }
66 
67 void ccSymbolCloud::setLabel(unsigned index, QString label) {
68  if (m_labels.size() > index) {
69  m_labels[index] = label;
70  }
71 }
72 
73 QString ccSymbolCloud::getLabel(unsigned index) const {
74  if (m_labels.size() > index) {
75  return m_labels[index];
76  }
77 
78  return QString();
79 }
80 
82 
85 
87 }
88 
89 void drawSymbolAt(double xp, double yp, double symbolRadius) {
90  // diamong with cross
91  // glFunc->glBegin(GL_LINES);
92  // glFunc->glVertex2d(xp, yp - symbolRadius);
93  // glFunc->glVertex2d(xp, yp + symbolRadius);
94  // glFunc->glVertex2d(xp - symbolRadius, yp);
95  // glFunc->glVertex2d(xp + symbolRadius, yp);
96  // glFunc->glEnd();
97  // glFunc->glBegin(GL_LINE_LOOP);
98  // glFunc->glVertex2d(xp, yp - symbolRadius);
99  // glFunc->glVertex2d(xp + symbolRadius, yp);
100  // glFunc->glVertex2d(xp, yp + symbolRadius);
101  // glFunc->glVertex2d(xp - symbolRadius, yp);
102  // glFunc->glEnd();
103 }
104 
106  if (m_points.empty()) return;
107 
108  // nothing to do?!
109  if (!m_showSymbols && !m_showLabels) return;
110 
111  // get the set of OpenGL functions (version 2.1)
112  assert(ecvDisplayTools::GetCurrentScreen() != nullptr);
113 
114  if (ecvDisplayTools::GetCurrentScreen() == nullptr) return;
115 
116  if (MACRO_Draw3D(context)) {
117  // store the 3D camera parameters as we will need them for the 2D pass
118  //(and we need the real ones, especially if the rendering zoom is != 1)
120  }
121 
123  // we get display parameters
124  glDrawParams glParams;
125  getDrawingParameters(glParams);
126 
127  // standard case: list names pushing
128  bool entityPickingMode = MACRO_EntityPicking(context);
129  bool hasLabels = !m_labels.empty();
130  if (entityPickingMode) {
131  // not fast at all!
132  if (MACRO_FastEntityPicking(context)) return;
133  hasLabels = false; // no need to display labels in 'picking' mode
134  }
135 
136  // we should already be in orthoprojective & centered omde
137  // glFunc->glOrtho(-halfW, halfW, -halfH, halfH, -maxS, maxS);
138 
139  // default color
140  const ecvColor::Rgb* color = &context.pointsDefaultCol;
141  if (isColorOverridden()) {
142  color = &m_tempColor;
143  glParams.showColors = false;
144  }
145 
146  unsigned numberOfPoints = size();
147 
148  // viewport parameters (will be used to project 3D positions to 2D)
149  // ccGLCameraParameters camera;
150  // context.display->getGLCameraParameters(camera);
151 
152  // only useful when displaying labels!
153  QFont font(
154  ecvDisplayTools::GetTextDisplayFont()); // takes rendering zoom
155  // into account!
156  font.setPointSize(static_cast<int>(m_fontSize * context.renderZoom));
157  // font.setBold(true);
158  QFontMetrics fontMetrics(font);
159 
160  double symbolSizeBackup = m_symbolSize;
161  m_symbolSize *= static_cast<double>(context.renderZoom);
162 
163  double xpShift = 0.0;
165  xpShift = m_symbolSize / 2.0;
167  xpShift = -m_symbolSize / 2.0;
168 
169  double ypShift = 0.0;
171  ypShift = m_symbolSize / 2.0;
173  ypShift = -m_symbolSize / 2.0;
174 
175  // draw symbols + labels
176  {
177  for (unsigned i = 0; i < numberOfPoints; i++) {
178  // symbol center
179  const CCVector3* P = getPoint(i);
180 
181  // project it in 2D screen coordinates
182  CCVector3d Q2D;
183  m_lastCameraParams.project(*P, Q2D);
184 
185  // apply point color (if any)
186  if (glParams.showColors) {
187  color = &getPointColor(i);
188  }
189  // we must reset the color each time as the call to displayText
190  // may change the active color! glFunc->glColor3ubv(color->rgb);
191 
192  // draw associated symbol
193  if (m_showSymbols && m_symbolSize > 0.0) {
194  drawSymbolAt(Q2D.x - context.glW / 2,
195  Q2D.y - context.glH / 2, m_symbolSize / 2);
196  }
197 
198  // draw associated label?
199  if (m_showLabels && hasLabels && m_labels.size() > i &&
200  !m_labels[i].isNull()) {
201  // draw label
203  m_labels[i], static_cast<int>(Q2D.x + xpShift),
204  static_cast<int>(Q2D.y + ypShift),
205  m_labelAlignFlags, 0, color->rgb, &font);
206  }
207  }
208  }
209 
210  // restore original symbol size
211  m_symbolSize = symbolSizeBackup;
212  }
213 }
std::string name
int count
math::float4 color
void drawSymbolAt(double xp, double yp, double symbolRadius)
Type y
Definition: CVGeom.h:137
Type x
Definition: CVGeom.h:137
virtual bool isColorOverridden() const
ecvColor::Rgb m_tempColor
Temporary (unique) color.
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
void getDrawingParameters(glDrawParams &params) const override
Returns main OpenGL parameters for this entity.
bool reserve(unsigned numberOfPoints) override
Reserves memory for all the active features.
void clear() override
Clears the entity from all its points and features.
bool resize(unsigned numberOfPoints) override
Resizes all the active features arrays.
const ecvColor::Rgb & getPointColor(unsigned pointIndex) const override
Returns color corresponding to a given point.
bool resizeLabelArray(unsigned count)
Resizes memory for storing per-point labels.
bool m_showSymbols
Whether symbols are shown or not.
Definition: ccSymbolCloud.h:93
QString getLabel(unsigned index) const
Returns a given label.
void addLabel(QString label)
Adds a label.
virtual void clear() override
Clears the entity from all its points and features.
virtual bool reserve(unsigned numberOfPoints) override
inherited from ccPointCloud
virtual bool resize(unsigned numberOfPoints) override
Resizes all the active features arrays.
void setLabel(unsigned index, QString label)
Sets a given label.
unsigned char m_labelAlignFlags
Default label alignment flags.
Definition: ccSymbolCloud.h:99
bool m_showLabels
Whether labels are shown or not.
Definition: ccSymbolCloud.h:96
double m_symbolSize
Symbol size (in pixels)
Definition: ccSymbolCloud.h:87
ccGLCameraParameters m_lastCameraParams
Last 3D rendering parameters.
int m_fontSize
Label font size (in points)
Definition: ccSymbolCloud.h:90
bool reserveLabelArray(unsigned count)
Reserves memory for storing per-point labels.
virtual void drawMeOnly(CC_DRAW_CONTEXT &context) override
Draws the entity only (not its children)
void clearLabelArray()
Clears the label array.
std::vector< QString > m_labels
Labels array.
Definition: ccSymbolCloud.h:84
ccSymbolCloud(QString name=QString())
Default constructor.
std::vector< CCVector3 > m_points
3D Points database
unsigned size() const override
Returns the number of points.
Definition: PointCloudTpl.h:38
const CCVector3 * getPoint(unsigned index) const override
Returns the ith point.
RGB color structure.
Definition: ecvColorTypes.h:49
static void GetGLCameraParameters(ccGLCameraParameters &params)
Returns the current OpenGL camera parameters.
static QFont GetTextDisplayFont()
static QWidget * GetCurrentScreen()
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.
#define MACRO_Draw2D(context)
#define MACRO_Draw3D(context)
#define MACRO_Foreground(context)
#define MACRO_FastEntityPicking(context)
#define MACRO_EntityPicking(context)
ImGuiContext * context
Definition: Window.cpp:76
bool project(const CCVector3d &input3D, CCVector3d &output2D, bool *inFrustum=nullptr) const
Projects a 3D point in 2D (+ normalized 'z' coordinate)
Display context.
Display parameters of a 3D entity.
bool showColors
Display colors.