2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2023-03-19 11:21:08 +01:00
|
|
|
|
|
|
|
|
#include "BLI_math_matrix.hh"
|
|
|
|
|
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_volume.hh"
|
2024-02-28 22:15:10 +01:00
|
|
|
#include "BKE_volume_grid.hh"
|
2023-06-30 08:49:53 +02:00
|
|
|
#include "BKE_volume_openvdb.hh"
|
2023-03-19 11:21:08 +01:00
|
|
|
|
|
|
|
|
#include "GEO_points_to_volume.hh"
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_OPENVDB
|
|
|
|
|
# include <openvdb/openvdb.h>
|
|
|
|
|
# include <openvdb/tools/LevelSetUtil.h>
|
|
|
|
|
# include <openvdb/tools/ParticlesToLevelSet.h>
|
|
|
|
|
|
|
|
|
|
namespace blender::geometry {
|
|
|
|
|
|
|
|
|
|
/* Implements the interface required by #openvdb::tools::ParticlesToLevelSet. */
|
2024-02-28 22:15:10 +01:00
|
|
|
class OpenVDBParticleList {
|
|
|
|
|
public:
|
2023-03-19 11:21:08 +01:00
|
|
|
using PosType = openvdb::Vec3R;
|
|
|
|
|
|
2024-02-28 22:15:10 +01:00
|
|
|
private:
|
|
|
|
|
Span<float3> positions_;
|
|
|
|
|
Span<float> radii_;
|
|
|
|
|
float voxel_size_inv_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
OpenVDBParticleList(const Span<float3> positions,
|
|
|
|
|
const Span<float> radii,
|
|
|
|
|
const float voxel_size)
|
|
|
|
|
: positions_(positions), radii_(radii), voxel_size_inv_(math::rcp(voxel_size))
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(voxel_size > 0.0f);
|
|
|
|
|
}
|
2023-03-19 11:21:08 +01:00
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
|
{
|
2024-02-28 22:15:10 +01:00
|
|
|
return size_t(positions_.size());
|
2023-03-19 11:21:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void getPos(size_t n, openvdb::Vec3R &xyz) const
|
|
|
|
|
{
|
2024-02-28 22:15:10 +01:00
|
|
|
float3 pos = positions_[n] * voxel_size_inv_;
|
|
|
|
|
/* Better align generated grid with source points. */
|
|
|
|
|
pos -= float3(0.5f);
|
|
|
|
|
xyz = &pos.x;
|
2023-03-19 11:21:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void getPosRad(size_t n, openvdb::Vec3R &xyz, openvdb::Real &radius) const
|
|
|
|
|
{
|
2024-02-28 22:15:10 +01:00
|
|
|
this->getPos(n, xyz);
|
|
|
|
|
radius = radii_[n] * voxel_size_inv_;
|
2023-03-19 11:21:08 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-28 22:15:10 +01:00
|
|
|
static openvdb::FloatGrid::Ptr points_to_sdf_grid_impl(const Span<float3> positions,
|
|
|
|
|
const Span<float> radii,
|
|
|
|
|
const float voxel_size)
|
2023-03-19 11:21:08 +01:00
|
|
|
{
|
|
|
|
|
/* Create a new grid that will be filled. #ParticlesToLevelSet requires
|
|
|
|
|
* the background value to be positive */
|
|
|
|
|
openvdb::FloatGrid::Ptr new_grid = openvdb::FloatGrid::create(1.0f);
|
|
|
|
|
|
|
|
|
|
/* Create a narrow-band level set grid based on the positions and radii. */
|
|
|
|
|
openvdb::tools::ParticlesToLevelSet op{*new_grid};
|
|
|
|
|
/* Don't ignore particles based on their radius. */
|
|
|
|
|
op.setRmin(0.0f);
|
2023-11-11 21:09:54 +01:00
|
|
|
op.setRmax(std::numeric_limits<float>::max());
|
2024-02-28 22:15:10 +01:00
|
|
|
OpenVDBParticleList particles{positions, radii, voxel_size};
|
2023-03-19 11:21:08 +01:00
|
|
|
op.rasterizeSpheres(particles);
|
|
|
|
|
op.finalize();
|
|
|
|
|
|
2024-02-28 22:15:10 +01:00
|
|
|
new_grid->transform().postScale(voxel_size);
|
|
|
|
|
new_grid->setGridClass(openvdb::GRID_LEVEL_SET);
|
|
|
|
|
|
2023-03-19 11:21:08 +01:00
|
|
|
return new_grid;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 22:15:10 +01:00
|
|
|
bke::VolumeGrid<float> points_to_sdf_grid(const Span<float3> positions,
|
|
|
|
|
const Span<float> radii,
|
|
|
|
|
const float voxel_size)
|
|
|
|
|
{
|
|
|
|
|
return bke::VolumeGrid<float>(points_to_sdf_grid_impl(positions, radii, voxel_size));
|
|
|
|
|
}
|
|
|
|
|
|
Volumes: refactor volume grid storage
This refactors how volume grids are stored with the following new goals in mind:
* Get a **stand-alone volume grid** data structure that can be used by geometry nodes.
Previously, the `VolumeGrid` data structure was tightly coupled with the `Volume` data block.
* Support **implicit sharing of grids and trees**. Previously, it was possible to share data
when multiple `Volume` data blocks loaded grids from the same `.vdb` files but this was
not flexible enough.
* Get a safe API for **lazy-loading and unloading** of grids without requiring explicit calls
to some "load" function all the time.
* Get a safe API for **caching grids from files** that is not coupled to the `Volume` data block.
* Get a **tiered API** for different levels of `openvdb` involvement:
* No `OpenVDB`: Since `WITH_OPENVDB` is optional, it's helpful to have parts of the API that
still work in this case. This makes it possible to write high level code for volumes that does
not require `#ifdef WITH_OPENVDB` checks everywhere. This is in `BKE_volume_grid_fwd.hh`.
* Shallow `OpenVDB`: Code using this API requires `WITH_OPENVDB` checks. However, care
is taken to not include the expensive parts of `OpenVDB` and to use forward declarations as
much as possible. This is in `BKE_volume_grid.hh` and uses `openvdb_fwd.hh`.
* "Full" `OpenVDB`: This API requires more heavy `OpenVDB` includes. Fortunately, it turned
out to be not necessary for the common API. So this is only used for task specific APIs.
At the core of the new API is the `VolumeGridData` type. It's a wrapper around an
`openvdb::Grid` and adds some features on top like implicit sharing, lazy-loading and unloading.
Then there are `GVolumeGrid` and `VolumeGrid` which are containers for a volume grid.
Semantically, each `VolumeGrid` has its own independent grid, but this is cheap due to implicit
sharing. At highest level we currently have the `Volume` data-block which contains a list of
`VolumeGrid`.
```mermaid
flowchart LR
Volume --> VolumeGrid --> VolumeGridData --> openvdb::Grid
```
The loading of `.vdb` files is abstracted away behind the volume file cache API. This API makes
it easy to load and reuse entire files and individual grids from disk. It also supports caching
simplify levels for grids on disk.
An important new concept are the "tree access tokens". Whenever some code wants to work
with an openvdb tree, it has to retrieve an access token from the corresponding `VolumeGridData`.
This access token has to be kept alive for as long as the code works with the grid data. The same
token is valid for read and write access. The purpose of these access tokens is to make it possible
to detect when some code is currently working with the openvdb tree. This allows freeing it if it's
possible to reload it later on (e.g. from disk). It's possible to free a tree that is referenced by
multiple owners, but only no one is actively working with. In some sense, this is similar to the
existing `ImageUser` concept.
The most important new files to read are `BKE_volume_grid.hh` and `BKE_volume_grid_file_cache.hh`.
Most other changes are updates to existing code to use the new API.
Pull Request: https://projects.blender.org/blender/blender/pulls/116315
2023-12-20 15:32:52 +01:00
|
|
|
bke::VolumeGridData *fog_volume_grid_add_from_points(Volume *volume,
|
|
|
|
|
const StringRefNull name,
|
|
|
|
|
const Span<float3> positions,
|
|
|
|
|
const Span<float> radii,
|
|
|
|
|
const float voxel_size,
|
|
|
|
|
const float density)
|
2023-03-19 11:21:08 +01:00
|
|
|
{
|
2024-02-28 22:15:10 +01:00
|
|
|
openvdb::FloatGrid::Ptr new_grid = points_to_sdf_grid_impl(positions, radii, voxel_size);
|
2023-03-19 11:21:08 +01:00
|
|
|
new_grid->setGridClass(openvdb::GRID_FOG_VOLUME);
|
|
|
|
|
|
|
|
|
|
/* Convert the level set to a fog volume. This also sets the background value to zero. Inside the
|
|
|
|
|
* fog there will be a density of 1. */
|
|
|
|
|
openvdb::tools::sdfToFogVolume(*new_grid);
|
|
|
|
|
|
|
|
|
|
/* Take the desired density into account. */
|
|
|
|
|
openvdb::tools::foreach (new_grid->beginValueOn(),
|
|
|
|
|
[&](const openvdb::FloatGrid::ValueOnIter &iter) {
|
|
|
|
|
iter.modifyValue([&](float &value) { value *= density; });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return BKE_volume_grid_add_vdb(*volume, name, std::move(new_grid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace blender::geometry
|
|
|
|
|
#endif
|