summaryrefslogtreecommitdiff
path: root/src/Alpha_complex/test/Alpha_kernel_d_unit_test.cpp
blob: 70b65aadb78223c1e7c708c4560c10a759a7e825 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*    This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
 *    See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
 *    Author(s):       Vincent Rouvreau
 *
 *    Copyright (C) 2020 Inria
 *
 *    Modification(s):
 *      - YYYY/MM Author: Description of the modification
 */

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE "alpha_kernel_d"
#include <boost/test/unit_test.hpp>
#include <boost/mpl/list.hpp>

#include <CGAL/Epick_d.h>
#include <CGAL/Epeck_d.h>
#include <CGAL/NT_converter.h>

#include <iostream>
#include <vector>
#include <utility>  // for std::pair

#include <gudhi/Alpha_complex/Alpha_kernel_d.h>
#include <gudhi/Unitary_tests_utils.h>

// Use dynamic_dimension_tag for the user to be able to set dimension
typedef CGAL::Epeck_d< CGAL::Dynamic_dimension_tag > Exact_kernel_d;
// Use static dimension_tag for the user not to be able to set dimension
typedef CGAL::Epeck_d< CGAL::Dimension_tag<4> > Exact_kernel_s;
// Use dynamic_dimension_tag for the user to be able to set dimension
typedef CGAL::Epick_d< CGAL::Dynamic_dimension_tag > Inexact_kernel_d;
// Use static dimension_tag for the user not to be able to set dimension
typedef CGAL::Epick_d< CGAL::Dimension_tag<4> > Inexact_kernel_s;
// The triangulation uses the default instantiation of the TriangulationDataStructure template parameter

typedef boost::mpl::list<Exact_kernel_d, Exact_kernel_s, Inexact_kernel_d, Inexact_kernel_s> list_of_kernel_variants;

