259 const int max_iterations = params.
extract_input<
int>(
"Max Iterations"_ustr);
261 const float error_threshold = params.
extract_input<
float>(
"Error Threshold"_ustr);
275 const bool staggered =
true;
277 bke::VolumeTreeAccessToken tree_token;
278 const openvdb::FloatGrid &source_vdb_grid = source_grid.grid(tree_token);
279 const openvdb::math::Transform &grid_transform = source_vdb_grid.transform();
280 const float3 voxel_size = bke::VolumeGridTraits<float3>::to_blender(grid_transform.voxelSize());
281 if (!is_uniform_scale(voxel_size)) {
284 const float voxel_scale = voxel_size.x;
293 const float absolute_error_scale =
math::rcp(solution_scale);
295 openvdb::math::pcg::State solver_state;
296 solver_state.iterations = max_iterations;
297 switch (threshold_mode) {
299 solver_state.relativeError = double(error_threshold);
300 solver_state.absoluteError = 0.0;
303 solver_state.relativeError = 0.0;
304 solver_state.absoluteError = double(error_threshold * absolute_error_scale);
308 openvdb::FloatTree::Ptr solution_tree;
309 using PreconditionerType =
310 openvdb::math::pcg::JacobiPreconditioner<openvdb::tools::poisson::LaplacianMatrix>;
311 openvdb::util::NullInterrupter interrupter;
318 "Boundary Value"_ustr);
320 std::array<bke::GVolumeGrid, 1> grids;
321 bke::VolumeTreeAccessToken grid_token;
323 const openvdb::MaskTree::Ptr mask_tree = get_boundary_mask(source_vdb_grid.tree());
324 bke::volume_grid::evaluate_fields_to_grid(
325 *mask_tree, grid_transform, {boundary_value_field}, grids);
329 openvdb::FloatGrid::Ptr vdb_grid = openvdb::FloatGrid::create(boundary_value);
330 vdb_grid->setTransform(source_vdb_grid.constTransformPtr()->copy());
334 DirichletBoundaryOp boundary_op(grids[0].typed<float>().grid(grid_token));
335 solution_tree = openvdb::tools::poisson::solveWithBoundaryConditionsAndPreconditioner<
337 source_vdb_grid.tree(), boundary_op, solver_state, interrupter, staggered);
345 "Boundary Gradient"_ustr);
347 std::array<bke::GVolumeGrid, 1> grids;
348 bke::VolumeTreeAccessToken grid_token;
350 const openvdb::MaskTree::Ptr mask_tree = get_boundary_mask(source_vdb_grid.tree());
351 bke::volume_grid::evaluate_fields_to_grid(
352 *mask_tree, grid_transform, {boundary_gradient_field}, grids);
356 openvdb::Vec3SGrid::Ptr vdb_grid = openvdb::Vec3SGrid::create(
357 bke::VolumeGridTraits<float3>::to_openvdb(boundary_gradient));
358 vdb_grid->setTransform(source_vdb_grid.constTransformPtr()->copy());
362 NeumannBoundaryOp boundary_op(grids[0].typed<float3>().grid(grid_token));
363 solution_tree = openvdb::tools::poisson::solveWithBoundaryConditionsAndPreconditioner<
365 source_vdb_grid.tree(), boundary_op, solver_state, interrupter, staggered);
373 "Boundary Value"_ustr);
375 "Boundary Gradient"_ustr);
377 "Boundary Factor"_ustr);
378 const openvdb::MaskTree::Ptr mask_tree = get_boundary_mask(source_vdb_grid.tree());
380 std::array<bke::GVolumeGrid, 3> grids;
381 std::array<bke::VolumeTreeAccessToken, 3> grid_tokens;
386 bke::volume_grid::evaluate_fields_to_grid(
389 {boundary_value_field, boundary_gradient_field, boundary_factor_field},
396 openvdb::FloatGrid::Ptr vdb_value_grid = openvdb::FloatGrid::create(boundary_value);
397 openvdb::Vec3SGrid::Ptr vdb_gradient_grid = openvdb::Vec3SGrid::create(
398 bke::VolumeGridTraits<float3>::to_openvdb(boundary_gradient));
399 openvdb::FloatGrid::Ptr vdb_factor_grid = openvdb::FloatGrid::create(boundary_factor);
400 vdb_value_grid->setTransform(source_vdb_grid.constTransformPtr()->copy());
401 vdb_gradient_grid->setTransform(source_vdb_grid.constTransformPtr()->copy());
402 vdb_factor_grid->setTransform(source_vdb_grid.constTransformPtr()->copy());
408 MixedBoundaryOp boundary_op(grids[0].typed<float>().grid(grid_tokens[0]),
409 grids[1].typed<float3>().grid(grid_tokens[1]),
410 grids[2].typed<float>().grid(grid_tokens[2]));
411 solution_tree = openvdb::tools::poisson::solveWithBoundaryConditionsAndPreconditioner<
413 source_vdb_grid.tree(), boundary_op, solver_state, interrupter, staggered);
423 solution_tree = std::make_shared<openvdb::FloatTree>();
431 if (solver_state.absoluteError == 0.0) {
432 const bool all_finite = all_values_finite(*solution_tree);
434 solver_state.absoluteError = std::numeric_limits<float>::infinity();
435 solver_state.relativeError = std::numeric_limits<float>::infinity();
436 solver_state.success =
false;
440 openvdb::tools::foreach(solution_tree->beginValueOn(),
441 [&](
const typename openvdb::FloatTree::ValueOnIter &iter) {
442 iter.setValue(iter.getValue() * solution_scale);
445 openvdb::FloatGrid::Ptr solution_grid = openvdb::FloatGrid::create(std::move(solution_tree));
446 solution_grid->setTransform(grid_transform.copy());
449 params.
set_output(
"Iterations"_ustr, solver_state.iterations);
450 params.
set_output(
"Success"_ustr, solver_state.success);
451 params.
set_output(
"Absolute Error"_ustr,
float(solver_state.absoluteError * solution_scale));
452 params.
set_output(
"Relative Error"_ustr,
float(solver_state.relativeError));
454 params.
set_output(
"Debug Boundary Value"_ustr, std::move(debug_boundary_value));
457 params.
set_output(
"Debug Boundary Gradient"_ustr, std::move(debug_boundary_gradient));
460 params.
set_output(
"Debug Boundary Factor"_ustr, std::move(debug_boundary_factor));
463 if (!solver_state.success) {
466 "Poisson solver failed to converge within the iteration limit. Try increasing "
467 "the maximum iterations or tolerance.");