UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SphericalFibonacci.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "MathUtil.h"
6#include "VectorTypes.h"
7#include "MatrixTypes.h"
8
9namespace UE
10{
11namespace Geometry
12{
13
14using namespace UE::Math;
15
24template<typename RealType>
26{
27public:
28 int32 N = 64;
29
30 TSphericalFibonacci(int32 NumPoints = 64)
31 : N(NumPoints)
32 {
33 }
34
35 int32 Num() const
36 {
37 return N;
38 }
39
45 {
46 static const RealType PHI = ( TMathUtil<RealType>::Sqrt(5.0) + 1.0) / 2.0;
47
48 checkSlow(Index >= 0 && Index < N);
49 RealType div = (RealType)Index / PHI;
52
53 RealType z = 1.0 - (2.0 * (RealType)Index + 1.0) / (RealType)N;
54 RealType theta = TMathUtil<RealType>::ACos(z);
56
58 }
59
60
66 {
67 return Point(Index);
68 }
69
70
75 {
76 static const RealType PHI = (TMathUtil<RealType>::Sqrt(5.0) + 1.0) / 2.0;
77
79 RealType cosTheta = P.Z;
83
84 RealType F0 = TMathUtil<RealType>::Round(Fk);
85 RealType F1 = TMathUtil<RealType>::Round(Fk * PHI);
86
88 2.0 * TMathUtil<RealType>::Pi * MultiplyAddFrac(F0 + 1.0, PHI - 1.0) - 2.0 * TMathUtil<RealType>::Pi * (PHI - 1),
89 2.0 * TMathUtil<RealType>::Pi * MultiplyAddFrac(F1 + 1.0, PHI - 1.0) - 2.0 * TMathUtil<RealType>::Pi * (PHI - 1),
90 -2.0 * F0 / N, -2.0 * F1 / N);
92
93 //Vector2d c = floor(mul(invB, RealType2(phi, cosTheta - (1 - 1.0/N))));
94 TVector2<RealType> c(phi, cosTheta - (1.0 - 1.0/N));
95 c = invB * c;
97
98 RealType d = TMathUtil<RealType>::MaxReal, j = 0;
99 for (int32 s = 0; s < 4; ++s)
100 {
101 TVector2<RealType> cosTheta_second((RealType)(s % 2) + c.X, (RealType)(s / 2) + c.Y);
102 cosTheta = B.Row1.Dot(cosTheta_second) + (1.0 - 1.0 / N);
104 RealType i = TMathUtil<RealType>::Floor(N * 0.5 - cosTheta * N * 0.5);
106 cosTheta = 1.0 - (2.0 * i + 1.0) * (1.0 / N); // rcp(n);
111 cosTheta );
112 RealType SquaredDistance = DistanceSquared(q, P);
113 if (SquaredDistance < d)
114 {
115 d = SquaredDistance;
116 j = i;
117 }
118 }
119
120 return (int32)j;
121 }
122
123
124
125
126
127protected:
128 // MultiplyAddFrac(A,B) = multiply_add( A ,B, -floor(A*B) )
129 static RealType MultiplyAddFrac(RealType a, RealType b)
130 {
131 return a * b + -TMathUtil<RealType>::Floor(a * b);
132 }
133};
134
135
144template<typename RealType>
146{
147public:
148 int32 N = 64;
149
150 enum class EType
151 {
152 Square, // Fibonacci lattice on unit square
153 Disc // Fibonacci lattice on unit disc (spiral)
154 };
156
157public:
159 : N(NumPoints), Type(InType)
160 {
161 }
162
163 int32 Num() const
164 {
165 return N;
166 }
167
173 {
174 static const RealType PHI = (TMathUtil<RealType>::Sqrt(5.0) + 1.0) / 2.0;
175 checkSlow(Index >= 0 && Index < N);
176
177 // Use 1-based indices to exclude the zero/origin term.
178 // Internally remap the range: [1, N+1].
179 const int32 Idx = Index + 1;
180 const int32 NumPts = N + 1;
181
182 // Unit Square Lattice:
183 // (xs,ys) --> (modf(i/phi, unused), i/n)
184 RealType Div = (RealType)Idx / PHI;
185 RealType X = (Div - TMathUtil<RealType>::Floor(Div));
186 RealType Y = (RealType)Idx / (RealType)NumPts;
187
188 if (Type == EType::Disc)
189 {
190 // Unit Disc Lattice:
191 // (theta, r) --> (2*pi*xs, sqrt(ys))
192 // (xd, yd) --> (r*cos(theta), r*sin(theta))
194 RealType R = TMathUtil<RealType>::Sqrt(Y);
197 }
198 return TVector2<RealType>(X,Y);
199 }
200
206 {
207 return Point(Index);
208 }
209};
210
211
218template<typename RealType>
220{
221public:
222 int32 N = 64;
223
224 enum class EDistribution
225 {
226 Uniform,
227 Cosine
228 };
230
231public:
233 : N(NumPoints), Distribution(Dist)
234 {
235 }
236
237 int32 Num() const
238 {
239 return N;
240 }
241
247 {
248 checkSlow(Index >= 0 && Index < N);
249
251 switch (Distribution)
252 {
254 {
256 Point = Points[Index];
257 break;
258 }
260 {
262 const TVector2<RealType> Pt = Points[Index];
263
264 // Planar projection of Fibonacci spiral (unit disc [xd, yd]) to
265 // hemisphere to achieve cosine weighted ray distribution.
266 // (Malley's method).
267 //
268 // Hemisphere: (x,y,z) --> (xd, yd, 1-xd^2-yd^2)
269 //
270 // Reference: Physically Based Rendering: From Theory to Implementation
271 // http://www.pbr-book.org/3ed-2018/Monte_Carlo_Integration/2D_Sampling_with_Multidimensional_Transformations.html#Cosine-WeightedHemisphereSampling
272 RealType Z = TMathUtil<RealType>::Sqrt(TMathUtil<RealType>::Max(0.0, 1.0 - Pt.X * Pt.X - Pt.Y * Pt.Y));
273 Point = TVector<RealType>(Pt.X, Pt.Y, Z);
274 break;
275 }
276 }
277 return Point;
278 }
279
285 {
286 return Point(Index);
287 }
288};
289
290} // end namespace UE::Geometry
291} // end namespace UE
#define checkSlow(expr)
Definition AssertionMacros.h:332
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define X(Name, Desc)
Definition FormatStringSan.h:47
Definition MathUtil.h:150
static RealType Cos(const RealType Value)
Definition MathUtil.h:372
static RealType Sin(const RealType Value)
Definition MathUtil.h:366
static RealType Clamp(const RealType Value, const RealType ClampMin, const RealType ClampMax)
Definition MathUtil.h:222
static RealType Floor(const RealType Value)
Definition MathUtil.h:384
static RealType Max(const RealType A, const RealType B)
Definition MathUtil.h:246
static RealType Sqrt(const RealType Value)
Definition MathUtil.h:342
static RealType ACos(const RealType Value)
Definition MathUtil.h:378
static RealType Pow(const RealType Value, const RealType Power)
Definition MathUtil.h:402
static RealType Min(const RealType A, const RealType B)
Definition MathUtil.h:271
static RealType Round(const RealType Value)
Definition MathUtil.h:396
Definition SphericalFibonacci.h:146
TVector2< RealType > operator[](int32 Index) const
Definition SphericalFibonacci.h:205
EType
Definition SphericalFibonacci.h:151
int32 N
Definition SphericalFibonacci.h:148
TFibonacciLattice(int32 NumPoints=64, EType InType=EType::Square)
Definition SphericalFibonacci.h:158
int32 Num() const
Definition SphericalFibonacci.h:163
TVector2< RealType > Point(int32 Index) const
Definition SphericalFibonacci.h:172
EType Type
Definition SphericalFibonacci.h:155
Definition SphericalFibonacci.h:220
THemisphericalFibonacci(int32 NumPoints=64, EDistribution Dist=EDistribution::Uniform)
Definition SphericalFibonacci.h:232
EDistribution Distribution
Definition SphericalFibonacci.h:229
int32 Num() const
Definition SphericalFibonacci.h:237
TVector< RealType > Point(int32 Index) const
Definition SphericalFibonacci.h:246
TVector< RealType > operator[](int32 Index) const
Definition SphericalFibonacci.h:284
int32 N
Definition SphericalFibonacci.h:222
EDistribution
Definition SphericalFibonacci.h:225
Definition SphericalFibonacci.h:26
int32 FindIndex(const TVector< RealType > &P)
Definition SphericalFibonacci.h:74
TSphericalFibonacci(int32 NumPoints=64)
Definition SphericalFibonacci.h:30
TVector< RealType > Point(int32 Index) const
Definition SphericalFibonacci.h:44
int32 N
Definition SphericalFibonacci.h:28
int32 Num() const
Definition SphericalFibonacci.h:35
static RealType MultiplyAddFrac(RealType a, RealType b)
Definition SphericalFibonacci.h:129
TVector< RealType > operator[](int32 Index) const
Definition SphericalFibonacci.h:65
T DistanceSquared(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:82
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
U16 Index
Definition radfft.cpp:71
Definition MatrixTypes.h:283
TMatrix2< RealType > Inverse() const
Definition MatrixTypes.h:418
Definition Vector2D.h:38
T Y
Definition Vector2D.h:52
T X
Definition Vector2D.h:49
Definition Vector.h:51