[Bf-blender-cvs] [fdf4c4f80cd] soc-2018-cycles-volumes: Fixed incorrect preview of imported OpenVDB files.

Geraldine Chua noreply at git.blender.org
Thu Aug 2 18:10:07 CEST 2018


Commit: fdf4c4f80cd6822849c2cbbff6b042ec6becbea9
Author: Geraldine Chua
Date:   Thu Aug 2 23:15:49 2018 +0800
Branches: soc-2018-cycles-volumes
https://developer.blender.org/rBfdf4c4f80cd6822849c2cbbff6b042ec6becbea9

Fixed incorrect preview of imported OpenVDB files.

Error was caused by not taking into account minumum bound of the volume.
Also changed dense array to VDB grid converter in Cycles to use the
exporter in intern/openvdb.

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

M	intern/cycles/render/openvdb.cpp
M	intern/cycles/render/openvdb.h
M	intern/openvdb/intern/openvdb_dense_convert.cc
M	intern/openvdb/intern/openvdb_dense_convert.h
M	intern/openvdb/openvdb_capi.cc

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

diff --git a/intern/cycles/render/openvdb.cpp b/intern/cycles/render/openvdb.cpp
index d863ae42233..cda4b1563e9 100644
--- a/intern/cycles/render/openvdb.cpp
+++ b/intern/cycles/render/openvdb.cpp
@@ -393,34 +393,58 @@ void openvdb_load_image(const string& filepath,
 static device_memory *openvdb_load_device_intern_vec(Device *device,
                                                      const float *data,
                                                      const int3 resolution,
+                                                     const string& grid_name,
+                                                     const float clipping,
                                                      const string& mem_name,
                                                      const InterpolationType& interpolation,
                                                      const ExtensionType& extension)
 {
 	using namespace openvdb;
 
-	Vec3SGrid::Ptr grid = Vec3SGrid::create();
-	Vec3SGrid::Accessor accessor = grid->getAccessor();
+	/* Split data into 3 separate arrays. */
+	size_t data_size = resolution.x * resolution.y * resolution.z;
 
-	tools::changeBackground(grid->tree(), math::Vec3s(0.0f, 0.0f, 0.0f));
+	float *data_x = new float[data_size];
+	float *data_y = new float[data_size];
+	float *data_z = new float[data_size];
 
-	openvdb::math::Coord xyz;
-	int &x = xyz[0], &y = xyz[1], &z = xyz[2];
+	for(int i = 0; i < data_size; ++i) {
+		data_x[i] = data[i * 4 + 0];
+		data_y[i] = data[i * 4 + 1];
+		data_z[i] = data[i * 4 + 2];
+	}
 
-	for (z = 0; z < resolution.z; ++z) {
-		for (y = 0; y < resolution.y; ++y) {
-			for (x = 0; x < resolution.x; ++x) {
-				int index = (x + resolution.x * (y + z * resolution.y)) * 4;
-				openvdb::math::Vec3s val(data[index + 0], data[index + 1], data[index + 2]);
-				accessor.setValue(xyz, val);
-			}
-		}
+	GridBase::Ptr base_grid = internal::OpenVDB_export_vector_grid(
+	                              NULL, grid_name.c_str(), data_x, data_y,
+	                              data_z, (int*)&resolution, NULL,
+	                              openvdb::VEC_INVARIANT, (grid_name == "color"),
+	                              clipping, NULL);
+
+	delete data_x;
+	delete data_y;
+	delete data_z;
+
+	Vec3SGrid::Ptr grid = gridPtrCast<Vec3SGrid>(base_grid);
+
+	/* Check memory savings. */
+	const int dense_mem_use = resolution.x * resolution.y * resolution.z * 4 * sizeof(float);
+
+	if(grid->memUsage() < dense_mem_use) {
+		VLOG(1) << "Memory of " << grid_name << " decreased from "
+				<< string_human_readable_size(dense_mem_use) << " to "
+				<< string_human_readable_size(grid->memUsage());
+	}
+	else {
+		VLOG(1) << "Memory of " << grid_name << " increased from "
+		        << string_human_readable_size(dense_mem_use) << " to "
+		        << string_human_readable_size(grid->memUsage())
+		        << ", not using VDB grid";
+		return NULL;
 	}
 
-	Vec3SGrid::ConstAccessor c_accessor = grid->getConstAccessor();
 	device_openvdb<Vec3SGrid> *tex_img =
 			new device_openvdb<Vec3SGrid>(device, mem_name.c_str(), MEM_TEXTURE,
-	                                      grid, c_accessor, resolution);
+	                                      grid, grid->getConstAccessor(), resolution);
 
 	tex_img->interpolation = interpolation;
 	tex_img->extension = extension;
@@ -433,33 +457,36 @@ static device_memory *openvdb_load_device_intern_vec(Device *device,
 static device_memory *openvdb_load_device_intern_flt(Device *device,
                                                      const float *data,
                                                      const int3 resolution,
+                                                     const string& grid_name,
+                                                     const float clipping,
                                                      const string& mem_name,
                                                      const InterpolationType& interpolation,
                                                      const ExtensionType& extension)
 {
 	using namespace openvdb;
 
-	FloatGrid::Ptr grid = FloatGrid::create();
-	FloatGrid::Accessor accessor = grid->getAccessor();
-
-	tools::changeBackground(grid->tree(), 0.0f);
+	FloatGrid::Ptr grid = internal::OpenVDB_export_grid<FloatGrid>(
+	            NULL, grid_name.c_str(), data, (int*)&resolution, NULL, clipping, NULL);
 
-	openvdb::math::Coord xyz;
-	int &x = xyz[0], &y = xyz[1], &z = xyz[2];
+	/* Check memory savings. */
+	const int dense_mem_use = resolution.x * resolution.y * resolution.z * sizeof(float);
 
-	for (z = 0; z < resolution.z; ++z) {
-		for (y = 0; y < resolution.y; ++y) {
-			for (x = 0; x < resolution.x; ++x) {
-				int index = x + resolution.x * (y + z * resolution.y);
-				accessor.setValue(xyz, data[index]);
-			}
-		}
+	if(grid->memUsage() < dense_mem_use) {
+		VLOG(1) << "Memory of " << grid_name << " decreased from "
+				<< string_human_readable_size(dense_mem_use) << " to "
+				<< string_human_readable_size(grid->memUsage());
+	}
+	else {
+		VLOG(1) << "Memory of " << grid_name << " increased from "
+		        << string_human_readable_size(dense_mem_use) << " to "
+		        << string_human_readable_size(grid->memUsage())
+		        << ", not using VDB grid";
+		return NULL;
 	}
 
-	FloatGrid::ConstAccessor c_accessor = grid->getConstAccessor();
 	device_openvdb<FloatGrid> *tex_img =
 			new device_openvdb<FloatGrid>(device, mem_name.c_str(), MEM_TEXTURE,
-	                                      grid, c_accessor, resolution);
+	                                      grid, grid->getConstAccessor(), resolution);
 
 	tex_img->interpolation = interpolation;
 	tex_img->extension = extension;
@@ -472,6 +499,8 @@ static device_memory *openvdb_load_device_intern_flt(Device *device,
 device_memory *openvdb_load_device_intern(Device *device,
                                           const float *data,
                                           const int3 resolution,
+                                          const string& grid_name,
+                                          const float clipping,
                                           const string& mem_name,
                                           const InterpolationType& interpolation,
                                           const ExtensionType& extension,
@@ -479,11 +508,13 @@ device_memory *openvdb_load_device_intern(Device *device,
 {
 	if(is_vec) {
 		return openvdb_load_device_intern_vec(device, data, resolution,
-		                                      mem_name, interpolation, extension);
+		                                      grid_name, clipping, mem_name,
+		                                      interpolation, extension);
 	}
 	else {
 		return openvdb_load_device_intern_flt(device, data, resolution,
-		                                      mem_name, interpolation, extension);
+		                                      grid_name, clipping, mem_name,
+		                                      interpolation, extension);
 	}
 }
 
diff --git a/intern/cycles/render/openvdb.h b/intern/cycles/render/openvdb.h
index 37657800372..705ad7f4509 100644
--- a/intern/cycles/render/openvdb.h
+++ b/intern/cycles/render/openvdb.h
@@ -36,6 +36,8 @@ void openvdb_load_image(const string& filepath,
 device_memory *openvdb_load_device_intern(Device *device,
                                           const float *data,
                                           const int3 resolution,
+                                          const string& grid_name,
+                                          const float clipping,
                                           const string& mem_name,
                                           const InterpolationType& interpolation,
                                           const ExtensionType& extension,
diff --git a/intern/openvdb/intern/openvdb_dense_convert.cc b/intern/openvdb/intern/openvdb_dense_convert.cc
index bca33cf130a..71b1f4f5000 100644
--- a/intern/openvdb/intern/openvdb_dense_convert.cc
+++ b/intern/openvdb/intern/openvdb_dense_convert.cc
@@ -72,7 +72,7 @@ public:
 	}
 };
 
-openvdb::GridBase *OpenVDB_export_vector_grid(
+openvdb::GridBase::Ptr OpenVDB_export_vector_grid(
         OpenVDBWriter *writer,
         const openvdb::Name &name,
         const float *data_x, const float *data_y, const float *data_z,
@@ -86,8 +86,6 @@ openvdb::GridBase *OpenVDB_export_vector_grid(
 	using namespace openvdb;
 
 	math::CoordBBox bbox(Coord(0), Coord(res[0] - 1, res[1] - 1, res[2] - 1));
-	Mat4R mat = convertMatrix(fluid_mat);
-	math::Transform::Ptr transform = math::Transform::createLinearTransform(mat);
 
 	FloatGrid::Ptr grid[3];
 
@@ -114,7 +112,11 @@ openvdb::GridBase *OpenVDB_export_vector_grid(
 	MergeScalarGrids op(&(grid[0]->tree()), &(grid[1]->tree()), &(grid[2]->tree()));
 	tools::foreach(vecgrid->beginValueOn(), op, true, false);
 
-	vecgrid->setTransform(transform);
+	if(fluid_mat) {
+		Mat4R mat = convertMatrix(fluid_mat);
+		math::Transform::Ptr transform = math::Transform::createLinearTransform(mat);
+		vecgrid->setTransform(transform);
+	}
 
 	/* Avoid clipping against an empty grid. */
 	if (mask && !mask->tree().empty()) {
@@ -127,9 +129,11 @@ openvdb::GridBase *OpenVDB_export_vector_grid(
 	vecgrid->insertMeta("is_color", BoolMetadata(is_color));
 	vecgrid->setGridClass(GRID_STAGGERED);
 
-	writer->insert(vecgrid);
+	if(writer) {
+		writer->insert(vecgrid);
+	}
 
-	return vecgrid.get();
+	return vecgrid;
 }
 
 openvdb::Name do_name_versionning(const openvdb::Name &name)
diff --git a/intern/openvdb/intern/openvdb_dense_convert.h b/intern/openvdb/intern/openvdb_dense_convert.h
index 8541c51a620..8f5d7b271bc 100644
--- a/intern/openvdb/intern/openvdb_dense_convert.h
+++ b/intern/openvdb/intern/openvdb_dense_convert.h
@@ -70,7 +70,7 @@ openvdb::Name do_name_versionning(const openvdb::Name &name);
 openvdb::Mat4R convertMatrix(const float mat[4][4]);
 
 template <typename GridType, typename T>
-GridType *OpenVDB_export_grid(
+typename GridType::Ptr OpenVDB_export_grid(
         OpenVDBWriter *writer,
         const openvdb::Name &name,
         const T *data,
@@ -82,15 +82,17 @@ GridType *OpenVDB_export_grid(
 	using namespace openvdb;
 
 	math::CoordBBox bb

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list