16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-21 11:19:19 +02:00

lazy bounding radius computing for shape_node (avoids O(n^2) deserialization)

This commit is contained in:
milek7
2019-01-12 01:40:22 +01:00
parent 7167771ee6
commit b6e280f14d
3 changed files with 18 additions and 5 deletions

View File

@@ -259,7 +259,7 @@ basic_cell::insert( shape_node Shape ) {
// re-calculate cell radius, in case shape geometry extends outside the cell's boundaries // re-calculate cell radius, in case shape geometry extends outside the cell's boundaries
m_area.radius = std::max<float>( m_area.radius = std::max<float>(
m_area.radius, m_area.radius,
glm::length( m_area.center - Shape.data().area.center ) + Shape.data().area.radius ); glm::length( m_area.center - Shape.data().area.center ) + Shape.radius() );
auto const &shapedata { Shape.data() }; auto const &shapedata { Shape.data() };
auto &shapes = ( auto &shapes = (
@@ -1230,7 +1230,7 @@ basic_region::insert( shape_node Shape, scratch_data &Scratchpad, bool const Tra
// move the data into appropriate section(s) // move the data into appropriate section(s)
for( auto &shape : shapes ) { for( auto &shape : shapes ) {
// with the potential splitting done we can calculate each chunk's bounding radius // with the potential splitting done we can calculate each chunk's bounding radius
shape.compute_radius(); shape.invalidate_radius();
if( point_inside( shape.m_data.area.center ) ) { if( point_inside( shape.m_data.area.center ) ) {
// NOTE: nodes placed outside of region boundaries are discarded // NOTE: nodes placed outside of region boundaries are discarded
section( shape.m_data.area.center ).insert( shape ); section( shape.m_data.area.center ).insert( shape );

View File

@@ -407,8 +407,7 @@ shape_node::merge( shape_node &Shape ) {
m_data.vertices.insert( m_data.vertices.insert(
std::end( m_data.vertices ), std::end( m_data.vertices ),
std::begin( Shape.m_data.vertices ), std::end( Shape.m_data.vertices ) ); std::begin( Shape.m_data.vertices ), std::end( Shape.m_data.vertices ) );
// NOTE: we could recalculate radius with something other than brute force, but it'll do invalidate_radius();
compute_radius();
return true; return true;
} }
@@ -442,7 +441,16 @@ shape_node::compute_radius() {
m_data.area.radius = static_cast<float>( std::sqrt( squaredradius ) ); m_data.area.radius = static_cast<float>( std::sqrt( squaredradius ) );
} }
void shape_node::invalidate_radius() {
m_data.area.radius = -1.0f;
}
float shape_node::radius() {
if (m_data.area.radius == -1.0f)
compute_radius();
return m_data.area.radius;
}
// sends content of the struct to provided stream // sends content of the struct to provided stream
void void

View File

@@ -126,7 +126,10 @@ public:
create_geometry( gfx::geometrybank_handle const &Bank ); create_geometry( gfx::geometrybank_handle const &Bank );
// calculates shape's bounding radius // calculates shape's bounding radius
void void
compute_radius(); compute_radius();
// invalidates shape's bounding radius
void
invalidate_radius();
// set visibility // set visibility
void void
visible( bool State ); visible( bool State );
@@ -136,6 +139,8 @@ public:
// data access // data access
shapenode_data const & shapenode_data const &
data() const; data() const;
// get bounding radius
float radius();
private: private:
// members // members