Skip to content

Commit 210cca4

Browse files
author
devsh
committed
implement more AABB utilities
1 parent 898b259 commit 210cca4

File tree

3 files changed

+66
-55
lines changed

3 files changed

+66
-55
lines changed

include/nbl/asset/IGeometry.h

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,58 @@ class IGeometryBase : public virtual core::IReferenceCounted
103103
// using `nbl::hlsl::` concepts instead of `std::` so that `nbl::hlsl::float16_t` can be used
104104
union SAABBStorage
105105
{
106+
template<typename Visitor>
107+
inline void visit(const EAABBFormat format, Visitor&& visitor)
108+
{
109+
switch (format)
110+
{
111+
case EAABBFormat::F64:
112+
visitor(f64);
113+
break;
114+
case EAABBFormat::U64:
115+
visitor(u64);
116+
break;
117+
case EAABBFormat::S64:
118+
visitor(s64);
119+
break;
120+
case EAABBFormat::F32:
121+
visitor(f32);
122+
break;
123+
case EAABBFormat::U32:
124+
visitor(u32);
125+
break;
126+
case EAABBFormat::S32:
127+
visitor(s32);
128+
break;
129+
case EAABBFormat::F16:
130+
visitor(f16);
131+
break;
132+
case EAABBFormat::U16: [[fallthrough]];
133+
case EAABBFormat::U16_NORM:
134+
visitor(u16);
135+
break;
136+
case EAABBFormat::S16: [[fallthrough]];
137+
case EAABBFormat::S16_NORM:
138+
visitor(s16);
139+
break;
140+
case EAABBFormat::U8: [[fallthrough]];
141+
case EAABBFormat::U8_NORM:
142+
visitor(u8);
143+
break;
144+
case EAABBFormat::S8: [[fallthrough]];
145+
case EAABBFormat::S8_NORM:
146+
visitor(s8);
147+
break;
148+
default:
149+
break;
150+
}
151+
}
152+
template<typename Visitor>
153+
inline void visit(const EAABBFormat format, Visitor&& visitor) const
154+
{
155+
const_cast<SAABBStorage*>(this)->visit(format,std::forward<Visitor>(visitor));
156+
}
157+
106158
hlsl::shapes::AABB<4,hlsl::float64_t> f64 = hlsl::shapes::AABB<4,hlsl::float64_t>::create();
107159
hlsl::shapes::AABB<4,uint64_t> u64;
108160
hlsl::shapes::AABB<4,int64_t> s64;
@@ -154,57 +206,9 @@ class IGeometryBase : public virtual core::IReferenceCounted
154206

155207
//
156208
template<typename Visitor>
157-
inline void visitAABB(Visitor& visitor)
158-
{
159-
switch (rangeFormat)
160-
{
161-
case EAABBFormat::F64:
162-
visitor(encodedDataRange.f64);
163-
break;
164-
case EAABBFormat::U64:
165-
visitor(encodedDataRange.u64);
166-
break;
167-
case EAABBFormat::S64:
168-
visitor(encodedDataRange.s64);
169-
break;
170-
case EAABBFormat::F32:
171-
visitor(encodedDataRange.f32);
172-
break;
173-
case EAABBFormat::U32:
174-
visitor(encodedDataRange.u32);
175-
break;
176-
case EAABBFormat::S32:
177-
visitor(encodedDataRange.s32);
178-
break;
179-
case EAABBFormat::F16:
180-
visitor(encodedDataRange.f16);
181-
break;
182-
case EAABBFormat::U16: [[fallthrough]];
183-
case EAABBFormat::U16_NORM:
184-
visitor(encodedDataRange.u16);
185-
break;
186-
case EAABBFormat::S16: [[fallthrough]];
187-
case EAABBFormat::S16_NORM:
188-
visitor(encodedDataRange.s16);
189-
break;
190-
case EAABBFormat::U8: [[fallthrough]];
191-
case EAABBFormat::U8_NORM:
192-
visitor(encodedDataRange.u8);
193-
break;
194-
case EAABBFormat::S8: [[fallthrough]];
195-
case EAABBFormat::S8_NORM:
196-
visitor(encodedDataRange.s8);
197-
break;
198-
default:
199-
break;
200-
}
201-
}
209+
inline void visitAABB(Visitor&& visitor) {encodedDataRange.visit(rangeFormat,std::forward<Visitor>(visitor));}
202210
template<typename Visitor>
203-
inline void visitAABB(const Visitor& visitor) const
204-
{
205-
auto tmp = [&visitor](const auto& aabb)->void{visitor(aabb);};
206-
const_cast<typename std::decay_t<decltype(*this)>*>(this)->visitAABB(tmp);
207-
}
211+
inline void visitAABB(Visitor&& visitor) const {encodedDataRange.visit(rangeFormat,std::forward<Visitor>(visitor));}
208212

209213
//
210214
inline void resetRange(const EAABBFormat newFormat)
@@ -240,7 +244,13 @@ class IGeometryBase : public virtual core::IReferenceCounted
240244
EAABBFormat rangeFormat : int(EAABBFormat::BitCount) = EAABBFormat::F64;
241245
};
242246

