23 #ifndef BITMAP_CUBICAL_COMPLEX_BASE_H_ 24 #define BITMAP_CUBICAL_COMPLEX_BASE_H_ 26 #include <gudhi/Bitmap_cubical_complex/counter.h> 41 namespace cubical_complex {
65 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::cout <<
"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 unsigned size()
const {
return this->data.size(); }
218 template <
typename K>
219 friend std::ostream& operator<<(std::ostream& os, const Bitmap_cubical_complex_base<K>& b);
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::cout << 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);
526 template <
typename T>
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::cerr <<
"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::cerr <<
"After binning : " << this->data[i] << std::endl;
545 template <
typename T>
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::cerr <<
"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::cerr <<
"After binning : " << this->data[i] << std::endl;
563 template <
typename T>
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];
573 template <
typename K>
574 std::ostream& operator<<(std::ostream& out, const Bitmap_cubical_complex_base<K>& b) {
576 it != b.all_cells_const_end(); ++it) {
582 template <
typename T>
584 this->set_up_containers(sizes);
587 template <
typename T>
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.");
609 std::size_t index = 0;
617 template <
typename T>
619 const std::vector<T>& top_dimensional_cells) {
620 this->setup_bitmap_based_on_top_dimensional_cells_list(sizes_in_following_directions, top_dimensional_cells);
623 template <
typename T>
626 std::ifstream inFiltration;
627 inFiltration.open(perseus_style_file);
628 unsigned dimensionOfData;
629 inFiltration >> dimensionOfData;
632 std::cerr <<
"dimensionOfData : " << dimensionOfData << std::endl;
635 std::vector<unsigned> sizes;
636 sizes.reserve(dimensionOfData);
638 std::size_t dimensions = 1;
639 for (std::size_t i = 0; i != dimensionOfData; ++i) {
640 unsigned size_in_this_dimension;
641 inFiltration >> size_in_this_dimension;
642 sizes.push_back(size_in_this_dimension);
643 dimensions *= size_in_this_dimension;
645 std::cerr <<
"size_in_this_dimension : " << size_in_this_dimension << std::endl;
648 this->set_up_containers(sizes);
654 for (std::size_t i = 0; i < dimensions; ++i) {
655 if (!(inFiltration >> filtrationLevel) || (inFiltration.eof())) {
656 throw std::ios_base::failure(
"Bad Perseus file format.");
659 std::cerr <<
"Cell of an index : " << it.compute_index_in_bitmap()
661 <<
" get the value : " << filtrationLevel << std::endl;
667 inFiltration.close();
671 template <
typename T>
673 std::vector<bool> directions) {
677 this->read_perseus_style_file(perseus_style_file);
680 template <
typename T>
682 std::vector<bool> directions) {
686 this->set_up_containers(sizes);
689 template <
typename T>
691 const std::vector<T>& top_dimensional_cells,
692 std::vector<bool> directions) {
696 this->setup_bitmap_based_on_top_dimensional_cells_list(dimensions, top_dimensional_cells);
699 template <
typename T>
701 this->read_perseus_style_file(perseus_style_file);
704 template <
typename T>
706 std::vector<std::size_t> boundary_elements;
709 boundary_elements.reserve(this->
dimension() * 2);
711 std::size_t sum_of_dimensions = 0;
712 std::size_t cell1 = cell;
713 for (std::size_t i = this->multipliers.size(); i != 0; --i) {
714 unsigned position = cell1 / this->multipliers[i - 1];
715 if (position % 2 == 1) {
716 if (sum_of_dimensions % 2) {
717 boundary_elements.push_back(cell + this->multipliers[i - 1]);
718 boundary_elements.push_back(cell - this->multipliers[i - 1]);
720 boundary_elements.push_back(cell - this->multipliers[i - 1]);
721 boundary_elements.push_back(cell + this->multipliers[i - 1]);
725 cell1 = cell1 % this->multipliers[i - 1];
728 return boundary_elements;
731 template <
typename T>
733 std::vector<unsigned>
counter = this->compute_counter_for_given_cell(cell);
734 std::vector<std::size_t> coboundary_elements;
735 std::size_t cell1 = cell;
736 for (std::size_t i = this->multipliers.size(); i != 0; --i) {
737 unsigned position = cell1 / this->multipliers[i - 1];
738 if (position % 2 == 0) {
739 if ((cell > this->multipliers[i - 1]) && (counter[i - 1] != 0)) {
740 coboundary_elements.push_back(cell - this->multipliers[i - 1]);
742 if ((cell + this->multipliers[i - 1] < this->data.size()) && (counter[i - 1] != 2 * this->sizes[i - 1])) {
743 coboundary_elements.push_back(cell + this->multipliers[i - 1]);
746 cell1 = cell1 % this->multipliers[i - 1];
748 return coboundary_elements;
751 template <
typename T>
754 if (dbg) std::cerr <<
"\n\n\n Computing position o a cell of an index : " << cell << std::endl;
756 for (std::size_t i = this->multipliers.size(); i != 0; --i) {
757 unsigned position = cell / this->multipliers[i - 1];
760 std::cerr <<
"i-1 :" << i - 1 << std::endl;
761 std::cerr <<
"cell : " << cell << std::endl;
762 std::cerr <<
"position : " << position << std::endl;
763 std::cerr <<
"multipliers[" << i - 1 <<
"] = " << this->multipliers[i - 1] << std::endl;
766 if (position % 2 == 1) {
767 if (dbg) std::cerr <<
"Nonzero length in this direction \n";
770 cell = cell % this->multipliers[i - 1];
775 template <
typename T>
777 return this->data[cell];
780 template <
typename T>
785 std::vector<bool> is_this_cell_considered(this->data.size(),
false);
787 std::size_t size_to_reserve = 1;
788 for (std::size_t i = 0; i != this->multipliers.size(); ++i) {
789 size_to_reserve *= (std::size_t)((this->multipliers[i] - 1) / 2);
792 std::vector<std::size_t> indices_to_consider;
793 indices_to_consider.reserve(size_to_reserve);
798 indices_to_consider.push_back(it.compute_index_in_bitmap());
801 while (indices_to_consider.size()) {
803 std::cerr <<
"indices_to_consider in this iteration \n";
804 for (std::size_t i = 0; i != indices_to_consider.size(); ++i) {
805 std::cout << indices_to_consider[i] <<
" ";
808 std::vector<std::size_t> new_indices_to_consider;
809 for (std::size_t i = 0; i != indices_to_consider.size(); ++i) {
811 for (std::size_t boundaryIt = 0; boundaryIt != bd.size(); ++boundaryIt) {
813 std::cerr <<
"filtration of a cell : " << bd[boundaryIt] <<
" is : " << this->data[bd[boundaryIt]]
814 <<
" while of a cell: " << indices_to_consider[i] <<
" is: " << this->data[indices_to_consider[i]]
817 if (this->data[bd[boundaryIt]] > this->data[indices_to_consider[i]]) {
818 this->data[bd[boundaryIt]] = this->data[indices_to_consider[i]];
820 std::cerr <<
"Setting the value of a cell : " << bd[boundaryIt]
821 <<
" to : " << this->data[indices_to_consider[i]] << std::endl;
824 if (is_this_cell_considered[bd[boundaryIt]] ==
false) {
825 new_indices_to_consider.push_back(bd[boundaryIt]);
826 is_this_cell_considered[bd[boundaryIt]] =
true;
830 indices_to_consider.swap(new_indices_to_consider);
834 template <
typename T>
835 bool compareFirstElementsOfTuples(
const std::pair<std::pair<T, std::size_t>,
char>& first,
836 const std::pair<std::pair<T, std::size_t>,
char>& second) {
837 if (first.first.first < second.first.first) {
840 if (first.first.first > second.first.first) {
844 return first.second < second.second;
850 namespace Cubical_complex = cubical_complex;
854 #endif // BITMAP_CUBICAL_COMPLEX_BASE_H_ void impose_lower_star_filtration()
Definition: Bitmap_cubical_complex_base.h:781
unsigned size() const
Definition: Bitmap_cubical_complex_base.h:212
unsigned dimension() const
Definition: Bitmap_cubical_complex_base.h:207
void put_data_to_bins(std::size_t number_of_bins)
Definition: Bitmap_cubical_complex_base.h:527
T & get_cell_data(std::size_t cell)
Definition: Bitmap_cubical_complex_base.h:776
Boundary_range boundary_range(std::size_t sh)
Definition: Bitmap_cubical_complex_base.h:340
Cubical complex represented as a bitmap, class with basic implementation.
Definition: Bitmap_cubical_complex_base.h:63
virtual ~Bitmap_cubical_complex_base()
Definition: Bitmap_cubical_complex_base.h:94
unsigned get_dimension_of_a_cell(std::size_t cell) const
Definition: Bitmap_cubical_complex_base.h:752
All_cells_iterator all_cells_iterator_begin()
Definition: Bitmap_cubical_complex_base.h:299
Definition: SimplicialComplexForAlpha.h:26
virtual int compute_incidence_between_cells(std::size_t coface, std::size_t face) const
Definition: Bitmap_cubical_complex_base.h:143
This is an implementation of a counter being a vector of integers.
Definition: counter.h:44
Bitmap_cubical_complex_base()
Definition: Bitmap_cubical_complex_base.h:70
Top_dimensional_cells_iterator top_dimensional_cells_iterator_end()
Definition: Bitmap_cubical_complex_base.h:444
Top_dimensional_cells_iterator top_dimensional_cells_iterator_begin()
Definition: Bitmap_cubical_complex_base.h:436
Top_dimensional_cells_iterator_range class provides ranges for Top_dimensional_cells_iterator_range.
Definition: Bitmap_cubical_complex_base.h:456
Coboundary_range coboundary_range(std::size_t sh)
Definition: Bitmap_cubical_complex_base.h:352
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
Iterator through all cells in the complex (in order they appear in the structure – i...
Definition: Bitmap_cubical_complex_base.h:254
std::pair< T, T > min_max_filtration()
Definition: Bitmap_cubical_complex_base.h:564
All_cells_range class provides ranges for All_cells_iterator.
Definition: Bitmap_cubical_complex_base.h:316
std::vector< std::size_t >::const_iterator Boundary_iterator
Definition: Bitmap_cubical_complex_base.h:333
virtual std::vector< std::size_t > get_coboundary_of_a_cell(std::size_t cell) const
Definition: Bitmap_cubical_complex_base.h:732
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
virtual std::vector< std::size_t > get_boundary_of_a_cell(std::size_t cell) const
Definition: Bitmap_cubical_complex_base.h:705