| 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): Hannah Schreiber |
| 4 |
* |
| 5 |
* Copyright (C) 2023 Inria |
| 6 |
* |
| 7 |
* Modification(s): |
| 8 |
* - YYYY/MM Author: Description of the modification |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <iostream> |
| 12 |
#include <ostream> |
| 13 |
#include <fstream> |
| 14 |
#include <string> |
| 15 |
|
| 16 |
#include <gudhi/filtered_zigzag_persistence.h> |
| 17 |
|
| 18 |
using ZP = Gudhi::zigzag_persistence::Filtered_zigzag_persistence<>; |
| 19 |
using ID_handle = ZP::Cell_key; |
| 20 |
using Filtration_value = ZP::Filtration_value; |
| 21 |
using Dimension = ZP::Dimension; |
| 22 |
|
| 23 |
enum lineType : int { INCLUSION, REMOVAL, COMMENT }; |
| 24 |
|
| 25 |
lineType read_operation(std::string& line, std::vector<ID_handle>& cells, double& timestamp) { |
| 26 |
lineType type; |
| 27 |
cells.clear(); |
| 28 |
ID_handle num; |
| 29 |
|
| 30 |
size_t current = line.find_first_not_of(' ', 0); |
| 31 |
if (current == std::string::npos) return COMMENT; |
| 32 |
|
| 33 |
if (line[current] == 'i') |
| 34 |
type = INCLUSION; |
| 35 |
else if (line[current] == 'r') |
| 36 |
type = REMOVAL; |
| 37 |
else if (line[current] == '#') |
| 38 |
return COMMENT; |
| 39 |
else { |
| 40 |
std::clog << "(1) Syntaxe error in file." << std::endl; |
| 41 |
exit(0); |
| 42 |
} |
| 43 |
|
| 44 |
current = line.find_first_not_of(' ', current + 1); |
| 45 |
if (current == std::string::npos) { |
| 46 |
std::clog << "(2) Syntaxe error in file." << std::endl; |
| 47 |
exit(0); |
| 48 |
} |
| 49 |
size_t next = line.find_first_of(' ', current); |
| 50 |
timestamp = std::stod(line.substr(current, next - current)); |
| 51 |
|
| 52 |
current = line.find_first_not_of(' ', next); |
| 53 |
while (current != std::string::npos) { |
| 54 |
next = line.find_first_of(' ', current); |
| 55 |
num = std::stoi(line.substr(current, next - current)); |
| 56 |
cells.push_back(num); |
| 57 |
current = line.find_first_not_of(' ', next); |
| 58 |
} |
| 59 |
|
| 60 |
return type; |
| 61 |
} |
| 62 |
|
| 63 |
//example of input file: example/zigzag_filtration_example.txt |
| 64 |
int main(int argc, char* const argv[]) { |
| 65 |
if (argc != 2) { |
| 66 |
if (argc < 2) |
| 67 |
std::clog << "Missing argument: input file name is needed." << std::endl; |
| 68 |
else |
| 69 |
std::clog << "Too many arguments: only input file name is needed." << std::endl; |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
std::string line; |
| 74 |
std::ifstream file(argv[1]); |
| 75 |
|
| 76 |
//std::cout could be replaced by any other output stream |
| 77 |
ZP zp([](Dimension dim, Filtration_value birth, Filtration_value death) { |
| 78 |
std::cout << "[" << dim << "] "; |
| 79 |
std::cout << birth << " - " << death; |
| 80 |
std::cout << std::endl; |
| 81 |
}); |
| 82 |
|
| 83 |
if (file.is_open()) { |
| 84 |
std::vector<ID_handle> data; |
| 85 |
unsigned int id = 0; |
| 86 |
double timestamp; |
| 87 |
lineType type; |
| 88 |
|
| 89 |
while (getline(file, line, '\n') && read_operation(line, data, timestamp) == COMMENT); |
| 90 |
double lastTimestamp = timestamp; |
| 91 |
// first operation has to be an insertion. |
| 92 |
zp.insert_cell(id, data, 0, timestamp); |
| 93 |
|
| 94 |
while (getline(file, line, '\n')) { |
| 95 |
type = read_operation(line, data, timestamp); |
| 96 |
if (type != COMMENT && lastTimestamp != timestamp) { |
| 97 |
lastTimestamp = timestamp; |
| 98 |
} |
| 99 |
|
| 100 |
if (type == INCLUSION) { |
| 101 |
++id; |
| 102 |
int dim = data.size() == 0 ? 0 : data.size() - 1; |
| 103 |
zp.insert_cell(id, data, dim, timestamp); |
| 104 |
} else if (type == REMOVAL) { |
| 105 |
++id; |
| 106 |
zp.remove_cell(data[0], timestamp); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
file.close(); |
| 111 |
} else { |
| 112 |
std::clog << "Unable to open input file." << std::endl; |
| 113 |
file.setstate(std::ios::failbit); |
| 114 |
} |
| 115 |
|
| 116 |
//retrieve infinite bars remaining at the end |
| 117 |
//again std::cout could be replaced by any other output stream |
| 118 |
zp.get_current_infinite_intervals([](Dimension dim, Filtration_value birth) { |
| 119 |
std::cout << "[" << dim << "] "; |
| 120 |
std::cout << birth << " - inf"; |
| 121 |
std::cout << std::endl; |
| 122 |
}); |
| 123 |
|
| 124 |
return 0; |
| 125 |
} |