19# include <openvdb/tools/Interpolation.h>
36 {0,
nullptr, 0,
nullptr,
nullptr},
41 const bNode *node =
b.node_or_null();
47 b.add_input(data_type,
"Grid").hide_value().structure_type(StructureType::Grid);
53 .description(
"How to interpolate the values between neighboring voxels");
55 b.add_output(data_type,
"Value").dependent_field({1});
60 switch (socket.
type) {
84 bNode &node =
params.add_node(
"GeometryNodeSampleGrid");
86 params.update_and_connect_available_socket(node,
"Grid");
91 bNode &node =
params.add_node(
"GeometryNodeSampleGrid");
92 params.update_and_connect_available_socket(node,
"Position");
98 bNode &node =
params.add_node(
"GeometryNodeSampleGrid");
100 params.update_and_connect_available_socket(node,
"Value");
113void sample_grid(
const bke::OpenvdbGridType<T> &grid,
119 using GridType = bke::OpenvdbGridType<T>;
120 using GridValueT =
typename GridType::ValueType;
121 using AccessorT =
typename GridType::ConstUnsafeAccessor;
122 using TraitsT =
typename bke::VolumeGridTraits<T>;
123 AccessorT accessor = grid.getConstUnsafeAccessor();
125 auto sample_data = [&](
auto sampler_type_tag) {
126 using Sampler =
typename decltype(sampler_type_tag)::type;
129 const openvdb::Vec3R world_pos(
pos.x,
pos.y,
pos.z);
130 const openvdb::Vec3R index_pos = grid.transform().worldToIndex(world_pos);
132 Sampler::sample(accessor, index_pos, value);
133 dst[
i] = TraitsT::to_blender(value);
139 if constexpr (std::is_same_v<T, bool>) {
142 switch (real_interpolation) {
144 sample_data(TypeTag<openvdb::tools::BoxSampler>{});
148 sample_data(TypeTag<openvdb::tools::QuadraticSampler>{});
152 sample_data(TypeTag<openvdb::tools::PointSampler>{});
181class SampleGridFunction :
public mf::MultiFunction {
182 bke::GVolumeGrid grid_;
184 mf::Signature signature_;
187 bke::VolumeTreeAccessToken tree_token_;
188 const openvdb::GridBase *grid_base_ =
nullptr;
191 SampleGridFunction(bke::GVolumeGrid grid, InterpolationMode interpolation)
192 : grid_(std::move(grid)), interpolation_(interpolation)
196 const std::optional<eNodeSocketDatatype> data_type = bke::grid_type_to_socket_type(
198 const CPPType *cpp_type = bke::socket_type_to_geo_nodes_base_cpp_type(*data_type);
199 mf::SignatureBuilder builder{
"Sample Grid", signature_};
200 builder.single_input<
float3>(
"Position");
201 builder.single_output(
"Value", *cpp_type);
202 this->set_signature(&signature_);
204 grid_base_ = &grid_->grid(tree_token_);
205 grid_type_ = grid_->grid_type();
208 void call(
const IndexMask &
mask, mf::Params
params, mf::Context )
const override
210 const VArraySpan<float3> positions =
params.readonly_single_input<
float3>(0,
"Position");
211 GMutableSpan dst =
params.uninitialized_single_output(1,
"Value");
213 bke::VolumeTreeAccessToken tree_token;
215 using T =
decltype(dummy);
216 sample_grid<T>(
static_cast<const bke::OpenvdbGridType<T> &
>(*grid_base_),
230 bke::GVolumeGrid grid =
params.extract_input<bke::GVolumeGrid>(
"Grid");
232 params.set_default_remaining_outputs();
239 std::string error_message;
242 std::make_shared<SampleGridFunction>(std::move(grid), interpolation),
248 params.set_default_remaining_outputs();
253 params.set_output(
"Value", std::move(output_value));
269 "Node socket data type",
282 ntype.
ui_description =
"Retrieve values from the specified volume grid";
#define NODE_CLASS_GEOMETRY
#define GEO_NODE_SAMPLE_GRID
@ VOLUME_GRID_VECTOR_FLOAT
@ NODE_DEFAULT_INPUT_POSITION_FIELD
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
VecBase< float, 3 > float3
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void convert_to_static_type(const CPPType &cpp_type, const Func &func)
void node_register_type(bNodeType &ntype)
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static const EnumPropertyItem interpolation_mode_items[]
static void node_register()
static void node_geo_exec(GeoNodeExecParams params)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void node_rna(StructRNA *srna)
static void node_init(bNodeTree *, bNode *node)
static void node_gather_link_search_ops(GatherLinkSearchOpParams ¶ms)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
bool execute_multi_function_on_value_variant(const MultiFunction &fn, const std::shared_ptr< MultiFunction > &owned_fn, const Span< SocketValueVariant * > input_values, const Span< SocketValueVariant * > output_values, GeoNodesUserData *user_data, std::string &r_error_message)
const EnumPropertyItem * grid_socket_type_items_filter_fn(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams ¶ms)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
std::string ui_description
void(* initfunc)(bNodeTree *ntree, bNode *node)
NodeGeometryExecFunction geometry_node_execute
const char * enum_name_legacy
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
NodeDeclareFunction declare
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)