247+
virtual EAABBFormat getAABBFormat() const = 0;
243248
virtual const SAABBStorage& getAABB() const = 0;
249+
template<typename Visitor>
250+
inline void visitAABB(Visitor&& visitor) const
251+
{
252+
getAABB().visit(getAABBFormat(),std::forward<Visitor>(visitor));
253+
}
244254

245255
protected:
246256
virtual inline ~IGeometryBase() = default;
@@ -276,7 +286,7 @@ class IGeometry : public std::conditional_t<std::is_same_v<BufferType,ICPUBuffer
276286
explicit inline operator SBufferBinding<const BufferType>() const
277287
{
278288
if (*this)
279-
return {.offset=src.offset,.buffer=smart_refctd_ptr(src.buffer)};
289+
return {.offset=src.offset,.buffer=core::smart_refctd_ptr(src.buffer)};
280290
return {};
281291
}
282292

include/nbl/asset/IPolygonGeometry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class IPolygonGeometry : public IIndexableGeometry<BufferType>, public IPolygonG
160160
inline EPrimitiveType getPrimitiveType() const override final {return PrimitiveType;}
161161

162162
//
163+
inline IGeometryBase::EAABBFormat getAABBFormat() const override final {return base_t::m_positionView.composed.rangeFormat;}
163164
inline const IGeometryBase::SAABBStorage& getAABB() const override final {return base_t::m_positionView.composed.encodedDataRange;}
164165

165166
//

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ struct intersect_helper<AABB<D,Scalar>>
7070
static inline type __call(NBL_CONST_REF_ARG(type) lhs, NBL_CONST_REF_ARG(type) rhs)
7171
{
7272
type retval;
73-
retval.minVx = max<type::point_t>(lhs.minVx,rhs.minVx);
74-
retval.maxVx = min<type::point_t>(lhs.maxVx,rhs.maxVx);
73+
retval.minVx = hlsl::max<type::point_t>(lhs.minVx,rhs.minVx);
74+
retval.maxVx = hlsl::min<type::point_t>(lhs.maxVx,rhs.maxVx);
7575
return retval;
7676
}
7777
};
@@ -83,8 +83,8 @@ struct union_helper<AABB<D,Scalar>>
8383
static inline type __call(NBL_CONST_REF_ARG(type) lhs, NBL_CONST_REF_ARG(type) rhs)
8484
{
8585
type retval;
86-
retval.minVx = min<type::point_t>(lhs.minVx,rhs.minVx);
87-
retval.maxVx = max<type::point_t>(lhs.maxVx,rhs.maxVx);
86+
retval.minVx = hlsl::min<type::point_t>(lhs.minVx,rhs.minVx);
87+
retval.maxVx = hlsl::max<type::point_t>(lhs.maxVx,rhs.maxVx);
8888
return retval;
8989
}
9090
};

0 commit comments

Comments
 (0)