void QuadTree::add_object(GameObject *object) { Aabb aabb = object->get_bounding_box(); if (aabb.m_min.x < -m_radius || aabb.m_max.x > m_radius || aabb.m_min.z < -m_radius || aabb.m_max.z > m_radius) { get_root()->add_object(object); return; } const Vector3 offset(m_radius, m_radius, m_radius); Aabb shifted(aabb); shifted.m_min += offset; shifted.m_max += offset; shifted.m_min *= m_inv_radius; shifted.m_max *= m_inv_radius; const size_t min_x = static_cast<size_t>(clamp(std::floor(shifted.m_min.x), 0.0f, 255.0f)); const size_t min_y = static_cast<size_t>(clamp(std::floor(shifted.m_min.z), 0.0f, 255.0f)); const size_t max_x = static_cast<size_t>(clamp(std::ceil(shifted.m_max.x), 0.0f, 255.0f)); const size_t max_y = static_cast<size_t>(clamp(std::ceil(shifted.m_max.z), 0.0f, 255.0f)); const size_t x_xor = (min_x ^ max_x); // const size_t lx = !x_xor ? 7UL : 8UL - highest_bit(x_xor); const size_t lx = !x_xor ? 7UL : 7UL - highest_bit(x_xor); const size_t y_xor = (min_y ^ max_y); // const size_t ly = !y_xor ? 7UL : 8UL - highest_bit(y_xor); const size_t ly = !y_xor ? 7UL : 7UL - highest_bit(y_xor); const size_t level = (lx < ly) ? lx : ly; const size_t x = min_x >> (8UL - level); const size_t y = min_y >> (8UL - level); QuadTreeNode &node = m_levels[level][y * (1UL << level) + x]; DER_ASSERT( node.get_center().x - node.get_radius() <= aabb.m_min.x ); DER_ASSERT( node.get_center().x + node.get_radius() >= aabb.m_max.x ); DER_ASSERT( node.get_center().y - node.get_radius() <= aabb.m_min.z ); DER_ASSERT( node.get_center().y + node.get_radius() >= aabb.m_max.z ); m_object_node_map[object->getID()] = &node; node.add_object(object); }
uint16_t uint2048::num_bits() const{ uint16_t res = 0u; if (highest_bit(&res)) return ++res; else return res; }