Eager mesh bounds calculation for primitive nodes #105551

Closed
opened 2023-03-07 21:34:38 +01:00 by Hans Goudey · 15 comments
Member

The goal is to save a few milliseconds calculating the min and max position of mesh vertices. To measure the time spent doing that, put SCOPED_TIMER(__func__); in BKE_mesh_minmax.

Extend the changes in #105266 to apply to the sphere, cone, and circle mesh primitives. The bounds need to be calculated so that they're exactly correct based on the segment count of the circles. It's still constant time though, so it should save time in general.

The goal is to save a few milliseconds calculating the min and max position of mesh vertices. To measure the time spent doing that, put `SCOPED_TIMER(__func__);` in `BKE_mesh_minmax`. Extend the changes in #105266 to apply to the sphere, cone, and circle mesh primitives. The bounds need to be calculated so that they're exactly correct based on the segment count of the circles. It's still constant time though, so it should save time in general.
Hans Goudey added this to the Nodes & Physics project 2023-03-07 21:34:40 +01:00

Really good option for a first issues.
It implies:

  • Functions of geometry nodes.
  • Mesh structure.
  • Organization of generation of geometry primitives and how fastest to calculate the size of primitives in mathematical approximation.
Really good option for a first issues. It implies: - Functions of geometry nodes. - Mesh structure. - Organization of generation of geometry primitives and how fastest to calculate the size of primitives in mathematical approximation.

Hii I am interested to to solve this issue can you please assign it for

Hii I am interested to to solve this issue can you please assign it for

You can just get started with this @Sanjana-S-Rao :)

You can just get started with this @Sanjana-S-Rao :)

Hey @HooglyBoogly, could you please elaborate on the following:

The bounds need to be calculated so that they're exactly correct based on the segment count of the circles.

Hey @HooglyBoogly, could you please elaborate on the following: > The bounds need to be calculated so that they're exactly correct based on the segment count of the circles.

@Piyush-Aniruddha-Udhao You must calculate the bounding box not for a perfect ball with a given radius, but for a polygonal approximation of that shape.

But it seems that for now this task is busy

@Piyush-Aniruddha-Udhao You must calculate the bounding box not for a perfect ball with a given radius, but for a polygonal approximation of that shape. But it seems that for now this task is busy

In the case when the number of vertices of the circle is a multiple of four, the circle can be considered as a perfect, since the coordinates of the vertices on the main axes will correspond to the radius of the circle; as a result, the code suggested by @HooglyBoogly will do

In the case when the number of vertices of the circle is a multiple of four, the circle can be considered as a perfect, since the coordinates of the vertices on the main axes will correspond to the radius of the circle; as a result, the code suggested by @HooglyBoogly will do

@arevkov That's an interesting note! But also the task is to handle all other cases. And do this for all primitives)

@arevkov That's an interesting note! But also the task is to handle all other cases. And do this for all primitives)
Contributor

To handle all cases for the circles, you could go with sth like this, I think:

  /* Set bounds. */
  const float x_min = std::cos(std::round(0.5f * verts_num) * angle_delta) * radius;
  const float x_max = radius;
  const float y_min = std::sin(std::round(0.75f * verts_num) * angle_delta) * radius;
  const float y_max = std::sin(std::round(0.25f * verts_num) * angle_delta) * radius;

  const float3 bounds_min(x_min, y_min, 0.0f);
  const float3 bounds_max(x_max, y_max, 0.0f);

  mesh->bounds_set_eager({bounds_min, bounds_max});

Since x = \cos \alpha \cdot r and 0 \leq \alpha < 2\pi, x is minimal for \alpha = \pi. You can then solve for i in \alpha = i \cdot \Delta \alpha = i \frac{2 \pi}{n} = \pi. This gives you i = 0.5n. Rounding i then yields the index of the vertex with minimal x.

Getting the bounds for the cone works similarly as it's basically only two circles above each other. Note that you will not notice any performance improvements because the cones are translated after they are created and this will invalidate the bounds cache immediately.

The spheres seem to be more intricate but at least for the icosphere, for a sufficiently high number of subdivisions you will get all axis-aligned points and the bounds will simply be -r and r.

