-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMappingUtils.cpp
More file actions
109 lines (82 loc) · 4.03 KB
/
MappingUtils.cpp
File metadata and controls
109 lines (82 loc) · 4.03 KB
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
#include "MappingUtils.h"
#include <Dataset.h>
#include <LinkedData.h>
#include <PointData/PointData.h>
#include <Set.h>
#include <algorithm>
#include <cstdint>
#include <functional>
#include <map>
#include <tuple>
#include <ranges>
#include <utility>
#include <vector>
std::pair<const mv::LinkedData*, unsigned int> getSelectionMapping(const mv::Dataset<Points>& source, const mv::Dataset<Points>& target, LinkedDataCondition checkMapping) {
const std::vector<mv::LinkedData>& linkedDatas = source->getLinkedData();
if (linkedDatas.empty())
return { nullptr, 0 } ;
// find linked data between source and target OR source and target's parent, if target is derived and they have the same number of points
if (const auto result = std::ranges::find_if(
linkedDatas,
[&target, &checkMapping](const mv::LinkedData& linkedData) -> bool {
return checkMapping(linkedData, target);
});
result != linkedDatas.end())
{
return {&(*result), target->getNumPoints() };
}
return { nullptr, 0 };
}
std::pair<const mv::LinkedData*, unsigned int> getSelectionMappingColorsToPositions(const mv::Dataset<Points>& colors, const mv::Dataset<Points>& positions) {
auto testTargetAndParent = [](const mv::LinkedData& linkedData, const mv::Dataset<Points>& positions) -> bool {
const mv::Dataset<mv::DatasetImpl> mapTargetData = linkedData.getTargetDataset();
return mapTargetData == positions || parentHasSameNumPoints(mapTargetData, positions);
};
return getSelectionMapping(colors, positions, testTargetAndParent);
}
std::pair<const mv::LinkedData*, unsigned int> getSelectionMappingPositionsToColors(const mv::Dataset<Points>& positions, const mv::Dataset<Points>& colors) {
auto testTarget = [](const mv::LinkedData& linkedData, const mv::Dataset<Points>& colors) -> bool {
return linkedData.getTargetDataset() == colors;
};
auto [mapping, numTargetPoints] = getSelectionMapping(positions, colors, testTarget);
if (mapping && parentHasSameNumPoints(positions, positions)) {
const auto positionsParent = positions->getParent<Points>();
std::tie(mapping, numTargetPoints) = getSelectionMapping(positionsParent, colors, testTarget);
}
return { mapping, numTargetPoints };
}
std::pair<const mv::LinkedData*, unsigned int> getSelectionMappingPositionSourceToColors(const mv::Dataset<Points>& positions, const mv::Dataset<Points>& colors) {
if (!positions->isDerivedData())
return { nullptr, 0 };
const auto fullSourceData = positions->getSourceDataset<Points>()->getFullDataset<Points>();
if(!fullSourceData.isValid())
return { nullptr, 0 };
return getSelectionMappingPositionsToColors(fullSourceData, colors);
}
bool checkSurjectiveMapping(const mv::LinkedData& linkedData, const std::uint32_t numPointsInTarget) {
const std::map<std::uint32_t, std::vector<std::uint32_t>>& linkedMap = linkedData.getMapping().getMap();
std::vector<bool> found(numPointsInTarget, false);
std::uint32_t count = 0;
for (const auto& [key, vec] : linkedMap) {
for (std::uint32_t val : vec) {
if (val >= numPointsInTarget) continue; // Skip values that are too large
if (!found[val]) {
found[val] = true;
if (++count == numPointsInTarget)
return true;
}
}
}
return false; // The previous loop would have returned early if the entire taget set was covered
}
bool checkSelectionMapping(const mv::Dataset<Points>& colors, const mv::Dataset<Points>& positions) {
// Check if there is a mapping
auto [mapping, numTargetPoints] = getSelectionMappingColorsToPositions(colors, positions);
if (!mapping)
std::tie(mapping, numTargetPoints) = getSelectionMappingPositionsToColors(positions, colors);
if (!mapping)
std::tie(mapping, numTargetPoints) = getSelectionMappingPositionSourceToColors(positions, colors);
if (!mapping)
return false;
return true;
}