BOOST_AUTO_TEST_CASE_TEMPLATE(Alpha_kernel_d_dimension, TestedKernel, list_of_kernel_variants) {
  Gudhi::alpha_complex::Alpha_kernel_d<TestedKernel, false> kernel;
  std::vector<double> p0 {0., 1., 2., 3.};
  typename TestedKernel::Point_d p0_d(p0.begin(), p0.end());

  std::clog << "Dimension is " << kernel.get_dimension(p0_d) << std::endl;
  BOOST_CHECK(kernel.get_dimension(p0_d) == 4);

  Gudhi::alpha_complex::Alpha_kernel_d<TestedKernel, true> w_kernel;
  typename TestedKernel::Weighted_point_d w_p0_d(p0_d, 10.);

  std::clog << "Dimension is " << w_kernel.get_dimension(w_p0_d) << std::endl;
  BOOST_CHECK(w_kernel.get_dimension(w_p0_d) == 4);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(Alpha_kernel_d_sphere, TestedKernel, list_of_kernel_variants) {
  using Unweighted_kernel = Gudhi::alpha_complex::Alpha_kernel_d<TestedKernel, false>;
  // Sphere: (x-1)² + (y-1)² + z² + t² = 1
  // At least 5 points for a 3-sphere
  std::vector<double> p0 {1., 0., 0., 0.};
  std::vector<double> p1 {0., 1., 0., 0.};
  std::vector<double> p2 {1., 1., 1., 0.};
  std::vector<double> p3 {1., 1., 0., 1.};
  std::vector<double> p4 {1., 1., -1., 0.};

  using Point_d = typename Unweighted_kernel::Point_d;
  std::vector<Point_d> unw_pts;
  unw_pts.push_back(Point_d(p0.begin(), p0.end()));
  unw_pts.push_back(Point_d(p1.begin(), p1.end()));
  unw_pts.push_back(Point_d(p2.begin(), p2.end()));
  unw_pts.push_back(Point_d(p3.begin(), p3.end()));
  unw_pts.push_back(Point_d(p4.begin(), p4.end()));

  Unweighted_kernel kernel;
  auto unw_sphere = kernel.get_sphere(unw_pts.cbegin(), unw_pts.cend());

  std::clog << "Center is " << unw_sphere.first << " - squared radius is " << unw_sphere.second << std::endl;

  using Weighted_kernel = Gudhi::alpha_complex::Alpha_kernel_d<TestedKernel, true>;

  using Weighted_point_d = typename Weighted_kernel::Weighted_point_d;
  using Bare_point_d = typename Weighted_kernel::Bare_point_d;
  std::vector<Weighted_point_d> w_pts;
  w_pts.push_back(Weighted_point_d(Bare_point_d(p0.begin(), p0.end()), 0.));
  w_pts.push_back(Weighted_point_d(Bare_point_d(p1.begin(), p1.end()), 0.));
  w_pts.push_back(Weighted_point_d(Bare_point_d(p2.begin(), p2.end()), 0.));
  w_pts.push_back(Weighted_point_d(Bare_point_d(p3.begin(), p3.end()), 0.));
  w_pts.push_back(Weighted_point_d(Bare_point_d(p4.begin(), p4.end()), 0.));

  Weighted_kernel w_kernel;
  auto w_sphere = w_kernel.get_sphere(w_pts.cbegin(), w_pts.cend());

  std::clog << "Center is " << w_sphere.point() << " - squared radius is " << w_sphere.weight() << std::endl;

  CGAL::NT_converter<typename Weighted_kernel::FT, double> cast_to_double;
  // The results shall be the same with weights = 0.
  GUDHI_TEST_FLOAT_EQUALITY_CHECK(cast_to_double(unw_sphere.second), cast_to_double(w_sphere.weight()));
  BOOST_CHECK(unw_sphere.first == w_sphere.point());

  auto unw_sq_rd = kernel.get_squared_radius(unw_pts.cbegin(), unw_pts.cend());
  std::clog << "Squared radius is " << unw_sq_rd << std::endl;
  GUDHI_TEST_FLOAT_EQUALITY_CHECK(cast_to_double(unw_sphere.second), cast_to_double(unw_sq_rd));
  auto w_sq_rd = w_kernel.get_squared_radius(w_pts.cbegin(), w_pts.cend());
  std::clog << "Squared radius is " << w_sq_rd << std::endl;
  GUDHI_TEST_FLOAT_EQUALITY_CHECK(cast_to_double(w_sphere.weight()), cast_to_double(w_sq_rd));
}


BOOST_AUTO_TEST_CASE_TEMPLATE(Alpha_kernel_d_distance, TestedKernel, list_of_kernel_variants) {
  using Unweighted_kernel = Gudhi::alpha_complex::Alpha_kernel_d<TestedKernel, false>;

  std::vector<double> p0 {1., 0., 0., 0.};
  std::vector<double> p1 {0., 1., 0., 0.};

  using Point_d = typename Unweighted_kernel::Point_d;
  Unweighted_kernel kernel;
  auto dist_01 = kernel.get_squared_distance(Point_d(p0.begin(), p0.end()), Point_d(p1.begin(), p1.end()));
  std::clog << "Distance is " << dist_01 << std::endl;

  using Weighted_kernel = Gudhi::alpha_complex::Alpha_kernel_d<TestedKernel, true>;

  using Weighted_point_d = typename Weighted_kernel::Weighted_point_d;
  using Bare_point_d = typename Weighted_kernel::Bare_point_d;
  std::vector<Weighted_point_d> w_pts;

  Weighted_kernel w_kernel;
  auto w_dist_01 = w_kernel.get_squared_distance(Weighted_point_d(Bare_point_d(p0.begin(), p0.end()), 0.),
                                                 Weighted_point_d(Bare_point_d(p1.begin(), p1.end()), 0.));
  std::clog << "Distance is " << w_dist_01 << std::endl;

  CGAL::NT_converter<typename Weighted_kernel::FT, double> cast_to_double;
  // The results shall be the same with weights = 0.
  GUDHI_TEST_FLOAT_EQUALITY_CHECK(cast_to_double(dist_01), cast_to_double(w_dist_01));
}