OpenVDB  12.0.0
PointRasterizeFrustum.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: Apache-2.0
3 
4 /// @author Dan Bailey, Rick Hankins
5 ///
6 /// @file PointRasterizeFrustum.h
7 ///
8 /// @brief Volume rasterization of VDB Points using velocity and camera motion-blur
9 
10 #ifndef OPENVDB_POINTS_POINT_RASTERIZE_FRUSTUM_HAS_BEEN_INCLUDED
11 #define OPENVDB_POINTS_POINT_RASTERIZE_FRUSTUM_HAS_BEEN_INCLUDED
12 
13 #include <openvdb/math/Ray.h>
14 #include <openvdb/math/DDA.h>
16 #include <openvdb/util/Assert.h>
17 #include <openvdb/thread/Threading.h>
18 #include <openvdb/tools/GridTransformer.h> // for tools::resampleToMatch()
20 #include "PointCount.h"
21 #include "PointDataGrid.h"
22 
23 namespace openvdb {
25 namespace OPENVDB_VERSION_NAME {
26 namespace points {
27 
28 
29 /// @brief How to composite points into a volume.
30 enum class RasterMode
31 {
32  ACCUMULATE = 0,
33  MAXIMUM,
34  AVERAGE
35 };
36 
37 
38 /// @brief A camera class that provides an interface for camera motion blur when rasterizing
40 {
41 public:
42  explicit RasterCamera(const math::Transform& transform);
43 
44  bool isStatic() const;
45 
46  void clear();
47  void appendTransform(const math::Transform&, float weight = 1.0f);
48 
49  size_t size() const;
50 
51  void simplify();
52 
53  bool hasWeight(Index i) const;
54  float weight(Index i) const;
55 
56  const math::Transform& transform(Index i) const;
57  const math::Transform& firstTransform() const;
58  const math::Transform& lastTransform() const;
59 
60  void setShutter(float start, float end);
61  float shutterStart() const;
62  float shutterEnd() const;
63 
64 private:
65  std::deque<math::Transform> mTransforms;
66  std::deque<float> mWeights;
67  // default to 180 degree film shutter
68  float mShutterStart = -0.25f,
69  mShutterEnd = 0.25f;
70 }; // class RasterCamera
71 
72 
73 /// @brief A group of shared settings to be used in the Volume Rasterizer
74 /// @param scaleByVoxelVolume scale particle contributions by the volume of the receiving voxel
75 /// @param velocityAttribute the name of the velocity attribute
76 /// @param velocityMotionBlur bake the point velocities into the volume
77 /// @param clipToFrustum if enabled and the transform is a frustum transform, eliminate
78 /// points whose position does not lie within the frustum
79 /// @param clipBBox an optional world-space bounding box to clip the points
80 /// during rasterization
81 /// @param clipMask an optional mask, each point samples the mask using a
82 /// nearest-neighbor sampling and is only rasterized if active
83 /// @param invertMask if mask is provided, only rasterize if sample is inactive
84 /// @param framesPerSecond the global value for frames / second for computing motion blur
85 /// @param threaded if enabled, use threading to accelerate rasterization
86 /// @note rasterization can clip can using any combination of bounding box, mask and frustum
88 {
89  FrustumRasterizerSettings() = delete;
90 
91  explicit FrustumRasterizerSettings(const math::Transform& _transform)
92  : transform(new math::Transform(_transform))
93  , camera(_transform) { }
94 
97  bool scaleByVoxelVolume = false,
98  useRadius = false,
99  accurateFrustumRadius = false,
100  accurateSphereMotionBlur = false,
101  velocityMotionBlur = false,
102  threaded = true;
103  float threshold = 1e-6f,
104  radiusScale = 1.0f,
105  framesPerSecond = 24.0f;
106  Name velocityAttribute = "v",
107  radiusAttribute = "pscale";
108  int motionSamples = 2;
109 }; // struct FrustumRasterizerSettings
110 
111 
113 {
115 
116  FrustumRasterizerMask() = default;
117 
118  explicit FrustumRasterizerMask(
119  const math::Transform& transform,
120  const MaskGrid* mask = nullptr,
121  const BBoxd& bbox = BBoxd(),
122  const bool clipToFrustum = true,
123  const bool invert = false);
124 
125  operator bool() const;
126 
127  MaskTree::ConstPtr getTreePtr() const;
128 
129  bool valid(const Coord& ijk, AccessorT* acc) const;
130 
131  const CoordBBox& clipBBox() const;
132 
133 private:
134  MaskGrid::Ptr mMask;
135  CoordBBox mClipBBox;
136  bool mInvert = false;
137 }; // struct FrustumRasterizerMask
138 
139 
140 namespace point_rasterize_internal {
141 
142 template <typename PointDataGridT>
144 
145 } // namespace point_rasterize_internal
146 
147 
148 /// @brief Efficient rasterization of one or more VDB Points grids into a linear
149 /// or frustum volume with the option to bake in camera or geometry motion blur.
150 ///
151 /// @details The camera transform can be provided using a RasterCamera object to
152 /// offer linear camera motion blur and geometry motion blur is computed from reading
153 /// a velocity attribute on the points. Sub-sampled camera motion blur is planned.
154 ///
155 /// @note For maximum memory efficiency, the data can optionally be streamed from
156 /// disk where the input VDB point grids are collapsed as they are read.
157 ///
158 /// @note The total contribution for each point is spread across all the voxels being
159 /// rasterized into and weighted by the total volume represented by each voxel. In an
160 /// example use case where a point is moving away from a camera that is used to
161 /// generate a frustum volume being rasterized into, each successive voxel is larger in
162 /// size.
163 template<typename PointDataGridT>
165 {
166 public:
167  using GridPtr = typename PointDataGridT::Ptr;
168  using GridConstPtr = typename PointDataGridT::ConstPtr;
170 
171  /// @brief main constructor
172  /// @param settings the shared settings for rasterizing, see class for more details
173  /// @param mask a spatial mask to use to define the areas of rasterization
174  /// @param interrupt a pointer adhering to the util::NullInterrupter interface
175  explicit FrustumRasterizer(
176  const FrustumRasterizerSettings& settings,
178  util::NullInterrupter* interrupt = nullptr);
179 
180  /// @brief Append a PointDataGrid to the rasterizer (but don't rasterize yet).
181  /// @param points the PointDataGrid
182  void addPoints(GridConstPtr& points);
183 
184  /// @brief Append a PointDataGrid to the rasterizer (but don't rasterize yet).
185  /// @param points the non-const PointDataGrid
186  /// @param stream if true, will destructively collapse attributes while
187  /// accessing so as to minimize the memory footprint.
188  void addPoints(GridPtr& points, bool stream = false);
189 
190  /// @brief Clear all PointDataGrids in the rasterizer.
191  void clear();
192 
193  /// @brief Return number of PointDataGrids in the rasterizer.
194  size_t size() const;
195 
196  /// @brief Return memory usage of the rasterizer.
197  size_t memUsage() const;
198 
199  template <typename FilterT = points::NullFilter>
201  rasterizeUniformDensity(RasterMode mode=RasterMode::MAXIMUM,
202  bool reduceMemory = false, float scale = 1.0f, const FilterT& filter = FilterT());
203 
204  template <typename FilterT = points::NullFilter>
206  rasterizeDensity(const openvdb::Name& attribute, RasterMode mode=RasterMode::MAXIMUM,
207  bool reduceMemory = false, float scale = 1.0f, const FilterT& filter = FilterT());
208 
209  template <typename FilterT = points::NullFilter>
211  rasterizeAttribute(const Name& attribute, RasterMode mode=RasterMode::ACCUMULATE,
212  bool reduceMemory = false, float scale = 1.0f, const FilterT& filter = FilterT());
213 
214  template <typename GridT, typename AttributeT, typename FilterT = points::NullFilter>
215  typename GridT::Ptr
216  rasterizeAttribute(const Name& attribute, RasterMode mode=RasterMode::ACCUMULATE,
217  bool reduceMemory = false, float scale = 1.0f, const FilterT& filter = FilterT());
218 
219  template <typename GridT, typename FilterT = points::NullFilter>
220  typename GridT::Ptr
221  rasterizeMask(bool reduceMemory = false, const FilterT& filter = FilterT());
222 
223 private:
224  template <typename AttributeT, typename GridT, typename FilterT>
225  void
226  performRasterization(
227  GridT& grid, RasterMode mode, const openvdb::Name& attribute,
228  bool reduceMemory, float scale, const FilterT& filter);
229 
230 private:
231  FrustumRasterizerSettings mSettings;
232  FrustumRasterizerMask mMask;
233 
234  util::NullInterrupter* mInterrupter;
235  std::vector<GridToRasterize> mPointGrids;
236 }; // class FrustumRasterizer
237 
238 
239 /// @brief A struct that stores all include/exclude attribute names as strings
240 /// and is internally converted into the resolved MultiGroupFilter
242 {
243  std::vector<Name> includeNames;
244  std::vector<Name> excludeNames;
245 }; // class RasterGroups
246 
247 
248 } // namespace points
249 } // namespace OPENVDB_VERSION_NAME
250 } // namespace openvdb
251 
253 
254 #endif // OPENVDB_POINTS_POINT_RASTERIZE_FRUSTUM_HAS_BEEN_INCLUDED
The Value Accessor Implementation and API methods. The majoirty of the API matches the API of a compa...
Definition: ValueAccessor.h:68
std::vector< Name > excludeNames
Definition: PointRasterizeFrustum.h:244
Definition: PointRasterizeFrustum.h:112
typename PointDataGridT::ConstPtr GridConstPtr
Definition: PointRasterizeFrustum.h:168
FrustumRasterizerSettings(const math::Transform &_transform)
Definition: PointRasterizeFrustum.h:91
RasterCamera camera
Definition: PointRasterizeFrustum.h:96
Methods for counting points in VDB Point grids.
SharedPtr< const Tree > ConstPtr
Definition: Tree.h:198
math::BBox< Vec3d > BBoxd
Definition: Types.h:84
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
Index32 Index
Definition: Types.h:54
Base class for interrupters.
Definition: NullInterrupter.h:25
OPENVDB_API SharedPtr< MapBase > simplify(SharedPtr< AffineMap > affine)
reduces an AffineMap to a ScaleMap or a ScaleTranslateMap when it can
typename PointDataGridT::Ptr GridPtr
Definition: PointRasterizeFrustum.h:167
Index64 memUsage(const TreeT &tree, bool threaded=true)
Return the total amount of memory in bytes occupied by this tree.
Definition: Count.h:493
math::Transform::Ptr transform
Definition: PointRasterizeFrustum.h:95
RasterMode
How to composite points into a volume.
Definition: PointRasterizeFrustum.h:30
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:615
OutGridT XformOp bool threaded
Definition: ValueTransformer.h:140
SharedPtr< Grid > Ptr
Definition: Grid.h:573
Definition: Exceptions.h:13
OutGridT const XformOp bool bool
Definition: ValueTransformer.h:609
SharedPtr< Transform > Ptr
Definition: Transform.h:42
std::vector< Name > includeNames
Definition: PointRasterizeFrustum.h:243
Efficient rasterization of one or more VDB Points grids into a linear or frustum volume with the opti...
Definition: PointRasterizeFrustum.h:164
A struct that stores all include/exclude attribute names as strings and is internally converted into ...
Definition: PointRasterizeFrustum.h:241
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
Definition: Transform.h:39
std::string Name
Definition: Name.h:19
Digital Differential Analyzers specialized for VDB.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:28
A group of shared settings to be used in the Volume Rasterizer.
Definition: PointRasterizeFrustum.h:87
A camera class that provides an interface for camera motion blur when rasterizing.
Definition: PointRasterizeFrustum.h:39
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218