forked from varoudis/depthmapX
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathattributetable.cpp
531 lines (452 loc) · 13.1 KB
/
attributetable.cpp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Copyright (C) 2017 Christian Sailer
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "attributetable.h"
#include "displayparams.h"
#include <genlib/stringutils.h>
#include <genlib/readwritehelpers.h>
#include <sstream>
#include <numeric>
const std::string &AttributeColumnImpl::getName() const
{
return m_name;
}
bool AttributeColumnImpl::isLocked() const
{
return m_locked;
}
void AttributeColumnImpl::setLock(bool lock)
{
m_locked = lock;
}
bool AttributeColumnImpl::isHidden() const
{
return m_hidden;
}
void AttributeColumnImpl::setHidden(bool hidden)
{
m_hidden = hidden;
}
void AttributeColumnImpl::setFormula(std::string newFormula)
{
m_formula = newFormula;
}
const std::string &AttributeColumnImpl::getFormula() const
{
return m_formula;
}
const AttributeColumnStats &AttributeColumnImpl::getStats() const
{
return m_stats;
}
void AttributeColumnImpl::updateStats(float val, float oldVal) const
{
if (m_stats.total < 0)
{
m_stats.total = val;
}
else
{
m_stats.total += val;
m_stats.total -= oldVal;
}
if (val > m_stats.max)
{
m_stats.max = val;
}
if (m_stats.min < 0 || val < m_stats.min)
{
m_stats.min = val;
}
}
void AttributeColumnImpl::setName(const std::string &name)
{
m_name = name;
}
size_t AttributeColumnImpl::read(std::istream &stream)
{
m_name = dXstring::readString(stream);
float val;
stream.read((char *)&val, sizeof(float));
m_stats.min = val;
stream.read((char *)&val, sizeof(float));
m_stats.max = val;
stream.read((char *)&m_stats.total, sizeof(double));
int physical_column;
stream.read((char *)&physical_column, sizeof(int)); // physical column is obsolete
stream.read((char *)&m_hidden, sizeof(bool));
stream.read((char *)&m_locked, sizeof(bool));
stream.read((char*)&m_displayParams,sizeof(DisplayParams));
m_formula = dXstring::readString(stream);
return physical_column;
}
void AttributeColumnImpl::write(std::ostream &stream, int physicalCol)
{
dXstring::writeString(stream, m_name);
float min = (float)m_stats.min;
float max = (float)m_stats.max;
stream.write((char *)&min, sizeof(float));
stream.write((char *)&max, sizeof(float));
stream.write((char *)&m_stats.total, sizeof(m_stats.total));
stream.write((char *)&physicalCol, sizeof(int));
stream.write((char *)&m_hidden, sizeof(bool));
stream.write((char *)&m_locked, sizeof(bool));
stream.write((char *)&m_displayParams, sizeof(DisplayParams));
dXstring::writeString(stream, m_formula);
}
// AttributeRow implementation
float AttributeRowImpl::getValue(const std::string &column) const
{
return getValue(m_colManager.getColumnIndex(column));
}
float AttributeRowImpl::getValue(size_t index) const
{
checkIndex(index);
return m_data[index];
}
float AttributeRowImpl::getNormalisedValue(size_t index) const
{
checkIndex(index);
auto& colStats = m_colManager.getColumn(index).getStats();
if (colStats.max == colStats.min)
{
return 0.5f;
}
return m_data[index] < 0 ? -1.0f : float((m_data[index] - colStats.min)/(colStats.max - colStats.min));
}
AttributeRow& AttributeRowImpl::setValue(const std::string &column, float value)
{
return setValue(m_colManager.getColumnIndex(column), value);
}
AttributeRow& AttributeRowImpl::setValue(size_t index, float value)
{
checkIndex(index);
float oldVal = m_data[index];
m_data[index] = value;
if (oldVal < 0.0f)
{
oldVal = 0.0f;
}
m_colManager.getColumn(index).updateStats(value, oldVal);
return *this;
}
AttributeRow& AttributeRowImpl::setSelection(bool selected)
{
m_selected = selected;
return *this;
}
bool AttributeRowImpl::isSelected() const
{
return m_selected;
}
void AttributeRowImpl::addColumn()
{
m_data.push_back(-1);
}
void AttributeRowImpl::removeColumn(size_t index)
{
checkIndex(index);
m_data.erase(m_data.begin() + index);
}
void AttributeRowImpl::read(std::istream &stream)
{
stream.read((char *)&m_layerKey, sizeof(m_layerKey));
dXreadwrite::readIntoVector(stream, m_data);
}
void AttributeRowImpl::write(std::ostream &stream)
{
stream.write((char *)&m_layerKey, sizeof(m_layerKey));
dXreadwrite::writeVector(stream, m_data);
}
void AttributeRowImpl::checkIndex(size_t index) const
{
if( index >= m_data.size())
{
throw std::out_of_range("AttributeColumn index out of range");
}
}
AttributeRow &AttributeRowImpl::incrValue(size_t index, float value)
{
checkIndex(index);
float val = m_data[index];
if ( val < 0)
{
setValue(index, value);
}
else
{
setValue(index, val + value);
}
return *this;
}
AttributeRow &AttributeRowImpl::incrValue(const std::string &colName, float value)
{
return incrValue(m_colManager.getColumnIndex(colName), value);
}
AttributeRow &AttributeTable::getRow(const AttributeKey &key)
{
auto* row = getRowPtr(key);
if (row == 0)
{
throw std::out_of_range("Invalid row key");
}
return *row;
}
const AttributeRow& AttributeTable::getRow(const AttributeKey &key) const
{
auto* row = getRowPtr(key);
if (row == 0)
{
throw std::out_of_range("Invalid row key");
}
return *row;
}
AttributeRow *AttributeTable::getRowPtr(const AttributeKey &key)
{
auto iter = m_rows.find(key);
if (iter == m_rows.end())
{
return 0;
}
return iter->second.get();
}
const AttributeRow *AttributeTable::getRowPtr(const AttributeKey &key) const
{
auto iter = m_rows.find(key);
if (iter == m_rows.end())
{
return 0;
}
return iter->second.get();
}
AttributeRow &AttributeTable::addRow(const AttributeKey &key)
{
auto iter = m_rows.find(key);
if (iter != m_rows.end())
{
throw new std::invalid_argument("Duplicate key");
}
auto res = m_rows.insert(std::make_pair(key, std::unique_ptr<AttributeRowImpl>(new AttributeRowImpl(*this))));
return *res.first->second;
}
void AttributeTable::removeRow(const AttributeKey &key)
{
auto iter = m_rows.find(key);
if (iter == m_rows.end())
{
throw new std::invalid_argument("Row does not exist");
}
m_rows.erase(iter);
}
AttributeColumn &AttributeTable::getColumn(size_t index)
{
if(index == size_t(-1)) {
return m_keyColumn;
}
checkColumnIndex(index);
return m_columns[index];
}
size_t AttributeTable::insertOrResetColumn(const std::string &columnName, const std::string &formula)
{
auto iter = m_columnMapping.find(columnName);
if (iter == m_columnMapping.end())
{
return addColumnInternal(columnName, formula);
}
// it exists - we need to reset it
m_columns[iter->second].m_stats = AttributeColumnStats();
m_columns[iter->second].setLock(false);
for (auto& row : m_rows)
{
row.second->setValue(iter->second, -1.0f);
}
return iter->second;
}
size_t AttributeTable::insertOrResetLockedColumn(const std::string &columnName, const std::string &formula)
{
size_t index = insertOrResetColumn(columnName, formula);
m_columns[index].setLock(true);
return index;
}
size_t AttributeTable::getOrInsertColumn(const std::string &columnName, const std::string &formula)
{
auto iter = m_columnMapping.find(columnName);
if ( iter != m_columnMapping.end())
{
return iter->second;
}
return addColumnInternal(columnName, formula);
}
size_t AttributeTable::getOrInsertLockedColumn(const std::string &columnName, const std::string &formula)
{
size_t index = getOrInsertColumn(columnName, formula);
m_columns[index].setLock(true);
return index;
}
void AttributeTable::removeColumn(size_t colIndex)
{
checkColumnIndex(colIndex);
const std::string& name = m_columns[colIndex].getName();
auto iter = m_columnMapping.find(name);
m_columnMapping.erase(iter);
for (auto& elem : m_columnMapping)
{
if (elem.second > colIndex)
{
elem.second--;
}
}
m_columns.erase(m_columns.begin()+colIndex);
for (auto& row : m_rows)
{
row.second->removeColumn(colIndex);
}
}
void AttributeTable::renameColumn(const std::string &oldName, const std::string &newName)
{
auto iter = m_columnMapping.find(oldName);
if (iter == m_columnMapping.end())
{
throw std::out_of_range("Invalid column name");
}
size_t colIndex = iter->second;
m_columns[colIndex].setName(newName);
m_columnMapping.erase(iter);
m_columnMapping[newName] = colIndex;
}
void AttributeTable::deselectAllRows()
{
for (auto& row : m_rows)
{
row.second->setSelection(false);
}
}
void AttributeTable::setDisplayParamsForAllAttributes(const DisplayParams ¶ms)
{
for (auto& col: m_columns)
{
col.setDisplayParams(params);
}
}
void AttributeTable::read(std::istream &stream, LayerManager &layerManager)
{
layerManager.read(stream);
int colcount;
stream.read((char *)&colcount, sizeof(colcount));
std::map<size_t, AttributeColumnImpl> tmp;
for (int j = 0; j < colcount; j++) {
AttributeColumnImpl col("");
tmp[col.read(stream)] = col;
}
for (auto & c : tmp)
{
m_columnMapping[c.second.getName()] = m_columns.size();
m_columns.push_back(c.second);
}
int rowcount, rowkey;
stream.read((char *)&rowcount, sizeof(rowcount));
for (int i = 0; i < rowcount; i++) {
stream.read((char *)&rowkey, sizeof(rowkey));
auto row = std::unique_ptr<AttributeRowImpl>(new AttributeRowImpl(*this));
row->read(stream);
m_rows.insert(std::make_pair(AttributeKey(rowkey),std::move(row)));
}
// ref column display params
stream.read((char *)&m_displayParams,sizeof(DisplayParams));
}
void AttributeTable::write(std::ostream &stream, const LayerManager &layerManager)
{
layerManager.write(stream);
int colCount = (int)m_columns.size();
stream.write((char *)&colCount, sizeof(int));
// TODO: For binary compatibility write the columns in alphabetical order
// but the physical columns in the order inserted
std::vector<size_t> indices(m_columns.size());
std::iota(indices.begin(), indices.end(), static_cast<size_t>(0));
std::sort(indices.begin(), indices.end(),
[&](size_t a, size_t b) {
return m_columns[a].getName() < m_columns[b].getName();
});
for (int idx: indices) {
m_columns[idx].write(stream, m_columnMapping[m_columns[idx].getName()]);
}
int rowcount = (int)m_rows.size();
stream.write((char *)&rowcount, sizeof(int));
for ( auto &kvp : m_rows)
{
kvp.first.write(stream);
kvp.second->write(stream);
}
stream.write((const char *)&m_displayParams, sizeof(DisplayParams));
}
void AttributeTable::clear() {
m_rows.clear();
m_columns.clear();
m_columnMapping.clear();
}
size_t AttributeTable::getColumnIndex(const std::string &name) const
{
auto iter = m_columnMapping.find(name);
if (iter == m_columnMapping.end())
{
std::stringstream message;
message << "Unknown column name " << name;
throw std::out_of_range(message.str());
}
return iter->second;
}
// TODO: Compatibility. Method to retreive a column's index
// if the set of columns was sorted
size_t AttributeTable::getColumnSortedIndex(size_t index) const
{
if(index == size_t(-1) || index == size_t(-2)) return index;
if(index >= m_columns.size()) return -1;
return std::distance(m_columnMapping.begin(), m_columnMapping.find(getColumnName(index)));
}
const AttributeColumn &AttributeTable::getColumn(size_t index) const
{
if(index == size_t(-1)) {
return m_keyColumn;
}
checkColumnIndex(index);
return m_columns[index];
}
const std::string &AttributeTable::getColumnName(size_t index) const
{
return getColumn(index).getName();
}
size_t AttributeTable::getNumColumns() const
{
return m_columns.size();
}
bool AttributeTable::hasColumn(const std::string &name) const
{
auto iter = m_columnMapping.find(name);
return(iter != m_columnMapping.end());
}
void AttributeTable::checkColumnIndex(size_t index) const
{
if (index >= m_columns.size())
{
throw std::out_of_range("ColumnIndex out of range");
}
}
size_t AttributeTable::addColumnInternal(const std::string &name, const std::string &formula)
{
size_t colIndex = m_columns.size();
m_columns.push_back(AttributeColumnImpl(name, formula));
m_columnMapping[name] = colIndex;
for (auto& elem : m_rows)
{
elem.second->addColumn();
}
return colIndex;
}