jaulib v1.3.8
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
float_util.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11
12#ifndef JAU_MATH_FLOAT_UTIL_HPP_
13#define JAU_MATH_FLOAT_UTIL_HPP_
14
15#include <cmath>
16#include <climits>
17#include <numbers>
18
19#include <jau/base_math.hpp>
20#include <jau/string_util.hpp>
21#include <jau/math/vec3f.hpp>
22
23namespace jau::math::util {
24
25 /** \addtogroup Math
26 *
27 * @{
28 */
29
30 /**
31 * Returns resolution of Z buffer of given parameter,
32 * see <a href="http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html">Love Your Z-Buffer</a>.
33 * <pre>
34 * return z * z / ( zNear * (1&lt;&lt;zBits) - z )
35 * </pre>
36 * Examples:
37 * <pre>
38 * 1.5256461E-4 = 16 zBits, -0.2 zDist, 0.1 zNear
39 * 6.1033297E-6 = 16 zBits, -1.0 zDist, 0.1 zNear
40 * </pre>
41 * @param zBits number of bits of Z precision, i.e. z-buffer depth
42 * @param z distance from the eye to the object
43 * @param zNear distance from eye to near clip plane
44 * @return smallest resolvable Z separation at this range.
45 */
46 constexpr float getZBufferEpsilon(int zBits, float z, float zNear) noexcept {
47 return z * z / ( zNear * float( 1 << zBits ) - z );
48 }
49
50 /**
51 * Returns Z buffer value of given parameter,
52 * see <a href="http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html">Love Your Z-Buffer</a>.
53 * <pre>
54 * float a = zFar / ( zFar - zNear )
55 * float b = zFar * zNear / ( zNear - zFar )
56 * return (int) ( (1&lt;&lt;zBits) * ( a + b / z ) )
57 * </pre>
58 * @param zBits number of bits of Z precision, i.e. z-buffer depth
59 * @param z distance from the eye to the object
60 * @param zNear distance from eye to near clip plane
61 * @param zFar distance from eye to far clip plane
62 * @return z buffer value
63 */
64 constexpr int getZBufferValue(int zBits, float z, float zNear, float zFar) noexcept {
65 const float a = zFar / ( zFar - zNear );
66 const float b = zFar * zNear / ( zNear - zFar );
67 return (int) ( float(1<<zBits) * ( a + b / z ) );
68 }
69
70 /**
71 * Returns orthogonal distance
72 * (1f/zNear-1f/orthoZ) / (1f/zNear-1f/zFar);
73 */
74 constexpr float getOrthoWinZ(float orthoZ, float zNear, float zFar) noexcept {
75 return (1.0f/zNear-1.0f/orthoZ) / (1.0f/zNear-1.0f/zFar);
76 }
77
78 /**
79 * Returns an orientation vector for given eurler X/Y/Z angles in radians.
80 *
81 * Returned vector reflect each axis and is either `1` for not-flipped or `-1` for flipped orientation..
82 */
83 constexpr jau::math::Vec3f getEulerAngleOrientation(const jau::math::Vec3f& eulerRotation) noexcept {
84 constexpr float half_pi = std::numbers::pi_v<float>/2.0f;
85 const float x_rot = std::abs(eulerRotation.x);
86 const float y_rot = std::abs(eulerRotation.y);
87 const float z_rot = std::abs(eulerRotation.z);
88 return jau::math::Vec3f(
89 half_pi <= y_rot && y_rot <= 3*half_pi ? -1 : 1,
90 half_pi <= x_rot && x_rot <= 3*half_pi ? -1 : 1,
91 half_pi <= z_rot && z_rot <= 3*half_pi ? -1 : 1);
92 }
93
94 /**@}*/
95
96} // namespace jau
97
98#endif /* JAU_MATH_FLOAT_UTIL_HPP_ */
constexpr float getOrthoWinZ(float orthoZ, float zNear, float zFar) noexcept
Returns orthogonal distance (1f/zNear-1f/orthoZ) / (1f/zNear-1f/zFar);.
constexpr float getZBufferEpsilon(int zBits, float z, float zNear) noexcept
Returns resolution of Z buffer of given parameter, see Love Your Z-Buffer.
constexpr jau::math::Vec3f getEulerAngleOrientation(const jau::math::Vec3f &eulerRotation) noexcept
Returns an orientation vector for given eurler X/Y/Z angles in radians.
constexpr int getZBufferValue(int zBits, float z, float zNear, float zFar) noexcept
Returns Z buffer value of given parameter, see Love Your Z-Buffer.
Vector3F< float > Vec3f
Definition vec3f.hpp:433