Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hschreiber committed Sep 24, 2024
1 parent d2a2168 commit 02cb463
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 19 deletions.
53 changes: 46 additions & 7 deletions src/Multi_filtration/include/gudhi/Multi_critical_filtration.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ class Multi_critical_filtration {
return multi_filtration_[0];
}

/**
* @brief Casts the object into the type of a generator.
* @pre The filtration value is 1-critical. If there are more than one generator, only the first will be preserved
* and if there is no generator, the method will segfault.
*/
operator const Generator() const {
GUDHI_CHECK(num_generators() == 1, "Casting a " + std::to_string(num_generators()) +
"-critical filtration value into an 1-critical filtration value.");
return multi_filtration_[0];
}

// like numpy
/**
* @brief Returns a copy with entries casted into the type given as template parameter.
Expand Down Expand Up @@ -847,14 +858,42 @@ class Multi_critical_filtration {
// no nan values and if there is an inf/-inf, then 'end - curr == 1'
// modifies multi_filtration_ only if true is returned.
bool _generator_can_be_added(const Generator &x, std::size_t curr, std::size_t &end) {
if (x.empty() || x.is_nan() || (x.is_plus_inf() && end - curr != 0)) return false;
if (x.empty() || x.is_nan()) return false;

if (x.is_minus_inf()) {
if (end - curr == 1 && multi_filtration_[curr].is_minus_inf()) return false;
// assumes that everything between curr and end is already simplified
// so, if end - curr != 1, there can be no minus_inf anymore.
end = curr;
return true;
// assumes that everything between curr and end is simplified
// so, only multi_filtration_[curr] can be at inf or -inf.
if constexpr (co) {
if (multi_filtration_[curr].is_plus_inf() || (x.is_minus_inf() && end - curr != 0)) {
return false;
}
if (multi_filtration_[curr].is_minus_inf()) {
if (x.is_minus_inf()) {
return false;
}
end = curr;
return true;
}
if (x.is_plus_inf()) {
if (multi_filtration_[curr].is_plus_inf()) return false;
end = curr;
return true;
}
} else {
if (multi_filtration_[curr].is_minus_inf() || (x.is_plus_inf() && end - curr != 0)) {
return false;
}
if (multi_filtration_[curr].is_plus_inf()) {
if (x.is_plus_inf()) {
return false;
}
end = curr;
return true;
}
if (x.is_minus_inf()) {
if (multi_filtration_[curr].is_minus_inf()) return false;
end = curr;
return true;
}
}

while (curr != end) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(multi_critical_filtration_add, T, list_of_tested_v
BOOST_CHECK_EQUAL(f5[0][0], 0);
BOOST_CHECK_EQUAL(f5[0][1], 1);
BOOST_CHECK_EQUAL(f5[0][2], 2);

Multi_critical_filtration<T, true> f6 = Multi_critical_filtration<T, true>::minus_inf();
bool change = f6.add_generator(Multi_critical_filtration<T, true>::inf());
BOOST_CHECK(change);
BOOST_CHECK(f6.is_plus_inf());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(multi_critical_filtration_friends, T, list_of_tested_variants)
Expand Down
20 changes: 13 additions & 7 deletions src/Multi_persistence/include/gudhi/Multi_persistence/Line.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef LINE_FILTRATION_TRANSLATION_H_INCLUDED
#define LINE_FILTRATION_TRANSLATION_H_INCLUDED

#include <cmath>
#include <cstddef>
#include <stdexcept>
#include <limits>
Expand Down Expand Up @@ -85,6 +86,12 @@ class Line {
GUDHI_CHECK(direction_.empty() || direction_.size() == basePoint_.size(),
"Direction and base point do not have the same dimension.");

if constexpr (std::numeric_limits<T>::has_quiet_NaN){ //to avoid windows error
if (std::isnan(t)) return Point::nan();
}
if (t == Point::T_inf) return Point::inf();
if (t == -Point::T_inf) return Point::minus_inf();

Point x(basePoint_.size());

if (direction_.size() > 0) {
Expand Down Expand Up @@ -225,22 +232,21 @@ class Line {
}

/**
* @brief Given a box, returns the intersection of this box and the line.
* @brief Given a box, returns "time" parameter of the intersection of this box and the line.
*
* @param box Box to intersect.
* @return A pair representing the two bounding points of the intersection, such that the first element is the
* smallest of the two. If the box and the line do not intersect, returns the pair {inf, inf}.
* If the box is trivial, returns {NaN, NaN}.
* smallest of the two. If the box and the line do not intersect or the box is trivial, returns the pair {inf, -inf}.
*/
std::pair<Point, Point> get_bounds(const Box<T> &box) const {
if (box.is_trivial()) return {Point::nan(), Point::nan()};
std::pair<T, T> get_bounds(const Box<T> &box) const {
if (box.is_trivial()) return {Point::T_inf, -Point::T_inf};

T bottom = compute_forward_intersection(box.get_lower_corner());
T top = compute_backward_intersection(box.get_upper_corner());

if (bottom > top) return {Point::inf(), Point::inf()}; // no intersection
if (bottom > top) return {Point::T_inf, -Point::T_inf}; // no intersection

return {(*this)[bottom], (*this)[top]};
return {bottom, top};
}

private:
Expand Down
12 changes: 7 additions & 5 deletions src/Multi_persistence/test/multipersistence_line_unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(line_intersections, T, list_of_tested_variants)
t = l.template compute_backward_intersection<double>(KP({P{2,1,3}, P{2,3,3}}));
BOOST_CHECK_EQUAL(t, 0);

std::pair<P, P> bounds = l.get_bounds({{-10, 0, 10}, {10, 4, 10}});
auto& bottom = bounds.first;
auto& top = bounds.second;
std::pair<T, T> bounds = l.get_bounds({{-10, 0, 10}, {10, 4, 10}});
auto bottom = l[bounds.first];
auto top = l[bounds.second];
BOOST_CHECK_EQUAL(bottom.size(), 3);
BOOST_CHECK_EQUAL(bottom[0], T(5. + 2. / 3.));
BOOST_CHECK_EQUAL(bottom[1], 2);
Expand All @@ -100,8 +100,10 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(line_intersections, T, list_of_tested_variants)
BOOST_CHECK_EQUAL(top[2], T(3. + T(7. / 6.) * 6.));

bounds = l.get_bounds({{-10, 0, 10}, {10, 1, 10}});
BOOST_CHECK(bounds.first.is_plus_inf());
BOOST_CHECK(bounds.second.is_plus_inf());
BOOST_CHECK_EQUAL(bounds.first, P::T_inf);
BOOST_CHECK_EQUAL(bounds.second, -P::T_inf);
BOOST_CHECK(l[bounds.first].is_plus_inf());
BOOST_CHECK(l[bounds.second].is_minus_inf());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(line_other, T, list_of_tested_variants)
Expand Down

0 comments on commit 02cb463

Please sign in to comment.