Debian Codegraph @

plugins/core/Standard/qCloudLayers/src/ccCloudLayersHelper.cpp

raw
1 #include "../include/ccCloudLayersHelper.h"
2 #include "../include/ccMouseCircle.h"
3
4 //CC
5 #include <ccPointCloud.h>
6 #include <ccScalarField.h>
7 #include <ccMainAppInterface.h>
8
9 //QT
10 #include <QStringList>
11
12 //System
13 #include <thread>
14
15 ccCloudLayersHelper::ccCloudLayersHelper(ccMainAppInterface* app, ccPointCloud* cloud)
16 : m_app ( app )
17 , m_cloud( cloud )
18 , m_formerCloudColors( nullptr )
19 , m_formerCloudColorsWereShown( false )
20 , m_formerCloudSFWasShown( false )
21 , m_parameters{}
22 , m_scalarFieldIndex( 0 )
23 , m_modified( false )
24 {
25 if (m_cloud)
26 {
27 m_projectedPoints.resize(m_cloud->size());
28 m_pointInFrustum.resize(m_cloud->size());
29
30 m_formerCloudColorsWereShown = m_cloud->colorsShown();
31 m_formerCloudSFWasShown = m_cloud->sfShown();
32
33 if (m_cloud->hasColors())
34 {
35 // store the original colors
36 m_formerCloudColors = m_cloud->rgbaColors()->clone();
37 if (!m_formerCloudColors)
38 {
39 ccLog::Error("Not enough memory to backup previous colors");
40 }
41 }
42 else
43 {
44 // check memory for rgb colors
45 if (!m_cloud->resizeTheRGBTable())
46 {
47 ccLog::Error("Not enough memory to show colors");
48 }
49 }
50
51 m_cloud->showColors(true);
52 m_cloud->showSF(false);
53 }
54 }
55
56 ccCloudLayersHelper::~ccCloudLayersHelper()
57 {
58 if (m_cloud)
59 {
60 if (m_formerCloudColors)
61 {
62 if (m_cloud->rgbaColors())
63 {
64 // restore original colors
65 m_formerCloudColors->copy(*m_cloud->rgbaColors());
66 }
67
68 delete m_formerCloudColors;
69 m_formerCloudColors = nullptr;
70 }
71 else
72 {
73 m_cloud->unallocateColors();
74 }
75
76 m_cloud->showColors(m_formerCloudColorsWereShown);
77 m_cloud->showSF(m_formerCloudSFWasShown);
78 m_cloud->redrawDisplay();
79 }
80 m_cloud = nullptr;
81 }
82
83 QStringList ccCloudLayersHelper::getScalarFields()
84 {
85 unsigned sfCount = m_cloud->getNumberOfScalarFields();
86 QStringList scalarFields;
87 if (m_cloud->hasScalarFields())
88 {
89 for (unsigned i = 0; i < sfCount; ++i)
90 {
91 scalarFields.append(QString(m_cloud->getScalarFieldName(i)));
92 }
93 }
94 return scalarFields;
95 }
96
97 void ccCloudLayersHelper::setScalarFieldIndex(int index)
98 {
99 m_scalarFieldIndex = index;
100 }
101
102 void ccCloudLayersHelper::keepCurrentSFVisible()
103 {
104 m_formerCloudSFWasShown = true;
105 m_cloud->setCurrentDisplayedScalarField(m_scalarFieldIndex);
106 }
107
108 void ccCloudLayersHelper::setVisible(bool value)
109 {
110 unsigned pointCount = m_cloud->size();
111 for (unsigned i = 0; i < pointCount; ++i)
112 {
113 ccColor::Rgba color = m_cloud->getPointColor(i);
114 color.a = value ? ccColor::MAX : 0;
115 m_cloud->setPointColor(i, color);
116 }
117
118 m_cloud->redrawDisplay();
119 }
120
121 void ccCloudLayersHelper::apply(QList<ccAsprsModel::AsprsItem>& items)
122 {
123 if (m_scalarFieldIndex >= m_cloud->getNumberOfScalarFields())
124 return;
125
126 m_cloud->setColor(ccColor::black);
127
128 for (int i = 0; i < items.size(); ++i)
129 {
130 items[i].count = apply(items[i]);
131 }
132
133 m_cloud->redrawDisplay();
134 }
135
136 int ccCloudLayersHelper::apply(ccAsprsModel::AsprsItem& item, bool redrawDisplay)
137 {
138 ccColor::Rgba ccColor = ccColor::FromQColora(item.color);
139 ccColor.a = item.visible ? ccColor::MAX : 0;
140 CCCoreLib::ScalarField* sf = m_cloud->getScalarField(m_scalarFieldIndex);
141 if (!sf)
142 return 0;
143
144 ScalarType code = static_cast<ScalarType>(item.code);
145 int affected = 0;
146 int counter = 0;
147 for (auto it = sf->begin(); it != sf->end(); ++it, ++counter)
148 {
149 if ((*it) == code)
150 {
151 m_cloud->setPointColor(counter, ccColor);
152 ++affected;
153 }
154 }
155
156 if (redrawDisplay)
157 m_cloud->redrawDisplay();
158
159 return affected;
160 }
161
162 void ccCloudLayersHelper::changeCode(const ccAsprsModel::AsprsItem& item, ScalarType oldCode)
163 {
164 CCCoreLib::ScalarField* sf = m_cloud->getScalarField(m_scalarFieldIndex);
165 if (!sf)
166 return;
167
168 ScalarType code = static_cast<ScalarType>(item.code);
169 int counter = 0;
170 for (auto it = sf->begin(); it != sf->end(); ++it, ++counter)
171 {
172 if ((*it) == oldCode)
173 {
174 sf->setValue(counter, code);
175 }
176 }
177 }
178
179 int ccCloudLayersHelper::moveItem(const ccAsprsModel::AsprsItem& from, const ccAsprsModel::AsprsItem* to, bool redrawDisplay)
180 {
181 CCCoreLib::ScalarField* sf = m_cloud->getScalarField(m_scalarFieldIndex);
182 if (!sf)
183 return 0;
184
185 ScalarType code = static_cast<ScalarType>(from.code);
186 ScalarType emptyCode = to != nullptr ? static_cast<ScalarType>(to->code) : static_cast<ScalarType>(0);
187 const ccColor::Rgba color = to != nullptr ? ccColor::FromQColora(to->color) : ccColor::black;
188
189 int affected = 0;
190 int counter = 0;
191 for (auto it = sf->begin(); it != sf->end(); ++it, ++counter)
192 {
193 if ((*it) == code)
194 {
195 sf->setValue(counter, emptyCode);
196 m_cloud->setPointColor(counter, color);
197 ++affected;
198 }
199 }
200
201 if (redrawDisplay)
202 m_cloud->redrawDisplay();
203
204 return affected;
205 }
206
207 void ccCloudLayersHelper::saveState()
208 {
209 CCCoreLib::ScalarField* sf = m_cloud->getScalarField(m_scalarFieldIndex);
210 if (!sf)
211 return;
212
213 unsigned cloudSize = m_cloud->size();
214 m_cloudState.resize(cloudSize);
215 for (unsigned i = 0; i < cloudSize; ++i)
216 {
217 m_cloudState[i].update(sf->getValue(i), m_cloud->getPointColor(i));
218 }
219 }
220
221 void ccCloudLayersHelper::restoreState()
222 {
223 CCCoreLib::ScalarField* sf = (m_cloud ? m_cloud->getScalarField(m_scalarFieldIndex) : nullptr);
224 if (!sf)
225 {
226 assert(false);
227 return;
228 }
229
230 unsigned cloudSize = m_cloud->size();
231 if (m_cloudState.size() != cloudSize)
232 {
233 assert(false);
234 return;
235 }
236
237 for (unsigned i = 0; i < cloudSize; ++i)
238 {
239 const CloudState& state = m_cloudState[i];
240 sf->setValue(i, state.code);
241 m_cloud->setPointColor(i, state.color);
242 }
243 }
244
245 void ccCloudLayersHelper::project(const ccGLCameraParameters& camera, unsigned start, unsigned end)
246 {
247 const double half_w = camera.viewport[2] / 2.0;
248 const double half_h = camera.viewport[3] / 2.0;
249
250 CCVector3d Q2D;
251 bool pointInFrustum = false;
252 for (unsigned i = start; i < end; ++i)
253 {
254 const CCVector3* P3D = m_cloud->getPoint(i);
255 camera.project(*P3D, Q2D, &pointInFrustum);
256 m_projectedPoints[i] = CCVector2(static_cast<PointCoordinateType>(Q2D.x - half_w), static_cast<PointCoordinateType>(Q2D.y - half_h));
257 m_pointInFrustum[i] = pointInFrustum;
258 }
259 }
260
261 PointCoordinateType ccCloudLayersHelper::ComputeSquaredEuclideanDistance(const CCVector2& a, const CCVector2& b)
262 {
263 return (b - a).norm2();
264 }
265
266 void ccCloudLayersHelper::mouseMove(const CCVector2& center, float squareDist, std::map<ScalarType, int>& affected)
267 {
268 if (m_parameters.output == nullptr ||
269 ((!m_parameters.anyPoints && !m_parameters.visiblePoints) && m_parameters.input == nullptr))
270 {
271 return;
272 }
273
274 CCCoreLib::ScalarField* sf = m_cloud->getScalarField(m_scalarFieldIndex);
275 if (!sf)
276 return;
277
278 ScalarType inputCode = m_parameters.input != nullptr ? static_cast<ScalarType>(m_parameters.input->code) : 0;
279 ScalarType outputCode = static_cast<ScalarType>(m_parameters.output->code);
280
281 unsigned char alpha = m_parameters.output->visible ? ccColor::MAX : 0;
282 ccColor::Rgba outputColor = ccColor::Rgba(ccColor::FromQColor(m_parameters.output->color), alpha);
283
284 unsigned cloudSize = m_cloud->size();
285 for (unsigned i = 0; i < cloudSize; ++i)
286 {
287 // skip camera outside point
288 if (!m_pointInFrustum[i])
289 continue;
290
291 const auto& color = m_cloud->getPointColor(i);
292
293 // skip invisible points
294 if (m_parameters.visiblePoints && color.a != ccColor::MAX)
295 continue;
296
297 ScalarType code = sf->getValue(i);
298
299 // skip other codes
300 if (m_parameters.input && code != inputCode)
301 continue;
302
303 // skip circle outside point
304 if (ComputeSquaredEuclideanDistance(center, m_projectedPoints[i]) > squareDist)
305 continue;
306
307 if (code != outputCode)
308 {
309 sf->setValue(i, outputCode);
310 m_cloud->setPointColor(i, outputColor);
311
312 --affected[code];
313 ++affected[outputCode];
314 }
315
316 m_modified = true;
317 }
318
319 m_cloud->redrawDisplay();
320 }
321
322 void ccCloudLayersHelper::projectCloud(const ccGLCameraParameters& camera)
323 {
324 // check camera parameters changes
325 bool hasChanges = false;
326 auto a = m_cameraParameters.modelViewMat.data();
327 auto b = camera.modelViewMat.data();
328 for (int i = 0; i < OPENGL_MATRIX_SIZE; ++i)
329 {
330 if (std::abs(a[i] - b[i]) > 1e-6)
331 {
332 hasChanges = true;
333 break;
334 }
335 }
336
337 if (!hasChanges)
338 return;
339
340 m_cameraParameters = camera;
341 unsigned cloudSize = m_cloud->size();
342
343 unsigned processorCount = std::thread::hardware_concurrency();
344 if (processorCount == 0)
345 processorCount = 1;
346
347 const size_t part_size = cloudSize / processorCount;
348 std::vector<std::thread*> threads;
349 threads.resize(processorCount, nullptr);
350 for (unsigned i = 0; i < processorCount; ++i)
351 {
352 size_t start = i * part_size;
353 size_t end = start + part_size;
354
355 if (i == processorCount - 1)
356 end = cloudSize;
357
358 threads[i] = new std::thread(&ccCloudLayersHelper::project, this, camera, start, end);
359 }
360
361 for (auto it = threads.begin(); it != threads.end(); ++it)
362 (*it)->join();
363
364 for (auto it = threads.begin(); it != threads.end(); ++it)
365 delete (*it);
366 }
367
368 ccCloudLayersHelper::Parameters& ccCloudLayersHelper::getParameters()
369 {
370 return m_parameters;
371 }