Cleanup: Use FunctionRef instead of std::function

This is clearer about the lack of need for ownership of callback
data and can be faster as well.
This commit is contained in:
Hans Goudey
2023-12-30 20:09:25 -05:00
parent d721dcd767
commit 854d7afa1e
11 changed files with 25 additions and 24 deletions

View File

@@ -4,8 +4,6 @@
#pragma once
#include <functional>
#include "BLI_math_vector.hh"
#include "BLI_rect.h"
#include "BLI_vector.hh"

View File

@@ -11,8 +11,8 @@
/* The boolean functions in Blenlib use exact arithmetic, so require GMP. */
#ifdef WITH_GMP
# include "BLI_function_ref.hh"
# include "BLI_mesh_intersect.hh"
# include <functional>
namespace blender::meshintersect {
@@ -46,7 +46,7 @@ enum class BoolOpType {
IMesh boolean_mesh(IMesh &imesh,
BoolOpType op,
int nshapes,
std::function<int(int)> shape_fn,
FunctionRef<int(int)> shape_fn,
bool use_self,
bool hole_tolerant,
IMesh *imesh_triangulated,
@@ -60,7 +60,7 @@ IMesh boolean_mesh(IMesh &imesh,
IMesh boolean_trimesh(IMesh &tm_in,
BoolOpType op,
int nshapes,
std::function<int(int)> shape_fn,
FunctionRef<int(int)> shape_fn,
bool use_self,
bool hole_tolerant,
IMeshArena *arena);

View File

@@ -16,6 +16,7 @@
# include <iosfwd>
# include "BLI_array.hh"
# include "BLI_function_ref.hh"
# include "BLI_index_range.hh"
# include "BLI_map.hh"
# include "BLI_math_mpq.hh"
@@ -403,7 +404,7 @@ IMesh trimesh_self_intersect(const IMesh &tm_in, IMeshArena *arena);
IMesh trimesh_nary_intersect(const IMesh &tm_in,
int nshapes,
std::function<int(int)> shape_fn,
FunctionRef<int(int)> shape_fn,
bool use_self,
IMeshArena *arena);

View File

@@ -2198,7 +2198,7 @@ static void propagate_windings_and_in_output_volume(PatchesInfo &pinfo,
int c_ambient,
BoolOpType op,
int nshapes,
std::function<int(int)> shape_fn)
FunctionRef<int(int)> shape_fn)
{
int dbg_level = 0;
if (dbg_level > 0) {
@@ -2514,12 +2514,12 @@ static double3 calc_point_inside_tri_db(const Face &tri)
class InsideShapeTestData {
public:
const IMesh &tm;
std::function<int(int)> shape_fn;
FunctionRef<int(int)> shape_fn;
int nshapes;
/* A per-shape vector of parity of hits of that shape. */
Array<int> hit_parity;
InsideShapeTestData(const IMesh &tm, std::function<int(int)> shape_fn, int nshapes)
InsideShapeTestData(const IMesh &tm, FunctionRef<int(int)> shape_fn, int nshapes)
: tm(tm), shape_fn(shape_fn), nshapes(nshapes)
{
}
@@ -2578,7 +2578,7 @@ static void inside_shape_callback(void *userdata,
* \param tree: Contains all the triangles of \a tm and can be used for fast ray-casting.
*/
static void test_tri_inside_shapes(const IMesh &tm,
std::function<int(int)> shape_fn,
FunctionRef<int(int)> shape_fn,
int nshapes,
int test_t_index,
BVHTree *tree,
@@ -2726,11 +2726,8 @@ static void raycast_add_flipped(Vector<Face *> &out_faces, Face &tri, IMeshArena
* when the input is not PWN, some patches can be both inside and outside
* some shapes (e.g., a plane cutting through Suzanne's open eyes).
*/
static IMesh raycast_tris_boolean(const IMesh &tm,
BoolOpType op,
int nshapes,
std::function<int(int)> shape_fn,
IMeshArena *arena)
static IMesh raycast_tris_boolean(
const IMesh &tm, BoolOpType op, int nshapes, FunctionRef<int(int)> shape_fn, IMeshArena *arena)
{
constexpr int dbg_level = 0;
if (dbg_level > 0) {
@@ -2804,7 +2801,7 @@ static IMesh raycast_tris_boolean(const IMesh &tm,
static IMesh raycast_patches_boolean(const IMesh &tm,
BoolOpType op,
int nshapes,
std::function<int(int)> shape_fn,
FunctionRef<int(int)> shape_fn,
const PatchesInfo &pinfo,
IMeshArena *arena)
{
@@ -3543,7 +3540,7 @@ static IMesh polymesh_from_trimesh_with_dissolve(const IMesh &tm_out,
IMesh boolean_trimesh(IMesh &tm_in,
BoolOpType op,
int nshapes,
std::function<int(int)> shape_fn,
FunctionRef<int(int)> shape_fn,
bool use_self,
bool hole_tolerant,
IMeshArena *arena)
@@ -3691,7 +3688,7 @@ static void dump_test_spec(IMesh &imesh)
IMesh boolean_mesh(IMesh &imesh,
BoolOpType op,
int nshapes,
std::function<int(int)> shape_fn,
FunctionRef<int(int)> shape_fn,
bool use_self,
bool hole_tolerant,
IMesh *imesh_triangulated,

View File

@@ -2935,7 +2935,7 @@ IMesh trimesh_self_intersect(const IMesh &tm_in, IMeshArena *arena)
IMesh trimesh_nary_intersect(const IMesh &tm_in,
int nshapes,
std::function<int(int)> shape_fn,
const FunctionRef<int(int)> shape_fn,
bool use_self,
IMeshArena *arena)
{

View File

@@ -8,6 +8,8 @@
* Main functions for boolean on a #BMesh (used by the tool and modifier)
*/
#include <functional>
#include "BLI_array.hh"
#include "BLI_math_mpq.hh"
#include "BLI_mesh_boolean.hh"

View File

@@ -8,6 +8,8 @@
#pragma once
#include "BLI_function_ref.hh"
#include "DRW_gpu_wrapper.hh"
#include "DRW_render.h"
#include "UI_resources.hh"
@@ -129,7 +131,7 @@ class ShaderModule {
}
ShaderPtr selectable_shader(const char *create_info_name);
ShaderPtr selectable_shader(const char *create_info_name,
std::function<void(gpu::shader::ShaderCreateInfo &info)> patch);
FunctionRef<void(gpu::shader::ShaderCreateInfo &info)> patch);
};
struct Resources : public select::SelectMap {

View File

@@ -34,7 +34,8 @@ ShaderModule::ShaderPtr ShaderModule::selectable_shader(const char *create_info_
}
ShaderModule::ShaderPtr ShaderModule::selectable_shader(
const char *create_info_name, std::function<void(gpu::shader::ShaderCreateInfo &info)> patch)
const char *create_info_name,
const FunctionRef<void(gpu::shader::ShaderCreateInfo &info)> patch)
{
gpu::shader::ShaderCreateInfo info = *reinterpret_cast<const gpu::shader::ShaderCreateInfo *>(
GPU_shader_create_info_get(create_info_name));

View File

@@ -336,7 +336,7 @@ static int get_smooth_group(const OBJMesh &mesh, const OBJExportParams &params,
void OBJWriter::write_poly_elements(FormatHandler &fh,
const IndexOffsets &offsets,
const OBJMesh &obj_mesh_data,
std::function<const char *(int)> matname_fn)
FunctionRef<const char *(int)> matname_fn)
{
const func_vert_uv_normal_indices poly_element_writer = get_poly_element_writer(
obj_mesh_data.tot_uv_vertices());

View File

@@ -99,7 +99,7 @@ class OBJWriter : NonMovable, NonCopyable {
void write_poly_elements(FormatHandler &fh,
const IndexOffsets &offsets,
const OBJMesh &obj_mesh_data,
std::function<const char *(int)> matname_fn);
FunctionRef<const char *(int)> matname_fn);
/**
* Write loose edges of a mesh as "l v1 v2".
*/

View File

@@ -213,7 +213,7 @@ static void write_mesh_objects(Vector<std::unique_ptr<OBJMesh>> exportable_as_me
/* This function takes a 0-indexed slot index for the obj_mesh object and
* returns the material name that we are using in the .obj file for it. */
const auto *obj_mtlindices = mtlindices.is_empty() ? nullptr : &mtlindices[i];
std::function<const char *(int)> matname_fn = [&](int s) -> const char * {
auto matname_fn = [&](int s) -> const char * {
if (!obj_mtlindices || s < 0 || s >= obj_mtlindices->size()) {
return nullptr;
}