As described in T91186, this commit moves mesh vertex normals into a contiguous array of float vectors in a custom data layer, how face normals are currently stored. The main interface is documented in `BKE_mesh.h`. Vertex and face normals are now calculated on-demand and cached, retrieved with an "ensure" function. Since the logical state of a mesh is now "has normals when necessary", they can be retrieved from a `const` mesh. The goal is to use on-demand calculation for all derived data, but leave room for eager calculation for performance purposes (modifier evaluation is threaded, but viewport data generation is not). **Benefits** This moves us closer to a SoA approach rather than the current AoS paradigm. Accessing a contiguous `float3` is much more efficient than retrieving data from a larger struct. The memory requirements for accessing only normals or vertex locations are smaller, and at the cost of more memory usage for just normals, they now don't have to be converted between float and short, which also simplifies code In the future, the remaining items can be removed from `MVert`, leaving only `float3`, which has similar benefits (see T93602). Removing the combination of derived and original data makes it conceptually simpler to only calculate normals when necessary. This is especially important now that we have more opportunities for temporary meshes in geometry nodes. **Performance** In addition to the theoretical future performance improvements by making `MVert == float3`, I've done some basic performance testing on this patch directly. The data is fairly rough, but it gives an idea about where things stand generally. - Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms), showing that accessing just `MVert` is now more efficient. - Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight change that at least shows there is no regression. - Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small but observable speedup. - Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms), shows that using normals in geometry nodes is faster. - Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms), shows that calculating normals is slightly faster now. - File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB), Normals are not saved in files, which can help with large meshes. As for memory usage, it may be slightly more in some cases, but I didn't observe any difference in the production files I tested. **Tests** Some modifiers and cycles test results need to be updated with this commit, for two reasons: - The subdivision surface modifier is not responsible for calculating normals anymore. In master, the modifier creates different normals than the result of the `Mesh` normal calculation, so this is a bug fix. - There are small differences in the results of some modifiers that use normals because they are not converted to and from `short` anymore. **Future improvements** - Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier already retrieves normals if they are needed anyway. - Copy normals as part of a better CoW system for attributes. - Make more areas use lazy instead of eager normal calculation. - Remove `BKE_mesh_normals_tag_dirty` in more places since that is now the default state of a new mesh. - Possibly apply a similar change to derived face corner normals. Differential Revision: https://developer.blender.org/D12770
236 lines
8.3 KiB
C
236 lines
8.3 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.
|
|
*
|
|
* The Original Code is Copyright (C) Blender Foundation.
|
|
* All rights reserved.
|
|
*/
|
|
#pragma once
|
|
|
|
/** \file
|
|
* \ingroup bke
|
|
*/
|
|
|
|
/* Shrinkwrap stuff */
|
|
#include "BKE_bvhutils.h"
|
|
#include "BLI_bitmap.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/*
|
|
* Shrinkwrap is composed by a set of functions and options that define the type of shrink.
|
|
*
|
|
* 3 modes are available:
|
|
* - Nearest vertex.
|
|
* - Nearest surface.
|
|
* - Normal projection.
|
|
*
|
|
* #ShrinkwrapCalcData encapsulates all needed data for shrink-wrap functions.
|
|
* (So that you don't have to pass an enormous amount of arguments to functions)
|
|
*/
|
|
|
|
struct BVHTree;
|
|
struct MDeformVert;
|
|
struct Mesh;
|
|
struct ModifierEvalContext;
|
|
struct Object;
|
|
struct ShrinkwrapModifierData;
|
|
struct ShrinkwrapGpencilModifierData;
|
|
struct SpaceTransform;
|
|
|
|
/* Information about boundary edges in the mesh. */
|
|
typedef struct ShrinkwrapBoundaryVertData {
|
|
/* Average direction of edges that meet here. */
|
|
float direction[3];
|
|
|
|
/* Closest vector to direction that is orthogonal to vertex normal. */
|
|
float normal_plane[3];
|
|
} ShrinkwrapBoundaryVertData;
|
|
|
|
typedef struct ShrinkwrapBoundaryData {
|
|
/* True if the edge belongs to exactly one face. */
|
|
const BLI_bitmap *edge_is_boundary;
|
|
/* True if the looptri has any boundary edges. */
|
|
const BLI_bitmap *looptri_has_boundary;
|
|
|
|
/* Mapping from vertex index to boundary vertex index, or -1.
|
|
* Used for compact storage of data about boundary vertices. */
|
|
const int *vert_boundary_id;
|
|
unsigned int num_boundary_verts;
|
|
|
|
/* Direction data about boundary vertices. */
|
|
const ShrinkwrapBoundaryVertData *boundary_verts;
|
|
} ShrinkwrapBoundaryData;
|
|
|
|
/**
|
|
* Free boundary data for target project.
|
|
*/
|
|
void BKE_shrinkwrap_discard_boundary_data(struct Mesh *mesh);
|
|
void BKE_shrinkwrap_compute_boundary_data(struct Mesh *mesh);
|
|
|
|
/* Information about a mesh and BVH tree. */
|
|
typedef struct ShrinkwrapTreeData {
|
|
Mesh *mesh;
|
|
|
|
BVHTree *bvh;
|
|
BVHTreeFromMesh treeData;
|
|
|
|
const float (*pnors)[3];
|
|
float (*clnors)[3];
|
|
ShrinkwrapBoundaryData *boundary;
|
|
} ShrinkwrapTreeData;
|
|
|
|
/**
|
|
* Checks if the modifier needs target normals with these settings.
|
|
*/
|
|
bool BKE_shrinkwrap_needs_normals(int shrinkType, int shrinkMode);
|
|
|
|
/**
|
|
* Initializes the mesh data structure from the given mesh and settings.
|
|
*/
|
|
bool BKE_shrinkwrap_init_tree(struct ShrinkwrapTreeData *data,
|
|
Mesh *mesh,
|
|
int shrinkType,
|
|
int shrinkMode,
|
|
bool force_normals);
|
|
|
|
/**
|
|
* Frees the tree data if necessary.
|
|
*/
|
|
void BKE_shrinkwrap_free_tree(struct ShrinkwrapTreeData *data);
|
|
|
|
/**
|
|
* Main shrink-wrap function (implementation of the shrink-wrap modifier).
|
|
*/
|
|
void shrinkwrapModifier_deform(struct ShrinkwrapModifierData *smd,
|
|
const struct ModifierEvalContext *ctx,
|
|
struct Scene *scene,
|
|
struct Object *ob,
|
|
struct Mesh *mesh,
|
|
struct MDeformVert *dvert,
|
|
int defgrp_index,
|
|
float (*vertexCos)[3],
|
|
int numVerts);
|
|
/* Implementation of the Shrinkwrap Grease Pencil modifier. */
|
|
void shrinkwrapGpencilModifier_deform(struct ShrinkwrapGpencilModifierData *mmd,
|
|
struct Object *ob,
|
|
struct MDeformVert *dvert,
|
|
int defgrp_index,
|
|
float (*vertexCos)[3],
|
|
int numVerts);
|
|
|
|
/**
|
|
* Used in `editmesh_mask_extract.c` to shrink-wrap the extracted mesh to the sculpt.
|
|
*/
|
|
void BKE_shrinkwrap_mesh_nearest_surface_deform(struct bContext *C,
|
|
struct Object *ob_source,
|
|
struct Object *ob_target);
|
|
|
|
/**
|
|
* Used in `object_remesh.cc` to preserve the details and volume in the voxel remesher.
|
|
*/
|
|
void BKE_shrinkwrap_remesh_target_project(struct Mesh *src_me,
|
|
struct Mesh *target_me,
|
|
struct Object *ob_target);
|
|
|
|
/**
|
|
* This function ray-cast a single vertex and updates the hit if the "hit" is considered valid.
|
|
*
|
|
* \param options: Opts control whether an hit is valid or not.
|
|
* Supported options are:
|
|
* - #MOD_SHRINKWRAP_CULL_TARGET_FRONTFACE (front faces hits are ignored)
|
|
* - #MOD_SHRINKWRAP_CULL_TARGET_BACKFACE (back faces hits are ignored)
|
|
*
|
|
* \param transf: Take into consideration the space_transform, that is:
|
|
* if `transf` was configured with `SPACE_TRANSFORM_SETUP( &transf, ob1, ob2)`
|
|
* then the input (vert, dir, #BVHTreeRayHit) must be defined in ob1 coordinates space
|
|
* and the #BVHTree must be built in ob2 coordinate space.
|
|
* Thus it provides an easy way to cast the same ray across several trees
|
|
* (where each tree was built on its own coords space).
|
|
*
|
|
* \return true if "hit" was updated.
|
|
*/
|
|
bool BKE_shrinkwrap_project_normal(char options,
|
|
const float vert[3],
|
|
const float dir[3],
|
|
float ray_radius,
|
|
const struct SpaceTransform *transf,
|
|
struct ShrinkwrapTreeData *tree,
|
|
BVHTreeRayHit *hit);
|
|
|
|
/**
|
|
* Maps the point to the nearest surface, either by simple nearest, or by target normal projection.
|
|
*/
|
|
void BKE_shrinkwrap_find_nearest_surface(struct ShrinkwrapTreeData *tree,
|
|
struct BVHTreeNearest *nearest,
|
|
float co[3],
|
|
int type);
|
|
|
|
/**
|
|
* Compute a smooth normal of the target (if applicable) at the hit location.
|
|
*
|
|
* \param tree: information about the mesh.
|
|
* \param transform: transform from the hit coordinate space to the object space; may be null.
|
|
* \param r_no: output in hit coordinate space; may be shared with inputs.
|
|
*/
|
|
void BKE_shrinkwrap_compute_smooth_normal(const struct ShrinkwrapTreeData *tree,
|
|
const struct SpaceTransform *transform,
|
|
int looptri_idx,
|
|
const float hit_co[3],
|
|
const float hit_no[3],
|
|
float r_no[3]);
|
|
|
|
/**
|
|
* Apply the shrink to surface modes to the given original coordinates and nearest point.
|
|
*
|
|
* \param tree: mesh data for smooth normals.
|
|
* \param transform: transform from the hit coordinate space to the object space; may be null.
|
|
* \param r_point_co: may be the same memory location as `point_co`, `hit_co`, or `hit_no`.
|
|
*/
|
|
void BKE_shrinkwrap_snap_point_to_surface(const struct ShrinkwrapTreeData *tree,
|
|
const struct SpaceTransform *transform,
|
|
int mode,
|
|
int hit_idx,
|
|
const float hit_co[3],
|
|
const float hit_no[3],
|
|
float goal_dist,
|
|
const float point_co[3],
|
|
float r_point_co[3]);
|
|
|
|
/*
|
|
* NULL initializes to local data
|
|
*/
|
|
#define NULL_ShrinkwrapCalcData \
|
|
{ \
|
|
NULL, \
|
|
}
|
|
#define NULL_BVHTreeFromMesh \
|
|
{ \
|
|
NULL, \
|
|
}
|
|
#define NULL_BVHTreeRayHit \
|
|
{ \
|
|
NULL, \
|
|
}
|
|
#define NULL_BVHTreeNearest \
|
|
{ \
|
|
0, \
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|