Commit 3012930e authored by Janick Gerstenberger's avatar Janick Gerstenberger

cleanup `geometry/utility.hh`

move `isHomogeneous` into a seperate header for type traits
parent 5e1eae75
set(HEADERS
commoperation.hh
typetraits.hh
)
install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dune/vof/common)
#ifndef DUNE_VOF_COMMON_TYPETRAITS_HH
#define DUNE_VOF_COMMON_TYPETRAITS_HH
#include <type_traits>
namespace Dune
{
namespace VoF
{
namespace Impl
{
template<class T, class... Us>
struct _isHomogeneous;
template<class T>
struct _isHomogeneous<T> : std::true_type
{
using type = T;
};
template<class T, class ... Us>
struct _isHomogeneous<T, T, Us...> : _isHomogeneous<T, Us...> {};
template<class T, class U, class... Vs>
struct _isHomogeneous<T, U, Vs...> : std::false_type {};
}
// isHomogeneous
// -------------
/**
* \brief check if all types in a parameter pack are the same
*
* \tparam ...Ts pack of types
*/
template<class... Ts>
struct isHomogeneous : Impl::_isHomogeneous<Ts...> {};
template<>
struct isHomogeneous<> : std::false_type {};
template<class... Ts>
using isHomogeneous_t = typename isHomogeneous<Ts...>::type;
} // namespace VoF
} // namespace Dune
#endif // #ifndef DUNE_VOF_COMMON_TYPETRAITS_HH
......@@ -2,62 +2,26 @@
#define DUNE_VOF_GEOMETRY_UTILITY_HH
#include <cassert>
#include <cstddef>
#include <type_traits>
#include <utility>
#include <dune/common/deprecated.hh>
#include <dune/common/fmatrix.hh>
#include <dune/common/fvector.hh>
#include <dune/common/rangeutilities.hh>
#include <dune/common/typetraits.hh>
#include <dune/vof/common/typetraits.hh>
namespace Dune
{
namespace VoF
{
/**
* /brief namespace containing specific implementations
*/
namespace __impl {
// isHomogeneous
// -------------
template< class T, class ... Us >
struct isHomogeneousHelper;
template< class T >
struct isHomogeneousHelper< T > : std::true_type
{
using type = T;
};
template< class T, class ... Us >
struct isHomogeneousHelper< T, T, Us ... > : isHomogeneousHelper< T, Us ... >
{};
template< class T, class U, class ... Vs >
struct isHomogeneousHelper< T, U, Vs ... > : std::false_type
{};
/**
* \brief check if all types in a parameter pack are the same
*
* \tparam ...Ts pack of types
*/
template< class ... Ts >
struct isHomogeneous : isHomogeneousHelper< Ts ... >
{};
/**
* \brief specialisation of isHomogeneous for an empty pack
*/
template<>
struct isHomogeneous<> : std::false_type
{};
namespace Impl
{
// GCPImpl
// -------
......@@ -67,34 +31,30 @@ namespace Dune
* \tparam T type of coordinate
* \tparam I number of arguments
*/
template< class T, std::size_t I, class SFINAE = std::enable_if_t< T::dimension-1 == I > >
struct GCPImpl;
template<class T, std::size_t I, class = std::enable_if_t<T::dimension-1 == I>>
struct _GCP;
/**
* \brief generalized cross product implementation for 2D
*/
template< class T >
struct GCPImpl< T, 1 >
template<class K>
struct _GCP<FieldVector<K, 2>, 1>
{
static_assert( T::dimension == 2, "" );
using Coordinate = T;
using Coordinate = FieldVector<K, 2>;
static Coordinate apply( const Coordinate& v )
{
return { -v[ 1 ], v[ 0 ] };
}
inline static auto apply(const Coordinate& v) -> Coordinate { return {-v[1], v[0]}; }
};
/**
* \brief generalized cross product implementation for 3D
*/
template< class T >
struct GCPImpl< T, 2 >
template<class K>
struct _GCP<FieldVector<K, 3>, 2>
{
static_assert( T::dimension == 3, "" );
using Coordinate = T;
using Coordinate = FieldVector<K, 3>;
static Coordinate apply( const Coordinate& v, const Coordinate& w )
inline static auto apply( const Coordinate& v, const Coordinate& w )
-> Coordinate
{
return { v[ 1 ] * w[ 2 ] - v[ 2 ] * w[ 1 ],
v[ 2 ] * w[ 0 ] - v[ 0 ] * w[ 2 ],
......@@ -102,8 +62,8 @@ namespace Dune
}
};
} // namespace Impl
} // namespace __impl
// generalizedCrossProduct
......@@ -116,27 +76,28 @@ namespace Dune
* \tparam ...Coords global coordinates
* \param coords pack of coordinates
*/
template< class ... Coords >
auto generalizedCrossProduct ( const Coords& ... coords ) -> typename __impl::GCPImpl< typename __impl::isHomogeneous< Coords ... >::type, sizeof...(Coords) >::Coordinate
template<class... Ts>
auto generalizedCrossProduct (const Ts& ...coords)
-> typename Impl::_GCP<isHomogeneous_t<Ts...>, sizeof...(Ts)>::Coordinate
{
return __impl::GCPImpl< typename __impl::isHomogeneous< Coords ... >::type, sizeof...(Coords) >::apply( coords ... );
return Impl::_GCP<isHomogeneous_t<Ts...>, sizeof...(Ts)>::apply(coords...);
}
template< class Coord, int dim >
auto generalizedCrossProduct ( std::array< Coord, dim > coords );
template< class Coord >
auto generalizedCrossProduct ( std::array< Coord, 1 > coords )
template<class Coordinate, int N>
auto generalizedCrossProduct (std::array<Coordinate, N> coords)
-> decltype(generalizedCrossProduct(coords, std::make_index_sequence<N>{}))
{
return generalizedCrossProduct( coords[ 0 ] );
return generalizedCrossProduct(coords, std::make_index_sequence<N>{});
}
template< class Coord >
auto generalizedCrossProduct ( std::array< Coord, 2 > coords )
template<class Coordinate, int N, std::size_t... Is>
auto generalizedCrossProduct (std::array<Coordinate, N> coords, std::index_sequence<Is...>)
-> typename Impl::_GCP<Coordinate, N>::Coordinate
{
return generalizedCrossProduct( coords[ 0 ], coords[ 1 ] );
return typename Impl::_GCP<Coordinate, N>::apply(coords[Is]...);
}
// normalize
// ---------
......@@ -147,25 +108,24 @@ namespace Dune
* \tparam Coord global coordinate type
* \param v vector
*/
template< class Coord >
auto normalize ( Coord &v ) -> void_t< decltype( std::declval< Coord >().two_norm() ) >
template<class Coordinate>
auto normalize (Coordinate& v) -> void_t<decltype(v.two_norm())>
{
assert( v.two_norm() > 0.0 );
assert(v.two_norm() > 0.0);
v /= v.two_norm();
}
// outerProduct
// ------------
template < class ctype, int dim >
FieldMatrix< ctype, dim, dim > outerProduct ( const FieldVector< ctype, dim > &a, const FieldVector< ctype, dim > &b )
template <class K, int N, int M>
auto outerProduct (const FieldVector<K, N>& u, const FieldVector<K, M>& v)
-> FieldMatrix<K, N, M>
{
FieldMatrix< ctype, dim, dim > m( 0.0 );
for ( std::size_t i = 0; i < dim; ++i )
m[ i ].axpy( a[ i ], b );
return m;
FieldMatrix<K, N, M> mat;
for (auto i : range(N))
mat[i].axpy(u[i], v);
return mat;
}
} // namespace VoF
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment