Gyselalib++
barycentric_coordinates.hpp
1 #pragma once
2 
3 #include <ddc/ddc.hpp>
4 
5 #include "coordinate_converter.hpp"
6 
20 template <class X, class Y, class Corner1Tag, class Corner2Tag, class Corner3Tag>
22  : public CoordinateConverter<
23  ddc::Coordinate<X, Y>,
24  ddc::Coordinate<Corner1Tag, Corner2Tag, Corner3Tag>>
25  , public CoordinateConverter<
26  ddc::Coordinate<Corner1Tag, Corner2Tag, Corner3Tag>,
27  ddc::Coordinate<X, Y>>
28 {
29 public:
31  using BarycentricCoord = ddc::Coordinate<Corner1Tag, Corner2Tag, Corner3Tag>;
32 
34  using CartesianCoord = ddc::Coordinate<X, Y>;
35 
36 private:
37  CartesianCoord m_corner1;
38  CartesianCoord m_corner2;
39  CartesianCoord m_corner3;
40 
41 public:
50  CartesianCoord const& corner1,
51  CartesianCoord const& corner2,
52  CartesianCoord const& corner3)
53  : m_corner1(corner1)
54  , m_corner2(corner2)
55  , m_corner3(corner3)
56  {
57  }
58 
64 
70 
73 
80  = default;
81 
88 
96  KOKKOS_FUNCTION BarycentricCoord operator()(CartesianCoord const& pos) const final
97  {
98  const double x = ddc::get<X>(pos);
99  const double y = ddc::get<Y>(pos);
100  const double x1 = ddc::get<X>(m_corner1);
101  const double x2 = ddc::get<X>(m_corner2);
102  const double x3 = ddc::get<X>(m_corner3);
103  const double y1 = ddc::get<Y>(m_corner1);
104  const double y2 = ddc::get<Y>(m_corner2);
105  const double y3 = ddc::get<Y>(m_corner3);
106 
107  const double div = ((y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3));
108  const double lam1 = ((y2 - y3) * (x - x3) + (x3 - x2) * (y - y3)) / div;
109  const double lam2 = ((y - y3) * (x1 - x3) + (x3 - x) * (y1 - y3)) / div;
110  const double lam3 = 1 - lam1 - lam2;
111  return BarycentricCoord(lam1, lam2, lam3);
112  }
113 
121  KOKKOS_FUNCTION CartesianCoord operator()(BarycentricCoord const& pos) const final
122  {
123  const double l1 = ddc::get<Corner1Tag>(pos);
124  const double l2 = ddc::get<Corner2Tag>(pos);
125  const double l3 = ddc::get<Corner3Tag>(pos);
126 
127  const double x1 = ddc::get<X>(m_corner1);
128  const double x2 = ddc::get<X>(m_corner2);
129  const double x3 = ddc::get<X>(m_corner3);
130  const double y1 = ddc::get<Y>(m_corner1);
131  const double y2 = ddc::get<Y>(m_corner2);
132  const double y3 = ddc::get<Y>(m_corner3);
133 
134  const double x = x1 * l1 + x2 * l2 + x3 * l3;
135  const double y = y1 * l1 + y2 * l2 + y3 * l3;
136 
137  return CartesianCoord(x, y);
138  }
139 };
A class to convert cartesian coordinates to barycentric coordinates on a triangle.
Definition: barycentric_coordinates.hpp:28
ddc::Coordinate< X, Y > CartesianCoord
The type of a coordinate in the cartesian coordinate system.
Definition: barycentric_coordinates.hpp:34
CartesianToBarycentricCoordinates(CartesianToBarycentricCoordinates &&x)=default
A r-value copy operator for the mapping operator.
KOKKOS_FUNCTION CartesianCoord operator()(BarycentricCoord const &pos) const final
The operator to get the equivalent cartesian coordinate of the barycentric coordinate.
Definition: barycentric_coordinates.hpp:121
CartesianToBarycentricCoordinates(CartesianToBarycentricCoordinates const &other)=default
A copy operator for the mapping operator.
CartesianToBarycentricCoordinates(CartesianCoord const &corner1, CartesianCoord const &corner2, CartesianCoord const &corner3)
Construct the operator which converts between the coordinate systems.
Definition: barycentric_coordinates.hpp:49
~CartesianToBarycentricCoordinates()=default
The destructor of the mapping operator.
CartesianToBarycentricCoordinates & operator=(CartesianToBarycentricCoordinates &&x)=default
A r-value copy operator for the mapping operator.
CartesianToBarycentricCoordinates & operator=(CartesianToBarycentricCoordinates const &x)=default
A copy operator for the mapping operator.
KOKKOS_FUNCTION BarycentricCoord operator()(CartesianCoord const &pos) const final
The operator to get the equivalent barycentric coordinate of the cartesian coordinate.
Definition: barycentric_coordinates.hpp:96
ddc::Coordinate< Corner1Tag, Corner2Tag, Corner3Tag > BarycentricCoord
The type of a coordinate in the barycentric coordinate system.
Definition: barycentric_coordinates.hpp:31
An operator to convert from the start coordinate type to the end coordinate type.
Definition: coordinate_converter.hpp:13