62# include <openvdb/openvdb.h>
63# include <openvdb/points/PointDataGrid.h>
64# include <openvdb/tools/GridTransformer.h>
69#define VOLUME_FRAME_NONE INT_MAX
71using bke::GVolumeGrid;
81struct VolumeGridVector :
public std::list<GVolumeGrid> {
82 VolumeGridVector() : metadata(new openvdb::MetaMap())
87 VolumeGridVector(
const VolumeGridVector &other)
88 : std::list<GVolumeGrid>(other), error_msg(other.error_msg), metadata(other.metadata)
90 memcpy(filepath, other.filepath,
sizeof(filepath));
95 return filepath[0] !=
'\0';
100 std::list<GVolumeGrid>::clear();
112 std::string error_msg;
114 openvdb::MetaMap::Ptr metadata;
123 openvdb::initialize();
131 Volume *volume = id_cast<Volume *>(
id);
135 volume->
runtime = MEM_new<bke::VolumeRuntime>(__func__);
143 std::optional<Library *> ,
148 Volume *volume_dst = id_cast<Volume *>(id_dst);
149 const Volume *volume_src = id_cast<const Volume *>(id_src);
150 volume_dst->
runtime = MEM_new<bke::VolumeRuntime>(__func__);
156 volume_dst->
mat = MEM_dupalloc(volume_src->
mat);
159 const VolumeGridVector &grids_src = *(volume_src->
runtime->
grids);
160 volume_dst->
runtime->
grids = MEM_new<VolumeGridVector>(__func__, grids_src);
179 Volume *volume = id_cast<Volume *>(
id);
182 MEM_SAFE_DELETE(volume->
mat);
191 bke::volume_grid::file_cache::unload_unused();
198 Volume *volume = id_cast<Volume *>(
id);
199 for (
int i = 0; i < volume->
totcol; i++) {
208 Volume *volume = id_cast<Volume *>(
id);
214 function_callback(
id, &key,
reinterpret_cast<void **
>(&volume->
runtime->
grids), 0, user_data);
254 Volume *volume = id_cast<Volume *>(
id);
255 const bool is_undo = writer->
is_undo();
274 Volume *volume = id_cast<Volume *>(
id);
275 volume->
runtime = MEM_new<bke::VolumeRuntime>(__func__);
295 .id_code = Volume::id_type,
299 .struct_size =
sizeof(
Volume),
301 .name_plural =
N_(
"volumes"),
304 .asset_type_info =
nullptr,
309 .make_local =
nullptr,
313 .foreach_working_space_color =
nullptr,
314 .foreach_asset_weak_reference =
nullptr,
315 .owner_pointer_get =
nullptr,
321 .blend_read_undo_preserve =
nullptr,
323 .lib_override_apply_post =
nullptr,
330 volume->
runtime->
grids = MEM_new<VolumeGridVector>(__func__);
352 int path_frame, path_digits;
363 if (frame_duration == 0) {
367 int frame = scene_frame - frame_start + 1;
371 if (frame < 1 || frame > frame_duration) {
377 frame =
clamp_i(frame, 1, frame_duration);
381 frame = frame % frame_duration;
383 frame += frame_duration;
386 frame = frame_duration;
391 const int pingpong_duration = frame_duration * 2 - 2;
392 frame = frame % pingpong_duration;
394 frame += pingpong_duration;
397 frame = pingpong_duration;
399 if (frame > frame_duration) {
400 frame = frame_duration * 2 - frame;
407 frame += frame_offset;
413static void volume_filepath_get(
const Main *bmain,
const Volume *volume,
char r_filepath[
FILE_MAX])
418 int path_frame, path_digits;
419 if (volume->is_sequence &&
BLI_path_frame_get(r_filepath, &path_frame, &path_digits)) {
443 const std::string base_name = ref_base_name;
454 const StringRefNull postfixes[][3] = {{
"x",
"y",
"z"}, {
".x",
".y",
".z"}, {
"_x",
"_y",
"_z"}};
458 for (
int i = 0; i < 3; i++) {
459 std::string post_fixed_name = ref_base_name + postfix[i];
489 const VolumeGridVector &const_grids = *volume->
runtime->
grids;
497 return const_grids.error_msg.empty();
501 std::lock_guard
lock(const_grids.mutex);
503 return const_grids.error_msg.empty();
508 VolumeGridVector &grids =
const_cast<VolumeGridVector &
>(const_grids);
511 const char *volume_name = volume->
id.
name + 2;
513 volume_filepath_get(bmain, volume, filepath);
515 CLOG_INFO(&
LOG,
"Volume %s: load %s", volume_name, filepath);
520 CLOG_INFO(&
LOG,
"Volume %s: %s", volume_name, grids.error_msg.c_str());
524 bke::volume_grid::file_cache::GridsFromFile grids_from_file =
525 bke::volume_grid::file_cache::get_all_grids_from_file(filepath, 0);
527 if (!grids_from_file.error_message.empty()) {
528 grids.error_msg = grids_from_file.error_message;
529 CLOG_INFO(&
LOG,
"Volume %s: %s", volume_name, grids.error_msg.c_str());
533 grids.metadata = std::move(grids_from_file.file_meta_data);
534 for (GVolumeGrid &
volume_grid : grids_from_file.grids) {
539 const char *common_velocity_names[] = {
"velocity",
"vel",
"v"};
540 for (
const char *common_velocity_name : common_velocity_names) {
546 STRNCPY(grids.filepath, filepath);
548 return grids.error_msg.empty();
559 if (grids.filepath[0] !=
'\0') {
560 const char *volume_name = volume->
id.
name + 2;
574 const char *filepath)
583 openvdb::GridCPtrVec vdb_grids;
588 for (
const GVolumeGrid &grid : grids) {
590 vdb_grids.push_back(grid->grid_ptr(tree_tokens.
last()));
594 openvdb::io::File file(filepath);
595 file.write(vdb_grids, *grids.metadata);
598 catch (
const openvdb::IoError &
e) {
617 if (
const VolumeGridVector *grids = volume.
runtime->
grids) {
618 for (
const GVolumeGrid &grid : *grids) {
619 grid->count_memory(memory);
634 std::optional<Bounds<float3>>
result;
637 bke::VolumeTreeAccessToken tree_token;
653 if (grids.metadata) {
654 openvdb::StringMetadata::ConstPtr creator =
655 grids.metadata->getMetadata<openvdb::StringMetadata>(
"creator");
657 creator = grids.metadata->getMetadata<openvdb::StringMetadata>(
"Creator");
659 return (creator && creator->str().starts_with(
"Houdini"));
671 if (num_grids == 0) {
675 for (
int i = 0; i < num_grids; i++) {
695 std::list<GVolumeGrid> new_grids;
696 for (
const GVolumeGrid &old_grid : grids) {
697 GVolumeGrid simple_grid = bke::volume_grid::file_cache::get_grid_from_file(
698 grids.filepath, old_grid->name(), simplify_level);
700 new_grids.push_back(std::move(simple_grid));
702 grids.swap(new_grids);
728 for (; md; md = md->
next) {
772 Volume *volume = volume_component.release();
773 if (volume !=
nullptr) {
798 if (volume_eval ==
nullptr) {
803 const bool eval_is_owned = (volume != volume_eval);
805 object->runtime->geometry_set_eval =
new bke::GeometrySet(std::move(geometry_set));
816 if (!grids->is_loaded()) {
889 const VolumeGridVector &grids = *volume->
runtime->
grids;
890 for (
const GVolumeGrid &grid : grids) {
891 if (grid_index-- == 0) {
906 for (GVolumeGrid &grid_ptr : grids) {
907 if (grid_index-- == 0) {
908 return &grid_ptr.get_for_write();
921 if (num_grids == 0) {
932 for (
int i = 0; i < num_grids; i++) {
945 for (
int i = 0; i < num_grids; i++) {
964 volume_dst->
mat = MEM_dupalloc(volume_src->
mat);
974 return reinterpret_cast<Volume *
>(
980 template<
typename Gr
idType>
typename openvdb::GridBase::Ptr
operator()()
982 if constexpr (std::is_same_v<GridType, openvdb::points::PointDataGrid>) {
986 return GridType::create();
993bke::VolumeGridData *BKE_volume_grid_add_vdb(
Volume &volume,
995 openvdb::GridBase::Ptr vdb_grid)
997 VolumeGridVector &grids = *volume.runtime->grids;
1001 vdb_grid->setName(name);
1002 grids.emplace_back(GVolumeGrid(std::move(vdb_grid)));
1003 return &grids.back().get_for_write();
1006void BKE_volume_metadata_set(
Volume &volume, openvdb::MetaMap::Ptr
metadata)
1008 volume.runtime->grids->metadata =
metadata;
1016 for (VolumeGridVector::iterator it = grids.begin(); it != grids.end(); it++) {
1017 if (&it->get() == grid) {
1031 grids.push_back(GVolumeGrid(&grid));
1041 return std::abs(
determinant) >= 3.0 * openvdb::math::Tolerance<double>::value();
1064 if (simplify == 0.0f) {
1089std::optional<Bounds<float3>> BKE_volume_grid_bounds(openvdb::GridBase::ConstPtr grid)
1092 openvdb::CoordBBox coordbbox;
1093 if (!grid->baseTree().evalLeafBoundingBox(coordbbox)) {
1094 return std::nullopt;
1097 openvdb::BBoxd index_bbox = {
1098 openvdb::BBoxd(coordbbox.min().asVec3d(), coordbbox.max().asVec3d())};
1100 index_bbox.expand(0.5);
1102 const openvdb::BBoxd bbox = grid->transform().indexToWorld(index_bbox);
1106openvdb::GridBase::ConstPtr BKE_volume_grid_shallow_transform(openvdb::GridBase::ConstPtr grid,
1109 openvdb::math::Transform::Ptr grid_transform = grid->transform().copy();
1110 grid_transform->postMult(openvdb::Mat4d((
float *)
transform.ptr()));
1113 return grid->copyGridReplacingTransform(grid_transform);
1116float4x4 BKE_volume_transform_to_blender(
const openvdb::math::Transform &
transform)
1120 const openvdb::math::Mat4f matrix =
transform.baseMap()->getAffineMap()->getMat4();
1123 for (
int col = 0; col < 4; col++) {
1124 for (
int row = 0; row < 4; row++) {
1125 result[col][row] = matrix(col, row);
1131openvdb::math::Transform BKE_volume_transform_to_openvdb(
const float4x4 &
transform)
1133 openvdb::math::Mat4f matrix_openvdb;
1134 for (
int col = 0; col < 4; col++) {
1135 for (
int row = 0; row < 4; row++) {
1136 matrix_openvdb(col, row) =
transform[col][row];
1139 return openvdb::math::Transform(std::make_shared<openvdb::math::AffineMap>(matrix_openvdb));
1149template<
typename Gr
idType>
1150static typename GridType::Ptr create_grid_with_changed_resolution(
const GridType &old_grid,
1151 const float resolution_factor)
1155 openvdb::Mat4R xform;
1156 xform.setToScale(openvdb::Vec3d(resolution_factor));
1157 openvdb::tools::GridTransformer transformer{xform};
1159 typename GridType::Ptr new_grid = old_grid.copyWithNewTree();
1160 transformer.transformGrid<openvdb::tools::BoxSampler>(old_grid, *new_grid);
1161 new_grid->transform() = old_grid.transform();
1162 new_grid->transform().preScale(1.0f / resolution_factor);
1163 new_grid->transform().postTranslate(-new_grid->voxelSize() / 2.0f);
1167struct CreateGridWithChangedResolutionOp {
1168 const openvdb::GridBase &grid;
1169 const float resolution_factor;
1171 template<
typename Gr
idType>
typename openvdb::GridBase::Ptr
operator()()
1173 return create_grid_with_changed_resolution(
static_cast<const GridType &
>(grid),
1178openvdb::GridBase::Ptr BKE_volume_grid_create_with_changed_resolution(
1180 const openvdb::GridBase &old_grid,
1181 const float resolution_factor)
1183 CreateGridWithChangedResolutionOp op{old_grid, resolution_factor};
1184 return BKE_volume_grid_type_operation(grid_type, op);
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
General operations, lookup, etc. for blender objects.
File and directory operations.
#define BLT_I18NCONTEXT_ID_VOLUME
#define CLOG_INFO(clg_ref,...)
#define ID_BLEND_PATH(_bmain, _id)
#define ID_IS_OVERRIDE_LIBRARY(_id)
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define VOLUME_FRAME_NONE
constexpr IndexRange()=default
const T & last(const int64_t n=0) const
void append_as(ForwardValue &&...value)
void copy_utf8_truncated(char *dst, int64_t dst_size) const
b[1] b &&[1] b &&[1] b &&[1] b &&[1] b[3] b &&[1] b[3] float determinant(MatBase< C, R >) RET
std::string get_name(const VolumeGridData &grid)
VolumeGridType get_type(const VolumeGridData &grid)
bool is_loaded(const VolumeGridData &grid)
Bounds< T > merge(const Bounds< T > &a, const Bounds< T > &b)
T determinant(const MatBase< T, Size, Size > &mat)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_packedfile_blend_write(BlendWriter *writer, const PackedFile *pf)
bke::VolumeGridData * BKE_volume_grid_find_for_write(Volume *volume, StringRef name)
char * STRNCPY(char(&dst)[N], const char *src)
bke::VolumeGridData * BKE_volume_grid_get_for_write(Volume *volume, int grid_index)
void BKE_volume_batch_cache_dirty_tag(Volume *volume, int mode)
PackedFile * BKE_packedfile_duplicate(const PackedFile *pf_src)
std::optional< Bounds< float3 > > BKE_volume_min_max(const Volume *volume)
int BKE_volume_num_grids(const Volume *volume)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void BKE_volume_batch_cache_free(Volume *volume)
static void volume_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
bool BKE_volume_set_velocity_grid_by_name(Volume *volume, StringRef base_name)
bool DEG_is_active(const Depsgraph *depsgraph)
void BLO_read_pointer_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size)
void BKE_volume_count_memory(const Volume &volume, MemoryCounter &memory)
Volume * BKE_volume_add(Main *bmain, const char *name)
const bke::VolumeGridData * BKE_volume_grid_find(const Volume *volume, StringRef name)
void BKE_volume_data_update(Depsgraph *depsgraph, Scene *scene, Object *object)
static void volume_foreach_path(ID *id, BPathForeachPathData *bpath_data)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BKE_object_eval_assign_data(Object *object, ID *data, bool is_owned)
bool BKE_volume_grid_transform_valid(const float4x4 &transform)
void(*)(ID *id, const IDCacheKey *cache_key, void **cache_p, uint flags, void *user_data) IDTypeForeachCacheFunctionCallback
const bke::VolumeGridData * BKE_volume_grid_get(const Volume *volume, int grid_index)
bool BLI_path_frame(char *path, size_t path_maxncpy, int frame, int digits) ATTR_NONNULL(1)
void BKE_packedfile_free(PackedFile *pf)
int BKE_volume_simplify_level(const Depsgraph *depsgraph)
Volume * BKE_volume_new_for_eval(const Volume *volume_src)
MatBase< float, 4, 4 > float4x4
bool BLI_path_frame_get(const char *path, int *r_frame, int *r_digits_len) ATTR_NONNULL(1
void BKE_id_blend_write(BlendWriter *writer, ID *id)
static void volume_blend_read_after_liblink(BlendLibReader *, ID *id)
@ VOLUME_SEQUENCE_PING_PONG
bool BKE_volume_is_y_up(const Volume *volume)
const char * BKE_volume_grids_error_msg(const Volume *volume)
bool BKE_volume_is_loaded(const Volume *volume)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_extension_ensure(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
static void volume_evaluate_modifiers(Depsgraph *depsgraph, Scene *scene, Object *object, bke::GeometrySet &geometry_set)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static void volume_foreach_cache(ID *id, IDTypeForeachCacheFunctionCallback function_callback, void *user_data)
void BKE_object_free_derived_caches(Object *ob)
Volume * BKE_volume_copy_for_eval(const Volume *volume_src)
Main * DEG_get_bmain(const Depsgraph *graph)
BMesh const char void * data
void(*) BKE_volume_batch_cache_free_cb(Volume *volume)
void BKE_animdata_free(ID *id, bool do_id_user)
const char * BKE_volume_grids_frame_filepath(const Volume *volume)
void(*) BKE_volume_batch_cache_dirty_tag_cb(Volume *volume, int mode)
ModifierData * BKE_modifiers_get_virtual_modifierlist(const Object *ob, VirtualModifierData *data)
static void volume_foreach_id(ID *id, LibraryForeachIDData *data)
void BKE_volume_unload(Volume *volume)
static void volume_blend_write(BlendWriter *writer, ID *id, const void *id_address)
bool BKE_volume_is_points_only(const Volume *volume)
bool BKE_volume_save(const Volume *volume, const Main *bmain, ReportList *reports, const char *filepath)
void BKE_volume_grids_backup_restore(Volume *volume, VolumeGridVector *grids, const char *filepath)
bool BKE_volume_load(const Volume *volume, const Main *bmain)
float BKE_volume_simplify_factor(const Depsgraph *depsgraph)
void BKE_modifiers_clear_errors(Object *ob)
void BKE_volume_grid_add(Volume *volume, const bke::VolumeGridData &grid)
const bke::VolumeGridData * BKE_volume_grid_active_get_for_read(const Volume *volume)
void BKE_volume_init_grids(Volume *volume)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
void BKE_bpath_foreach_path_readonly_process(BPathForeachPathData *bpath_data, const char *path)
void BKE_volume_grid_remove(Volume *volume, const bke::VolumeGridData *grid)
void BKE_volume_eval_geometry(Depsgraph *depsgraph, Volume *volume)
void * BKE_id_new_nomain(short type, const char *name)
@ BKE_BPATH_FOREACH_PATH_SKIP_PACKED
@ BKE_BPATH_FOREACH_PATH_EXPAND_SEQUENCES
void BKE_bpath_sequence_filepaths_foreach(const char *abs_filepath, blender::FunctionRef< void(blender::StringRef frame_filepath)> callback)
static int volume_sequence_frame(const Depsgraph *depsgraph, const Volume *volume)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
T * DEG_get_original(T *id)
bool BKE_bpath_foreach_path_fixed_process(BPathForeachPathData *bpath_data, char *path, size_t path_maxncpy)
static Volume * take_volume_ownership_from_geometry_set(bke::GeometrySet &geometry_set)
Scene * DEG_get_input_scene(const Depsgraph *graph)
bool BKE_volume_grid_determinant_valid(double determinant)
static void volume_init_data(ID *id)
static void volume_update_simplify_level(Main *bmain, Volume *volume, const Depsgraph *depsgraph)
void BKE_packedfile_blend_read(BlendDataReader *reader, PackedFile **pf_p, StringRefNull filepath)
bool void BLI_path_frame_strip(char *path, char *r_ext, size_t ext_maxncpy) ATTR_NONNULL(1
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
static void volume_free_data(ID *id)
VecBase< float, 3 > float3
bool BKE_modifier_is_enabled(const Scene *scene, ModifierData *md, int required_mode)
MINLINE int clamp_i(int value, int min, int max)
float DEG_get_ctime(const Depsgraph *graph)
const ModifierTypeInfo * BKE_modifier_get_info(ModifierType type)
void * BKE_id_new(Main *bmain, short type, const char *name)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
static void volume_blend_read_data(BlendDataReader *reader, ID *id)
bool BKE_volume_voxel_size_valid(const float3 &voxel_size)
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
const char * absolute_base_path
void write_pointer_array(int64_t num, const void *data)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
struct ModifierData * next
void(*) modify_geometry_set(ModifierData *md, const ModifierEvalContext *ctx, bke::GeometrySet *geometry_set)
draw::VolumeBatchCache * batch_cache
VolumeSequenceMode sequence_mode
bke::VolumeRuntime * runtime
struct PackedFile * packedfile
void replace_volume(Volume *volume, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
bool has(const GeometryComponent::Type component_type) const
void remove(const GeometryComponent::Type component_type)
std::unique_ptr< bake::BakeMaterialsList > bake_materials