To handle all cases for the circles, you could go with sth like this, I think: ```Cpp /* Set bounds. */ const float x_min = std::cos(std::round(0.5f * verts_num) * angle_delta) * radius; const float x_max = radius; const float y_min = std::sin(std::round(0.75f * verts_num) * angle_delta) * radius; const float y_max = std::sin(std::round(0.25f * verts_num) * angle_delta) * radius; const float3 bounds_min(x_min, y_min, 0.0f); const float3 bounds_max(x_max, y_max, 0.0f); mesh->bounds_set_eager({bounds_min, bounds_max}); ``` Since $x = \cos \alpha \cdot r$ and $0 \leq \alpha < 2\pi$, $x$ is minimal for $\alpha = \pi$. You can then solve for $i$ in $\alpha = i \cdot \Delta \alpha = i \frac{2 \pi}{n} = \pi$. This gives you $i = 0.5n$. Rounding $i$ then yields the index of the vertex with minimal $x$. Getting the bounds for the cone works similarly as it's basically only two circles above each other. Note that you will not notice any performance improvements because the cones are translated after they are created and this will invalidate the bounds cache immediately. The spheres seem to be more intricate but at least for the icosphere, for a sufficiently high number of subdivisions you will get all axis-aligned points and the bounds will simply be $-r$ and $r$.

I propose this solution based on already calculated vertices for a circle:

  const float max_x = positions[0].x;
  const float max_y = positions[(verts_num + 2) >> 2].y;
  const float min_x = positions[verts_num >> 1].x;
  const float min_y = -max_y;

  const float3 bounds_min(min_x, min_y, 0.0f);
  const float3 bounds_max(max_x, max_y, 0.0f);

  mesh->bounds_set_eager({bounds_min, bounds_max});
I propose this solution based on already calculated vertices for a circle: ``` const float max_x = positions[0].x; const float max_y = positions[(verts_num + 2) >> 2].y; const float min_x = positions[verts_num >> 1].x; const float min_y = -max_y; const float3 bounds_min(min_x, min_y, 0.0f); const float3 bounds_max(max_x, max_y, 0.0f); mesh->bounds_set_eager({bounds_min, bounds_max}); ```
Contributor

@arevkov Do you want to go ahead and extend the changes to the cone and spheres as well? I do have solutions for those but perhaps it is better to just do all of them in one PR?

I'll leave my code snippets here for you. Feel free to use and improve them.

Cone:

static void calculate_cone_bounds(const ConeConfig &config, Mesh *mesh)
{
  const float radius = std::max(config.radius_bottom, config.radius_top);
  const float angle_delta = 2.0f * (M_PI / float(config.circle_segments));

  float x_min = 0.0f;
  float x_max = 0.0f;
  float y_min = 0.0f;
  float y_max = 0.0f;

  if (radius > 0.0f) {
    x_min = std::cos(std::round(0.5f * config.circle_segments) * angle_delta) * radius;
    x_max = radius;
    y_min = std::sin(std::round(0.75f * config.circle_segments) * angle_delta) * radius;
    y_max = std::sin(std::round(0.25f * config.circle_segments) * angle_delta) * radius;
  }

  const float3 bounds_min(x_min, y_min, -config.height);
  const float3 bounds_max(x_max, y_max, config.height);

  mesh->bounds_set_eager({bounds_min, bounds_max});
}

Icosphere:

  /* After three subdivisions the icosphere contains all axis-aligned points
   * which are spaced exactly at distance `radius` from the center. */
  if (subdivisions >= 3) {
    mesh->bounds_set_eager({float3(-radius), float3(radius)});
  }

UV Sphere:

  const float delta_theta = M_PI / rings;
  const float delta_phi = (2.0f * M_PI) / segments;
  const float sin_max = std::sin(std::round(0.5f * rings) * delta_theta);

  const float x_min = sin_max * std::cos(std::round(0.5f * segments) * delta_phi) * radius;
  const float x_max = sin_max * radius;
  const float y_min = sin_max * std::sin(std::round(0.25f * segments) * delta_phi) * radius;
  const float y_max = sin_max * std::sin(std::round(0.75f * segments) * delta_phi) * radius;

  const float3 bounds_min(x_min, y_min, -radius);
  const float3 bounds_max(x_max, y_max, radius);

  mesh->bounds_set_eager({bounds_min, bounds_max});
