Knoodle
Knoodle is a Free Open-Source alternative for graph-based texture designers. It is a tool for creating procedural textures using a node-based graph editor. It is designed to be easy to use, to be able to create complex textures with a minimum of effort, and to be pluggable to any game engine and front-end.
 
Loading...
Searching...
No Matches
kn_math.hpp
1/**************************************************************************/
2/* vector3.hpp */
3/**************************************************************************/
4/* This file is part of: */
5/* Knoodle */
6/* https://knoodlegraph.org */
7/**************************************************************************/
8/* Copyright (c) 2025 Knoodle contributors (vide AUTHORS.md) */
9/* */
10/* Permission is hereby granted, free of charge, to any person obtaining */
11/* a copy of this software and associated documentation files (the */
12/* "Software"), to deal in the Software without restriction, including */
13/* without limitation the rights to use, copy, modify, merge, publish, */
14/* distribute, sublicense, and/or sell copies of the Software, and to */
15/* permit persons to whom the Software is furnished to do so, subject to */
16/* the following conditions: */
17/* */
18/* The above copyright notice and this permission notice shall be */
19/* included in all copies or substantial portions of the Software. */
20/* */
21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28/**************************************************************************/
29
30#pragma once
31
32#include "common.hpp"
33
34namespace kn::math {
35
36template <typename T>
37concept number = std::integral<T> or std::floating_point<T>;
38
39/** @brief Returns the absolute value of x.
40 * @param x The value to return the absolute value of.
41 * @return The absolute value of x.
42 */
43constexpr auto abs(number auto x) {
44 return x < 0 ? -x : x;
45}
46
47/** @brief Returns the minimum of a and b.
48 * @param a The first value to compare.
49 * @param b The second value to compare.
50 * @return The minimum of a and b.
51 */
52constexpr auto min(number auto a, number auto b) {
53 return a < b ? a : b;
54}
55
56/** @brief Returns the maximum of a and b.
57 * @param a The first value to compare.
58 * @param b The second value to compare.
59 * @return The maximum of a and b.
60 */
61constexpr auto max(number auto a, number auto b) {
62 return a > b ? a : b;
63}
64
65/** @brief Returns the clamp of x between min and max.
66 * @param x The value to clamp.
67 * @param min The minimum value.
68 * @param max The maximum value.
69 * @return The clamped value of x.
70 */
71constexpr auto clamp(number auto x, number auto min, number auto max) {
72 return x < min ? min : (x > max ? max : x);
73}
74
75/** @brief Returns the square of x.
76 * @param x The value to square.
77 * @return The square of x.
78 */
79constexpr auto pow2(number auto x) {
80 return x * x;
81}
82
83/** @brief Returns the cube of x.
84 * @param x The value to cube.
85 * @return The cube of x.
86 */
87constexpr auto pow3(number auto x) {
88 return x * x * x;
89}
90
91/** @brief Returns the value of x to the power of 4.
92 * @param x The value to raise to the power of 4.
93 * @return The value of x to the power of 4.
94 */
95constexpr auto pow4(number auto x) {
96 return x * x * x * x;
97}
98
99/** @brief Returns the value of x to the power of 5.
100 * @param x The value to raise to the power of 5.
101 * @return The value of x to the power of 5.
102 */
103constexpr auto pow5(number auto x) {
104 return x * x * x * x * x;
105}
106
107/** @brief Returns square root of x.
108 * @param x The value to return the square root of.
109 * @return The square root of x.
110 */
111auto sqrt(number auto x) {
112 if constexpr (std::same_as<decltype(x), float>)
113 return ::sqrtf(x);
114 else
115 return ::sqrt(static_cast<real_t>(x));
116}
117
118/** @brief Returns fast inverse square root of x.
119 * Implements the Quake III algorithm.
120 * @param x The value to return the inverse square root of.
121 * @return The inverse square root of x.
122 */
123inline float rsqrt(float x) {
124 return 1.0f / ::sqrtf(x);
125}
126
127/** @brief Returns the linear interpolation between a and b by t.
128 * @param a The first value to interpolate.
129 * @param b The second value to interpolate.
130 * @param t The interpolation factor.
131 * @return The linear interpolation between a and b by t.
132 */
133constexpr auto lerp(number auto a, number auto b, number auto t) {
134 return a + t * (b - a);
135}
136} // namespace kn::math