58 std::vector<dataType> &img2,
59 bool emptyFieldDistance =
false) {
60 size_t const noPoints = img1.size();
62 std::vector<dataType> secondField = img2;
63 if(emptyFieldDistance)
64 secondField = std::vector<dataType>(noPoints, 0);
66 dataType distance = 0;
68 for(
size_t i = 0; i < noPoints; ++i)
69 distance += std::pow((img1[i] - secondField[i]), 2);
71 distance = std::sqrt(distance);
169 std::vector<int> &removed,
171 std::vector<std::vector<dataType>> &barycentersL2) {
172 std::vector<bool> treeRemoved(mTrees.size(),
false);
175 toRemoved = std::min(toRemoved, (
int)(mTrees.size() - 3));
177 std::vector<std::vector<dataType>> images(
fieldL2_.size());
178 for(
size_t i = 0; i <
fieldL2_.size(); ++i)
179 for(
size_t j = 0; j <
fieldL2_[i].size(); ++j)
180 images[i].push_back(
static_cast<dataType
>(
fieldL2_[i][j]));
182 for(
int iter = 0; iter < toRemoved; ++iter) {
183 dataType bestCost = std::numeric_limits<dataType>::max();
184 int bestMiddleIndex = -1;
186 std::vector<std::tuple<ftm::MergeTree<dataType>,
int>>
187 bestBarycentersOnPath;
188 std::vector<dataType> bestBarycenterL2;
189 std::vector<std::tuple<std::vector<dataType>,
int>>
190 bestBarycentersL2OnPath;
193 printMsg(
"Compute barycenter for each pair of trees",
195 unsigned int index1 = 0, index2 = 0;
196 while(index2 != mTrees.size() - 1) {
199 int middleIndex = index1 + 1;
200 while(treeRemoved[middleIndex])
204 index2 = middleIndex + 1;
205 while(treeRemoved[index2])
210 double const alpha =
computeAlpha(index1, middleIndex, index2);
212 std::vector<dataType> barycenterL2;
214 barycenter = computeBarycenter<dataType>(
215 mTrees[index1], mTrees[index2], alpha);
217 barycenterL2 = computeL2Barycenter<dataType>(
218 images[index1], images[index2], alpha);
226 cost = computeDistance<dataType>(barycenter, mTrees[middleIndex]);
229 = computeL2Distance<dataType>(barycenterL2, images[middleIndex]);
232 printMsg(
"Compute distances of previously removed trees",
234 std::vector<std::tuple<ftm::MergeTree<dataType>,
int>>
236 std::vector<std::tuple<std::vector<dataType>,
int>>
238 for(
unsigned int i = 0; i < 2; ++i) {
239 int const toReach = (i == 0 ? index1 : index2);
240 int const offset = (i == 0 ? -1 : 1);
241 int tIndex = middleIndex + offset;
242 while(tIndex != toReach) {
245 double const alphaT =
computeAlpha(index1, tIndex, index2);
247 std::vector<dataType> barycenterPL2;
249 barycenterP = computeBarycenter<dataType>(
250 mTrees[index1], mTrees[index2], alphaT);
252 barycenterPL2 = computeL2Barycenter<dataType>(
253 images[index1], images[index2], alphaT);
258 costP = computeDistance<dataType>(barycenterP, mTrees[tIndex]);
261 = computeL2Distance<dataType>(barycenterPL2, images[tIndex]);
265 barycentersOnPath.push_back(
266 std::make_tuple(barycenterP, tIndex));
268 barycentersL2OnPath.push_back(
269 std::make_tuple(barycenterPL2, tIndex));
275 if(cost < bestCost) {
277 bestMiddleIndex = middleIndex;
279 bestBarycenter = barycenter;
280 bestBarycentersOnPath = barycentersOnPath;
282 bestBarycenterL2 = barycenterL2;
283 bestBarycentersL2OnPath = barycentersL2OnPath;
288 index1 = middleIndex;
294 removed.push_back(bestMiddleIndex);
295 treeRemoved[bestMiddleIndex] =
true;
297 barycenters[bestMiddleIndex] = bestBarycenter;
298 for(
auto &tup : bestBarycentersOnPath)
299 barycenters[std::get<1>(tup)] = std::get<0>(tup);
301 barycentersL2[bestMiddleIndex] = bestBarycenterL2;
302 for(
auto &tup : bestBarycentersL2OnPath)
303 barycentersL2[std::get<1>(tup)] = std::get<0>(tup);
310 std::vector<double> &emptyTreeDistances,
317 for(
unsigned int i = 0; i < mTrees.size(); ++i) {
323 printTreesStats<dataType>(mTrees);
327 std::vector<ftm::MergeTree<dataType>> barycenters(mTrees.size());
328 std::vector<std::vector<dataType>> barycentersL2(mTrees.size());
329 std::vector<int> removed;
332 for(
size_t i = 0; i < mTrees.size(); ++i)
335 temporalSubsampling<dataType>(
336 mTrees, removed, barycenters, barycentersL2);
339 std::vector<std::vector<dataType>> images(
fieldL2_.size());
340 for(
size_t i = 0; i <
fieldL2_.size(); ++i)
341 for(
size_t j = 0; j <
fieldL2_[i].size(); ++j)
342 images[i].push_back(
static_cast<dataType
>(
fieldL2_[i][j]));
344 for(
auto &mt : mTrees)
346 std::vector<bool> removedB(mTrees.size(),
false);
347 for(
auto r : removed)
349 for(
unsigned int i = 0; i < barycenters.size(); ++i)
352 allMT.push_back(barycenters[i]);
354 images.push_back(barycentersL2[i]);
358 unsigned int const distMatSize
360 for(
unsigned int i = 0; i < distMatSize; ++i) {
363 distance = computeDistance<dataType>(allMT[i], allMT[i],
true);
365 distance = computeL2Distance<dataType>(images[i], images[i],
true);
366 emptyTreeDistances.push_back(distance);
371 for(
unsigned int i = 0; i < allMT.size(); ++i)
372 postprocessingPipeline<dataType>(&(allMT[i].tree));
373 for(
unsigned int i = 0; i < mTrees.size(); ++i)
374 postprocessingPipeline<dataType>(&(mTrees[i].tree));
378 std::stringstream ss, ss2, ss3;
379 ss <<
"input size = " << mTrees.size();
381 ss2 <<
"output size = "
382 << mTrees.size() - (distMatSize - mTrees.size());
385 for(
unsigned int i = 0; i < removed.size(); ++i) {
388 if(i < removed.size() - 1)
393 sort(removed.begin(), removed.end());