TTK
Loading...
Searching...
No Matches
MergeTreeUtils.h
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <FTMTreeUtils.h>
10#include <stack>
11
12namespace ttk {
13
14 template <class dataType>
16 ftm::idNode tree1Node,
17 ftm::FTMTree_MT *tree2,
18 ftm::idNode tree2Node) {
19 dataType tree1NodeDeath
20 = std::get<1>(tree1->getBirthDeath<dataType>(tree1Node));
21 dataType tree2NodeDeath
22 = std::get<1>(tree2->getBirthDeath<dataType>(tree2Node));
23 return tree1NodeDeath > tree2NodeDeath;
24 }
25
26 // --------------------
27 // Normalized Wasserstein
28 // --------------------
29 template <class dataType>
31 ftm::idNode nodeId,
32 bool getMin = true) {
33 auto nodeIdParent = tree->getParentSafe(nodeId);
34
35 if(tree->notNeedToNormalize(nodeId))
36 return getMin ? 0.0 : 1.0;
37
38 // General case
39 std::tuple<dataType, dataType> birthDeath
40 = tree->getBirthDeath<dataType>(nodeIdParent);
41
42 // Verify inconsistency
43 if(tree->isParentInconsistent<dataType>(nodeId)) {
44 // printErr("inconsistency");
45 // if(tree->getNumberOfNodes() < 1000)
46 tree->printTree();
47 // printPairsFromTree<dataType>(tree, true);
48 tree->printNode2<dataType>(nodeId);
49 tree->printNode2<dataType>(nodeIdParent);
50 }
51
52 return getMin ? std::get<0>(birthDeath) : std::get<1>(birthDeath);
53 }
54
55 template <class dataType>
56 std::tuple<dataType, dataType> getMinMaxLocalT(ftm::FTMTree_MT *tree,
57 ftm::idNode nodeId) {
58 dataType min = getMinMaxLocal<dataType>(tree, nodeId);
59 dataType max = getMinMaxLocal<dataType>(tree, nodeId, false);
60 return std::make_tuple(min, max);
61 }
62
63 template <class dataType>
64 std::tuple<double, double>
66 ftm::idNode nodeId,
67 dataType newMin = 0.0,
68 dataType newMax = 1.0) {
69 auto birthDeath = tree->getBirthDeath<dataType>(nodeId);
70 double birth = std::get<0>(birthDeath);
71 double death = std::get<1>(birthDeath);
72 dataType shiftMin = getMinMaxLocal<dataType>(tree, nodeId);
73 dataType shiftMax = getMinMaxLocal<dataType>(tree, nodeId, false);
74 if((shiftMax - shiftMin) == 0)
75 return std::make_tuple(0, 0);
76 birth = (newMax - newMin) * (birth - shiftMin)
77 / (shiftMax - shiftMin); // + newMin;
78 death = (newMax - newMin) * (death - shiftMin)
79 / (shiftMax - shiftMin); // + newMin;
80 return std::make_tuple(birth, death);
81 }
82
83 template <class dataType>
84 std::tuple<dataType, dataType> getNormalizedBirthDeath(ftm::FTMTree_MT *tree,
85 ftm::idNode nodeId,
86 dataType newMin = 0.0,
87 dataType newMax
88 = 1.0) {
89 auto birthDeath = tree->getBirthDeath<dataType>(nodeId);
90 dataType birth = std::get<0>(birthDeath);
91 dataType death = std::get<1>(birthDeath);
92 dataType shiftMin = getMinMaxLocal<dataType>(tree, nodeId);
93 dataType shiftMax = getMinMaxLocal<dataType>(tree, nodeId, false);
94 if((shiftMax - shiftMin) == 0)
95 return std::make_tuple(0, 0);
96 birth = (newMax - newMin) * (birth - shiftMin)
97 / (shiftMax - shiftMin); // + newMin;
98 death = (newMax - newMin) * (death - shiftMin)
99 / (shiftMax - shiftMin); // + newMin;
100 return std::make_tuple(birth, death);
101 }
102
103 template <class dataType>
104 std::tuple<dataType, dataType> getParametrizedBirthDeath(
105 ftm::FTMTree_MT *tree, ftm::idNode node, bool normalize) {
106 return normalize ? getNormalizedBirthDeath<dataType>(tree, node)
107 : tree->getBirthDeath<dataType>(node);
108 }
109
110 template <class dataType>
112 ftm::idNode nodeId,
113 std::vector<dataType> &scalarsVector,
114 bool getMin = true) {
115 auto nodeIdParent = tree->getParentSafe(nodeId);
116
117 if(tree->notNeedToNormalize(nodeId))
118 return getMin ? 0.0 : 1.0;
119
120 // General case
121 dataType death = scalarsVector[nodeIdParent];
122 dataType birth = scalarsVector[tree->getNode(nodeIdParent)->getOrigin()];
123 if(death < birth) {
124 dataType temp = death;
125 death = birth;
126 birth = temp;
127 }
128
129 return getMin ? birth : death;
130 }
131
132 // --------------------
133 // Testing
134 // --------------------
135 template <class dataType>
136 std::shared_ptr<ftm::FTMTree_MT>
137 makeFakeTree(dataType *nodesScalar,
138 std::vector<SimplexId> &nodes,
139 std::vector<std::tuple<ftm::idNode, ftm::idNode>> &arcs) {
140 // Init Scalars
141 auto scalars = std::make_shared<ftm::Scalars>();
142 scalars->size = nodes.size();
143 scalars->values = (void *)nodesScalar;
144
145 // Init Tree
146 auto params = std::make_shared<ftm::Params>();
147 auto tree
148 = std::make_shared<ftm::FTMTree_MT>(params, scalars, ftm::Join_Split);
149 tree->makeAlloc();
150
151 // Add Nodes
152 for(auto i : nodes)
153 tree->makeNode(i);
154
155 // Add Arcs
156 for(std::tuple<ftm::idNode, ftm::idNode> arc : arcs)
157 tree->makeSuperArc(std::get<0>(arc), std::get<1>(arc)); // (down, Up)
158
159 return tree;
160 }
161
162 template <class dataType>
163 ftm::MergeTree<dataType>
164 makeFakeMergeTree(std::vector<dataType> &scalarsVector,
165 std::vector<std::tuple<ftm::idNode, ftm::idNode>> &arcs) {
167 = ttk::ftm::createEmptyMergeTree<dataType>(scalarsVector.size());
168 ttk::ftm::setTreeScalars<dataType>(mergeTree, scalarsVector);
169 ftm::FTMTree_MT *tree = &(mergeTree.tree);
170
171 // Add Nodes
172 for(unsigned int i = 0; i < scalarsVector.size(); ++i)
173 tree->makeNode(i);
174
175 // Add Arcs
176 for(std::tuple<ftm::idNode, ftm::idNode> arc : arcs)
177 tree->makeSuperArc(std::get<0>(arc), std::get<1>(arc)); // (down, Up)
178
179 return mergeTree;
180 }
181
182} // namespace ttk
bool notNeedToNormalize(idNode nodeId)
std::tuple< dataType, dataType > getBirthDeath(idNode nodeId)
bool isParentInconsistent(ftm::idNode nodeId)
idNode getParentSafe(idNode nodeId)
std::stringstream printNode2(idNode nodeId, bool doPrint=true)
idNode makeNode(SimplexId vertexId, SimplexId linked=nullVertex)
Definition: FTMTree_MT.cpp:473
idSuperArc makeSuperArc(idNode downNodeId, idNode upNodeId)
Definition: FTMTree_MT.cpp:499
Node * getNode(idNode nodeId)
Definition: FTMTree_MT.h:393
std::stringstream printTree(bool doPrint=true)
unsigned int idNode
Node index in vect_nodes_.
Definition: FTMDataTypes.h:39
The Topology ToolKit.
dataType getMinMaxLocalFromVector(ftm::FTMTree_MT *tree, ftm::idNode nodeId, std::vector< dataType > &scalarsVector, bool getMin=true)
std::shared_ptr< ftm::FTMTree_MT > makeFakeTree(dataType *nodesScalar, std::vector< SimplexId > &nodes, std::vector< std::tuple< ftm::idNode, ftm::idNode > > &arcs)
ftm::MergeTree< dataType > makeFakeMergeTree(std::vector< dataType > &scalarsVector, std::vector< std::tuple< ftm::idNode, ftm::idNode > > &arcs)
std::tuple< double, double > getNormalizedBirthDeathDouble(ftm::FTMTree_MT *tree, ftm::idNode nodeId, dataType newMin=0.0, dataType newMax=1.0)
dataType getMinMaxLocal(ftm::FTMTree_MT *tree, ftm::idNode nodeId, bool getMin=true)
std::tuple< dataType, dataType > getMinMaxLocalT(ftm::FTMTree_MT *tree, ftm::idNode nodeId)
std::tuple< dataType, dataType > getNormalizedBirthDeath(ftm::FTMTree_MT *tree, ftm::idNode nodeId, dataType newMin=0.0, dataType newMax=1.0)
bool isDeathHigher(ftm::FTMTree_MT *tree1, ftm::idNode tree1Node, ftm::FTMTree_MT *tree2, ftm::idNode tree2Node)
std::tuple< dataType, dataType > getParametrizedBirthDeath(ftm::FTMTree_MT *tree, ftm::idNode node, bool normalize)
ftm::FTMTree_MT tree
Definition: FTMTree_MT.h:901