From 73fd422701b446ef926cd8efdf02731aeef04e88 Mon Sep 17 00:00:00 2001 From: Sean Kim Date: Mon, 16 Jun 2025 23:42:05 +0200 Subject: [PATCH] Cleanup: Use C++ math types for `multires_inline.hh` * Use `float3x3` and `float3` for the resulting tangent matrix and constituent derivatives, respectively. Pull Request: https://projects.blender.org/blender/blender/pulls/140442 --- source/blender/blenkernel/BKE_multires.hh | 7 ++-- .../blenkernel/intern/multires_inline.hh | 39 +++++++++---------- .../blenkernel/intern/multires_reshape.hh | 4 +- .../intern/multires_reshape_apply_base.cc | 4 +- .../intern/multires_reshape_smooth.cc | 19 ++++----- .../intern/multires_reshape_util.cc | 16 ++++---- .../intern/subdiv_displacement_multires.cc | 10 ++--- 7 files changed, 50 insertions(+), 49 deletions(-) diff --git a/source/blender/blenkernel/BKE_multires.hh b/source/blender/blenkernel/BKE_multires.hh index a8e4c8ed8f6..b2a37d535c0 100644 --- a/source/blender/blenkernel/BKE_multires.hh +++ b/source/blender/blenkernel/BKE_multires.hh @@ -9,6 +9,7 @@ */ #include "BKE_subsurf.hh" +#include "BLI_math_matrix_types.hh" #include "BLI_utildefines.h" struct Depsgraph; @@ -202,9 +203,9 @@ void BKE_multires_subdiv_mesh_settings_init(blender::bke::subdiv::ToMeshSettings * Corner needs to be known to properly "rotate" partial derivatives when the * matrix is being constructed for quad. For non-quad the corner is to be set to 0. */ -BLI_INLINE void BKE_multires_construct_tangent_matrix(float tangent_matrix[3][3], - const float dPdu[3], - const float dPdv[3], +BLI_INLINE void BKE_multires_construct_tangent_matrix(blender::float3x3 &tangent_matrix, + const blender::float3 &dPdu, + const blender::float3 &dPdv, int corner); /* Versioning. */ diff --git a/source/blender/blenkernel/intern/multires_inline.hh b/source/blender/blenkernel/intern/multires_inline.hh index 9c39244f7fc..abe0db434a1 100644 --- a/source/blender/blenkernel/intern/multires_inline.hh +++ b/source/blender/blenkernel/intern/multires_inline.hh @@ -9,38 +9,37 @@ #pragma once #include "BKE_multires.hh" -#include "BLI_math_vector.h" +#include "BLI_math_matrix_types.hh" +#include "BLI_math_vector.hh" +#include "BLI_math_vector_types.hh" -BLI_INLINE void BKE_multires_construct_tangent_matrix(float tangent_matrix[3][3], - const float dPdu[3], - const float dPdv[3], +BLI_INLINE void BKE_multires_construct_tangent_matrix(blender::float3x3 &tangent_matrix, + const blender::float3 &dPdu, + const blender::float3 &dPdv, const int corner) { if (corner == 0) { - copy_v3_v3(tangent_matrix[0], dPdv); - copy_v3_v3(tangent_matrix[1], dPdu); - mul_v3_fl(tangent_matrix[0], -1.0f); - mul_v3_fl(tangent_matrix[1], -1.0f); + tangent_matrix.x_axis() = dPdv * -1.0f; + tangent_matrix.y_axis() = dPdu * -1.0f; } else if (corner == 1) { - copy_v3_v3(tangent_matrix[0], dPdu); - copy_v3_v3(tangent_matrix[1], dPdv); - mul_v3_fl(tangent_matrix[1], -1.0f); + tangent_matrix.x_axis() = dPdu; + tangent_matrix.y_axis() = dPdv * -1.0f; } else if (corner == 2) { - copy_v3_v3(tangent_matrix[0], dPdv); - copy_v3_v3(tangent_matrix[1], dPdu); + tangent_matrix.x_axis() = dPdv; + tangent_matrix.y_axis() = dPdu; } else if (corner == 3) { - copy_v3_v3(tangent_matrix[0], dPdu); - copy_v3_v3(tangent_matrix[1], dPdv); - mul_v3_fl(tangent_matrix[0], -1.0f); + tangent_matrix.x_axis() = dPdu * -1.0f; + tangent_matrix.y_axis() = dPdv; } else { BLI_assert_msg(0, "Unhandled corner index"); } - cross_v3_v3v3(tangent_matrix[2], dPdu, dPdv); - normalize_v3(tangent_matrix[0]); - normalize_v3(tangent_matrix[1]); - normalize_v3(tangent_matrix[2]); + tangent_matrix.z_axis() = blender::math::cross(dPdu, dPdv); + + tangent_matrix.x_axis() = blender::math::normalize(tangent_matrix.x_axis()); + tangent_matrix.y_axis() = blender::math::normalize(tangent_matrix.y_axis()); + tangent_matrix.z_axis() = blender::math::normalize(tangent_matrix.z_axis()); } diff --git a/source/blender/blenkernel/intern/multires_reshape.hh b/source/blender/blenkernel/intern/multires_reshape.hh index 2747b187fa9..fd13738b6e7 100644 --- a/source/blender/blenkernel/intern/multires_reshape.hh +++ b/source/blender/blenkernel/intern/multires_reshape.hh @@ -229,7 +229,7 @@ void multires_reshape_tangent_matrix_for_corner(const MultiresReshapeContext *re int corner, const float dPdu[3], const float dPdv[3], - float r_tangent_matrix[3][3]); + blender::float3x3 &r_tangent_matrix); /** * Get grid elements which are to be reshaped at a given or PTEX coordinate. @@ -257,7 +257,7 @@ ReshapeConstGridElement multires_reshape_orig_grid_element_for_grid_coord( void multires_reshape_evaluate_limit_at_grid(const MultiresReshapeContext *reshape_context, const GridCoord *grid_coord, blender::float3 &r_P, - float r_tangent_matrix[3][3]); + blender::float3x3 &r_tangent_matrix); /* -------------------------------------------------------------------- * Custom data preparation. diff --git a/source/blender/blenkernel/intern/multires_reshape_apply_base.cc b/source/blender/blenkernel/intern/multires_reshape_apply_base.cc index 181cf590c2a..c27a133c88f 100644 --- a/source/blender/blenkernel/intern/multires_reshape_apply_base.cc +++ b/source/blender/blenkernel/intern/multires_reshape_apply_base.cc @@ -35,13 +35,13 @@ void multires_reshape_apply_base_update_mesh_coords(MultiresReshapeContext *resh grid_coord.v = 1.0f; blender::float3 P; - float tangent_matrix[3][3]; + blender::float3x3 tangent_matrix; multires_reshape_evaluate_limit_at_grid(reshape_context, &grid_coord, P, tangent_matrix); ReshapeConstGridElement grid_element = multires_reshape_orig_grid_element_for_grid_coord( reshape_context, &grid_coord); float D[3]; - mul_v3_m3v3(D, tangent_matrix, grid_element.displacement); + mul_v3_m3v3(D, tangent_matrix.ptr(), grid_element.displacement); add_v3_v3v3(base_positions[corner_verts[loop_index]], P, D); } diff --git a/source/blender/blenkernel/intern/multires_reshape_smooth.cc b/source/blender/blenkernel/intern/multires_reshape_smooth.cc index 37749ed2b7e..6dff0ff69f2 100644 --- a/source/blender/blenkernel/intern/multires_reshape_smooth.cc +++ b/source/blender/blenkernel/intern/multires_reshape_smooth.cc @@ -1095,14 +1095,14 @@ static void reshape_subdiv_refine_orig_P( } blender::float3 limit_P; - float tangent_matrix[3][3]; + blender::float3x3 tangent_matrix; multires_reshape_evaluate_limit_at_grid(reshape_context, grid_coord, limit_P, tangent_matrix); const ReshapeConstGridElement orig_grid_element = multires_reshape_orig_grid_element_for_grid_coord(reshape_context, grid_coord); float D[3]; - mul_v3_m3v3(D, tangent_matrix, orig_grid_element.displacement); + mul_v3_m3v3(D, tangent_matrix.ptr(), orig_grid_element.displacement); add_v3_v3v3(r_P, limit_P, D); } @@ -1141,7 +1141,7 @@ static void reshape_subdiv_evaluate_limit_at_grid( const PTexCoord *ptex_coord, const GridCoord *grid_coord, blender::float3 &limit_P, - float r_tangent_matrix[3][3]) + blender::float3x3 &r_tangent_matrix) { const MultiresReshapeContext *reshape_context = reshape_smooth_context->reshape_context; @@ -1295,11 +1295,12 @@ static void evaluate_base_surface_grids(const MultiresReshapeSmoothContext *resh foreach_toplevel_grid_coord( reshape_smooth_context, [&](const PTexCoord *ptex_coord, const GridCoord *grid_coord) { blender::float3 limit_P; - float tangent_matrix[3][3]; + blender::float3x3 tangent_matrix; reshape_subdiv_evaluate_limit_at_grid( reshape_smooth_context, ptex_coord, grid_coord, limit_P, tangent_matrix); - base_surface_grids_write(reshape_smooth_context, grid_coord, limit_P, tangent_matrix); + base_surface_grids_write( + reshape_smooth_context, grid_coord, limit_P, tangent_matrix.ptr()); }); } @@ -1324,13 +1325,13 @@ static void evaluate_final_original_point( /* Limit surface of the base mesh. */ blender::float3 base_mesh_limit_P; - float base_mesh_tangent_matrix[3][3]; + blender::float3x3 base_mesh_tangent_matrix; multires_reshape_evaluate_limit_at_grid( reshape_context, grid_coord, base_mesh_limit_P, base_mesh_tangent_matrix); /* Convert original displacement from tangent space to object space. */ float orig_displacement[3]; - mul_v3_m3v3(orig_displacement, base_mesh_tangent_matrix, orig_grid_element.displacement); + mul_v3_m3v3(orig_displacement, base_mesh_tangent_matrix.ptr(), orig_grid_element.displacement); /* Final point = limit surface + displacement. */ add_v3_v3v3(r_orig_final_P, base_mesh_limit_P, orig_displacement); @@ -1365,13 +1366,13 @@ static void evaluate_higher_grid_positions_with_details( /* Limit surface of smoothed (subdivided) edited sculpt level. */ blender::float3 smooth_limit_P; - float smooth_tangent_matrix[3][3]; + blender::float3x3 smooth_tangent_matrix; reshape_subdiv_evaluate_limit_at_grid( reshape_smooth_context, ptex_coord, grid_coord, smooth_limit_P, smooth_tangent_matrix); /* Add original detail to the smoothed surface. */ float smooth_delta[3]; - mul_v3_m3v3(smooth_delta, smooth_tangent_matrix, original_detail_delta_tangent); + mul_v3_m3v3(smooth_delta, smooth_tangent_matrix.ptr(), original_detail_delta_tangent); /* Grid element of the result. * diff --git a/source/blender/blenkernel/intern/multires_reshape_util.cc b/source/blender/blenkernel/intern/multires_reshape_util.cc index b52418c1962..75f782b3cd6 100644 --- a/source/blender/blenkernel/intern/multires_reshape_util.cc +++ b/source/blender/blenkernel/intern/multires_reshape_util.cc @@ -461,7 +461,7 @@ void multires_reshape_tangent_matrix_for_corner(const MultiresReshapeContext *re const int corner, const float dPdu[3], const float dPdv[3], - float r_tangent_matrix[3][3]) + blender::float3x3 &r_tangent_matrix) { /* For a quad faces we would need to flip the tangent, since they will use * use different coordinates within displacement grid compared to the ptex face. */ @@ -541,7 +541,7 @@ ReshapeConstGridElement multires_reshape_orig_grid_element_for_grid_coord( void multires_reshape_evaluate_limit_at_grid(const MultiresReshapeContext *reshape_context, const GridCoord *grid_coord, blender::float3 &r_P, - float r_tangent_matrix[3][3]) + blender::float3x3 &r_tangent_matrix) { blender::float3 dPdu; blender::float3 dPdv; @@ -739,11 +739,11 @@ static void object_grid_element_to_tangent_displacement( void * /*userdata_v*/) { blender::float3 P; - float tangent_matrix[3][3]; + blender::float3x3 tangent_matrix; multires_reshape_evaluate_limit_at_grid(reshape_context, grid_coord, P, tangent_matrix); float inv_tangent_matrix[3][3]; - invert_m3_m3(inv_tangent_matrix, tangent_matrix); + invert_m3_m3(inv_tangent_matrix, tangent_matrix.ptr()); ReshapeGridElement grid_element = multires_reshape_grid_element_for_grid_coord(reshape_context, grid_coord); @@ -780,13 +780,13 @@ static void assign_final_coords_from_mdisps(const MultiresReshapeContext *reshap void * /*userdata_v*/) { blender::float3 P; - float tangent_matrix[3][3]; + blender::float3x3 tangent_matrix; multires_reshape_evaluate_limit_at_grid(reshape_context, grid_coord, P, tangent_matrix); ReshapeGridElement grid_element = multires_reshape_grid_element_for_grid_coord(reshape_context, grid_coord); float D[3]; - mul_v3_m3v3(D, tangent_matrix, grid_element.displacement); + mul_v3_m3v3(D, tangent_matrix.ptr(), grid_element.displacement); add_v3_v3v3(grid_element.displacement, P, D); } @@ -803,14 +803,14 @@ static void assign_final_elements_from_orig_mdisps(const MultiresReshapeContext void * /*userdata_v*/) { blender::float3 P; - float tangent_matrix[3][3]; + blender::float3x3 tangent_matrix; multires_reshape_evaluate_limit_at_grid(reshape_context, grid_coord, P, tangent_matrix); const ReshapeConstGridElement orig_grid_element = multires_reshape_orig_grid_element_for_grid_coord(reshape_context, grid_coord); float D[3]; - mul_v3_m3v3(D, tangent_matrix, orig_grid_element.displacement); + mul_v3_m3v3(D, tangent_matrix.ptr(), orig_grid_element.displacement); ReshapeGridElement grid_element = multires_reshape_grid_element_for_grid_coord(reshape_context, grid_coord); diff --git a/source/blender/blenkernel/intern/subdiv_displacement_multires.cc b/source/blender/blenkernel/intern/subdiv_displacement_multires.cc index 68e55e6fc91..0def6ab4021 100644 --- a/source/blender/blenkernel/intern/subdiv_displacement_multires.cc +++ b/source/blender/blenkernel/intern/subdiv_displacement_multires.cc @@ -149,7 +149,7 @@ static void average_construct_tangent_matrix(Subdiv &subdiv, const int corner, const float u, const float v, - float r_tangent_matrix[3][3]) + float3x3 &r_tangent_matrix) { const bool is_quad = num_corners == 4; const int quad_corner = is_quad ? corner : 0; @@ -185,14 +185,14 @@ static void average_read_displacement_object(const MultiresDisplacementData &dat average_convert_grid_coord_to_ptex(num_corners, corner_index, grid_u, grid_v, u, v); /* Construct tangent matrix which corresponds to partial derivatives * calculated for the other PTEX face. */ - float tangent_matrix[3][3]; + float3x3 tangent_matrix; average_construct_tangent_matrix( *data.subdiv, num_corners, ptex_face_index, corner_index, u, v, tangent_matrix); /* Read displacement from other grid in a tangent space. */ float tangent_D[3]; average_read_displacement_tangent(data, displacement_grid, grid_u, grid_v, tangent_D); /* Convert displacement to object space. */ - mul_v3_m3v3(r_D, tangent_matrix, tangent_D); + mul_v3_m3v3(r_D, tangent_matrix.ptr(), tangent_D); } static void average_get_other_ptex_and_corner(const MultiresDisplacementData &data, @@ -348,9 +348,9 @@ static void eval_displacement(Displacement *displacement, const AverageWith average_with = read_displacement_grid( *displacement_grid, grid_size, grid_u, grid_v, tangent_D); /* Convert it to the object space. */ - float tangent_matrix[3][3]; + float3x3 tangent_matrix; BKE_multires_construct_tangent_matrix(tangent_matrix, dPdu, dPdv, corner_of_quad); - mul_v3_m3v3(r_D, tangent_matrix, tangent_D); + mul_v3_m3v3(r_D, tangent_matrix.ptr(), tangent_D); /* For the boundary points of grid average two (or all) neighbor grids. */ const int corner = displacement_get_face_corner(data, ptex_face_index, u, v); average_displacement(*displacement, average_with, ptex_face_index, corner, grid_u, grid_v, r_D);