Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1#ifndef _BBM_ENOKI_MATH_H_
2#define _BBM_ENOKI_MATH_H_
3
4#include "backbone/vec.h"
5#include "backbone/array.h"
6#include "enoki/special.h"
7#include "core/attribute.h"
8
9/************************************************************************/
10/*! \file math.h
11
12 \brief Connect the bbm math functions to the corresponding enoki methods.
13
14 Satisfies concepts::backbone::has_math_functions
15
16*************************************************************************/
17
18namespace backbone {
19
20 /**********************************************************************/
21 /*! \brief Helper Macro for mapping math operations to enoki. Takes in
22 account to pass the 'value' of variables and to cast scalars to the
23 correct type to preserve differentiability.
24 ***********************************************************************/
25#define BBM_ENOKI_MATHOP(OpName) \
26 template<typename... Ts> requires requires(const Ts&... t) {{enoki::OpName(bbm::value(t)...)};} \
27 inline constexpr auto OpName(const Ts&... t) \
28 { \
29 constexpr bool has_floating_scalar = (( std::is_scalar_v< std::decay_t<decltype(bbm::value(t))> > && std::is_floating_point_v< std::decay_t<decltype(bbm::value(t))> > ) || ...); \
30 \
31 if constexpr (has_floating_scalar) \
32 { \
33 using base_type = decltype( (std::declval<std::conditional_t< std::is_scalar_v< std::decay_t<decltype(bbm::value(t))> >, float, decltype(bbm::value(t)) >>() + ...) ); \
34 using type = enoki::scalar_t<base_type>; \
35 return enoki::OpName( std::conditional_t<std::is_scalar_v<std::decay_t<decltype(bbm::value(t))>>, type, decltype(bbm::value(t))>(bbm::value(t))... ); \
36 } \
37 else return enoki::OpName( bbm::value(t)... ); \
38 } \
39
40 /********************************************************************/
41 /*! @{ \name Map math operations to Enoki functions
42 *******************************************************************/
72 BBM_ENOKI_MATHOP(safe_sqrt);
73 BBM_ENOKI_MATHOP(safe_asin);
74 BBM_ENOKI_MATHOP(safe_acos);
87 //! @}
88
89 // clean up maco
90 #undef BBM_ENOKI_MATHOP
91
92 /********************************************************************/
93 /*! \brief cossin
94
95 \param a = angle
96 \returns the cos and sin in a vec2d
97 ********************************************************************/
98 template<typename T>
99 vec2d<T> cossin(const T& a)
100 {
101 auto tmp = enoki::sincos(a);
102 return {tmp.second, tmp.first};
103 }
104
105} // end backbone namespace
106
107
108namespace enoki {
109
110 namespace detail {
111
112 template<typename T, typename D>
113 using binary_cast_t = std::conditional_t<
114 std::floating_point<enoki::scalar_t<std::decay_t<bbm::attribute_value_t<D>>>> && std::is_scalar_v<std::decay_t<bbm::attribute_value_t<T>>>,
115 const enoki::scalar_t<std::decay_t<bbm::attribute_value_t<D>>>&,
116 const std::decay_t<bbm::attribute_value_t<T>>& >;
117
118
119 } // end detail namespace
120
121 /**********************************************************************/
122 /*! \brief Helper Macro for that ensures that binary math operators with
123 rvalues do not cast the non-rvalue operant
124 ***********************************************************************/
125#define BBM_ENOKI_BINARY_OP(OpName) \
126 template<typename T, typename D> \
127 inline constexpr auto OpName(T&& t, D&& d) -> decltype( enoki::OpName< enoki::detail::binary_cast_t<T,D>, enoki::detail::binary_cast_t<D,T> >( t, d ) ) \
128 { \
129 return enoki::OpName<enoki::detail::binary_cast_t<T,D>, enoki::detail::binary_cast_t<D,T>>(t, d); \
130 } \
131
132 /*
133 BBM_ENOKI_BINARY_OP(operator+);
134 BBM_ENOKI_BINARY_OP(operator-);
135 BBM_ENOKI_BINARY_OP(operator*);
136 BBM_ENOKI_BINARY_OP(operator/);
137 */
138 #undef BBM_ENOKI_BINARY_OP
139}
140
141
142
143
144#endif /* _BBM_ENOKI_MATH_H_ */
#define BBM_ENOKI_MATHOP(OpName)
Helper Macro for mapping math operations to enoki. Takes in account to pass the 'value' of variables ...
Definition: math.h:25
A warpper class to attach a properties to a class via a property struct. The propert expects by defau...
Random number generator; built on top of Drjit.
Definition: backbone.h:53
auto min(T &&t, U &&u)
min between two variables
Definition: math.h:84
vec2d< T > cossin(const T &a)
cossin
Definition: math.h:107
drjit::Array< T, 2 > vec2d
Definition: vec.h:13
auto max(T &&t, U &&u)
max between two variables
Definition: math.h:75
Definition: math.h:108