[Bf-blender-cvs] [32d12dea8f0] cycles_oneapi: BLI: add kdtree range search method that accepts c++ lambda
Jacques Lucke
noreply at git.blender.org
Wed Jun 29 10:38:39 CEST 2022
Commit: 32d12dea8f04ac8b5c76dd551b3c4f6555e45e11
Author: Jacques Lucke
Date: Tue Jun 28 13:11:44 2022 +0200
Branches: cycles_oneapi
https://developer.blender.org/rB32d12dea8f04ac8b5c76dd551b3c4f6555e45e11
BLI: add kdtree range search method that accepts c++ lambda
This is easier to use in C++ code compared to passing a function
and user-data separately.
===================================================================
M source/blender/blenlib/BLI_kdtree.h
M source/blender/blenlib/BLI_kdtree_impl.h
===================================================================
diff --git a/source/blender/blenlib/BLI_kdtree.h b/source/blender/blenlib/BLI_kdtree.h
index 696b2206b5f..954282b099a 100644
--- a/source/blender/blenlib/BLI_kdtree.h
+++ b/source/blender/blenlib/BLI_kdtree.h
@@ -2,10 +2,6 @@
#pragma once
-#ifdef __cplusplus
-extern "C" {
-#endif
-
/** \file
* \ingroup bli
* \brief A KD-tree for nearest neighbor search.
@@ -54,7 +50,3 @@ extern "C" {
#undef KDTree
#undef KDTreeNearest
#undef KDTREE_PREFIX_ID
-
-#ifdef __cplusplus
-}
-#endif
diff --git a/source/blender/blenlib/BLI_kdtree_impl.h b/source/blender/blenlib/BLI_kdtree_impl.h
index 08fa40fd972..4187724fbda 100644
--- a/source/blender/blenlib/BLI_kdtree_impl.h
+++ b/source/blender/blenlib/BLI_kdtree_impl.h
@@ -12,6 +12,10 @@
#define _BLI_CONCAT(MACRO_ARG1, MACRO_ARG2) _BLI_CONCAT_AUX(MACRO_ARG1, MACRO_ARG2)
#define BLI_kdtree_nd_(id) _BLI_CONCAT(KDTREE_PREFIX_ID, _##id)
+#ifdef __cplusplus
+extern "C" {
+#endif
+
struct KDTree;
typedef struct KDTree KDTree;
@@ -80,6 +84,29 @@ int BLI_kdtree_nd_(range_search_with_len_squared_cb)(
const void *user_data),
const void *user_data) ATTR_NONNULL(1, 2) ATTR_WARN_UNUSED_RESULT;
+#ifdef __cplusplus
+}
+#endif
+
+#ifdef __cplusplus
+template<typename Fn>
+inline void BLI_kdtree_nd_(range_search_cb_cpp)(const KDTree *tree,
+ const float co[KD_DIMS],
+ float distance,
+ const Fn &fn)
+{
+ BLI_kdtree_nd_(range_search_cb)(
+ tree,
+ co,
+ distance,
+ [](void *user_data, const int index, const float *co, const float dist_sq) {
+ const Fn &fn = *static_cast<const Fn *>(user_data);
+ return fn(index, co, dist_sq);
+ },
+ const_cast<Fn *>(&fn));
+}
+#endif
+
#undef _BLI_CONCAT_AUX
#undef _BLI_CONCAT
#undef BLI_kdtree_nd_
More information about the Bf-blender-cvs
mailing list