[Bf-blender-cvs] [0e2a1ef1322] master: BKE: Add a utility to transform a shallow copy of a volume grid

Hans Goudey noreply at git.blender.org
Thu Apr 8 20:31:00 CEST 2021


Commit: 0e2a1ef13223925a874d6dbb139e0ce9f4eecab5
Author: Hans Goudey
Date:   Thu Apr 8 13:28:35 2021 -0500
Branches: master
https://developer.blender.org/rB0e2a1ef13223925a874d6dbb139e0ce9f4eecab5

BKE: Add a utility to transform a shallow copy of a volume grid

Often you need to apply a transformation to a grid without changing the
original, and it's necessary to avoid a deep copy of the actual data.
OpenVDB has a function to do this, this commit simply adds a wrapper
to transform and use that function with blender's `float4x4` data type.

Split from D10906

===================================================================

M	source/blender/blenkernel/BKE_volume.h
M	source/blender/blenkernel/intern/volume.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_volume.h b/source/blender/blenkernel/BKE_volume.h
index 2b17cf26e0e..5d32dd3e91e 100644
--- a/source/blender/blenkernel/BKE_volume.h
+++ b/source/blender/blenkernel/BKE_volume.h
@@ -160,9 +160,15 @@ bool BKE_volume_save(const struct Volume *volume,
  * Access to OpenVDB grid for C++. These will automatically load grids from
  * file or copy shared grids to make them writeable. */
 
-#if defined(__cplusplus) && defined(WITH_OPENVDB)
-#  include <openvdb/openvdb.h>
-#  include <openvdb/points/PointDataGrid.h>
+#ifdef __cplusplus
+#  include "BLI_float4x4.hh"
+
+#  ifdef WITH_OPENVDB
+#    include <openvdb/openvdb.h>
+#    include <openvdb/points/PointDataGrid.h>
+
+openvdb::GridBase::ConstPtr BKE_volume_grid_shallow_transform(openvdb::GridBase::ConstPtr grid,
+                                                              const blender::float4x4 &transform);
 
 openvdb::GridBase::ConstPtr BKE_volume_grid_openvdb_for_metadata(const struct VolumeGrid *grid);
 openvdb::GridBase::ConstPtr BKE_volume_grid_openvdb_for_read(const struct Volume *volume,
@@ -213,4 +219,5 @@ openvdb::GridBase::Ptr BKE_volume_grid_create_with_changed_resolution(
     const openvdb::GridBase &old_grid,
     const float resolution_factor);
 
+#  endif
 #endif
diff --git a/source/blender/blenkernel/intern/volume.cc b/source/blender/blenkernel/intern/volume.cc
index a51828453ca..a22f4c3eddb 100644
--- a/source/blender/blenkernel/intern/volume.cc
+++ b/source/blender/blenkernel/intern/volume.cc
@@ -28,7 +28,10 @@
 
 #include "BLI_compiler_compat.h"
 #include "BLI_fileops.h"
+#include "BLI_float3.hh"
+#include "BLI_float4x4.hh"
 #include "BLI_ghash.h"
+#include "BLI_index_range.hh"
 #include "BLI_map.hh"
 #include "BLI_math.h"
 #include "BLI_path_util.h"
@@ -63,6 +66,10 @@ static CLG_LogRef LOG = {"bke.volume"};
 
 #define VOLUME_FRAME_NONE INT_MAX
 
+using blender::float3;
+using blender::float4x4;
+using blender::IndexRange;
+
 #ifdef WITH_OPENVDB
 #  include <atomic>
 #  include <list>
@@ -1496,6 +1503,22 @@ float BKE_volume_simplify_factor(const Depsgraph *depsgraph)
 /* OpenVDB Grid Access */
 
 #ifdef WITH_OPENVDB
+
+/**
+ * Return a new grid pointer with only the metadata and transform changed.
+ * This is useful for instances, where there is a separate transform on top of the original
+ * grid transform that must be applied for some operations that only take a grid argument.
+ */
+openvdb::GridBase::ConstPtr BKE_volume_grid_shallow_transform(openvdb::GridBase::ConstPtr grid,
+                                                              const blender::float4x4 &transform)
+{
+  openvdb::math::Transform::Ptr grid_transform = grid->transform().copy();
+  grid_transform->postMult(openvdb::Mat4d(((float *)transform.values)));
+
+  /* Create a transformed grid. The underlying tree is shared. */
+  return grid->copyGridReplacingTransform(grid_transform);
+}
+
 openvdb::GridBase::ConstPtr BKE_volume_grid_openvdb_for_metadata(const VolumeGrid *grid)
 {
   return grid->grid();



More information about the Bf-blender-cvs mailing list