Skip to content

Commit a3733b1

Browse files
committed
moved polar coord stuff into its own file
1 parent 340cee3 commit a3733b1

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

include/nbl/builtin/hlsl/math/functions.hlsl

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,6 @@ typename cpp_compat_intrinsics_impl::mul_helper<Lhs, Rhs>::return_t applyChainRu
192192
return hlsl::mul(dFdG, dGdR);
193193
}
194194

195-
// takes in normalized vectors
196-
template<typename T>
197-
vector<T, 3> polarToCartesian(vector<T, 2> theta_phi)
198-
{
199-
return vector<T, 3>(hlsl::cos(theta_phi.y) * hlsl::cos(theta_phi.x),
200-
hlsl::sin(theta_phi.y) * hlsl::cos(theta_phi.x),
201-
hlsl::sin(theta_phi.x));
202-
}
203-
204-
template<typename T>
205-
vector<T, 2> cartesianToPolar(vector<T, 3> coords)
206-
{
207-
return vector<T, 2>(hlsl::acos(clamp<float>(coords.z, -1, 1)), hlsl::atan2(coords.y, coords.x));
208-
}
209-
210195
}
211196
}
212197
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (C) 2018-2025 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#ifndef _NBL_BUILTIN_HLSL_MATH_POLAR_INCLUDED_
5+
#define _NBL_BUILTIN_HLSL_MATH_POLAR_INCLUDED_
6+
7+
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
8+
#include "nbl/builtin/hlsl/numbers.hlsl"
9+
10+
namespace nbl
11+
{
12+
namespace hlsl
13+
{
14+
namespace math
15+
{
16+
17+
template<typename T>
18+
struct Polar
19+
{
20+
using scalar_type = T;
21+
using vector2_type = vector<T, 2>;
22+
using vector3_type = vector<T, 3>;
23+
24+
// should be normalized
25+
static Polar<T> createFromCartesian(const vector3_type coords)
26+
{
27+
Polar<T> retval;
28+
retval.theta = hlsl::acos(coords.z);
29+
retval.phi = hlsl::atan2(coords.y, coords.x);
30+
return retval;
31+
}
32+
33+
static vector3_type ToCartesian(const scalar_type theta, const scalar_type phi)
34+
{
35+
return vector<T, 3>(hlsl::cos(phi) * hlsl::cos(theta),
36+
hlsl::sin(phi) * hlsl::cos(theta),
37+
hlsl::sin(theta));
38+
}
39+
40+
vector3_type getCartesian()
41+
{
42+
return ToCartesian(theta, phi);
43+
}
44+
45+
scalar_type theta;
46+
scalar_type phi;
47+
};
48+
49+
}
50+
}
51+
}
52+
53+
#endif

src/nbl/builtin/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/linalg/transform.hlsl")
223223
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/functions.hlsl")
224224
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/geometry.hlsl")
225225
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/intutil.hlsl")
226+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/polar.hlsl")
226227
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/angle_adding.hlsl")
227228
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/equations/quadratic.hlsl")
228229
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/equations/cubic.hlsl")

0 commit comments

Comments
 (0)