File: ccSymbolCloud.cpp

package info (click to toggle)
cloudcompare 2.10.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,916 kB
  • sloc: cpp: 219,837; ansic: 29,944; makefile: 67; sh: 45
file content (260 lines) | stat: -rw-r--r-- 5,877 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "ccSymbolCloud.h"

//qCC_db
#include <ccGenericGLDisplay.h>

ccSymbolCloud::ccSymbolCloud(QString name/*=QString()*/)
	: ccPointCloud(name)
	, m_symbolSize(10.0)
	, m_fontSize(12)
	, m_showSymbols(true)
	, m_showLabels(true)
	, m_labelAlignFlags(ccGenericGLDisplay::ALIGN_HMIDDLE | ccGenericGLDisplay::ALIGN_VBOTTOM)
{
}

bool ccSymbolCloud::reserve(unsigned numberOfPoints)
{
	if (!ccPointCloud::reserve(numberOfPoints))
		return false;
	
	if (m_labels.size())
		return reserveLabelArray(numberOfPoints);
	
	return true;
}

bool ccSymbolCloud::resize(unsigned numberOfPoints)
{
	if (!ccPointCloud::resize(numberOfPoints))
		return false;
	
	if (m_labels.size())
		return resizeLabelArray(numberOfPoints);
	
	return true;
}

bool ccSymbolCloud::reserveLabelArray(unsigned count)
{
	try
	{
		m_labels.reserve(count);
	}
	catch (const std::bad_alloc&)
	{
		//not enough memory
		return false;
	}
	return true;
}

void ccSymbolCloud::addLabel(QString label)
{
	try
	{
		m_labels.push_back(label);
	}
	catch (const std::bad_alloc&)
	{
		//not enough memory
		//TODO?
	}
}

bool ccSymbolCloud::resizeLabelArray(unsigned count)
{
	try
	{
		m_labels.resize(count);
	}
	catch (const std::bad_alloc&)
	{
		//not enough memory
		return false;
	}
	return true;
}

void ccSymbolCloud::setLabel(unsigned index, QString label)
{
	if (m_labels.size() > index)
	{
		m_labels[index] = label;
	}
}

QString ccSymbolCloud::getLabel(unsigned index) const
{
	if (m_labels.size() > index)
	{
		return m_labels[index];
	}
	
	return QString();
}

void ccSymbolCloud::clearLabelArray()
{
	m_labels.clear();
}

void ccSymbolCloud::clear()
{
	ccPointCloud::clear();

	clearLabelArray();
}

template <class QOpenGLFunctions> void drawSymbolAt(QOpenGLFunctions* glFunc, double xp, double yp, double symbolRadius)
{
	if (!glFunc)
	{
		assert(false);
		return;
	}
	//diamong with cross
	glFunc->glBegin(GL_LINES);
	glFunc->glVertex2d(xp, yp - symbolRadius);
	glFunc->glVertex2d(xp, yp + symbolRadius);
	glFunc->glVertex2d(xp - symbolRadius, yp);
	glFunc->glVertex2d(xp + symbolRadius, yp);
	glFunc->glEnd();
	glFunc->glBegin(GL_LINE_LOOP);
	glFunc->glVertex2d(xp, yp - symbolRadius);
	glFunc->glVertex2d(xp + symbolRadius, yp);
	glFunc->glVertex2d(xp, yp + symbolRadius);
	glFunc->glVertex2d(xp - symbolRadius, yp);
	glFunc->glEnd();
}

void ccSymbolCloud::drawMeOnly(CC_DRAW_CONTEXT& context)
{
	if (m_points.empty())
		return;

	//nothing to do?!
	if (!m_showSymbols && !m_showLabels)
		return;

	//get the set of OpenGL functions (version 2.1)
	QOpenGLFunctions_2_1* glFunc = context.glFunctions<QOpenGLFunctions_2_1>();
	assert(glFunc != nullptr);

	if (glFunc == nullptr)
		return;

	if (MACRO_Draw3D(context))
	{
		//store the 3D camera parameters as we will need them for the 2D pass
		//(and we need the real ones, especially if the rendering zoom is != 1)
		context.display->getGLCameraParameters(m_lastCameraParams);
	}

	if (MACRO_Draw2D(context) && MACRO_Foreground(context))
	{
		//we get display parameters
		glDrawParams glParams;
		getDrawingParameters(glParams);

		//standard case: list names pushing
		bool pushName = MACRO_DrawEntityNames(context);
		bool hasLabels = !m_labels.empty();
		if (pushName)
		{
			//not fast at all!
			if (MACRO_DrawFastNamesOnly(context))
				return;

			glFunc->glPushName(getUniqueID());
			hasLabels = false; //no need to display labels in 'picking' mode
		}

		//we should already be in orthoprojective & centered omde
		//glFunc->glOrtho(-halfW, halfW, -halfH, halfH, -maxS, maxS);

		//default color
		const ccColor::Rgb* color = &context.pointsDefaultCol;
		if (isColorOverriden())
		{
			color = &m_tempColor;
			glParams.showColors = false;
		}
		
		unsigned numberOfPoints = size();

		//viewport parameters (will be used to project 3D positions to 2D)
		//ccGLCameraParameters camera;
		//context.display->getGLCameraParameters(camera);

		//only useful when displaying labels!
		QFont font(context.display->getTextDisplayFont()); //takes rendering zoom into account!
		font.setPointSize(static_cast<int>(m_fontSize * context.renderZoom));
		//font.setBold(true);
		QFontMetrics fontMetrics(font);

		double symbolSizeBackup = m_symbolSize;
		m_symbolSize *= static_cast<double>(context.renderZoom);

		double xpShift = 0.0;
		if (m_labelAlignFlags & ccGenericGLDisplay::ALIGN_HLEFT)
			xpShift = m_symbolSize / 2.0;
		else if (m_labelAlignFlags & ccGenericGLDisplay::ALIGN_HRIGHT)
			xpShift = -m_symbolSize / 2.0;

		double ypShift = 0.0;
		if (m_labelAlignFlags & ccGenericGLDisplay::ALIGN_VTOP)
			ypShift = m_symbolSize / 2.0;
		else if (m_labelAlignFlags & ccGenericGLDisplay::ALIGN_VBOTTOM)
			ypShift = -m_symbolSize / 2.0;

		//draw symbols + labels
		{
			for (unsigned i = 0; i < numberOfPoints; i++)
			{
				//symbol center
				const CCVector3* P = getPoint(i);

				//project it in 2D screen coordinates
				CCVector3d Q2D;
				m_lastCameraParams.project(*P, Q2D);

				//apply point color (if any)
				if (glParams.showColors)
				{
					color = &getPointColor(i);
				}
				//we must reset the color each time as the call to displayText may change the active color!
				glFunc->glColor3ubv(color->rgb);

				//draw associated symbol
				if (m_showSymbols && m_symbolSize > 0.0)
				{
					drawSymbolAt<QOpenGLFunctions_2_1>(glFunc, Q2D.x - context.glW / 2, Q2D.y - context.glH / 2, m_symbolSize / 2);
				}

				//draw associated label?
				if (m_showLabels && hasLabels && m_labels.size() > i && !m_labels[i].isNull())
				{
					//draw label
					context.display->displayText(	m_labels[i],
													static_cast<int>(Q2D.x + xpShift),
													static_cast<int>(Q2D.y + ypShift),
													m_labelAlignFlags,
													0,
													color->rgb,
													&font);
				}

			}
		}

		//restore original symbol size
		m_symbolSize = symbolSizeBackup;

		if (pushName)
		{
			glFunc->glPopName();
		}
	}
}