This patch implements the vector types (i.e:`float2`) by making heavy usage of templating. All vector functions are now outside of the vector classes (inside the `blender::math` namespace) and are not vector size dependent for the most part. In the ongoing effort to make shaders less GL centric, we are aiming to share more code between GLSL and C++ to avoid code duplication. ####Motivations: - We are aiming to share UBO and SSBO structures between GLSL and C++. This means we will use many of the existing vector types and others we currently don't have (uintX, intX). All these variations were asking for many more code duplication. - Deduplicate existing code which is duplicated for each vector size. - We also want to share small functions. Which means that vector functions should be static and not in the class namespace. - Reduce friction to use these types in new projects due to their incompleteness. - The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a bit of a let down. Most clases are incomplete, out of sync with each others with different codestyles, and some functions that should be static are not (i.e: `float3::reflect()`). ####Upsides: - Still support `.x, .y, .z, .w` for readability. - Compact, readable and easilly extendable. - All of the vector functions are available for all the vectors types and can be restricted to certain types. Also template specialization let us define exception for special class (like mpq). - With optimization ON, the compiler unroll the loops and performance is the same. ####Downsides: - Might impact debugability. Though I would arge that the bugs are rarelly caused by the vector class itself (since the operations are quite trivial) but by the type conversions. - Might impact compile time. I did not saw a significant impact since the usage is not really widespread. - Functions needs to be rewritten to support arbitrary vector length. For instance, one can't call `len_squared_v3v3` in `math::length_squared()` and call it a day. - Type cast does not work with the template version of the `math::` vector functions. Meaning you need to manually cast `float *` and `(float *)[3]` to `float3` for the function calls. i.e: `math::distance_squared(float3(nearest.co), positions[i]);` - Some parts might loose in readability: `float3::dot(v1.normalized(), v2.normalized())` becoming `math::dot(math::normalize(v1), math::normalize(v2))` But I propose, when appropriate, to use `using namespace blender::math;` on function local or file scope to increase readability. `dot(normalize(v1), normalize(v2))` ####Consideration: - Include back `.length()` method. It is quite handy and is more C++ oriented. - I considered the GLM library as a candidate for replacement. It felt like too much for what we need and would be difficult to extend / modify to our needs. - I used Macros to reduce code in operators declaration and potential copy paste bugs. This could reduce debugability and could be reverted. - This touches `delaunay_2d.cc` and the intersection code. I would like to know @howardt opinion on the matter. - The `noexcept` on the copy constructor of `mpq(2|3)` is being removed. But according to @JacquesLucke it is not a real problem for now. I would like to give a huge thanks to @JacquesLucke who helped during this and pushed me to reduce the duplication further. Reviewed By: brecht, sergey, JacquesLucke Differential Revision: https://developer.blender.org/D13791
236 lines
8.4 KiB
C++
236 lines
8.4 KiB
C++
/*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
/** \file
|
|
* \ingroup bke
|
|
* \brief Volume data-block.
|
|
*/
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
struct BoundBox;
|
|
struct Depsgraph;
|
|
struct Main;
|
|
struct Object;
|
|
struct ReportList;
|
|
struct Scene;
|
|
struct Volume;
|
|
struct VolumeGridVector;
|
|
|
|
/* Module */
|
|
|
|
void BKE_volumes_init(void);
|
|
|
|
/* Data-block Management */
|
|
|
|
void BKE_volume_init_grids(struct Volume *volume);
|
|
void *BKE_volume_add(struct Main *bmain, const char *name);
|
|
|
|
struct BoundBox *BKE_volume_boundbox_get(struct Object *ob);
|
|
|
|
bool BKE_volume_is_y_up(const struct Volume *volume);
|
|
bool BKE_volume_is_points_only(const struct Volume *volume);
|
|
|
|
/* Depsgraph */
|
|
|
|
void BKE_volume_eval_geometry(struct Depsgraph *depsgraph, struct Volume *volume);
|
|
void BKE_volume_data_update(struct Depsgraph *depsgraph,
|
|
struct Scene *scene,
|
|
struct Object *object);
|
|
|
|
void BKE_volume_grids_backup_restore(struct Volume *volume,
|
|
struct VolumeGridVector *grids,
|
|
const char *filepath);
|
|
|
|
/* Draw Cache */
|
|
|
|
enum {
|
|
BKE_VOLUME_BATCH_DIRTY_ALL = 0,
|
|
};
|
|
|
|
void BKE_volume_batch_cache_dirty_tag(struct Volume *volume, int mode);
|
|
void BKE_volume_batch_cache_free(struct Volume *volume);
|
|
|
|
extern void (*BKE_volume_batch_cache_dirty_tag_cb)(struct Volume *volume, int mode);
|
|
extern void (*BKE_volume_batch_cache_free_cb)(struct Volume *volume);
|
|
|
|
/* Grids
|
|
*
|
|
* For volumes referencing a file, the list of grids and metadata must be
|
|
* loaded before it can be accessed. This happens on-demand, only when needed
|
|
* by the user interface, dependency graph or render engine. */
|
|
|
|
typedef struct VolumeGrid VolumeGrid;
|
|
|
|
bool BKE_volume_load(const struct Volume *volume, const struct Main *bmain);
|
|
void BKE_volume_unload(struct Volume *volume);
|
|
bool BKE_volume_is_loaded(const struct Volume *volume);
|
|
|
|
int BKE_volume_num_grids(const struct Volume *volume);
|
|
const char *BKE_volume_grids_error_msg(const struct Volume *volume);
|
|
const char *BKE_volume_grids_frame_filepath(const struct Volume *volume);
|
|
const VolumeGrid *BKE_volume_grid_get_for_read(const struct Volume *volume, int grid_index);
|
|
VolumeGrid *BKE_volume_grid_get_for_write(struct Volume *volume, int grid_index);
|
|
const VolumeGrid *BKE_volume_grid_active_get_for_read(const struct Volume *volume);
|
|
/* Tries to find a grid with the given name. Make sure that the volume has been loaded. */
|
|
const VolumeGrid *BKE_volume_grid_find_for_read(const struct Volume *volume, const char *name);
|
|
|
|
/* Grid
|
|
*
|
|
* By default only grid metadata is loaded, for access to the tree and voxels
|
|
* BKE_volume_grid_load must be called first. */
|
|
|
|
typedef enum VolumeGridType {
|
|
VOLUME_GRID_UNKNOWN = 0,
|
|
VOLUME_GRID_BOOLEAN,
|
|
VOLUME_GRID_FLOAT,
|
|
VOLUME_GRID_DOUBLE,
|
|
VOLUME_GRID_INT,
|
|
VOLUME_GRID_INT64,
|
|
VOLUME_GRID_MASK,
|
|
VOLUME_GRID_STRING,
|
|
VOLUME_GRID_VECTOR_FLOAT,
|
|
VOLUME_GRID_VECTOR_DOUBLE,
|
|
VOLUME_GRID_VECTOR_INT,
|
|
VOLUME_GRID_POINTS,
|
|
} VolumeGridType;
|
|
|
|
bool BKE_volume_grid_load(const struct Volume *volume, const struct VolumeGrid *grid);
|
|
void BKE_volume_grid_unload(const struct Volume *volume, const struct VolumeGrid *grid);
|
|
bool BKE_volume_grid_is_loaded(const struct VolumeGrid *grid);
|
|
|
|
/* Metadata */
|
|
|
|
const char *BKE_volume_grid_name(const struct VolumeGrid *grid);
|
|
VolumeGridType BKE_volume_grid_type(const struct VolumeGrid *grid);
|
|
int BKE_volume_grid_channels(const struct VolumeGrid *grid);
|
|
/**
|
|
* Transformation from index space to object space.
|
|
*/
|
|
void BKE_volume_grid_transform_matrix(const struct VolumeGrid *grid, float mat[4][4]);
|
|
|
|
/* Volume Editing
|
|
*
|
|
* These are intended for modifiers to use on evaluated data-blocks.
|
|
*
|
|
* new_for_eval creates a volume data-block with no grids or file path, but
|
|
* preserves other settings such as viewport display options.
|
|
*
|
|
* copy_for_eval creates a volume data-block preserving everything except the
|
|
* file path. Grids are shared with the source data-block, not copied. */
|
|
|
|
struct Volume *BKE_volume_new_for_eval(const struct Volume *volume_src);
|
|
struct Volume *BKE_volume_copy_for_eval(struct Volume *volume_src, bool reference);
|
|
|
|
struct VolumeGrid *BKE_volume_grid_add(struct Volume *volume,
|
|
const char *name,
|
|
VolumeGridType type);
|
|
void BKE_volume_grid_remove(struct Volume *volume, struct VolumeGrid *grid);
|
|
|
|
/* Simplify */
|
|
int BKE_volume_simplify_level(const struct Depsgraph *depsgraph);
|
|
float BKE_volume_simplify_factor(const struct Depsgraph *depsgraph);
|
|
|
|
/* File Save */
|
|
bool BKE_volume_save(const struct Volume *volume,
|
|
const struct Main *bmain,
|
|
struct ReportList *reports,
|
|
const char *filepath);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
/* OpenVDB Grid Access
|
|
*
|
|
* Access to OpenVDB grid for C++. These will automatically load grids from
|
|
* file or copy shared grids to make them writeable. */
|
|
|
|
#ifdef __cplusplus
|
|
# include "BLI_float4x4.hh"
|
|
# include "BLI_math_vec_types.hh"
|
|
# include "BLI_string_ref.hh"
|
|
|
|
bool BKE_volume_min_max(const Volume *volume, blender::float3 &r_min, blender::float3 &r_max);
|
|
|
|
# ifdef WITH_OPENVDB
|
|
# include <openvdb/openvdb.h>
|
|
# include <openvdb/points/PointDataGrid.h>
|
|
|
|
VolumeGrid *BKE_volume_grid_add_vdb(Volume &volume,
|
|
blender::StringRef name,
|
|
openvdb::GridBase::Ptr vdb_grid);
|
|
|
|
bool BKE_volume_grid_bounds(openvdb::GridBase::ConstPtr grid,
|
|
blender::float3 &r_min,
|
|
blender::float3 &r_max);
|
|
|
|
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,
|
|
const struct VolumeGrid *grid);
|
|
openvdb::GridBase::Ptr BKE_volume_grid_openvdb_for_write(const struct Volume *volume,
|
|
struct VolumeGrid *grid,
|
|
bool clear);
|
|
|
|
VolumeGridType BKE_volume_grid_type_openvdb(const openvdb::GridBase &grid);
|
|
|
|
template<typename OpType>
|
|
auto BKE_volume_grid_type_operation(const VolumeGridType grid_type, OpType &&op)
|
|
{
|
|
switch (grid_type) {
|
|
case VOLUME_GRID_FLOAT:
|
|
return op.template operator()<openvdb::FloatGrid>();
|
|
case VOLUME_GRID_VECTOR_FLOAT:
|
|
return op.template operator()<openvdb::Vec3fGrid>();
|
|
case VOLUME_GRID_BOOLEAN:
|
|
return op.template operator()<openvdb::BoolGrid>();
|
|
case VOLUME_GRID_DOUBLE:
|
|
return op.template operator()<openvdb::DoubleGrid>();
|
|
case VOLUME_GRID_INT:
|
|
return op.template operator()<openvdb::Int32Grid>();
|
|
case VOLUME_GRID_INT64:
|
|
return op.template operator()<openvdb::Int64Grid>();
|
|
case VOLUME_GRID_VECTOR_INT:
|
|
return op.template operator()<openvdb::Vec3IGrid>();
|
|
case VOLUME_GRID_VECTOR_DOUBLE:
|
|
return op.template operator()<openvdb::Vec3dGrid>();
|
|
case VOLUME_GRID_STRING:
|
|
return op.template operator()<openvdb::StringGrid>();
|
|
case VOLUME_GRID_MASK:
|
|
return op.template operator()<openvdb::MaskGrid>();
|
|
case VOLUME_GRID_POINTS:
|
|
return op.template operator()<openvdb::points::PointDataGrid>();
|
|
case VOLUME_GRID_UNKNOWN:
|
|
break;
|
|
}
|
|
|
|
/* Should never be called. */
|
|
BLI_assert_msg(0, "should never be reached");
|
|
return op.template operator()<openvdb::FloatGrid>();
|
|
}
|
|
|
|
openvdb::GridBase::Ptr BKE_volume_grid_create_with_changed_resolution(
|
|
const VolumeGridType grid_type, const openvdb::GridBase &old_grid, float resolution_factor);
|
|
|
|
# endif
|
|
#endif
|