MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Hull.cpp
Go to the documentation of this file.
1#include "Hull.hpp"
2
5
6namespace MayaFlux::Nexus {
7
8Hull::Hull(std::shared_ptr<Buffers::VKBuffer> buf,
11 std::optional<uint32_t> collection)
12 : Expanse(ContainsFn {}, std::move(on_enter), std::move(on_exit))
13 , m_buf(std::move(buf))
14 , m_collection(collection)
15{
16 if (!m_buf) {
18 "Hull needs a buffer");
19 return;
20 }
21
23 if (!m_local) {
25 "Hull contains nothing: no geometry could be read from the buffer");
26 }
27}
28
29bool Hull::contains(const glm::vec3& p) const
30{
31 if (!m_buf || !m_local) {
32 return false;
33 }
34
35 const glm::mat4 drawn = read_placement(m_buf);
36 const float det = glm::determinant(glm::mat3(drawn));
37 if (det == 0.0F || !std::isfinite(det)) {
38 return false;
39 }
40
41 return m_local->contains(glm::vec3(glm::inverse(drawn) * glm::vec4(p, 1.0F)));
42}
43
44std::pair<glm::vec3, float> Hull::reach() const
45{
46 if (!m_buf || !m_local) {
47 return Expanse::reach();
48 }
49
50 const glm::mat4 drawn = read_placement(m_buf);
51 const glm::vec3 center = glm::vec3(drawn * glm::vec4(m_local->center(), 1.0F));
52
53 float radius = 0.0F;
54 for (int corner = 0; corner < 8; ++corner) {
55 const glm::vec3 local {
56 (corner & 1) != 0 ? m_local->max.x : m_local->min.x,
57 (corner & 2) != 0 ? m_local->max.y : m_local->min.y,
58 (corner & 4) != 0 ? m_local->max.z : m_local->min.z,
59 };
60 radius = std::max(radius, glm::length(glm::vec3(drawn * glm::vec4(local, 1.0F)) - center));
61 }
62 return { center, radius };
63}
64
66{
67 if (!m_buf) {
68 return;
69 }
70
71 if (const auto bounds = read_bounds(m_buf, m_collection)) {
73 } else {
75 "Hull::refit kept the previous bounds: no geometry could be read from the buffer");
76 }
77}
78
79} // namespace MayaFlux::Nexus
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
Expanse::CrossingFn on_enter
Definition Decoder.cpp:518
Expanse::CrossingFn on_exit
Definition Decoder.cpp:519
float radius
std::function< void(uint32_t)> CrossingFn
Definition Expanse.hpp:58
const std::optional< Kinesis::AABB3D > & bounds() const
The box, if set.
Definition Expanse.hpp:153
std::function< bool(const glm::vec3 &)> ContainsFn
Definition Expanse.hpp:57
virtual std::pair< glm::vec3, float > reach() const
Where the region is centred and how far it reaches, for perceptions.
Definition Expanse.hpp:314
A defined region of space with a containment test and crossing actions.
Definition Expanse.hpp:55
void refit()
Read the buffer's geometry again and take its bounds.
Definition Hull.cpp:65
Hull(std::shared_ptr< Buffers::VKBuffer > buf, CrossingFn on_enter={}, CrossingFn on_exit={}, std::optional< uint32_t > collection=std::nullopt)
Build from a buffer.
Definition Hull.cpp:8
std::optional< uint32_t > m_collection
Definition Hull.hpp:62
std::optional< Kinesis::AABB3D > m_local
Definition Hull.hpp:63
bool contains(const glm::vec3 &p) const override
Test whether a world position lies within the buffer's bounds as drawn.
Definition Hull.cpp:29
std::pair< glm::vec3, float > reach() const override
The centre of the bounds as drawn, and the distance to their farthest corner.
Definition Hull.cpp:44
std::shared_ptr< Buffers::VKBuffer > m_buf
Definition Hull.hpp:61
@ Init
Engine/subsystem initialization.
@ Runtime
General runtime operations (default fallback)
@ Nexus
Spatial indexing and scheduling for user-defined behaviour.
glm::mat4 read_placement(const std::shared_ptr< Buffers::VKBuffer > &buf)
Where a buffer is drawn: the geometry transform of its primary render processor.
Definition Survey.cpp:303
std::optional< Kinesis::AABB3D > read_bounds(const std::shared_ptr< Buffers::VKBuffer > &buf, const std::optional< uint32_t > &collection)
Bounds of a buffer's geometry in its own stored coordinates.
Definition Survey.cpp:256