Skip to content

Commit 3251746

Browse files
add: expand mathematical constants documentation with detailed descriptions and usage examples
1 parent 9f35324 commit 3251746

1 file changed

Lines changed: 339 additions & 0 deletions

File tree

docs/engine/cpp-api-reference/runtime/core/math/constants.md

Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,342 @@ tags:
66
- math
77
- constants
88
---
9+
10+
# Constants
11+
12+
The `Constants` struct provides a collection of mathematical constants and conversion factors for floating-point types. It includes values such as Pi, Euler's number, and various limits and thresholds that are commonly used in mathematical computations.
13+
14+
## Definition
15+
16+
```cpp showLineNumbers
17+
template <Concepts::IsFloatingPoint T>
18+
struct Constants final
19+
```
20+
21+
:::warning
22+
23+
**Note:** The `Constants` struct is designed for floating-point types and should not be used with integer types. Attempting to use it with non-floating-point types will result in a compilation error due to the `IsFloatingPoint` concept constraint.
24+
25+
:::
26+
27+
## Public Members
28+
29+
### Pi
30+
31+
```cpp
32+
static constexpr T Pi;
33+
```
34+
The mathematical constant π ≈ `3.14159265358979323846`. Sourced from `std::numbers::pi_v<T>`.
35+
36+
---
37+
38+
```cpp
39+
static constexpr T TwoPi;
40+
```
41+
Two times π ≈ `6.28318530717958647692`. Represents a full rotation in radians and is commonly used in periodic functions and angular wrapping.
42+
43+
---
44+
45+
```cpp
46+
static constexpr T HalfPi;
47+
```
48+
Half of π ≈ `1.57079632679489661923`. Represents a 90-degree rotation in radians. Frequently used in trigonometric identities and orientation math.
49+
50+
---
51+
52+
```cpp
53+
static constexpr T QuarterPi;
54+
```
55+
One quarter of π ≈ `0.78539816339744830961`. Represents a 45-degree rotation in radians.
56+
57+
---
58+
59+
```cpp
60+
static constexpr T InvPi;
61+
```
62+
The multiplicative inverse of π ≈ `0.31830988618379067153`. Sourced from `std::numbers::inv_pi_v<T>`. Useful for normalizing values that are expressed as multiples of π.
63+
64+
---
65+
66+
```cpp
67+
static constexpr T InvTwoPi;
68+
```
69+
The multiplicative inverse of 2π ≈ `0.15915494309189533576`. Useful for converting radians into a normalized `[0, 1]` frequency range.
70+
71+
---
72+
73+
### Conversion
74+
75+
```cpp
76+
static constexpr T DegToRad;
77+
```
78+
Conversion factor from degrees to radians — equivalent to `π / 180`. Multiply a degree value by this constant to obtain its radian equivalent.
79+
80+
---
81+
82+
```cpp
83+
static constexpr T RadToDeg;
84+
```
85+
Conversion factor from radians to degrees — equivalent to `180 / π`. Multiply a radian value by this constant to obtain its degree equivalent.
86+
87+
---
88+
89+
### Mathematical Constants
90+
91+
```cpp
92+
static constexpr T E;
93+
```
94+
Euler's number ≈ `2.71828182845904523536`. The base of the natural logarithm. Sourced from `std::numbers::e_v<T>`.
95+
96+
---
97+
98+
```cpp
99+
static constexpr T Sqrt2;
100+
```
101+
The square root of 2 ≈ `1.41421356237309504880`. Sourced from `std::numbers::sqrt2_v<T>`. Commonly appears in diagonal distance calculations and rotation matrices.
102+
103+
---
104+
105+
```cpp
106+
static constexpr T Sqrt3;
107+
```
108+
The square root of 3 ≈ `1.73205080756887729352`. Frequently used in hexagonal grid math and equilateral triangle geometry.
109+
110+
---
111+
112+
```cpp
113+
static constexpr T InvSqrt2;
114+
```
115+
The multiplicative inverse of √2 ≈ `0.70710678118654752440`. Equivalent to `1 / √2` or `√2 / 2`. Commonly used to construct 45-degree unit vectors.
116+
117+
---
118+
119+
```cpp
120+
static constexpr T Ln2;
121+
```
122+
The natural logarithm of 2 ≈ `0.69314718055994530941`. Sourced from `std::numbers::ln2_v<T>`. Used in exponential decay and octave-based frequency calculations.
123+
124+
---
125+
126+
```cpp
127+
static constexpr T Ln10;
128+
```
129+
The natural logarithm of 10 ≈ `2.30258509299404568401`. Sourced from `std::numbers::ln10_v<T>`. Used for converting between natural and base-10 logarithms.
130+
131+
---
132+
133+
```cpp
134+
static constexpr T Log2E;
135+
```
136+
The base-2 logarithm of Euler's number ≈ `1.44269504088896340735`. Sourced from `std::numbers::log2e_v<T>`. Useful for fast power-of-two exponent computations.
137+
138+
---
139+
140+
```cpp
141+
static constexpr T Log10E;
142+
```
143+
The base-10 logarithm of Euler's number ≈ `0.43429448190325182765`. Sourced from `std::numbers::log10e_v<T>`.
144+
145+
---
146+
147+
```cpp
148+
static constexpr T Phi;
149+
```
150+
The golden ratio φ ≈ `1.61803398874989484820`. Sourced from `std::numbers::phi_v<T>`. Appears in procedural geometry, aesthetics-driven layout systems, and Fibonacci-based sequences.
151+
152+
---
153+
154+
### Limits
155+
156+
```cpp
157+
static constexpr T Epsilon;
158+
```
159+
The smallest representable difference between two distinct floating-point values of type `T`. Sourced from `std::numeric_limits<T>::epsilon()`. Used as a baseline for floating-point equality comparisons.
160+
161+
---
162+
163+
```cpp
164+
static constexpr T Infinity;
165+
```
166+
Positive infinity for type `T`. Sourced from `std::numeric_limits<T>::infinity()`. Useful as a sentinel value in distance queries, BVH traversal, and min/max initialization.
167+
168+
---
169+
170+
### Tolerance
171+
172+
```cpp
173+
static constexpr T SmallNumber;
174+
```
175+
A very small number (`1e-8`). Suitable for high-precision near-zero comparisons where `Epsilon` may be too strict.
176+
177+
---
178+
179+
```cpp
180+
static constexpr T KindaSmallNumber;
181+
```
182+
A moderately small number (`1e-4`). Used when a looser near-zero threshold is acceptable, such as in physics simulations or animation blending.
183+
184+
---
185+
186+
```cpp
187+
static constexpr T BigNumber;
188+
```
189+
A large finite number (`3.4e+38`). Useful as a practical stand-in for infinity in systems where IEEE infinity is undesirable, such as certain physics solvers or serialized data ranges.
190+
191+
---
192+
193+
```cpp
194+
static constexpr T Delta;
195+
```
196+
A general-purpose small delta value (`0.00001`). Commonly used as a nudge factor or a safe margin in geometric intersection tests.
197+
198+
---
199+
200+
### Thresholds
201+
202+
Thresholds are used to determine when two floating-point numbers are considered equal or when a number is close enough to zero. All threshold values are defined within the nested `Thresholds` struct.
203+
204+
```cpp showLineNumbers
205+
struct Thresholds final
206+
```
207+
208+
---
209+
210+
```cpp
211+
static constexpr T Normal;
212+
```
213+
Minimum acceptable length for a vector to be considered normalized (`0.0001`). Set to half the maximum meaningful value to prevent dot product overflow when operating on normalized vectors.
214+
215+
---
216+
217+
```cpp
218+
static constexpr T PointOnPlane;
219+
```
220+
Half-thickness of a plane used in front/back/inside classification tests (`0.10`). Points within this distance of a plane are considered coplanar.
221+
222+
---
223+
224+
```cpp
225+
static constexpr T PointOnSide;
226+
```
227+
Half-thickness of a polygon edge's side-plane used in point-inside/outside/on-side tests (`0.20`).
228+
229+
---
230+
231+
```cpp
232+
static constexpr T PointsAreSame;
233+
```
234+
Maximum distance between two points for them to be considered identical (`0.00002`). Used in mesh welding, deduplication, and CSG operations.
235+
236+
---
237+
238+
```cpp
239+
static constexpr T PointsAreNear;
240+
```
241+
Maximum distance between two points for them to be considered near-enough to merge when imprecise math is acceptable (`0.015`).
242+
243+
---
244+
245+
```cpp
246+
static constexpr T NormalsAreSame;
247+
```
248+
Maximum angular deviation between two normals for them to be considered identical (`0.00002`). Used in normal averaging and smoothing group classification.
249+
250+
---
251+
252+
```cpp
253+
static constexpr T UVsAreSame;
254+
```
255+
Maximum difference between two UV coordinates for them to be considered the same (`0.0009765625`, equivalent to `1 / 1024`).
256+
257+
:::caution
258+
Setting this value too large will cause incorrect CSG classification and UV seam artifacts.
259+
:::
260+
261+
---
262+
263+
```cpp
264+
static constexpr T VectorsAreNear;
265+
```
266+
Maximum distance between two vectors for them to be considered near-enough to merge when imprecise math is acceptable (`0.0004`).
267+
268+
:::caution
269+
Setting this value too large can introduce lighting artifacts caused by inaccurate texture coordinate interpolation.
270+
:::
271+
272+
---
273+
274+
```cpp
275+
static constexpr T SplitPolygonWithPlane;
276+
```
277+
Distance threshold at which a plane is considered to split a polygon in half (`0.25`). Used in BSP tree construction and convex decomposition.
278+
279+
---
280+
281+
```cpp
282+
static constexpr T SplitPolygonPrecisely;
283+
```
284+
A tighter distance threshold for precise polygon splitting (`0.01`). Used when high-fidelity BSP splits are required.
285+
286+
---
287+
288+
```cpp
289+
static constexpr T ZeroNormSquared;
290+
```
291+
The squared magnitude threshold below which a unit normal is considered degenerate (zero-length) (`0.0001`).
292+
293+
---
294+
295+
```cpp
296+
static constexpr T NormalsAreParallel;
297+
```
298+
Minimum absolute dot product between two unit vectors for them to be considered parallel (`0.999845`). Approximately equivalent to `cos(1.0°)`.
299+
300+
---
301+
302+
```cpp
303+
static constexpr T NormalsAreOrthogonal;
304+
```
305+
Maximum absolute dot product between two unit vectors for them to be considered orthogonal (perpendicular) (`0.017455`). Approximately equivalent to `cos(89.0°)`.
306+
307+
---
308+
309+
```cpp
310+
static constexpr T VectorNormalized;
311+
```
312+
Maximum allowed deviation of a vector's squared magnitude from `1.0` for it to be considered normalized (`0.01`).
313+
314+
---
315+
316+
```cpp
317+
static constexpr T QuaternionNormalized;
318+
```
319+
Maximum allowed deviation of a quaternion's squared magnitude from `1.0` for it to be considered normalized (`0.01`).
320+
321+
---
322+
323+
## Example Usage
324+
325+
```cpp showLineNumbers
326+
#include "Math/Constants.hpp"
327+
328+
using FloatConstants = GP::Math::Constants<float>;
329+
using DoubleConstants = GP::Math::Constants<double>;
330+
331+
// Convert 90 degrees to radians
332+
float radians = 90.0f * FloatConstants::DegToRad;
333+
334+
// Check if a vector length is degenerate
335+
bool isDegenerate = squaredLength < FloatConstants::Thresholds::ZeroNormSquared;
336+
337+
// Use infinity as a sentinel for BVH traversal
338+
float tMin = FloatConstants::Infinity;
339+
```
340+
341+
---
342+
343+
## Source
344+
345+
The full source for `Constants` is available in the engine repository:
346+
347+
[`Source/Runtime/Core/Public/Math/Utils/Constants.hpp`](https://github.com/GraphicalPlayground/gp-engine/blob/main/Source/Runtime/Core/Public/Math/Utils/Constants.hpp)

0 commit comments

Comments
 (0)