Debian Codegraph @

plugins/core/Standard/qSRA/src/ccSymbolCloud.cpp

raw
1 #include "ccSymbolCloud.h"
2
3 //qCC_db
4 #include <ccGenericGLDisplay.h>
5
6 ccSymbolCloud::ccSymbolCloud(QString name/*=QString()*/)
7 : ccPointCloud(name)
8 , m_symbolSize(10.0)
9 , m_fontSize(12)
10 , m_showSymbols(true)
11 , m_showLabels(true)
12 , m_labelAlignFlags(ccGenericGLDisplay::ALIGN_HMIDDLE | ccGenericGLDisplay::ALIGN_VBOTTOM)
13 {
14 }
15
16 bool ccSymbolCloud::reserve(unsigned numberOfPoints)
17 {
18 if (!ccPointCloud::reserve(numberOfPoints))
19 return false;
20
21 if (m_labels.size())
22 return reserveLabelArray(numberOfPoints);
23
24 return true;
25 }
26
27 bool ccSymbolCloud::resize(unsigned numberOfPoints)
28 {
29 if (!ccPointCloud::resize(numberOfPoints))
30 return false;
31
32 if (m_labels.size())
33 return resizeLabelArray(numberOfPoints);
34
35 return true;
36 }
37
38 bool ccSymbolCloud::reserveLabelArray(unsigned count)
39 {
40 try
41 {
42 m_labels.reserve(count);
43 }
44 catch (const std::bad_alloc&)
45 {
46 //not enough memory
47 return false;
48 }
49 return true;
50 }
51
52 void ccSymbolCloud::addLabel(QString label)
53 {
54 try
55 {
56 m_labels.push_back(label);
57 }
58 catch (const std::bad_alloc&)
59 {
60 //not enough memory
61 //TODO?
62 }
63 }
64
65 bool ccSymbolCloud::resizeLabelArray(unsigned count)
66 {
67 try
68 {
69 m_labels.resize(count);
70 }
71 catch (const std::bad_alloc&)
72 {
73 //not enough memory
74 return false;
75 }
76 return true;
77 }
78
79 void ccSymbolCloud::setLabel(unsigned index, QString label)
80 {
81 if (m_labels.size() > index)
82 {
83 m_labels[index] = label;
84 }
85 }
86
87 QString ccSymbolCloud::getLabel(unsigned index) const
88 {
89 if (m_labels.size() > index)
90 {
91 return m_labels[index];
92 }
93
94 return QString();
95 }
96
97 void ccSymbolCloud::clearLabelArray()
98 {
99 m_labels.clear();
100 }
101
102 void ccSymbolCloud::clear()
103 {
104 ccPointCloud::clear();
105
106 clearLabelArray();
107 }
108
109 template <class QOpenGLFunctions> void drawSymbolAt(QOpenGLFunctions* glFunc, double xp, double yp, double symbolRadius)
110 {
111 if (!glFunc)
112 {
113 assert(false);
114 return;
115 }
116 //diamong with cross
117 glFunc->glBegin(GL_LINES);
118 glFunc->glVertex2d(xp, yp - symbolRadius);
119 glFunc->glVertex2d(xp, yp + symbolRadius);
120 glFunc->glVertex2d(xp - symbolRadius, yp);
121 glFunc->glVertex2d(xp + symbolRadius, yp);
122 glFunc->glEnd();
123 glFunc->glBegin(GL_LINE_LOOP);
124 glFunc->glVertex2d(xp, yp - symbolRadius);
125 glFunc->glVertex2d(xp + symbolRadius, yp);
126 glFunc->glVertex2d(xp, yp + symbolRadius);
127 glFunc->glVertex2d(xp - symbolRadius, yp);
128 glFunc->glEnd();
129 }
130
131 void ccSymbolCloud::drawMeOnly(CC_DRAW_CONTEXT& context)
132 {
133 if (m_points.empty())
134 return;
135
136 //nothing to do?!
137 if (!m_showSymbols && !m_showLabels)
138 return;
139
140 //get the set of OpenGL functions (version 2.1)
141 QOpenGLFunctions_2_1* glFunc = context.glFunctions<QOpenGLFunctions_2_1>();
142 assert(glFunc != nullptr);
143
144 if (glFunc == nullptr)
145 return;
146
147 if (MACRO_Draw3D(context))
148 {
149 //store the 3D camera parameters as we will need them for the 2D pass
150 //(and we need the real ones, especially if the rendering zoom is != 1)
151 context.display->getGLCameraParameters(m_lastCameraParams);
152 }
153
154 if (MACRO_Draw2D(context) && MACRO_Foreground(context))
155 {
156 //we get display parameters
157 glDrawParams glParams;
158 getDrawingParameters(glParams);
159
160 bool hasLabels = !m_labels.empty();
161
162 bool entityPickingMode = MACRO_EntityPicking(context);
163 ccColor::Rgba pickingColor;
164 if (entityPickingMode)
165 {
166 //not fast at all!
167 if (MACRO_FastEntityPicking(context))
168 {
169 return;
170 }
171
172 pickingColor = ccColor::Rgba(context.entityPicking.registerEntity(this), 255);
173 hasLabels = false; //no need to display labels in 'picking' mode
174 }
175
176 //we should already be in orthoprojective & centered omde
177 //glFunc->glOrtho(-halfW, halfW, -halfH, halfH, -maxS, maxS);
178
179 //default color
180 const ccColor::Rgba* color = &context.pointsDefaultCol;
181 if (entityPickingMode)
182 {
183 color = &pickingColor;
184 glParams.showColors = false;
185 }
186 else if (isColorOverridden())
187 {
188 color = &m_tempColor;
189 glParams.showColors = false;
190 }
191
192 unsigned numberOfPoints = size();
193
194 //viewport parameters (will be used to project 3D positions to 2D)
195 //ccGLCameraParameters camera;
196 //context.display->getGLCameraParameters(camera);
197
198 //only useful when displaying labels!
199 QFont font(context.display->getTextDisplayFont()); //takes rendering zoom into account!
200 font.setPointSize(static_cast<int>(m_fontSize * context.renderZoom));
201 //font.setBold(true);
202 QFontMetrics fontMetrics(font);
203
204 double symbolSizeBackup = m_symbolSize;
205 m_symbolSize *= static_cast<double>(context.renderZoom);
206
207 double xpShift = 0.0;
208 if (m_labelAlignFlags & ccGenericGLDisplay::ALIGN_HLEFT)
209 xpShift = m_symbolSize / 2.0;
210 else if (m_labelAlignFlags & ccGenericGLDisplay::ALIGN_HRIGHT)
211 xpShift = -m_symbolSize / 2.0;
212
213 double ypShift = 0.0;
214 if (m_labelAlignFlags & ccGenericGLDisplay::ALIGN_VTOP)
215 ypShift = m_symbolSize / 2.0;
216 else if (m_labelAlignFlags & ccGenericGLDisplay::ALIGN_VBOTTOM)
217 ypShift = -m_symbolSize / 2.0;
218
219 //draw symbols + labels
220 {
221 for (unsigned i = 0; i < numberOfPoints; i++)
222 {
223 //symbol center
224 const CCVector3* P = getPoint(i);
225
226 //project it in 2D screen coordinates
227 CCVector3d Q2D;
228 m_lastCameraParams.project(*P, Q2D);
229
230 //apply point color (if any)
231 if (glParams.showColors)
232 {
233 color = &getPointColor(i);
234 }
235 //we must reset the color each time as the call to displayText may change the active color!
236 ccGL::Color(glFunc, *color);
237
238 //draw associated symbol
239 if (m_showSymbols && m_symbolSize > 0.0)
240 {
241 drawSymbolAt<QOpenGLFunctions_2_1>(glFunc, Q2D.x - context.glW / 2, Q2D.y - context.glH / 2, m_symbolSize / 2);
242 }
243
244 //draw associated label?
245 if (m_showLabels && hasLabels && m_labels.size() > i && !m_labels[i].isNull())
246 {
247 //draw label
248 context.display->displayText( m_labels[i],
249 static_cast<int>(Q2D.x + xpShift),
250 static_cast<int>(Q2D.y + ypShift),
251 m_labelAlignFlags,
252 0,
253 color,
254 &font);
255 }
256
257 }
258 }
259
260 //restore original symbol size
261 m_symbolSize = symbolSizeBackup;
262 }
263 }