Bar_code_persistence.h
1 /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2  * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3  * Author(s): David Salinas
4  *
5  * Copyright (C) 2014 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #include <math.h> // isfinite
12 
13 #include <QtGui/QApplication>
14 
15 #include <QGraphicsView>
16 #include <QGraphicsScene>
17 #include <QPointF>
18 #include <QVector>
19 #include <QGraphicsTextItem>
20 
21 #include <iostream>
22 #include <vector>
23 #include <limits> // NaN, infinity
24 #include <utility> // for pair
25 #include <string>
26 
27 #ifndef UTILS_BAR_CODE_PERSISTENCE_H_
28 #define UTILS_BAR_CODE_PERSISTENCE_H_
29 
30 class Bar_code_persistence {
31  private:
32  typedef std::vector<std::pair<double, double>> Persistence;
33  Persistence persistence_vector;
34  double min_birth;
35  double max_death;
36 
37  public:
38  Bar_code_persistence()
39  : min_birth(std::numeric_limits<double>::quiet_NaN()),
40  max_death(std::numeric_limits<double>::quiet_NaN()) { }
41 
42  void insert(double birth, double death) {
43  persistence_vector.push_back(std::make_pair(birth, death));
44  if (std::isfinite(birth)) {
45  if ((birth < min_birth) || (std::isnan(min_birth)))
46  min_birth = birth;
47  if ((birth > max_death) || (std::isnan(max_death)))
48  max_death = birth;
49  }
50  if (std::isfinite(death))
51  if ((death > max_death) || (std::isnan(max_death)))
52  max_death = death;
53  }
54 
55  void show(const std::string& window_title) {
56  // Create a view, put a scene in it
57  QGraphicsView * view = new QGraphicsView();
58  QGraphicsScene * scene = new QGraphicsScene();
59  view->setScene(scene);
60  double ratio = 600.0 / (max_death - min_birth);
61  // std::clog << "min_birth=" << min_birth << " - max_death=" << max_death << " - ratio=" << ratio << std::endl;
62 
63  double height = 0.0, birth = 0.0, death = 0.0;
64  int pers_num = 1;
65  for (auto& persistence : persistence_vector) {
66  height = 5.0 * pers_num;
67  // std::clog << "[" << pers_num << "] birth=" << persistence.first << " - death=" << persistence.second << std::endl;
68  if (std::isfinite(persistence.first))
69  birth = ((persistence.first - min_birth) * ratio) + 50.0;
70  else
71  birth = 0.0;
72 
73  if (std::isfinite(persistence.second))
74  death = ((persistence.second - min_birth) * ratio) + 50.0;
75  else
76  death = 700.0;
77 
78  scene->addLine(birth, height, death, height, QPen(Qt::blue, 2));
79  pers_num++;
80  }
81  height += 10.0;
82  // scale line
83  scene->addLine(0, height, 700.0, height, QPen(Qt::black, 1));
84  int modulo = 0;
85  for (double scale = 50.0; scale < 700.0; scale += 50.0) {
86  modulo++;
87  // scale small dash
88  scene->addLine(scale, height - 3.0, scale, height + 3.0, QPen(Qt::black, 1));
89  // scale text
90  QString scale_value = QString::number(((scale - 50.0) / ratio) + min_birth);
91  QGraphicsTextItem* dimText = scene->addText(scale_value, QFont("Helvetica", 8));
92  dimText->setPos(scale - (3.0 * scale_value.size()), height + 9.0 * (modulo % 2));
93  }
94  view->setWindowTitle(window_title.c_str());
95  // Show the view
96  view->show();
97  }
98 };
99 
100 #endif // UTILS_BAR_CODE_PERSISTENCE_H_
GUDHIdev  Version 3.5.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Fri Jan 14 2022 18:28:42 for GUDHIdev by Doxygen 1.9.1