1 // 2 // SPDX-License-Identifier: BSD-3-Clause 3 // Copyright Contributors to the OpenEXR Project. 4 // 5 6 // clang-format off 7 8 #ifndef _PyImathFunOperators_h_ 9 #define _PyImathFunOperators_h_ 10 11 #include <ImathVec.h> 12 #include <ImathMatrixAlgo.h> 13 #include <ImathColorAlgo.h> 14 #include <ImathFun.h> 15 #include <cmath> 16 17 namespace PyImath { 18 19 template <class T> 20 struct rotationXYZWithUpDir_op 21 { 22 static IMATH_NAMESPACE::Vec3<T> applyrotationXYZWithUpDir_op23 apply(const IMATH_NAMESPACE::Vec3<T> &from, const IMATH_NAMESPACE::Vec3<T> &to, 24 const IMATH_NAMESPACE::Vec3<T> &up) 25 { 26 IMATH_NAMESPACE::Vec3<T> retval; 27 IMATH_NAMESPACE::extractEulerXYZ(IMATH_NAMESPACE::rotationMatrixWithUpDir(from,to,up),retval); 28 return retval; 29 } 30 }; 31 32 template <class T> 33 struct abs_op 34 { 35 static T applyabs_op36 apply(T value) 37 { 38 return IMATH_NAMESPACE::abs<T>(value); 39 } 40 }; 41 42 template <class T> 43 struct sign_op 44 { 45 static T applysign_op46 apply(T value) 47 { 48 return IMATH_NAMESPACE::sign<T>(value); 49 } 50 }; 51 52 template <class T> 53 struct log_op 54 { 55 static T applylog_op56 apply(T value) 57 { 58 return ::log(value); 59 } 60 }; 61 62 template <class T> 63 struct log10_op 64 { 65 static T applylog10_op66 apply(T value) 67 { 68 return ::log10(value); 69 } 70 }; 71 72 template <class T> 73 struct lerp_op 74 { 75 static T applylerp_op76 apply(T a, T b, T t) 77 { 78 return IMATH_NAMESPACE::lerp<T>(a,b,t); 79 } 80 }; 81 82 template <class T> 83 struct ulerp_op 84 { 85 static T applyulerp_op86 apply(T a, T b, T t) 87 { 88 return IMATH_NAMESPACE::ulerp<T>(a,b,t); 89 } 90 }; 91 92 template <class T> 93 struct lerpfactor_op 94 { 95 static T applylerpfactor_op96 apply(T a, T b, T t) 97 { 98 return IMATH_NAMESPACE::lerpfactor<T>(a,b,t); 99 } 100 }; 101 102 template <class T> 103 struct clamp_op 104 { 105 static T applyclamp_op106 apply(T value, T low, T high) 107 { 108 return IMATH_NAMESPACE::clamp<T>(value,low,high); 109 } 110 }; 111 112 template <class T> 113 struct cmp_op 114 { 115 static T applycmp_op116 apply(T value) 117 { 118 return IMATH_NAMESPACE::cmp<T>(value); 119 } 120 }; 121 122 template <class T> 123 struct cmpt_op 124 { 125 static T applycmpt_op126 apply(T value) 127 { 128 return IMATH_NAMESPACE::cmpt<T>(value); 129 } 130 }; 131 132 template <class T> 133 struct iszero_op 134 { 135 static T applyiszero_op136 apply(T value) 137 { 138 return IMATH_NAMESPACE::iszero<T>(value); 139 } 140 }; 141 142 template <class T> 143 struct equal_op 144 { 145 static T applyequal_op146 apply(T value) 147 { 148 return IMATH_NAMESPACE::equal<T>(value); 149 } 150 }; 151 152 template <class T> 153 struct floor_op 154 { 155 static int applyfloor_op156 apply(T value) 157 { 158 return IMATH_NAMESPACE::floor<T>(value); 159 } 160 }; 161 162 template <class T> 163 struct ceil_op 164 { 165 static int applyceil_op166 apply(T value) 167 { 168 return IMATH_NAMESPACE::ceil<T>(value); 169 } 170 }; 171 172 template <class T> 173 struct trunc_op 174 { 175 static int applytrunc_op176 apply(T value) 177 { 178 return IMATH_NAMESPACE::trunc<T>(value); 179 } 180 }; 181 182 struct divs_op 183 { 184 static int applydivs_op185 apply(int x, int y) 186 { 187 return IMATH_NAMESPACE::divs(x,y); 188 } 189 }; 190 191 struct mods_op 192 { 193 static int applymods_op194 apply(int x, int y) 195 { 196 return IMATH_NAMESPACE::mods(x,y); 197 } 198 }; 199 200 struct divp_op 201 { 202 static int applydivp_op203 apply(int x, int y) 204 { 205 return IMATH_NAMESPACE::divp(x,y); 206 } 207 }; 208 209 struct modp_op 210 { 211 static int applymodp_op212 apply(int x, int y) 213 { 214 return IMATH_NAMESPACE::modp(x,y); 215 } 216 }; 217 218 struct bias_op 219 { 220 static inline float applybias_op221 apply(float x, float b) 222 { 223 if (b != 0.5f) 224 { 225 static const float inverse_log_half = 1.0f / std::log(0.5f); 226 const float biasPow = std::log(b)*inverse_log_half; 227 return std::pow(x, biasPow); 228 } 229 return x; 230 } 231 }; 232 233 struct gain_op 234 { 235 static inline float applygain_op236 apply(float x, float g) 237 { 238 if (x < 0.5f) 239 return 0.5f*bias_op::apply(2.0f*x, 1.0f - g); 240 else 241 return 1.0f - 0.5f*bias_op::apply(2.0f - 2.0f*x, 1.0f - g); 242 } 243 }; 244 245 template <class T> 246 struct rgb2hsv_op 247 { 248 static inline IMATH_NAMESPACE::Vec3<T> applyrgb2hsv_op249 apply(const IMATH_NAMESPACE::Vec3<T> &rgb) 250 { 251 return IMATH_NAMESPACE::rgb2hsv(rgb); 252 } 253 }; 254 255 template <class T> 256 struct hsv2rgb_op 257 { 258 static inline IMATH_NAMESPACE::Vec3<T> applyhsv2rgb_op259 apply(const IMATH_NAMESPACE::Vec3<T> &rgb) 260 { 261 return IMATH_NAMESPACE::hsv2rgb(rgb); 262 } 263 }; 264 265 template <class T> 266 struct min_op 267 { 268 static inline T applymin_op269 apply(T x, T y) 270 { 271 return std::min(x,y); 272 } 273 }; 274 275 template <class T> 276 struct max_op 277 { 278 static inline T applymax_op279 apply(T x, T y) 280 { 281 return std::max(x,y); 282 } 283 }; 284 285 template <class T> 286 struct sin_op { 287 static inline T applysin_op288 apply(T theta) 289 { 290 return std::sin(theta); 291 } 292 }; 293 294 template <class T> 295 struct cos_op { 296 static inline T applycos_op297 apply(T theta) 298 { 299 return std::cos(theta); 300 } 301 }; 302 303 template <class T> 304 struct tan_op { 305 static inline T applytan_op306 apply(T theta) 307 { 308 return std::tan(theta); 309 } 310 }; 311 312 template <class T> 313 struct asin_op { 314 static inline T applyasin_op315 apply(T x) 316 { 317 return std::asin(x); 318 } 319 }; 320 321 template <class T> 322 struct acos_op { 323 static inline T applyacos_op324 apply(T x) 325 { 326 return std::acos(x); 327 } 328 }; 329 330 template <class T> 331 struct atan_op { 332 static inline float applyatan_op333 apply(T x) 334 { 335 return std::atan(x); 336 } 337 }; 338 339 template <class T> 340 struct atan2_op { 341 static inline T applyatan2_op342 apply(T y, T x) 343 { 344 return std::atan2(y,x); 345 } 346 }; 347 348 template <class T> 349 struct sqrt_op { 350 static inline T applysqrt_op351 apply(T x) 352 { 353 return std::sqrt(x); 354 } 355 }; 356 357 template <class T> 358 struct pow_op { 359 static inline T applypow_op360 apply(T x, T y) 361 { 362 return std::pow(x,y); 363 } 364 }; 365 366 template <class T> 367 struct exp_op 368 { 369 static inline T applyexp_op370 apply(T x) 371 { 372 return std::exp(x); 373 } 374 }; 375 376 template <class T> 377 struct sinh_op 378 { 379 static inline T applysinh_op380 apply(T x) 381 { 382 return std::sinh(x); 383 } 384 }; 385 386 template <class T> 387 struct cosh_op 388 { 389 static inline T applycosh_op390 apply(T x) 391 { 392 return std::cosh(x); 393 } 394 }; 395 396 } // namespace PyImath 397 398 #endif // _PyImathFunOperators_h_ 399