Kale
Loading...
Searching...
No Matches
Utils.hpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
20
21#include <limits>
22#include <random>
23
24namespace Kale {
25
30 enum class AngleUnit : bool {
32 };
33
39 constexpr float degToRad(float deg) {
40 return deg / 180.0f * PI;
41 }
42
48 constexpr float radToDeg(float rad) {
49 return rad / PI * 180.0f;
50 }
51
58 template <typename T> bool isFloating0(T num, T epsilon = std::numeric_limits<T>::epsilon()) {
59 return num <= epsilon && num >= -epsilon;
60 }
61
69 template <typename T> bool isFloatingEqual(T num1, T num2, T epsilon = std::numeric_limits<T>::epsilon()) {
70 T tmp = num1 - num2;
71 return tmp <= epsilon && tmp >= -epsilon;
72 }
73
79 template <typename T> int sign(T val) {
80 return (T(0) < val) - (val < T(0));
81 }
82
83 template <typename T> typename std::enable_if<std::is_floating_point<T>::value, T>::type randomRange(T min, T max) {
84 static thread_local std::random_device randomDevice;
85 static thread_local std::mt19937 generator(randomDevice());
86 std::uniform_real_distribution<T> distribution(min,max);
87 return distribution(generator);
88 }
89
90 template <typename T> typename std::enable_if<std::is_integral<T>::value, T>::type randomRange(T min, T max) {
91 static thread_local std::random_device randomDevice;
92 static thread_local std::mt19937 generator(randomDevice());
93 std::uniform_int_distribution<T> distribution(min,max);
94 return distribution(generator);
95 }
96
97}
std::enable_if< std::is_floating_point< T >::value, T >::type randomRange(T min, T max)
Definition Utils.hpp:83
AngleUnit
Definition Utils.hpp:30
bool isFloatingEqual(T num1, T num2, T epsilon=std::numeric_limits< T >::epsilon())
Definition Utils.hpp:69
constexpr float radToDeg(float rad)
Definition Utils.hpp:48
bool isFloating0(T num, T epsilon=std::numeric_limits< T >::epsilon())
Definition Utils.hpp:58
int sign(T val)
Definition Utils.hpp:79
constexpr float PI
Definition Constants.hpp:24
constexpr float degToRad(float deg)
Definition Utils.hpp:39