Wildmeshing Toolkit
CastAttributeTransferStrategy.hpp
Go to the documentation of this file.
1 
2 
3 #pragma once
4 #include <vector>
6 namespace wmtk {
7 class Mesh;
8 namespace simplex {
9 class Simplex;
10 }
11 } // namespace wmtk
12 
14 
15 
16 template <typename MyType, typename ParentType>
18 {
19 public:
21  using MyVecType = typename BaseType::MyVecType;
23 
24  static MyVecType convert(const ParentMatType& v)
25  {
26  auto eval = [](const auto& vec) -> MyVecType {
27  if (vec.cols() == 1) {
28  return vec;
29  } else {
30  if constexpr (std::is_same_v<MyType, wmtk::Rational>) {
31  return vec.rowwise().sum() / int(vec.cols());
32  } else {
33  return vec.rowwise().mean();
34  }
35  }
36  };
37 
38  if constexpr (std::is_same_v<MyType, wmtk::Rational>) {
39  if constexpr (std::is_same_v<ParentType, wmtk::Rational>) {
40  return eval(v);
41  } else {
42  constexpr auto cast_rational = [](const auto& x) -> MyType {
43  if constexpr (std::is_same_v<ParentType, double>) {
44  return wmtk::Rational(x, true);
45  } else {
46  return wmtk::Rational(int(x), true);
47  }
48  };
49  return eval(v.unaryExpr(cast_rational));
50  }
51 
52 
53  } else { // my type is not rational
54  if constexpr (std::is_same_v<ParentType, wmtk::Rational>) {
55  constexpr auto cast_from_rational = [](const auto& x) -> MyType {
56  return x.to_double();
57  };
58  return eval(v.unaryExpr(cast_from_rational));
59  } else {
60  return eval(v.template cast<MyType>());
61  }
62  }
63  }
64 
66  const attribute::MeshAttributeHandle& my_handle,
67  const attribute::MeshAttributeHandle& parent_handle)
68  : BaseType(my_handle, parent_handle, &convert)
69  {}
70 };
71 } // namespace wmtk::operations::utils
CastAttributeTransferStrategy(const attribute::MeshAttributeHandle &my_handle, const attribute::MeshAttributeHandle &parent_handle)
Definition: Accessor.hpp:6