11#ifndef BITMAP_CUBICAL_COMPLEX_BASE_H_
12#define BITMAP_CUBICAL_COMPLEX_BASE_H_
14#include <gudhi/Bitmap_cubical_complex/counter.h>
16#include <boost/config.hpp>
31namespace cubical_complex {
55 typedef T filtration_type;
145 std::vector<unsigned> coface_counter = this->compute_counter_for_given_cell(coface);
146 std::vector<unsigned> face_counter = this->compute_counter_for_given_cell(face);
149 int number_of_position_in_which_counters_do_not_agree = -1;
150 std::size_t number_of_full_faces_that_comes_before = 0;
151 for (std::size_t i = 0; i != coface_counter.size(); ++i) {
152 if ((coface_counter[i] % 2 == 1) && (number_of_position_in_which_counters_do_not_agree == -1)) {
153 ++number_of_full_faces_that_comes_before;
155 if (coface_counter[i] != face_counter[i]) {
156 if (number_of_position_in_which_counters_do_not_agree != -1) {
157 std::cerr <<
"Cells given to compute_incidence_between_cells procedure do not form a pair of coface-face.\n";
158 throw std::logic_error(
159 "Cells given to compute_incidence_between_cells procedure do not form a pair of coface-face.");
161 number_of_position_in_which_counters_do_not_agree = i;
166 if (number_of_full_faces_that_comes_before % 2) incidence = -1;
168 if (coface_counter[number_of_position_in_which_counters_do_not_agree] + 1 ==
169 face_counter[number_of_position_in_which_counters_do_not_agree]) {
207 inline unsigned dimension()
const {
return sizes.size(); }
212 inline std::size_t
size()
const {
return this->data.size(); }
218 template <
typename K>
276 if (this->
counter != rhs.counter)
return false;
289 std::size_t operator*() {
return this->
counter; }
309 a.counter = this->data.size();
334 typedef typename std::vector<std::size_t> Boundary_range;
346 typedef typename std::vector<std::size_t> Coboundary_range;
368 while ((dim != this->b.
dimension()) && (this->counter[dim] == this->b.sizes[dim] - 1)) ++dim;
372 for (std::size_t i = 0; i != dim; ++i) {
394 if (&this->b != &rhs.b)
return false;
395 if (this->
counter.size() != rhs.counter.size())
return false;
396 for (std::size_t i = 0; i != this->
counter.size(); ++i) {
397 if (this->
counter[i] != rhs.counter[i])
return false;
411 std::size_t operator*() {
return this->compute_index_in_bitmap(); }
413 std::size_t compute_index_in_bitmap()
const {
414 std::size_t index = 0;
415 for (std::size_t i = 0; i != this->
counter.size(); ++i) {
416 index += (2 * this->
counter[i] + 1) * this->b.multipliers[i];
421 void print_counter()
const {
422 for (std::size_t i = 0; i != this->
counter.size(); ++i) {
423 std::clog << this->
counter[i] <<
" ";
429 std::vector<std::size_t>
counter;
446 for (std::size_t i = 0; i != this->
dimension(); ++i) {
447 a.counter[i] = this->sizes[i] - 1;
475 inline std::size_t number_cells()
const {
return this->total_number_of_cells; }
483 std::vector<unsigned> sizes;
484 std::vector<unsigned> multipliers;
486 std::size_t total_number_of_cells;
488 void set_up_containers(
const std::vector<unsigned>& sizes) {
489 unsigned multiplier = 1;
490 for (std::size_t i = 0; i != sizes.size(); ++i) {
491 this->sizes.push_back(sizes[i]);
492 this->multipliers.push_back(multiplier);
493 multiplier *= 2 * sizes[i] + 1;
495 this->data = std::vector<T>(multiplier, std::numeric_limits<T>::infinity());
496 this->total_number_of_cells = multiplier;
499 std::size_t compute_position_in_bitmap(
const std::vector<unsigned>& counter) {
500 std::size_t position = 0;
501 for (std::size_t i = 0; i != this->multipliers.size(); ++i) {
502 position += this->multipliers[i] * counter[i];
507 std::vector<unsigned> compute_counter_for_given_cell(std::size_t cell)
const {
508 std::vector<unsigned> counter;
509 counter.reserve(this->sizes.size());
510 for (std::size_t dim = this->sizes.size(); dim != 0; --dim) {
511 counter.push_back(cell / this->multipliers[dim - 1]);
512 cell = cell % this->multipliers[dim - 1];
514 std::reverse(counter.begin(), counter.end());
517 void read_perseus_style_file(
const char* perseus_style_file);
518 void setup_bitmap_based_on_top_dimensional_cells_list(
const std::vector<unsigned>& sizes_in_following_directions,
519 const std::vector<T>& top_dimensional_cells);
523 std::vector<bool> directions);
530 std::pair<T, T> min_max = this->min_max_filtration();
531 T dx = (min_max.second - min_max.first) / (T)number_of_bins;
534 for (std::size_t i = 0; i != this->data.size(); ++i) {
536 std::clog <<
"Before binning : " << this->data[i] << std::endl;
538 this->data[i] = min_max.first + dx * (this->data[i] - min_max.first) / number_of_bins;
540 std::clog <<
"After binning : " << this->data[i] << std::endl;
548 std::pair<T, T> min_max = this->min_max_filtration();
550 std::size_t number_of_bins = (min_max.second - min_max.first) / diameter_of_bin;
552 for (std::size_t i = 0; i != this->data.size(); ++i) {
554 std::clog <<
"Before binning : " << this->data[i] << std::endl;
556 this->data[i] = min_max.first + diameter_of_bin * (this->data[i] - min_max.first) / number_of_bins;
558 std::clog <<
"After binning : " << this->data[i] << std::endl;
565 std::pair<T, T> min_max(std::numeric_limits<T>::infinity(), -std::numeric_limits<T>::infinity());
566 for (std::size_t i = 0; i != this->data.size(); ++i) {
567 if (this->data[i] < min_max.first) min_max.first = this->data[i];
568 if (this->data[i] > min_max.second) min_max.second = this->data[i];
576 it != b.all_cells_const_end(); ++it) {
584 this->set_up_containers(sizes);
589 const std::vector<unsigned>& sizes_in_following_directions,
const std::vector<T>& top_dimensional_cells) {
590 this->set_up_containers(sizes_in_following_directions);
592 std::size_t number_of_top_dimensional_elements = 1;
593 for (std::size_t i = 0; i != sizes_in_following_directions.size(); ++i) {
594 number_of_top_dimensional_elements *= sizes_in_following_directions[i];
596 if (number_of_top_dimensional_elements != top_dimensional_cells.size()) {
597 std::cerr <<
"Error in constructor Bitmap_cubical_complex_base ( std::vector<std::size_t> "
598 <<
"sizes_in_following_directions, std::vector<T> top_dimensional_cells ). Number of top dimensional "
599 <<
"elements that follow from sizes_in_following_directions vector is different than the size of "
600 <<
"top_dimensional_cells vector."
603 "Error in constructor Bitmap_cubical_complex_base( std::vector<std::size_t> sizes_in_following_directions,"
604 "std::vector<T> top_dimensional_cells ). Number of top dimensional elements that follow from "
605 "sizes_in_following_directions vector is different than the size of top_dimensional_cells vector.");
608 Bitmap_cubical_complex_base<T>::Top_dimensional_cells_iterator it(*
this);
609 std::size_t index = 0;
610 for (it = this->top_dimensional_cells_iterator_begin(); it != this->top_dimensional_cells_iterator_end(); ++it) {
611 this->get_cell_data(*it) = top_dimensional_cells[index];
614 this->impose_lower_star_filtration();
619 if (this->get_dimension_of_a_cell(splx) == this->dimension()){
return splx;}
621 for (
auto v : this->get_coboundary_of_a_cell(splx)){
622 if(this->get_cell_data(v) == this->get_cell_data(splx)){
623 return this->get_top_dimensional_coface_of_a_cell(v);
627 BOOST_UNREACHABLE_RETURN(-2);
632 const std::vector<T>& top_dimensional_cells) {
633 this->setup_bitmap_based_on_top_dimensional_cells_list(sizes_in_following_directions, top_dimensional_cells);
639 std::ifstream inFiltration;
640 inFiltration.open(perseus_style_file);
641 unsigned dimensionOfData;
642 inFiltration >> dimensionOfData;
645 std::clog <<
"dimensionOfData : " << dimensionOfData << std::endl;
648 std::vector<unsigned> sizes;
649 sizes.reserve(dimensionOfData);
651 std::size_t dimensions = 1;
652 for (std::size_t i = 0; i != dimensionOfData; ++i) {
653 unsigned size_in_this_dimension;
654 inFiltration >> size_in_this_dimension;
655 sizes.push_back(size_in_this_dimension);
656 dimensions *= size_in_this_dimension;
658 std::clog <<
"size_in_this_dimension : " << size_in_this_dimension << std::endl;
661 this->set_up_containers(sizes);
663 Bitmap_cubical_complex_base<T>::Top_dimensional_cells_iterator it(*
this);
664 it = this->top_dimensional_cells_iterator_begin();
666 T filtrationLevel = 0.;
667 std::size_t filtration_counter = 0;
668 while (!inFiltration.eof()) {
670 getline(inFiltration, line);
671 if (line.length() != 0) {
672 int n = sscanf(line.c_str(),
"%lf", &filtrationLevel);
674 std::string perseus_error(
"Bad Perseus file format. This line is incorrect : " + line);
675 throw std::ios_base::failure(perseus_error.c_str());
679 std::clog <<
"Cell of an index : " << it.compute_index_in_bitmap()
680 <<
" and dimension: " << this->get_dimension_of_a_cell(it.compute_index_in_bitmap())
681 <<
" get the value : " << filtrationLevel << std::endl;
683 this->get_cell_data(*it) = filtrationLevel;
685 ++filtration_counter;
689 if (filtration_counter != dimensions) {
690 std::string perseus_error(
"Bad Perseus file format. Read " + std::to_string(filtration_counter) +
" expected " + \
691 std::to_string(dimensions) +
" values");
692 throw std::ios_base::failure(perseus_error.c_str());
695 inFiltration.close();
696 this->impose_lower_star_filtration();
701 std::vector<bool> directions) {
705 this->read_perseus_style_file(perseus_style_file);
710 std::vector<bool> directions) {
714 this->set_up_containers(sizes);
719 const std::vector<T>& top_dimensional_cells,
720 std::vector<bool> directions) {
724 this->setup_bitmap_based_on_top_dimensional_cells_list(dimensions, top_dimensional_cells);
729 this->read_perseus_style_file(perseus_style_file);
734 std::vector<std::size_t> boundary_elements;
737 boundary_elements.reserve(this->dimension() * 2);
739 std::size_t sum_of_dimensions = 0;
740 std::size_t cell1 = cell;
741 for (std::size_t i = this->multipliers.size(); i != 0; --i) {
742 unsigned position = cell1 / this->multipliers[i - 1];
743 if (position % 2 == 1) {
744 if (sum_of_dimensions % 2) {
745 boundary_elements.push_back(cell + this->multipliers[i - 1]);
746 boundary_elements.push_back(cell - this->multipliers[i - 1]);
748 boundary_elements.push_back(cell - this->multipliers[i - 1]);
749 boundary_elements.push_back(cell + this->multipliers[i - 1]);
753 cell1 = cell1 % this->multipliers[i - 1];
756 return boundary_elements;
761 std::vector<unsigned>
counter = this->compute_counter_for_given_cell(cell);
762 std::vector<std::size_t> coboundary_elements;
763 std::size_t cell1 = cell;
764 for (std::size_t i = this->multipliers.size(); i != 0; --i) {
765 unsigned position = cell1 / this->multipliers[i - 1];
766 if (position % 2 == 0) {
767 if ((cell > this->multipliers[i - 1]) && (
counter[i - 1] != 0)) {
768 coboundary_elements.push_back(cell - this->multipliers[i - 1]);
770 if ((cell + this->multipliers[i - 1] < this->data.size()) && (
counter[i - 1] != 2 * this->sizes[i - 1])) {
771 coboundary_elements.push_back(cell + this->multipliers[i - 1]);
774 cell1 = cell1 % this->multipliers[i - 1];
776 return coboundary_elements;
782 if (dbg) std::clog <<
"\n\n\n Computing position o a cell of an index : " << cell << std::endl;
783 unsigned dimension = 0;
784 for (std::size_t i = this->multipliers.size(); i != 0; --i) {
785 unsigned position = cell / this->multipliers[i - 1];
788 std::clog <<
"i-1 :" << i - 1 << std::endl;
789 std::clog <<
"cell : " << cell << std::endl;
790 std::clog <<
"position : " << position << std::endl;
791 std::clog <<
"multipliers[" << i - 1 <<
"] = " << this->multipliers[i - 1] << std::endl;
794 if (position % 2 == 1) {
795 if (dbg) std::clog <<
"Nonzero length in this direction \n";
798 cell = cell % this->multipliers[i - 1];
805 return this->data[cell];
813 std::vector<bool> is_this_cell_considered(this->data.size(),
false);
815 std::size_t size_to_reserve = 1;
816 for (std::size_t i = 0; i != this->multipliers.size(); ++i) {
817 size_to_reserve *= (std::size_t)((this->multipliers[i] - 1) / 2);
820 std::vector<std::size_t> indices_to_consider;
821 indices_to_consider.reserve(size_to_reserve);
825 for (it = this->top_dimensional_cells_iterator_begin(); it != this->top_dimensional_cells_iterator_end(); ++it) {
826 indices_to_consider.push_back(it.compute_index_in_bitmap());
829 while (indices_to_consider.size()) {
831 std::clog <<
"indices_to_consider in this iteration \n";
832 for (std::size_t i = 0; i != indices_to_consider.size(); ++i) {
833 std::clog << indices_to_consider[i] <<
" ";
836 std::vector<std::size_t> new_indices_to_consider;
837 for (std::size_t i = 0; i != indices_to_consider.size(); ++i) {
838 std::vector<std::size_t> bd = this->get_boundary_of_a_cell(indices_to_consider[i]);
839 for (std::size_t boundaryIt = 0; boundaryIt != bd.size(); ++boundaryIt) {
841 std::clog <<
"filtration of a cell : " << bd[boundaryIt] <<
" is : " << this->data[bd[boundaryIt]]
842 <<
" while of a cell: " << indices_to_consider[i] <<
" is: " << this->data[indices_to_consider[i]]
845 if (this->data[bd[boundaryIt]] > this->data[indices_to_consider[i]]) {
846 this->data[bd[boundaryIt]] = this->data[indices_to_consider[i]];
848 std::clog <<
"Setting the value of a cell : " << bd[boundaryIt]
849 <<
" to : " << this->data[indices_to_consider[i]] << std::endl;
852 if (is_this_cell_considered[bd[boundaryIt]] ==
false) {
853 new_indices_to_consider.push_back(bd[boundaryIt]);
854 is_this_cell_considered[bd[boundaryIt]] =
true;
858 indices_to_consider.swap(new_indices_to_consider);
863bool compareFirstElementsOfTuples(
const std::pair<std::pair<T, std::size_t>,
char>& first,
864 const std::pair<std::pair<T, std::size_t>,
char>& second) {
865 if (first.first.first < second.first.first) {
868 if (first.first.first > second.first.first) {
872 return first.second < second.second;
878namespace Cubical_complex = cubical_complex;
Iterator through all cells in the complex (in order they appear in the structure – i....
Definition: Bitmap_cubical_complex_base.h:254
All_cells_range class provides ranges for All_cells_iterator.
Definition: Bitmap_cubical_complex_base.h:316
Iterator through top dimensional cells of the complex. The cells appear in order they are stored in t...
Definition: Bitmap_cubical_complex_base.h:358
Top_dimensional_cells_iterator_range class provides ranges for Top_dimensional_cells_iterator_range.
Definition: Bitmap_cubical_complex_base.h:456
Cubical complex represented as a bitmap, class with basic implementation.
Definition: Bitmap_cubical_complex_base.h:53
All_cells_iterator all_cells_iterator_end()
Definition: Bitmap_cubical_complex_base.h:307
std::vector< std::size_t >::const_iterator Coboundary_iterator
Definition: Bitmap_cubical_complex_base.h:345
Bitmap_cubical_complex_base()
Definition: Bitmap_cubical_complex_base.h:60
void put_data_to_bins(std::size_t number_of_bins)
Definition: Bitmap_cubical_complex_base.h:527
void impose_lower_star_filtration()
Definition: Bitmap_cubical_complex_base.h:809
std::size_t size() const
Definition: Bitmap_cubical_complex_base.h:212
All_cells_iterator all_cells_iterator_begin()
Definition: Bitmap_cubical_complex_base.h:299
virtual std::vector< std::size_t > get_coboundary_of_a_cell(std::size_t cell) const
Definition: Bitmap_cubical_complex_base.h:760
unsigned get_dimension_of_a_cell(std::size_t cell) const
Definition: Bitmap_cubical_complex_base.h:780
unsigned dimension() const
Definition: Bitmap_cubical_complex_base.h:207
Top_dimensional_cells_iterator top_dimensional_cells_iterator_begin()
Definition: Bitmap_cubical_complex_base.h:436
friend std::ostream & operator<<(std::ostream &os, const Bitmap_cubical_complex_base< K > &b)
Definition: Bitmap_cubical_complex_base.h:574
virtual std::vector< std::size_t > get_boundary_of_a_cell(std::size_t cell) const
Definition: Bitmap_cubical_complex_base.h:733
Coboundary_range coboundary_range(std::size_t sh)
Definition: Bitmap_cubical_complex_base.h:352
std::pair< T, T > min_max_filtration()
Definition: Bitmap_cubical_complex_base.h:564
Top_dimensional_cells_iterator top_dimensional_cells_iterator_end()
Definition: Bitmap_cubical_complex_base.h:444
std::vector< std::size_t >::const_iterator Boundary_iterator
Definition: Bitmap_cubical_complex_base.h:333
size_t get_top_dimensional_coface_of_a_cell(size_t splx)
Definition: Bitmap_cubical_complex_base.h:618
T & get_cell_data(std::size_t cell)
Definition: Bitmap_cubical_complex_base.h:804
Boundary_range boundary_range(std::size_t sh)
Definition: Bitmap_cubical_complex_base.h:340
virtual int compute_incidence_between_cells(std::size_t coface, std::size_t face) const
Definition: Bitmap_cubical_complex_base.h:143
virtual ~Bitmap_cubical_complex_base()
Definition: Bitmap_cubical_complex_base.h:84
This is an implementation of a counter being a vector of integers.
Definition: counter.h:32
std::ostream & operator<<(std::ostream &os, const Permutahedral_representation< Vertex, OrderedSetPartition > &simplex)
Print a permutahedral representation to a stream.
Definition: Permutahedral_representation.h:173