Cleanup: Various non-functional PBVH raycast changes

A set of general tidying steps prior to making some PBVHVertRef changes

* Add const where possible
* Add r_ prefix for return parameters
* Use pass-by-reference instead of pointer for some return parameters
* Use std::array's .size() operator for some iteration bounds
* Use float3 for some variables

Pull Request: https://projects.blender.org/blender/blender/pulls/127832
This commit is contained in:
Sean Kim
2024-09-19 21:27:41 +02:00
committed by Sean Kim
parent fbc8ef56ef
commit 5d40992fe8
5 changed files with 93 additions and 98 deletions

View File

@@ -324,10 +324,10 @@ bool raycast_node(Tree &pbvh,
const float3 &ray_start,
const float3 &ray_normal,
IsectRayPrecalc *isect_precalc,
float *depth,
PBVHVertRef *active_vertex,
int *active_face_grid_index,
float *face_normal);
float *r_depth,
PBVHVertRef *r_active_vertex,
int &r_active_face_grid_index,
float3 &r_face_normal);
bool bmesh_node_raycast_detail(BMeshNode &node,
const float3 &ray_start,

View File

@@ -1649,21 +1649,21 @@ void raycast(Tree &pbvh,
}
bool ray_face_intersection_quad(const float3 &ray_start,
IsectRayPrecalc *isect_precalc,
const IsectRayPrecalc *isect_precalc,
const float3 &t0,
const float3 &t1,
const float3 &t2,
const float3 &t3,
float *depth)
float *r_depth)
{
float depth_test;
if ((isect_ray_tri_watertight_v3(ray_start, isect_precalc, t0, t1, t2, &depth_test, nullptr) &&
(depth_test < *depth)) ||
(depth_test < *r_depth)) ||
(isect_ray_tri_watertight_v3(ray_start, isect_precalc, t0, t2, t3, &depth_test, nullptr) &&
(depth_test < *depth)))
(depth_test < *r_depth)))
{
*depth = depth_test;
*r_depth = depth_test;
return true;
}
@@ -1671,17 +1671,17 @@ bool ray_face_intersection_quad(const float3 &ray_start,
}
bool ray_face_intersection_tri(const float3 &ray_start,
IsectRayPrecalc *isect_precalc,
const IsectRayPrecalc *isect_precalc,
const float3 &t0,
const float3 &t1,
const float3 &t2,
float *depth)
float *r_depth)
{
float depth_test;
if (isect_ray_tri_watertight_v3(ray_start, isect_precalc, t0, t1, t2, &depth_test, nullptr) &&
(depth_test < *depth))
(depth_test < *r_depth))
{
*depth = depth_test;
*r_depth = depth_test;
return true;
}
@@ -1774,30 +1774,27 @@ static void calc_mesh_intersect_data(const Span<int> corner_verts,
const int face_index,
const int tri_index,
const std::array<const float *, 3> co,
float *depth,
const float *depth,
PBVHVertRef *r_active_vertex,
int *r_active_face_index,
float *r_face_normal)
int &r_active_face_index,
float3 &r_face_normal)
{
float nearest_vertex_co[3] = {0.0f};
if (r_face_normal) {
normal_tri_v3(r_face_normal, co[0], co[1], co[2]);
}
float3 nearest_vertex_co(0.0f);
normal_tri_v3(r_face_normal, co[0], co[1], co[2]);
if (r_active_vertex) {
const float3 location = ray_start + ray_normal * *depth;
for (int j = 0; j < 3; j++) {
for (int i = 0; i < co.size(); i++) {
/* Always assign nearest_vertex_co in the first iteration to avoid comparison against
* uninitialized values. This stores the closest vertex in the current intersecting
* triangle. */
if (j == 0 ||
len_squared_v3v3(location, co[j]) < len_squared_v3v3(location, nearest_vertex_co))
if (i == 0 ||
len_squared_v3v3(location, co[i]) < len_squared_v3v3(location, nearest_vertex_co))
{
copy_v3_v3(nearest_vertex_co, co[j]);
r_active_vertex->i = corner_verts[corner_tris[tri_index][j]];
*r_active_face_index = face_index;
nearest_vertex_co = co[i];
r_active_vertex->i = corner_verts[corner_tris[tri_index][i]];
r_active_face_index = face_index;
}
}
}
@@ -1813,10 +1810,10 @@ static bool pbvh_faces_node_raycast(const MeshNode &node,
const float3 &ray_start,
const float3 &ray_normal,
IsectRayPrecalc *isect_precalc,
float *depth,
float *r_depth,
PBVHVertRef *r_active_vertex,
int *r_active_face_index,
float *r_face_normal)
int &r_active_face_index,
float3 &r_face_normal)
{
const Span<int> face_indices = node.faces();
@@ -1833,7 +1830,7 @@ static bool pbvh_faces_node_raycast(const MeshNode &node,
const std::array<const float *, 3> co{{vert_positions[corner_verts[tri[0]]],
vert_positions[corner_verts[tri[1]]],
vert_positions[corner_verts[tri[2]]]}};
if (ray_face_intersection_tri(ray_start, isect_precalc, co[0], co[1], co[2], depth)) {
if (ray_face_intersection_tri(ray_start, isect_precalc, co[0], co[1], co[2], r_depth)) {
hit = true;
calc_mesh_intersect_data(corner_verts,
corner_tris,
@@ -1842,7 +1839,7 @@ static bool pbvh_faces_node_raycast(const MeshNode &node,
face_i,
tri_i,
co,
depth,
r_depth,
r_active_vertex,
r_active_face_index,
r_face_normal);
@@ -1864,7 +1861,7 @@ static bool pbvh_faces_node_raycast(const MeshNode &node,
{node_positions[vert_map.index_of(corner_verts[tri[0]])],
node_positions[vert_map.index_of(corner_verts[tri[1]])],
node_positions[vert_map.index_of(corner_verts[tri[2]])]}};
if (ray_face_intersection_tri(ray_start, isect_precalc, co[0], co[1], co[2], depth)) {
if (ray_face_intersection_tri(ray_start, isect_precalc, co[0], co[1], co[2], r_depth)) {
hit = true;
calc_mesh_intersect_data(corner_verts,
corner_tris,
@@ -1873,7 +1870,7 @@ static bool pbvh_faces_node_raycast(const MeshNode &node,
face_i,
tri_i,
co,
depth,
r_depth,
r_active_vertex,
r_active_face_index,
r_face_normal);
@@ -1892,38 +1889,36 @@ static void calc_grids_intersect_data(const CCGKey &key,
const short x,
const short y,
const std::array<const float *, 4> co,
float *depth,
float *r_depth,
PBVHVertRef *r_active_vertex,
int *r_active_grid_index,
float *r_face_normal)
int &r_active_grid_index,
float3 &r_face_normal)
{
float3 nearest_vertex_co;
if (r_face_normal) {
normal_quad_v3(r_face_normal, co[0], co[1], co[2], co[3]);
}
normal_quad_v3(r_face_normal, co[0], co[1], co[2], co[3]);
if (r_active_vertex) {
const float3 location = ray_start + ray_normal * *depth;
const float3 location = ray_start + ray_normal * *r_depth;
const int x_it[4] = {0, 1, 1, 0};
const int y_it[4] = {1, 1, 0, 0};
for (int j = 0; j < 4; j++) {
for (int i = 0; i < co.size(); i++) {
/* Always assign nearest_vertex_co in the first iteration to avoid comparison against
* uninitialized values. This stores the closest vertex in the current intersecting
* quad. */
if (j == 0 ||
len_squared_v3v3(location, co[j]) < len_squared_v3v3(location, nearest_vertex_co))
if (i == 0 ||
len_squared_v3v3(location, co[i]) < len_squared_v3v3(location, nearest_vertex_co))
{
copy_v3_v3(nearest_vertex_co, co[j]);
copy_v3_v3(nearest_vertex_co, co[i]);
r_active_vertex->i = key.grid_area * grid + (y + y_it[j]) * key.grid_size + (x + x_it[j]);
r_active_vertex->i = key.grid_area * grid + (y + y_it[i]) * key.grid_size + (x + x_it[i]);
}
}
}
if (r_active_grid_index) {
*r_active_grid_index = grid;
r_active_grid_index = grid;
}
}
@@ -1932,11 +1927,11 @@ static bool pbvh_grids_node_raycast(const SubdivCCG &subdiv_ccg,
const Span<float3> node_positions,
const float3 &ray_start,
const float3 &ray_normal,
IsectRayPrecalc *isect_precalc,
float *depth,
const IsectRayPrecalc *isect_precalc,
float *r_depth,
PBVHVertRef *r_active_vertex,
int *r_active_grid_index,
float *r_face_normal)
int &r_active_grid_index,
float3 &r_face_normal)
{
const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
const Span<int> grids = node.grids();
@@ -1961,7 +1956,7 @@ static bool pbvh_grids_node_raycast(const SubdivCCG &subdiv_ccg,
grid_positions[CCG_grid_xy_to_index(grid_size, x + 1, y)],
grid_positions[CCG_grid_xy_to_index(grid_size, x, y)]}};
if (ray_face_intersection_quad(
ray_start, isect_precalc, co[0], co[1], co[2], co[3], depth))
ray_start, isect_precalc, co[0], co[1], co[2], co[3], r_depth))
{
hit = true;
calc_grids_intersect_data(key,
@@ -1971,7 +1966,7 @@ static bool pbvh_grids_node_raycast(const SubdivCCG &subdiv_ccg,
x,
y,
co,
depth,
r_depth,
r_active_vertex,
r_active_grid_index,
r_face_normal);
@@ -1996,7 +1991,7 @@ static bool pbvh_grids_node_raycast(const SubdivCCG &subdiv_ccg,
grid_positions[y * grid_size + x + 1],
grid_positions[y * grid_size + x]};
if (ray_face_intersection_quad(
ray_start, isect_precalc, co[0], co[1], co[2], co[3], depth))
ray_start, isect_precalc, co[0], co[1], co[2], co[3], r_depth))
{
hit = true;
calc_grids_intersect_data(key,
@@ -2006,7 +2001,7 @@ static bool pbvh_grids_node_raycast(const SubdivCCG &subdiv_ccg,
x,
y,
co,
depth,
r_depth,
r_active_vertex,
r_active_grid_index,
r_face_normal);
@@ -2032,10 +2027,10 @@ bool raycast_node(Tree &pbvh,
const float3 &ray_start,
const float3 &ray_normal,
IsectRayPrecalc *isect_precalc,
float *depth,
PBVHVertRef *active_vertex,
int *active_face_grid_index,
float *face_normal)
float *r_depth,
PBVHVertRef *r_active_vertex,
int &r_active_face_grid_index,
float3 &r_face_normal)
{
if (node.flag_ & PBVH_FullyHidden) {
return false;
@@ -2052,10 +2047,10 @@ bool raycast_node(Tree &pbvh,
ray_start,
ray_normal,
isect_precalc,
depth,
active_vertex,
active_face_grid_index,
face_normal);
r_depth,
r_active_vertex,
r_active_face_grid_index,
r_face_normal);
case Type::Grids:
return pbvh_grids_node_raycast(*subdiv_ccg,
static_cast<GridsNode &>(node),
@@ -2063,19 +2058,19 @@ bool raycast_node(Tree &pbvh,
ray_start,
ray_normal,
isect_precalc,
depth,
active_vertex,
active_face_grid_index,
face_normal);
r_depth,
r_active_vertex,
r_active_face_grid_index,
r_face_normal);
case Type::BMesh:
return bmesh_node_raycast(static_cast<BMeshNode &>(node),
ray_start,
ray_normal,
isect_precalc,
depth,
r_depth,
use_origco,
active_vertex,
face_normal);
r_active_vertex,
r_face_normal);
}
BLI_assert_unreachable();
return false;

View File

@@ -1856,25 +1856,25 @@ bool bmesh_node_raycast(BMeshNode &node,
const float3 &ray_start,
const float3 &ray_normal,
IsectRayPrecalc *isect_precalc,
float *depth,
float *r_depth,
bool use_original,
PBVHVertRef *r_active_vertex,
float *r_face_normal)
{
bool hit = false;
float nearest_vertex_co[3] = {0.0f};
float3 nearest_vertex_co(0.0f);
use_original = use_original && !node.orig_tris_.is_empty();
if (use_original) {
for (const int i : node.orig_tris_.index_range()) {
for (const int tri_idx : node.orig_tris_.index_range()) {
float *cos[3];
cos[0] = node.orig_positions_[node.orig_tris_[i][0]];
cos[1] = node.orig_positions_[node.orig_tris_[i][1]];
cos[2] = node.orig_positions_[node.orig_tris_[i][2]];
cos[0] = node.orig_positions_[node.orig_tris_[tri_idx][0]];
cos[1] = node.orig_positions_[node.orig_tris_[tri_idx][1]];
cos[2] = node.orig_positions_[node.orig_tris_[tri_idx][2]];
if (ray_face_intersection_tri(ray_start, isect_precalc, cos[0], cos[1], cos[2], depth)) {
if (ray_face_intersection_tri(ray_start, isect_precalc, cos[0], cos[1], cos[2], r_depth)) {
hit = true;
if (r_face_normal) {
@@ -1882,14 +1882,14 @@ bool bmesh_node_raycast(BMeshNode &node,
}
if (r_active_vertex) {
float location[3] = {0.0f};
madd_v3_v3v3fl(location, ray_start, ray_normal, *depth);
for (const int j : IndexRange(3)) {
if (j == 0 ||
len_squared_v3v3(location, cos[j]) < len_squared_v3v3(location, nearest_vertex_co))
float3 location(0.0f);
madd_v3_v3v3fl(location, ray_start, ray_normal, *r_depth);
for (const int i : IndexRange(3)) {
if (i == 0 ||
len_squared_v3v3(location, cos[i]) < len_squared_v3v3(location, nearest_vertex_co))
{
copy_v3_v3(nearest_vertex_co, cos[j]);
r_active_vertex->i = intptr_t(node.orig_verts_[node.orig_tris_[i][j]]);
copy_v3_v3(nearest_vertex_co, cos[i]);
r_active_vertex->i = intptr_t(node.orig_verts_[node.orig_tris_[tri_idx][i]]);
}
}
}
@@ -1905,7 +1905,7 @@ bool bmesh_node_raycast(BMeshNode &node,
BM_face_as_array_vert_tri(f, v_tri);
if (ray_face_intersection_tri(
ray_start, isect_precalc, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co, depth))
ray_start, isect_precalc, v_tri[0]->co, v_tri[1]->co, v_tri[2]->co, r_depth))
{
hit = true;
@@ -1914,14 +1914,14 @@ bool bmesh_node_raycast(BMeshNode &node,
}
if (r_active_vertex) {
float location[3] = {0.0f};
madd_v3_v3v3fl(location, ray_start, ray_normal, *depth);
for (const int j : IndexRange(3)) {
if (j == 0 || len_squared_v3v3(location, v_tri[j]->co) <
float3 location(0.0f);
madd_v3_v3v3fl(location, ray_start, ray_normal, *r_depth);
for (const int i : IndexRange(3)) {
if (i == 0 || len_squared_v3v3(location, v_tri[i]->co) <
len_squared_v3v3(location, nearest_vertex_co))
{
copy_v3_v3(nearest_vertex_co, v_tri[j]->co);
r_active_vertex->i = intptr_t(v_tri[j]);
copy_v3_v3(nearest_vertex_co, v_tri[i]->co);
r_active_vertex->i = intptr_t(v_tri[i]);
}
}
}

View File

@@ -15,18 +15,18 @@
namespace blender::bke::pbvh {
bool ray_face_intersection_quad(const float3 &ray_start,
IsectRayPrecalc *isect_precalc,
const IsectRayPrecalc *isect_precalc,
const float3 &t0,
const float3 &t1,
const float3 &t2,
const float3 &t3,
float *depth);
float *r_depth);
bool ray_face_intersection_tri(const float3 &ray_start,
IsectRayPrecalc *isect_precalc,
const IsectRayPrecalc *isect_precalc,
const float3 &t0,
const float3 &t1,
const float3 &t2,
float *depth);
float *r_depth);
bool ray_face_nearest_quad(const float3 &ray_start,
const float3 &ray_normal,
@@ -50,7 +50,7 @@ bool bmesh_node_raycast(blender::bke::pbvh::BMeshNode &node,
const float3 &ray_start,
const float3 &ray_normal,
IsectRayPrecalc *isect_precalc,
float *dist,
float *r_depth,
bool use_original,
PBVHVertRef *r_active_vertex,
float *r_face_normal);

View File

@@ -2634,7 +2634,7 @@ struct SculptRaycastData {
const SubdivCCG *subdiv_ccg;
PBVHVertRef active_vertex;
float *face_normal;
float3 face_normal;
int active_face_grid_index;
@@ -4447,7 +4447,7 @@ static void sculpt_raycast_cb(blender::bke::pbvh::Node &node, SculptRaycastData
&srd.isect_precalc,
&srd.depth,
&srd.active_vertex,
&srd.active_face_grid_index,
srd.active_face_grid_index,
srd.face_normal))
{
srd.hit = true;