37 {0,
nullptr, 0,
nullptr,
nullptr},
48 b.add_input(data_type,
"Grid"_ustr).hide_value().structure_type(StructureType::Grid);
56 .description(
"How to interpolate the values between neighboring voxels");
58 const std::array<int, 1> dynamic_inputs = {
position.index()};
59 b.add_output(data_type,
"Value"_ustr)
60 .inferred_structure_type(dynamic_inputs)
61 .propagate_references(dynamic_inputs);
66 switch (socket.
type) {
91 node.custom1 = *node_type;
105 node.custom1 = *node_type;
119void sample_grid(
const bke::OpenvdbGridType<T> &grid,
125 using GridType = bke::OpenvdbGridType<T>;
126 using GridValueT =
typename GridType::ValueType;
127 using AccessorT =
typename GridType::ConstUnsafeAccessor;
128 using TraitsT =
typename bke::VolumeGridTraits<T>;
129 AccessorT accessor = grid.getConstUnsafeAccessor();
131 auto sample_data = [&]<
typename Sampler>() {
133 const float3 &pos = positions[i];
134 const openvdb::Vec3R world_pos(pos.x, pos.y, pos.z);
135 const openvdb::Vec3R index_pos = grid.transform().worldToIndex(world_pos);
137 Sampler::sample(accessor, index_pos, value);
138 dst[i] = TraitsT::to_blender(value);
144 if constexpr (std::is_same_v<T, bool>) {
147 switch (real_interpolation) {
149 sample_data.template operator()<geometry::NearestPointSampler>();
153 sample_data.template operator()<geometry::LinearSampler>();
157 sample_data.template operator()<geometry::QuadraticBSplineSampler>();
161 sample_data.template operator()<geometry::CubicBSplineSampler>();
167class SampleGridFunction :
public mf::MultiFunction {
168 bke::GVolumeGrid grid_;
170 mf::Signature signature_;
173 bke::VolumeTreeAccessToken tree_token_;
174 const openvdb::GridBase *grid_base_ =
nullptr;
177 SampleGridFunction(bke::GVolumeGrid grid, InterpolationMode interpolation)
178 : grid_(std::move(grid)), interpolation_(interpolation)
182 const std::optional<eNodeSocketDatatype> data_type = bke::grid_type_to_socket_type(
184 const CPPType *cpp_type = bke::socket_type_to_geo_nodes_base_cpp_type(*data_type);
185 mf::SignatureBuilder builder{
"Sample Grid", signature_};
186 builder.single_input<
float3>(
"Position");
187 builder.single_output(
"Value", *cpp_type);
188 this->set_signature(&signature_);
190 grid_base_ = &grid_->grid(tree_token_);
191 grid_type_ = grid_->grid_type();
194 void call(
const IndexMask &
mask, mf::Params params, mf::Context )
const override
196 const VArraySpan<float3> positions = params.readonly_single_input<
float3>(0,
"Position");
197 GMutableSpan dst = params.uninitialized_single_output(1,
"Value");
199 BKE_volume_grid_type_to_blender_value_type(grid_type_, [&]<
typename T>() {
200 if constexpr (is_same_any_v<T, bool, float, int, float3>) {
201 sample_grid<T>(
static_cast<const bke::OpenvdbGridType<T> &
>(*grid_base_),
216 bke::GVolumeGrid grid = params.
extract_input<bke::GVolumeGrid>(
"Grid"_ustr);
226 std::string error_message;
229 std::make_shared<SampleGridFunction>(std::move(grid), interpolation),
240 params.
set_output(
"Value"_ustr, std::move(output_value));
256 "Node socket data type",
269 ntype.
ui_description =
"Retrieve values from the specified volume grid";
#define NODE_CLASS_GEOMETRY
#define GEO_NODE_SAMPLE_GRID
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
const bNodeSocket & other_socket() const
eNodeSocketInOut in_out() const
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
T get_input(const UString identifier) const
GeoNodesUserData * user_data() const
void set_default_remaining_outputs()
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
bNode & add_node(UString idname)
StructureType structure_type
VecBase< float, 3 > float3
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void node_register_type(bNodeType &ntype)
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static const EnumPropertyItem interpolation_mode_items[]
static void node_register()
static void node_geo_exec(GeoNodeExecParams params)
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)
@ NODE_DEFAULT_INPUT_POSITION_FIELD
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
std::string ui_description
NodeGeometryExecFunction geometry_node_execute
void(*) initfunc(bNodeTree *ntree, bNode *node)
const char * enum_name_legacy
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
NodeDeclareFunction declare
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)