11 #ifndef BITMAP_CUBICAL_COMPLEX_BASE_H_ 12 #define BITMAP_CUBICAL_COMPLEX_BASE_H_ 14 #include <gudhi/Bitmap_cubical_complex/counter.h> 29 namespace cubical_complex {
53 typedef T filtration_type;
133 std::vector<unsigned> coface_counter = this->compute_counter_for_given_cell(coface);
134 std::vector<unsigned> face_counter = this->compute_counter_for_given_cell(face);
137 int number_of_position_in_which_counters_do_not_agree = -1;
138 std::size_t number_of_full_faces_that_comes_before = 0;
139 for (std::size_t i = 0; i != coface_counter.size(); ++i) {
140 if ((coface_counter[i] % 2 == 1) && (number_of_position_in_which_counters_do_not_agree == -1)) {
141 ++number_of_full_faces_that_comes_before;
143 if (coface_counter[i] != face_counter[i]) {
144 if (number_of_position_in_which_counters_do_not_agree != -1) {
145 std::cout <<
"Cells given to compute_incidence_between_cells procedure do not form a pair of coface-face.\n";
146 throw std::logic_error(
147 "Cells given to compute_incidence_between_cells procedure do not form a pair of coface-face.");
149 number_of_position_in_which_counters_do_not_agree = i;
154 if (number_of_full_faces_that_comes_before % 2) incidence = -1;
156 if (coface_counter[number_of_position_in_which_counters_do_not_agree] + 1 ==
157 face_counter[number_of_position_in_which_counters_do_not_agree]) {
195 inline unsigned dimension()
const {
return sizes.size(); }
200 inline unsigned size()
const {
return this->data.size(); }
206 template <
typename K>
207 friend std::ostream& operator<<(std::ostream& os, const Bitmap_cubical_complex_base<K>& b);
264 if (this->
counter != rhs.counter)
return false;
277 std::size_t operator*() {
return this->
counter; }
297 a.counter = this->data.size();
322 typedef typename std::vector<std::size_t> Boundary_range;
334 typedef typename std::vector<std::size_t> Coboundary_range;
356 while ((dim != this->b.
dimension()) && (this->
counter[dim] == this->b.sizes[dim] - 1)) ++dim;
360 for (std::size_t i = 0; i != dim; ++i) {
382 if (&this->b != &rhs.b)
return false;
383 if (this->
counter.size() != rhs.counter.size())
return false;
384 for (std::size_t i = 0; i != this->
counter.size(); ++i) {
385 if (this->
counter[i] != rhs.counter[i])
return false;
399 std::size_t operator*() {
return this->compute_index_in_bitmap(); }
401 std::size_t compute_index_in_bitmap()
const {
402 std::size_t index = 0;
403 for (std::size_t i = 0; i != this->
counter.size(); ++i) {
404 index += (2 * this->
counter[i] + 1) * this->b.multipliers[i];
409 void print_counter()
const {
410 for (std::size_t i = 0; i != this->
counter.size(); ++i) {
411 std::cout << this->
counter[i] <<
" ";
417 std::vector<std::size_t>
counter;
434 for (std::size_t i = 0; i != this->
dimension(); ++i) {
435 a.counter[i] = this->sizes[i] - 1;
463 inline std::size_t number_cells()
const {
return this->total_number_of_cells; }
471 std::vector<unsigned> sizes;
472 std::vector<unsigned> multipliers;
474 std::size_t total_number_of_cells;
476 void set_up_containers(
const std::vector<unsigned>& sizes) {
477 unsigned multiplier = 1;
478 for (std::size_t i = 0; i != sizes.size(); ++i) {
479 this->sizes.push_back(sizes[i]);
480 this->multipliers.push_back(multiplier);
481 multiplier *= 2 * sizes[i] + 1;
483 this->data = std::vector<T>(multiplier, std::numeric_limits<T>::infinity());
484 this->total_number_of_cells = multiplier;
487 std::size_t compute_position_in_bitmap(
const std::vector<unsigned>&
counter) {
488 std::size_t position = 0;
489 for (std::size_t i = 0; i != this->multipliers.size(); ++i) {
490 position += this->multipliers[i] * counter[i];
495 std::vector<unsigned> compute_counter_for_given_cell(std::size_t cell)
const {
496 std::vector<unsigned> counter;
497 counter.reserve(this->sizes.size());
498 for (std::size_t dim = this->sizes.size(); dim != 0; --dim) {
499 counter.push_back(cell / this->multipliers[dim - 1]);
500 cell = cell % this->multipliers[dim - 1];
502 std::reverse(counter.begin(), counter.end());
505 void read_perseus_style_file(
const char* perseus_style_file);
506 void setup_bitmap_based_on_top_dimensional_cells_list(
const std::vector<unsigned>& sizes_in_following_directions,
507 const std::vector<T>& top_dimensional_cells);
511 std::vector<bool> directions);
514 template <
typename T>
519 T dx = (min_max.second - min_max.first) / (T)number_of_bins;
522 for (std::size_t i = 0; i != this->data.size(); ++i) {
524 std::cerr <<
"Before binning : " << this->data[i] << std::endl;
526 this->data[i] = min_max.first + dx * (this->data[i] - min_max.first) / number_of_bins;
528 std::cerr <<
"After binning : " << this->data[i] << std::endl;
533 template <
typename T>
538 std::size_t number_of_bins = (min_max.second - min_max.first) / diameter_of_bin;
540 for (std::size_t i = 0; i != this->data.size(); ++i) {
542 std::cerr <<
"Before binning : " << this->data[i] << std::endl;
544 this->data[i] = min_max.first + diameter_of_bin * (this->data[i] - min_max.first) / number_of_bins;
546 std::cerr <<
"After binning : " << this->data[i] << std::endl;
551 template <
typename T>
553 std::pair<T, T> min_max(std::numeric_limits<T>::infinity(), -std::numeric_limits<T>::infinity());
554 for (std::size_t i = 0; i != this->data.size(); ++i) {
555 if (this->data[i] < min_max.first) min_max.first = this->data[i];
556 if (this->data[i] > min_max.second) min_max.second = this->data[i];
561 template <
typename K>
562 std::ostream& operator<<(std::ostream& out, const Bitmap_cubical_complex_base<K>& b) {
564 it != b.all_cells_const_end(); ++it) {
570 template <
typename T>
572 this->set_up_containers(sizes);
575 template <
typename T>
577 const std::vector<unsigned>& sizes_in_following_directions,
const std::vector<T>& top_dimensional_cells) {
578 this->set_up_containers(sizes_in_following_directions);
580 std::size_t number_of_top_dimensional_elements = 1;
581 for (std::size_t i = 0; i != sizes_in_following_directions.size(); ++i) {
582 number_of_top_dimensional_elements *= sizes_in_following_directions[i];
584 if (number_of_top_dimensional_elements != top_dimensional_cells.size()) {
585 std::cerr <<
"Error in constructor Bitmap_cubical_complex_base ( std::vector<std::size_t> " 586 <<
"sizes_in_following_directions, std::vector<T> top_dimensional_cells ). Number of top dimensional " 587 <<
"elements that follow from sizes_in_following_directions vector is different than the size of " 588 <<
"top_dimensional_cells vector." 591 "Error in constructor Bitmap_cubical_complex_base( std::vector<std::size_t> sizes_in_following_directions," 592 "std::vector<T> top_dimensional_cells ). Number of top dimensional elements that follow from " 593 "sizes_in_following_directions vector is different than the size of top_dimensional_cells vector.");
597 std::size_t index = 0;
605 template <
typename T>
607 const std::vector<T>& top_dimensional_cells) {
608 this->setup_bitmap_based_on_top_dimensional_cells_list(sizes_in_following_directions, top_dimensional_cells);
611 template <
typename T>
614 std::ifstream inFiltration;
615 inFiltration.open(perseus_style_file);
616 unsigned dimensionOfData;
617 inFiltration >> dimensionOfData;
620 std::cerr <<
"dimensionOfData : " << dimensionOfData << std::endl;
623 std::vector<unsigned> sizes;
624 sizes.reserve(dimensionOfData);
626 std::size_t dimensions = 1;
627 for (std::size_t i = 0; i != dimensionOfData; ++i) {
628 unsigned size_in_this_dimension;
629 inFiltration >> size_in_this_dimension;
630 sizes.push_back(size_in_this_dimension);
631 dimensions *= size_in_this_dimension;
633 std::cerr <<
"size_in_this_dimension : " << size_in_this_dimension << std::endl;
636 this->set_up_containers(sizes);
641 T filtrationLevel = 0.;
642 std::size_t filtration_counter = 0;
643 while (!inFiltration.eof()) {
645 getline(inFiltration, line);
646 if (line.length() != 0) {
647 int n = sscanf(line.c_str(),
"%lf", &filtrationLevel);
649 std::string perseus_error(
"Bad Perseus file format. This line is incorrect : " + line);
650 throw std::ios_base::failure(perseus_error.c_str());
654 std::cerr <<
"Cell of an index : " << it.compute_index_in_bitmap()
656 <<
" get the value : " << filtrationLevel << std::endl;
660 ++filtration_counter;
664 if (filtration_counter != dimensions) {
665 std::string perseus_error(
"Bad Perseus file format. Read " + std::to_string(filtration_counter) +
" expected " + \
666 std::to_string(dimensions) +
" values");
667 throw std::ios_base::failure(perseus_error.c_str());
670 inFiltration.close();
674 template <
typename T>
676 std::vector<bool> directions) {
680 this->read_perseus_style_file(perseus_style_file);
683 template <
typename T>
685 std::vector<bool> directions) {
689 this->set_up_containers(sizes);
692 template <
typename T>
694 const std::vector<T>& top_dimensional_cells,
695 std::vector<bool> directions) {
699 this->setup_bitmap_based_on_top_dimensional_cells_list(dimensions, top_dimensional_cells);
702 template <
typename T>
704 this->read_perseus_style_file(perseus_style_file);
707 template <
typename T>
709 std::vector<std::size_t> boundary_elements;
712 boundary_elements.reserve(this->
dimension() * 2);
714 std::size_t sum_of_dimensions = 0;
715 std::size_t cell1 = cell;
716 for (std::size_t i = this->multipliers.size(); i != 0; --i) {
717 unsigned position = cell1 / this->multipliers[i - 1];
718 if (position % 2 == 1) {
719 if (sum_of_dimensions % 2) {
720 boundary_elements.push_back(cell + this->multipliers[i - 1]);
721 boundary_elements.push_back(cell - this->multipliers[i - 1]);
723 boundary_elements.push_back(cell - this->multipliers[i - 1]);
724 boundary_elements.push_back(cell + this->multipliers[i - 1]);
728 cell1 = cell1 % this->multipliers[i - 1];
731 return boundary_elements;
734 template <
typename T>
736 std::vector<unsigned>
counter = this->compute_counter_for_given_cell(cell);
737 std::vector<std::size_t> coboundary_elements;
738 std::size_t cell1 = cell;
739 for (std::size_t i = this->multipliers.size(); i != 0; --i) {
740 unsigned position = cell1 / this->multipliers[i - 1];
741 if (position % 2 == 0) {
742 if ((cell > this->multipliers[i - 1]) && (counter[i - 1] != 0)) {
743 coboundary_elements.push_back(cell - this->multipliers[i - 1]);
745 if ((cell + this->multipliers[i - 1] < this->data.size()) && (counter[i - 1] != 2 * this->sizes[i - 1])) {
746 coboundary_elements.push_back(cell + this->multipliers[i - 1]);
749 cell1 = cell1 % this->multipliers[i - 1];
751 return coboundary_elements;
754 template <
typename T>
757 if (dbg) std::cerr <<
"\n\n\n Computing position o a cell of an index : " << cell << std::endl;
759 for (std::size_t i = this->multipliers.size(); i != 0; --i) {
760 unsigned position = cell / this->multipliers[i - 1];
763 std::cerr <<
"i-1 :" << i - 1 << std::endl;
764 std::cerr <<
"cell : " << cell << std::endl;
765 std::cerr <<
"position : " << position << std::endl;
766 std::cerr <<
"multipliers[" << i - 1 <<
"] = " << this->multipliers[i - 1] << std::endl;
769 if (position % 2 == 1) {
770 if (dbg) std::cerr <<
"Nonzero length in this direction \n";
773 cell = cell % this->multipliers[i - 1];
778 template <
typename T>
780 return this->data[cell];
783 template <
typename T>
788 std::vector<bool> is_this_cell_considered(this->data.size(),
false);
790 std::size_t size_to_reserve = 1;
791 for (std::size_t i = 0; i != this->multipliers.size(); ++i) {
792 size_to_reserve *= (std::size_t)((this->multipliers[i] - 1) / 2);
795 std::vector<std::size_t> indices_to_consider;
796 indices_to_consider.reserve(size_to_reserve);
801 indices_to_consider.push_back(it.compute_index_in_bitmap());
804 while (indices_to_consider.size()) {
806 std::cerr <<
"indices_to_consider in this iteration \n";
807 for (std::size_t i = 0; i != indices_to_consider.size(); ++i) {
808 std::cout << indices_to_consider[i] <<
" ";
811 std::vector<std::size_t> new_indices_to_consider;
812 for (std::size_t i = 0; i != indices_to_consider.size(); ++i) {
814 for (std::size_t boundaryIt = 0; boundaryIt != bd.size(); ++boundaryIt) {
816 std::cerr <<
"filtration of a cell : " << bd[boundaryIt] <<
" is : " << this->data[bd[boundaryIt]]
817 <<
" while of a cell: " << indices_to_consider[i] <<
" is: " << this->data[indices_to_consider[i]]
820 if (this->data[bd[boundaryIt]] > this->data[indices_to_consider[i]]) {
821 this->data[bd[boundaryIt]] = this->data[indices_to_consider[i]];
823 std::cerr <<
"Setting the value of a cell : " << bd[boundaryIt]
824 <<
" to : " << this->data[indices_to_consider[i]] << std::endl;
827 if (is_this_cell_considered[bd[boundaryIt]] ==
false) {
828 new_indices_to_consider.push_back(bd[boundaryIt]);
829 is_this_cell_considered[bd[boundaryIt]] =
true;
833 indices_to_consider.swap(new_indices_to_consider);
837 template <
typename T>
838 bool compareFirstElementsOfTuples(
const std::pair<std::pair<T, std::size_t>,
char>& first,
839 const std::pair<std::pair<T, std::size_t>,
char>& second) {
840 if (first.first.first < second.first.first) {
843 if (first.first.first > second.first.first) {
847 return first.second < second.second;
853 namespace Cubical_complex = cubical_complex;
857 #endif // BITMAP_CUBICAL_COMPLEX_BASE_H_ void impose_lower_star_filtration()
Definition: Bitmap_cubical_complex_base.h:784
unsigned size() const
Definition: Bitmap_cubical_complex_base.h:200
unsigned dimension() const
Definition: Bitmap_cubical_complex_base.h:195
void put_data_to_bins(std::size_t number_of_bins)
Definition: Bitmap_cubical_complex_base.h:515
T & get_cell_data(std::size_t cell)
Definition: Bitmap_cubical_complex_base.h:779
Boundary_range boundary_range(std::size_t sh)
Definition: Bitmap_cubical_complex_base.h:328
Cubical complex represented as a bitmap, class with basic implementation.
Definition: Bitmap_cubical_complex_base.h:51
virtual ~Bitmap_cubical_complex_base()
Definition: Bitmap_cubical_complex_base.h:82
unsigned get_dimension_of_a_cell(std::size_t cell) const
Definition: Bitmap_cubical_complex_base.h:755
All_cells_iterator all_cells_iterator_begin()
Definition: Bitmap_cubical_complex_base.h:287
Definition: SimplicialComplexForAlpha.h:14
virtual int compute_incidence_between_cells(std::size_t coface, std::size_t face) const
Definition: Bitmap_cubical_complex_base.h:131
This is an implementation of a counter being a vector of integers.
Definition: counter.h:32
Bitmap_cubical_complex_base()
Definition: Bitmap_cubical_complex_base.h:58
Top_dimensional_cells_iterator top_dimensional_cells_iterator_end()
Definition: Bitmap_cubical_complex_base.h:432
Top_dimensional_cells_iterator top_dimensional_cells_iterator_begin()
Definition: Bitmap_cubical_complex_base.h:424
Top_dimensional_cells_iterator_range class provides ranges for Top_dimensional_cells_iterator_range.
Definition: Bitmap_cubical_complex_base.h:444
Coboundary_range coboundary_range(std::size_t sh)
Definition: Bitmap_cubical_complex_base.h:340
All_cells_iterator all_cells_iterator_end()
Definition: Bitmap_cubical_complex_base.h:295
std::vector< std::size_t >::const_iterator Coboundary_iterator
Definition: Bitmap_cubical_complex_base.h:333
Iterator through all cells in the complex (in order they appear in the structure – i...
Definition: Bitmap_cubical_complex_base.h:242
std::pair< T, T > min_max_filtration()
Definition: Bitmap_cubical_complex_base.h:552
All_cells_range class provides ranges for All_cells_iterator.
Definition: Bitmap_cubical_complex_base.h:304
std::vector< std::size_t >::const_iterator Boundary_iterator
Definition: Bitmap_cubical_complex_base.h:321
virtual std::vector< std::size_t > get_coboundary_of_a_cell(std::size_t cell) const
Definition: Bitmap_cubical_complex_base.h:735
Iterator through top dimensional cells of the complex. The cells appear in order they are stored in t...
Definition: Bitmap_cubical_complex_base.h:346
virtual std::vector< std::size_t > get_boundary_of_a_cell(std::size_t cell) const
Definition: Bitmap_cubical_complex_base.h:708