@arevkov Do you want to go ahead and extend the changes to the cone and spheres as well? I do have solutions for those but perhaps it is better to just do all of them in one PR? I'll leave my code snippets here for you. Feel free to use and improve them. Cone: ```Cpp static void calculate_cone_bounds(const ConeConfig &config, Mesh *mesh) { const float radius = std::max(config.radius_bottom, config.radius_top); const float angle_delta = 2.0f * (M_PI / float(config.circle_segments)); float x_min = 0.0f; float x_max = 0.0f; float y_min = 0.0f; float y_max = 0.0f; if (radius > 0.0f) { x_min = std::cos(std::round(0.5f * config.circle_segments) * angle_delta) * radius; x_max = radius; y_min = std::sin(std::round(0.75f * config.circle_segments) * angle_delta) * radius; y_max = std::sin(std::round(0.25f * config.circle_segments) * angle_delta) * radius; } const float3 bounds_min(x_min, y_min, -config.height); const float3 bounds_max(x_max, y_max, config.height); mesh->bounds_set_eager({bounds_min, bounds_max}); } ``` Icosphere: ```Cpp /* After three subdivisions the icosphere contains all axis-aligned points * which are spaced exactly at distance `radius` from the center. */ if (subdivisions >= 3) { mesh->bounds_set_eager({float3(-radius), float3(radius)}); } ``` UV Sphere: ```Cpp const float delta_theta = M_PI / rings; const float delta_phi = (2.0f * M_PI) / segments; const float sin_max = std::sin(std::round(0.5f * rings) * delta_theta); const float x_min = sin_max * std::cos(std::round(0.5f * segments) * delta_phi) * radius; const float x_max = sin_max * radius; const float y_min = sin_max * std::sin(std::round(0.25f * segments) * delta_phi) * radius; const float y_max = sin_max * std::sin(std::round(0.75f * segments) * delta_phi) * radius; const float3 bounds_min(x_min, y_min, -radius); const float3 bounds_max(x_max, y_max, radius); mesh->bounds_set_eager({bounds_min, bounds_max}); ```
Author
Member

@sixthat You might as well create a separate PR with your additions, there's room for two here :)

@sixthat You might as well create a separate PR with your additions, there's room for two here :)

@sixthat I opened a PR without consulting here. That's my fault. Your solution is universal. It makes sense if I close the PR and you offer your solution.
In my solution, I wanted to avoid using trigonometric formulas in order to reduce execution time, but perhaps this is not as critical as I thought.

@sixthat I opened a PR without consulting here. That's my fault. Your solution is universal. It makes sense if I close the PR and you offer your solution. In my solution, I wanted to avoid using trigonometric formulas in order to reduce execution time, but perhaps this is not as critical as I thought.

As long as it's not arbitrarily sized array traversal, that's fine.

As long as it's not arbitrarily sized array traversal, that's fine.
Contributor

@arevkov Please don't be discouraged and feel free to update your PR with my input! I believe that collaboration is key in these kind of projects and to me it doesn't really matter who pushes the code in the end. Having your first PR accepted and merged will also be a motivating experience. I am also working on getting my first one accepted. Quite thrilling tbh 🤩

@arevkov Please don't be discouraged and feel free to update your PR with my input! I believe that collaboration is key in these kind of projects and to me it doesn't really matter who pushes the code in the end. Having your first PR accepted and merged will also be a motivating experience. I am also working on getting my first one accepted. Quite thrilling tbh 🤩

In fact, you can work on your fork at the same time
But it's better to cooperate in PM in chat

In fact, you can work on your fork at the same time But it's better to cooperate in PM in chat
Blender Bot added the
Status
Archived
label 2023-03-22 12:46:39 +01:00
Hans Goudey added
Status
Resolved
and removed
Status
Archived
labels 2023-03-22 12:46:50 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No Assignees
7 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#105551
No description provided.