PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Concepts.h
1#ifndef PBAT_MATH_LINALG_MINI_CONCEPTS_H
2#define PBAT_MATH_LINALG_MINI_CONCEPTS_H
3
4#include <concepts>
5#include <type_traits>
6#include <utility>
7
8namespace pbat {
9namespace math {
10namespace linalg {
11namespace mini {
12
13template <class TMatrix>
14concept CMatrix = requires(TMatrix M)
15{
16 typename TMatrix::ScalarType;
17 {
18 TMatrix::kRows
19 } -> std::convertible_to<int>;
20 {
21 TMatrix::kCols
22 } -> std::convertible_to<int>;
23 {
24 TMatrix::bRowMajor
25 } -> std::convertible_to<bool>;
26 // WARNING: It seems that in our math/linalg/mini library, any concept constraint that uses
27 // member functions with the . notation fails. This probably has something to do with M not
28 // being a concrete type at the time when we are evaluating the concept constraint.
29 //{
30 // M.Rows()
31 //} -> std::convertible_to<int>;
32 //{
33 // M.Cols()
34 //} -> std::convertible_to<int>;
35 // WARNING: This constraint causes compile errors with nvcc, I don't know why!
36#if not defined(__CUDACC__)
37 {
38 M(std::declval<int>(), std::declval<int>())
39 } -> std::convertible_to<typename TMatrix::ScalarType>;
40#endif
41};
42
43// WARNING: These constraints don't compile with nvcc, unfortunately. Only use CMatrix concept in
44// CUDA code.
45#if not defined(__CUDACC__)
46
47template <class TMatrix>
48concept CReadableMatrix = CMatrix<TMatrix> and requires(TMatrix M)
49{
50 true;
51 // WARNING:
52 // Unfortunately, these constraints cause compilation error, due to ambiguity between the const
53 // and non-const versions of these member function calls (tested with MSVC).
54 //{M.Transpose()};
55 //{M.Slice(std::declval<int>(), std::declval<int>())};
56 //{M.Col(std::declval<int>())};
57 //{M.Row(std::declval<int>())};
58};
59
60template <class TMatrix>
61concept CWriteableMatrix = CMatrix<TMatrix> and requires(TMatrix M)
62{
63 {M(std::declval<int>(), std::declval<int>()) = std::declval<typename TMatrix::ScalarType>()};
64 {M = std::declval<TMatrix>()};
65};
66
67template <class TMatrix>
68concept CReadableVectorizedMatrix = CMatrix<TMatrix> and requires(TMatrix M)
69{
70 {
71 M(std::declval<int>())
72 } -> std::convertible_to<typename TMatrix::ScalarType>;
73 {
74 M[std::declval<int>()]
75 } -> std::convertible_to<typename TMatrix::ScalarType>;
76};
77
78template <class TMatrix>
79concept CWriteableVectorizedMatrix = CMatrix<TMatrix> and requires(TMatrix M)
80{
81 {M(std::declval<int>()) = std::declval<typename TMatrix::ScalarType>()};
82 {M[std::declval<int>()] = std::declval<typename TMatrix::ScalarType>()};
83};
84
85#else
86
87template <class TMatrix>
89
90template <class TMatrix>
92
93template <class TMatrix>
95
96template <class TMatrix>
98
99#endif // not defined(__CUDACC__)
100
101} // namespace mini
102} // namespace linalg
103} // namespace math
104} // namespace pbat
105
106#define PBAT_MINI_CHECK_CMATRIX(TMatrix) static_assert(CMatrix<TMatrix>, "CMatrix not satisfied");
107#define PBAT_MINI_CHECK_CREADABLEMATRIX(TMatrix) \
108 static_assert(CReadableMatrix<TMatrix>, "CReadableMatrix not satisfied");
109#define PBAT_MINI_CHECK_CWRITEABLEMATRIX(TMatrix) \
110 static_assert(CWriteableMatrix<TMatrix>, "CWriteableMatrix not satisfied");
111#define PBAT_MINI_CHECK_CREADABLEVECTORIZEDMATRIX(TMatrix) \
112 static_assert(CReadableVectorizedMatrix<TMatrix>, "CReadableVectorizedMatrix not satisfied");
113#define PBAT_MINI_CHECK_CWRITEABLEVECTORIZEDMATRIX(TMatrix) \
114 static_assert(CWriteableVectorizedMatrix<TMatrix>, "CWriteableVectorizedMatrix not satisfied");
115
116#define PBAT_MINI_CHECK_READABLE_CONCEPTS(TMatrix) \
117 PBAT_MINI_CHECK_CREADABLEMATRIX(TMatrix); \
118 PBAT_MINI_CHECK_CREADABLEVECTORIZEDMATRIX(TMatrix);
119
120#define PBAT_MINI_CHECK_WRITEABLE_CONCEPTS(TMatrix) \
121 PBAT_MINI_CHECK_CWRITEABLEMATRIX(TMatrix); \
122 PBAT_MINI_CHECK_CWRITEABLEVECTORIZEDMATRIX(TMatrix);
123
124#define PBAT_MINI_DIMENSIONS_API \
125 PBAT_HOST_DEVICE constexpr auto Rows() const \
126 { \
127 return kRows; \
128 } \
129 PBAT_HOST_DEVICE constexpr auto Cols() const \
130 { \
131 return kCols; \
132 }
133
134#endif // PBAT_MATH_LINALG_MINI_CONCEPTS_H
Definition Concepts.h:14
Mini linear algebra related functionality.
Definition Assign.h:12
Linear Algebra related functionality.
Definition FilterEigenvalues.h:7
Math related functionality.
Definition Concepts.h:19
The main namespace of the library.
Definition Aliases.h:15