jet_traits.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: sergiu.deitsch@gmail.com (Sergiu Deitsch)
  30. //
  31. #ifndef CERES_PUBLIC_INTERNAL_JET_TRAITS_H_
  32. #define CERES_PUBLIC_INTERNAL_JET_TRAITS_H_
  33. #include <tuple>
  34. #include <type_traits>
  35. #include <utility>
  36. #include "ceres/internal/integer_sequence_algorithm.h"
  37. #include "ceres/jet_fwd.h"
  38. namespace ceres {
  39. namespace internal {
  40. // Predicate that determines whether any of the Types is a Jet.
  41. template <typename... Types>
  42. struct AreAnyJet : std::false_type {};
  43. template <typename T, typename... Types>
  44. struct AreAnyJet<T, Types...> : AreAnyJet<Types...> {};
  45. template <typename T, int N, typename... Types>
  46. struct AreAnyJet<Jet<T, N>, Types...> : std::true_type {};
  47. // Convenience variable template for AreAnyJet.
  48. template <typename... Types>
  49. inline constexpr bool AreAnyJet_v = AreAnyJet<Types...>::value;
  50. // Extracts the underlying floating-point from a type T.
  51. template <typename T, typename E = void>
  52. struct UnderlyingScalar {
  53. using type = T;
  54. };
  55. template <typename T, int N>
  56. struct UnderlyingScalar<Jet<T, N>> : UnderlyingScalar<T> {};
  57. // Convenience template alias for UnderlyingScalar type trait.
  58. template <typename T>
  59. using UnderlyingScalar_t = typename UnderlyingScalar<T>::type;
  60. // Predicate determining whether all Types in the pack are the same.
  61. //
  62. // Specifically, the predicate applies std::is_same recursively to pairs of
  63. // Types in the pack.
  64. template <typename T1, typename... Types>
  65. inline constexpr bool AreAllSame_v = (std::is_same<T1, Types>::value && ...);
  66. // Determines the rank of a type. This allows to ensure that types passed as
  67. // arguments are compatible to each other. The rank of Jet is determined by the
  68. // dimensions of the dual part. The rank of a scalar is always 0.
  69. // Non-specialized types default to a rank of -1.
  70. template <typename T, typename E = void>
  71. struct Rank : std::integral_constant<int, -1> {};
  72. // The rank of a scalar is 0.
  73. template <typename T>
  74. struct Rank<T, std::enable_if_t<std::is_scalar<T>::value>>
  75. : std::integral_constant<int, 0> {};
  76. // The rank of a Jet is given by its dimensionality.
  77. template <typename T, int N>
  78. struct Rank<Jet<T, N>> : std::integral_constant<int, N> {};
  79. // Convenience variable template for Rank.
  80. template <typename T>
  81. inline constexpr int Rank_v = Rank<T>::value;
  82. // Constructs an integer sequence of ranks for each of the Types in the pack.
  83. template <typename... Types>
  84. using Ranks_t = std::integer_sequence<int, Rank_v<Types>...>;
  85. // Returns the scalar part of a type. This overload acts as an identity.
  86. template <typename T>
  87. constexpr decltype(auto) AsScalar(T&& value) noexcept {
  88. return std::forward<T>(value);
  89. }
  90. // Recursively unwraps the scalar part of a Jet until a non-Jet scalar type is
  91. // encountered.
  92. template <typename T, int N>
  93. constexpr decltype(auto) AsScalar(const Jet<T, N>& value) noexcept(
  94. noexcept(AsScalar(value.a))) {
  95. return AsScalar(value.a);
  96. }
  97. } // namespace internal
  98. // Type trait ensuring at least one of the types is a Jet,
  99. // the underlying scalar types are the same and Jet dimensions match.
  100. //
  101. // The type trait can be further specialized if necessary.
  102. //
  103. // This trait is a candidate for a concept definition once C++20 features can
  104. // be used.
  105. template <typename... Types>
  106. // clang-format off
  107. struct CompatibleJetOperands : std::integral_constant
  108. <
  109. bool,
  110. // At least one of the types is a Jet
  111. internal::AreAnyJet_v<Types...> &&
  112. // The underlying floating-point types are exactly the same
  113. internal::AreAllSame_v<internal::UnderlyingScalar_t<Types>...> &&
  114. // Non-zero ranks of types are equal
  115. internal::IsEmptyOrAreAllEqual_v<internal::RemoveValue_t<internal::Ranks_t<Types...>, 0>>
  116. >
  117. // clang-format on
  118. {};
  119. // Single Jet operand is always compatible.
  120. template <typename T, int N>
  121. struct CompatibleJetOperands<Jet<T, N>> : std::true_type {};
  122. // Single non-Jet operand is always incompatible.
  123. template <typename T>
  124. struct CompatibleJetOperands<T> : std::false_type {};
  125. // Empty operands are always incompatible.
  126. template <>
  127. struct CompatibleJetOperands<> : std::false_type {};
  128. // Convenience variable template ensuring at least one of the types is a Jet,
  129. // the underlying scalar types are the same and Jet dimensions match.
  130. //
  131. // This trait is a candidate for a concept definition once C++20 features can
  132. // be used.
  133. template <typename... Types>
  134. inline constexpr bool CompatibleJetOperands_v =
  135. CompatibleJetOperands<Types...>::value;
  136. // Type trait ensuring at least one of the types is a Jet,
  137. // the underlying scalar types are compatible among each other and Jet
  138. // dimensions match.
  139. //
  140. // The type trait can be further specialized if necessary.
  141. //
  142. // This trait is a candidate for a concept definition once C++20 features can
  143. // be used.
  144. template <typename... Types>
  145. // clang-format off
  146. struct PromotableJetOperands : std::integral_constant
  147. <
  148. bool,
  149. // Types can be compatible among each other
  150. internal::AreAnyJet_v<Types...> &&
  151. // Non-zero ranks of types are equal
  152. internal::IsEmptyOrAreAllEqual_v<internal::RemoveValue_t<internal::Ranks_t<Types...>, 0>>
  153. >
  154. // clang-format on
  155. {};
  156. // Convenience variable template ensuring at least one of the types is a Jet,
  157. // the underlying scalar types are compatible among each other and Jet
  158. // dimensions match.
  159. //
  160. // This trait is a candidate for a concept definition once C++20 features can
  161. // be used.
  162. template <typename... Types>
  163. inline constexpr bool PromotableJetOperands_v =
  164. PromotableJetOperands<Types...>::value;
  165. } // namespace ceres
  166. #endif // CERES_PUBLIC_INTERNAL_JET_TRAITS_H_