Skip to content

Commit 6ae203b

Browse files
author
devsh
committed
add hlsl::shapes::AABB transformation utilities
1 parent 92fd826 commit 6ae203b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

include/nbl/builtin/hlsl/shapes/aabb.hlsl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#ifndef _NBL_BUILTIN_HLSL_SHAPES_AABB_INCLUDED_
55
#define _NBL_BUILTIN_HLSL_SHAPES_AABB_INCLUDED_
66

7+
78
#include "nbl/builtin/hlsl/concepts.hlsl"
89
#include "nbl/builtin/hlsl/limits.hlsl"
910
#include "nbl/builtin/hlsl/shapes/util.hlsl"
1011

12+
1113
namespace nbl
1214
{
1315
namespace hlsl
@@ -88,6 +90,48 @@ struct union_helper<AABB<D,Scalar>>
8890
return retval;
8991
}
9092
};
93+
#define NBL_UNROLL [[unroll]]
94+
// without a translation component
95+
template<int16_t D, typename Scalar>
96+
struct transform_helper<AABB<D,Scalar>,matrix<Scalar,D,D> >
97+
{
98+
using type = AABB<D,Scalar>;
99+
using matrix_t = matrix<Scalar,D,D>;
100+
using vector_t = vector<Scalar,D>;
101+
102+
static inline type __call(NBL_CONST_REF_ARG(matrix_t) lhs, NBL_CONST_REF_ARG(type) rhs)
103+
{
104+
// take each column and tweak to get minimum and max
105+
const matrix_t M_T = hlsl::transpose(lhs);
106+
vector<bool,D> signM_T[D];
107+
NBL_UNROLL for (int16_t j=0; j<D; j++)
108+
NBL_UNROLL for (int16_t i=0; i<D; i++)
109+
signM_T[j][i] = M_T[j][i]<Scalar(0);
110+
111+
type retval;
112+
retval.minVx = M_T[0] * hlsl::mix<vector_t>(rhs.minVx.xxx, rhs.maxVx.xxx, signM_T[0]) + M_T[1] * hlsl::mix<vector_t>(rhs.minVx.yyy, rhs.maxVx.yyy, signM_T[1]) + M_T[2] * hlsl::mix<vector_t>(rhs.minVx.zzz, rhs.maxVx.zzz, signM_T[2]);
113+
retval.maxVx = M_T[0] * hlsl::mix<vector_t>(rhs.maxVx.xxx, rhs.minVx.xxx, signM_T[0]) + M_T[1] * hlsl::mix<vector_t>(rhs.maxVx.yyy, rhs.minVx.yyy, signM_T[1]) + M_T[2] * hlsl::mix<vector_t>(rhs.maxVx.zzz, rhs.minVx.zzz, signM_T[2]);
114+
return retval;
115+
}
116+
};
117+
// affine weird matrix
118+
template<int16_t D, typename Scalar>
119+
struct transform_helper<AABB<D,Scalar>,matrix<Scalar,D,D+1> >
120+
{
121+
using type = AABB<D,Scalar>;
122+
using matrix_t = matrix<Scalar,D,D+1>;
123+
using sub_matrix_t = matrix<Scalar,D,D>;
124+
125+
static inline type __call(NBL_CONST_REF_ARG(matrix_t) lhs, NBL_CONST_REF_ARG(type) rhs)
126+
{
127+
const vector<Scalar,D> translation = hlsl::transpose(lhs)[D];
128+
type retval = transform_helper<type,sub_matrix_t>::__call(sub_matrix_t(lhs),rhs);
129+
// retval.minVx += translation;
130+
// retval.maxVx += translation;
131+
return retval;
132+
}
133+
};
134+
#undef NBL_UNROLL
91135
}
92136
}
93137

include/nbl/builtin/hlsl/shapes/util.hlsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ template<typename T>
2222
struct intersect_helper;
2323
template<typename T>
2424
struct union_helper;
25+
template<typename T, typename M>
26+
struct transform_helper;
2527
}
2628

2729
template<typename T>
2830
T intersect(NBL_CONST_REF_ARG(T) lhs, NBL_CONST_REF_ARG(T) rhs) {return impl::intersect_helper<T>::__call(lhs,rhs);}
2931
// union is a keyword in C++
3032
template<typename T>
3133
T union_(NBL_CONST_REF_ARG(T) lhs, NBL_CONST_REF_ARG(T) rhs) {return impl::union_helper<T>::__call(lhs,rhs);}
34+
template<typename T, typename M>
35+
T transform(NBL_CONST_REF_ARG(M) lhs, NBL_CONST_REF_ARG(T) rhs) {return impl::transform_helper<T,M>::__call(lhs,rhs);}
3236

3337
}
3438
}

0 commit comments

Comments
 (0)