Cleanup: Continue loop to corner rename in some mesh code

Change mesh normals code and mesh topology map functions.
This commit is contained in:
Hans Goudey
2024-01-10 13:12:16 -05:00
parent 5680ea2986
commit ba4d7fc2d9
15 changed files with 541 additions and 540 deletions

View File

@@ -364,7 +364,7 @@ bool BKE_mesh_has_custom_loop_normals(struct Mesh *mesh);
/**
* Higher level functions hiding most of the code needed around call to
* #normals_loop_custom_set().
* #normals_corner_custom_set().
*
* \param r_custom_loop_normals: is not const, since code will replace zero_v3 normals there
* with automatically computed vectors.
@@ -372,7 +372,7 @@ bool BKE_mesh_has_custom_loop_normals(struct Mesh *mesh);
void BKE_mesh_set_custom_normals(struct Mesh *mesh, float (*r_custom_loop_normals)[3]);
/**
* Higher level functions hiding most of the code needed around call to
* #normals_loop_custom_set_from_verts().
* #normals_corner_custom_set_from_verts().
*
* \param r_custom_vert_normals: is not const, since code will replace zero_v3 normals there
* with automatically computed vectors.

View File

@@ -157,13 +157,12 @@ struct CornerNormalSpaceArray {
bool create_corners_by_space = false;
};
short2 lnor_space_custom_normal_to_data(const CornerNormalSpace &lnor_space,
const float3 &custom_lnor);
short2 corner_space_custom_normal_to_data(const CornerNormalSpace &lnor_space,
const float3 &custom_lnor);
/**
* Compute split normals, i.e. vertex normals associated with each face (hence 'loop normals').
* Useful to materialize sharp edges (or non-smooth faces) without actually modifying the geometry
* (splitting edges).
* Compute split normals, i.e. vertex normals associated with each face. Used to visualize sharp
* edges (or non-smooth faces) without actually modifying the geometry (splitting edges).
*
* \param sharp_edges: Optional array of sharp edge tags, used to split the evaluated normals on
* each side of the edge.
@@ -172,54 +171,54 @@ short2 lnor_space_custom_normal_to_data(const CornerNormalSpace &lnor_space,
* \param r_lnors_spacearr: Optional return data filled with information about the custom
* normals spaces for each grouped fan of face corners.
*/
void normals_calc_loop(Span<float3> vert_positions,
Span<int2> edges,
OffsetIndices<int> faces,
Span<int> corner_verts,
Span<int> corner_edges,
Span<int> loop_to_face_map,
Span<float3> vert_normals,
Span<float3> face_normals,
Span<bool> sharp_edges,
Span<bool> sharp_faces,
const short2 *clnors_data,
CornerNormalSpaceArray *r_lnors_spacearr,
MutableSpan<float3> r_loop_normals);
void normals_calc_corners(Span<float3> vert_positions,
Span<int2> edges,
OffsetIndices<int> faces,
Span<int> corner_verts,
Span<int> corner_edges,
Span<int> corner_to_face_map,
Span<float3> vert_normals,
Span<float3> face_normals,
Span<bool> sharp_edges,
Span<bool> sharp_faces,
const short2 *clnors_data,
CornerNormalSpaceArray *r_lnors_spacearr,
MutableSpan<float3> r_corner_normals);
/**
* \param sharp_faces: Optional array used to mark specific faces for sharp shading.
*/
void normals_loop_custom_set(Span<float3> vert_positions,
Span<int2> edges,
OffsetIndices<int> faces,
Span<int> corner_verts,
Span<int> corner_edges,
Span<float3> vert_normals,
Span<float3> face_normals,
Span<bool> sharp_faces,
MutableSpan<bool> sharp_edges,
MutableSpan<float3> r_custom_loop_normals,
MutableSpan<short2> r_clnors_data);
void normals_corner_custom_set(Span<float3> vert_positions,
Span<int2> edges,
OffsetIndices<int> faces,
Span<int> corner_verts,
Span<int> corner_edges,
Span<float3> vert_normals,
Span<float3> face_normals,
Span<bool> sharp_faces,
MutableSpan<bool> sharp_edges,
MutableSpan<float3> r_custom_corner_normals,
MutableSpan<short2> r_clnors_data);
/**
* \param sharp_faces: Optional array used to mark specific faces for sharp shading.
*/
void normals_loop_custom_set_from_verts(Span<float3> vert_positions,
Span<int2> edges,
OffsetIndices<int> faces,
Span<int> corner_verts,
Span<int> corner_edges,
Span<float3> vert_normals,
Span<float3> face_normals,
Span<bool> sharp_faces,
MutableSpan<bool> sharp_edges,
MutableSpan<float3> r_custom_vert_normals,
MutableSpan<short2> r_clnors_data);
void normals_corner_custom_set_from_verts(Span<float3> vert_positions,
Span<int2> edges,
OffsetIndices<int> faces,
Span<int> corner_verts,
Span<int> corner_edges,
Span<float3> vert_normals,
Span<float3> face_normals,
Span<bool> sharp_faces,
MutableSpan<bool> sharp_edges,
MutableSpan<float3> r_custom_vert_normals,
MutableSpan<short2> r_clnors_data);
/**
* Define sharp edges as needed to mimic 'autosmooth' from angle threshold.
*
* Used when defining an empty custom loop normals data layer,
* Used when defining an empty custom corner normals data layer,
* to keep same shading as with auto-smooth!
*
* \param sharp_faces: Optional array used to mark specific faces for sharp shading.
@@ -228,7 +227,7 @@ void edges_sharp_from_angle_set(OffsetIndices<int> faces,
Span<int> corner_verts,
Span<int> corner_edges,
Span<float3> face_normals,
Span<int> loop_to_face,
Span<int> corner_to_face,
Span<bool> sharp_faces,
const float split_angle,
MutableSpan<bool> sharp_edges);

View File

@@ -274,7 +274,7 @@ int *BKE_mesh_calc_smoothgroups(int edges_num,
namespace blender::bke::mesh {
Array<int> build_loop_to_face_map(OffsetIndices<int> faces);
Array<int> build_corner_to_face_map(OffsetIndices<int> faces);
GroupedSpan<int> build_vert_to_edge_map(Span<int2> edges,
int verts_num,
@@ -292,15 +292,15 @@ GroupedSpan<int> build_vert_to_face_map(OffsetIndices<int> faces,
Array<int> &r_indices);
Array<int> build_vert_to_corner_indices(Span<int> corner_verts, OffsetIndices<int> offsets);
GroupedSpan<int> build_vert_to_loop_map(Span<int> corner_verts,
int verts_num,
Array<int> &r_offsets,
Array<int> &r_indices);
GroupedSpan<int> build_vert_to_corner_map(Span<int> corner_verts,
int verts_num,
Array<int> &r_offsets,
Array<int> &r_indices);
GroupedSpan<int> build_edge_to_loop_map(Span<int> corner_edges,
int edges_num,
Array<int> &r_offsets,
Array<int> &r_indices);
GroupedSpan<int> build_edge_to_corner_map(Span<int> corner_edges,
int edges_num,
Array<int> &r_offsets,
Array<int> &r_indices);
GroupedSpan<int> build_edge_to_face_map(OffsetIndices<int> faces,
Span<int> corner_edges,

View File

@@ -379,17 +379,17 @@ static void data_transfer_dtdata_type_postprocess(Mesh *me_dst,
"sharp_edge", bke::AttrDomain::Edge);
const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", bke::AttrDomain::Face);
/* Note loop_nors_dst contains our custom normals as transferred from source... */
blender::bke::mesh::normals_loop_custom_set(me_dst->vert_positions(),
me_dst->edges(),
me_dst->faces(),
me_dst->corner_verts(),
me_dst->corner_edges(),
me_dst->vert_normals(),
me_dst->face_normals(),
sharp_faces,
sharp_edges.span,
{loop_nors_dst, me_dst->corners_num},
{custom_nors_dst, me_dst->corners_num});
blender::bke::mesh::normals_corner_custom_set(me_dst->vert_positions(),
me_dst->edges(),
me_dst->faces(),
me_dst->corner_verts(),
me_dst->corner_edges(),
me_dst->vert_normals(),
me_dst->face_normals(),
sharp_faces,
sharp_edges.span,
{loop_nors_dst, me_dst->corners_num},
{custom_nors_dst, me_dst->corners_num});
sharp_edges.finish();
CustomData_free_layers(ldata_dst, CD_NORMAL, me_dst->corners_num);
}

View File

@@ -2280,7 +2280,7 @@ void BKE_keyblock_mesh_calc_normals(const KeyBlock *kb,
const AttributeAccessor attributes = mesh->attributes();
const VArraySpan sharp_edges = *attributes.lookup<bool>("sharp_edge", AttrDomain::Edge);
const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", AttrDomain::Face);
mesh::normals_calc_loop(
mesh::normals_calc_corners(
positions,
edges,
faces,

View File

@@ -373,7 +373,7 @@ static GroupedSpan<int> gather_groups(const Span<int> group_indices,
return {OffsetIndices<int>(r_offsets), r_indices};
}
Array<int> build_loop_to_face_map(const OffsetIndices<int> faces)
Array<int> build_corner_to_face_map(const OffsetIndices<int> faces)
{
Array<int> map(faces.total_size());
offset_indices::build_reverse_map(faces, map);
@@ -430,18 +430,18 @@ Array<int> build_vert_to_corner_indices(const Span<int> corner_verts,
return reverse_indices_in_groups(corner_verts, offsets);
}
GroupedSpan<int> build_vert_to_loop_map(const Span<int> corner_verts,
const int verts_num,
Array<int> &r_offsets,
Array<int> &r_indices)
GroupedSpan<int> build_vert_to_corner_map(const Span<int> corner_verts,
const int verts_num,
Array<int> &r_offsets,
Array<int> &r_indices)
{
return gather_groups(corner_verts, verts_num, r_offsets, r_indices);
}
GroupedSpan<int> build_edge_to_loop_map(const Span<int> corner_edges,
const int edges_num,
Array<int> &r_offsets,
Array<int> &r_indices)
GroupedSpan<int> build_edge_to_corner_map(const Span<int> corner_edges,
const int edges_num,
Array<int> &r_offsets,
Array<int> &r_indices)
{
return gather_groups(corner_edges, edges_num, r_offsets, r_indices);
}
@@ -858,7 +858,7 @@ static bool mesh_calc_islands_loop_face_uv(const int totedge,
Array<int> edge_to_loop_indices;
GroupedSpan<int> edge_to_loop_map;
if (luvs) {
edge_to_loop_map = bke::mesh::build_edge_to_loop_map(
edge_to_loop_map = bke::mesh::build_edge_to_corner_map(
{corner_edges, totloop}, totedge, edge_to_loop_offsets, edge_to_loop_indices);
}

View File

@@ -411,19 +411,19 @@ Mesh *BKE_mesh_mirror_apply_mirror_on_axis_for_modifier(MirrorModifierData *mmd,
const bke::AttributeAccessor attributes = result->attributes();
const VArraySpan sharp_edges = *attributes.lookup<bool>("sharp_edge", AttrDomain::Edge);
const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", AttrDomain::Face);
blender::bke::mesh::normals_calc_loop(result->vert_positions(),
result_edges,
result_faces,
result_corner_verts,
result_corner_edges,
result->corner_to_face_map(),
result->vert_normals(),
result->face_normals(),
sharp_edges,
sharp_faces,
clnors,
&lnors_spacearr,
loop_normals);
blender::bke::mesh::normals_calc_corners(result->vert_positions(),
result_edges,
result_faces,
result_corner_verts,
result_corner_edges,
result->corner_to_face_map(),
result->vert_normals(),
result->face_normals(),
sharp_edges,
sharp_faces,
clnors,
&lnors_spacearr,
loop_normals);
/* mirroring has to account for loops being reversed in faces in second half */
for (const int i : src_faces.index_range()) {
@@ -440,7 +440,7 @@ Mesh *BKE_mesh_mirror_apply_mirror_on_axis_for_modifier(MirrorModifierData *mmd,
mul_m4_v3(mtx_nor, loop_normals[mirrorj]);
const int space_index = lnors_spacearr.corner_space_indices[mirrorj];
clnors[mirrorj] = blender::bke::mesh::lnor_space_custom_normal_to_data(
clnors[mirrorj] = blender::bke::mesh::corner_space_custom_normal_to_data(
lnors_spacearr.spaces[space_index], loop_normals[mirrorj]);
}
}

View File

@@ -294,19 +294,19 @@ blender::Span<blender::float3> Mesh::corner_normals() const
const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", AttrDomain::Face);
const short2 *custom_normals = static_cast<const short2 *>(
CustomData_get_layer(&this->corner_data, CD_CUSTOMLOOPNORMAL));
mesh::normals_calc_loop(this->vert_positions(),
this->edges(),
this->faces(),
this->corner_verts(),
this->corner_edges(),
this->corner_to_face_map(),
this->vert_normals(),
this->face_normals(),
sharp_edges,
sharp_faces,
custom_normals,
nullptr,
r_data);
mesh::normals_calc_corners(this->vert_positions(),
this->edges(),
this->faces(),
this->corner_verts(),
this->corner_edges(),
this->corner_to_face_map(),
this->vert_normals(),
this->face_normals(),
sharp_edges,
sharp_faces,
custom_normals,
nullptr,
r_data);
break;
}
}
@@ -391,10 +391,10 @@ MLoopNorSpace *BKE_lnor_space_create(MLoopNorSpaceArray *lnors_spacearr)
namespace blender::bke::mesh {
static CornerNormalSpace lnor_space_define(const float3 &lnor,
const float3 &vec_ref,
const float3 &vec_other,
const Span<float3> edge_vectors)
static CornerNormalSpace corner_fan_space_define(const float3 &lnor,
const float3 &vec_ref,
const float3 &vec_other,
const Span<float3> edge_vectors)
{
CornerNormalSpace lnor_space{};
const float pi2 = float(M_PI) * 2.0f;
@@ -462,7 +462,7 @@ void BKE_lnor_space_define(MLoopNorSpace *lnor_space,
const blender::Span<blender::float3> edge_vectors)
{
using namespace blender::bke::mesh;
const CornerNormalSpace space = lnor_space_define(lnor, vec_ref, vec_other, edge_vectors);
const CornerNormalSpace space = corner_fan_space_define(lnor, vec_ref, vec_other, edge_vectors);
copy_v3_v3(lnor_space->vec_lnor, space.vec_lnor);
copy_v3_v3(lnor_space->vec_ref, space.vec_ref);
copy_v3_v3(lnor_space->vec_ortho, space.vec_ortho);
@@ -507,8 +507,8 @@ MINLINE short unit_float_to_short(const float val)
namespace blender::bke::mesh {
static float3 lnor_space_custom_data_to_normal(const CornerNormalSpace &lnor_space,
const short2 clnor_data)
static float3 corner_space_custom_data_to_normal(const CornerNormalSpace &lnor_space,
const short2 clnor_data)
{
/* NOP custom normal data or invalid lnor space, return. */
if (clnor_data[0] == 0 || lnor_space.ref_alpha == 0.0f || lnor_space.ref_beta == 0.0f) {
@@ -554,13 +554,13 @@ void BKE_lnor_space_custom_data_to_normal(const MLoopNorSpace *lnor_space,
space.vec_ortho = lnor_space->vec_ortho;
space.ref_alpha = lnor_space->ref_alpha;
space.ref_beta = lnor_space->ref_beta;
copy_v3_v3(r_custom_lnor, lnor_space_custom_data_to_normal(space, clnor_data));
copy_v3_v3(r_custom_lnor, corner_space_custom_data_to_normal(space, clnor_data));
}
namespace blender::bke::mesh {
short2 lnor_space_custom_normal_to_data(const CornerNormalSpace &lnor_space,
const float3 &custom_lnor)
short2 corner_space_custom_normal_to_data(const CornerNormalSpace &lnor_space,
const float3 &custom_lnor)
{
/* We use zero vector as NOP custom normal (can be simpler than giving auto-computed `lnor`). */
if (is_zero_v3(custom_lnor) || compare_v3v3(lnor_space.vec_lnor, custom_lnor, 1e-4f)) {
@@ -620,17 +620,17 @@ void BKE_lnor_space_custom_normal_to_data(const MLoopNorSpace *lnor_space,
space.vec_ortho = lnor_space->vec_ortho;
space.ref_alpha = lnor_space->ref_alpha;
space.ref_beta = lnor_space->ref_beta;
copy_v2_v2_short(r_clnor_data, lnor_space_custom_normal_to_data(space, custom_lnor));
copy_v2_v2_short(r_clnor_data, corner_space_custom_normal_to_data(space, custom_lnor));
}
namespace blender::bke::mesh {
struct LoopSplitTaskDataCommon {
struct CornerSplitTaskDataCommon {
/* Read/write.
* Note we do not need to protect it, though, since two different tasks will *always* affect
* different elements in the arrays. */
CornerNormalSpaceArray *lnors_spacearr;
MutableSpan<float3> loop_normals;
MutableSpan<float3> corner_normals;
/* Read-only. */
Span<float3> positions;
@@ -638,8 +638,8 @@ struct LoopSplitTaskDataCommon {
Span<int> corner_verts;
Span<int> corner_edges;
OffsetIndices<int> faces;
Span<int2> edge_to_loops;
Span<int> loop_to_face;
Span<int2> edge_to_corners;
Span<int> corner_to_face;
Span<float3> face_normals;
Span<float3> vert_normals;
Span<short2> clnors_data;
@@ -647,18 +647,18 @@ struct LoopSplitTaskDataCommon {
#define INDEX_UNSET INT_MIN
#define INDEX_INVALID -1
/* See comment about edge_to_loops below. */
/* See comment about edge_to_corners below. */
#define IS_EDGE_SHARP(_e2l) ELEM((_e2l)[1], INDEX_UNSET, INDEX_INVALID)
static void mesh_edges_sharp_tag(const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int> corner_edges,
const Span<int> loop_to_face_map,
const Span<int> corner_to_face_map,
const Span<float3> face_normals,
const Span<bool> sharp_faces,
const Span<bool> sharp_edges,
const float split_angle,
MutableSpan<int2> edge_to_loops,
MutableSpan<int2> edge_to_corners,
MutableSpan<bool> r_sharp_edges)
{
const float split_angle_cos = cosf(split_angle);
@@ -667,51 +667,50 @@ static void mesh_edges_sharp_tag(const OffsetIndices<int> faces,
};
for (const int face_i : faces.index_range()) {
for (const int loop_index : faces[face_i]) {
const int vert_i = corner_verts[loop_index];
const int edge_i = corner_edges[loop_index];
for (const int corner : faces[face_i]) {
const int vert = corner_verts[corner];
const int edge = corner_edges[corner];
int2 &e2l = edge_to_loops[edge_i];
int2 &e2l = edge_to_corners[edge];
/* Check whether current edge might be smooth or sharp */
if ((e2l[0] | e2l[1]) == 0) {
/* 'Empty' edge until now, set e2l[0] (and e2l[1] to INDEX_UNSET to tag it as unset). */
e2l[0] = loop_index;
e2l[0] = corner;
/* We have to check this here too, else we might miss some flat faces!!! */
e2l[1] = face_is_smooth(face_i) ? INDEX_UNSET : INDEX_INVALID;
}
else if (e2l[1] == INDEX_UNSET) {
const bool is_angle_sharp = math::dot(face_normals[loop_to_face_map[e2l[0]]],
const bool is_angle_sharp = math::dot(face_normals[corner_to_face_map[e2l[0]]],
face_normals[face_i]) < split_angle_cos;
/* Second loop using this edge, time to test its sharpness.
/* Second corner using this edge, time to test its sharpness.
* An edge is sharp if it is tagged as such, or its face is not smooth,
* or both faces have opposed (flipped) normals, i.e. both loops on the same edge share the
* same vertex, or angle between both its faces' normals is above split_angle value.
*/
if (!face_is_smooth(face_i) || (!sharp_edges.is_empty() && sharp_edges[edge_i]) ||
vert_i == corner_verts[e2l[0]] || is_angle_sharp)
* or both faces have opposed (flipped) normals, i.e. both corners on the same edge share
* the same vertex, or angle between both its faces' normals is above split_angle value. */
if (!face_is_smooth(face_i) || (!sharp_edges.is_empty() && sharp_edges[edge]) ||
vert == corner_verts[e2l[0]] || is_angle_sharp)
{
/* NOTE: we are sure that loop != 0 here ;). */
/* NOTE: we are sure that corner != 0 here ;). */
e2l[1] = INDEX_INVALID;
/* We want to avoid tagging edges as sharp when it is already defined as such by
* other causes than angle threshold. */
if (is_angle_sharp) {
r_sharp_edges[edge_i] = true;
r_sharp_edges[edge] = true;
}
}
else {
e2l[1] = loop_index;
e2l[1] = corner;
}
}
else if (!IS_EDGE_SHARP(e2l)) {
/* More than two loops using this edge, tag as sharp if not yet done. */
/* More than two corners using this edge, tag as sharp if not yet done. */
e2l[1] = INDEX_INVALID;
/* We want to avoid tagging edges as sharp when it is already defined as such by
* other causes than angle threshold. */
r_sharp_edges[edge_i] = false;
r_sharp_edges[edge] = false;
}
/* Else, edge is already 'disqualified' (i.e. sharp)! */
}
@@ -722,48 +721,48 @@ static void mesh_edges_sharp_tag(const OffsetIndices<int> faces,
* Builds a simplified map from edges to face corners, marking special values when
* it encounters sharp edges or borders between faces with flipped winding orders.
*/
static void build_edge_to_loop_map_with_flip_and_sharp(const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int> corner_edges,
const Span<bool> sharp_faces,
const Span<bool> sharp_edges,
MutableSpan<int2> edge_to_loops)
static void build_edge_to_corner_map_with_flip_and_sharp(const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int> corner_edges,
const Span<bool> sharp_faces,
const Span<bool> sharp_edges,
MutableSpan<int2> edge_to_corners)
{
auto face_is_smooth = [&](const int face_i) {
return sharp_faces.is_empty() || !sharp_faces[face_i];
};
for (const int face_i : faces.index_range()) {
for (const int loop_index : faces[face_i]) {
const int vert_i = corner_verts[loop_index];
const int edge_i = corner_edges[loop_index];
for (const int corner : faces[face_i]) {
const int vert = corner_verts[corner];
const int edge = corner_edges[corner];
int2 &e2l = edge_to_loops[edge_i];
int2 &e2l = edge_to_corners[edge];
/* Check whether current edge might be smooth or sharp */
if ((e2l[0] | e2l[1]) == 0) {
/* 'Empty' edge until now, set e2l[0] (and e2l[1] to INDEX_UNSET to tag it as unset). */
e2l[0] = loop_index;
e2l[0] = corner;
/* We have to check this here too, else we might miss some flat faces!!! */
e2l[1] = !face_is_smooth(face_i) ? INDEX_INVALID : INDEX_UNSET;
}
else if (e2l[1] == INDEX_UNSET) {
/* Second loop using this edge, time to test its sharpness.
/* Second corner using this edge, time to test its sharpness.
* An edge is sharp if it is tagged as such, or its face is not smooth,
* or both face have opposed (flipped) normals, i.e. both loops on the same edge share the
* same vertex. */
if (!face_is_smooth(face_i) || (!sharp_edges.is_empty() && sharp_edges[edge_i]) ||
vert_i == corner_verts[e2l[0]])
* or both face have opposed (flipped) normals, i.e. both corners on the same edge share
* the same vertex. */
if (!face_is_smooth(face_i) || (!sharp_edges.is_empty() && sharp_edges[edge]) ||
vert == corner_verts[e2l[0]])
{
/* NOTE: we are sure that loop != 0 here ;). */
/* NOTE: we are sure that corner != 0 here ;). */
e2l[1] = INDEX_INVALID;
}
else {
e2l[1] = loop_index;
e2l[1] = corner;
}
}
else if (!IS_EDGE_SHARP(e2l)) {
/* More than two loops using this edge, tag as sharp if not yet done. */
/* More than two corners using this edge, tag as sharp if not yet done. */
e2l[1] = INDEX_INVALID;
}
/* Else, edge is already 'disqualified' (i.e. sharp)! */
@@ -775,7 +774,7 @@ void edges_sharp_from_angle_set(const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int> corner_edges,
const Span<float3> face_normals,
const Span<int> loop_to_face,
const Span<int> corner_to_face,
const Span<bool> sharp_faces,
const float split_angle,
MutableSpan<bool> sharp_edges)
@@ -785,36 +784,36 @@ void edges_sharp_from_angle_set(const OffsetIndices<int> faces,
return;
}
/* Mapping edge -> loops. See #bke::mesh::normals_calc_loop for details. */
Array<int2> edge_to_loops(sharp_edges.size(), int2(0));
/* Mapping edge -> corners. See #bke::mesh::normals_calc_corners for details. */
Array<int2> edge_to_corners(sharp_edges.size(), int2(0));
mesh_edges_sharp_tag(faces,
corner_verts,
corner_edges,
loop_to_face,
corner_to_face,
face_normals,
sharp_faces,
sharp_edges,
split_angle,
edge_to_loops,
edge_to_corners,
sharp_edges);
}
static void loop_manifold_fan_around_vert_next(const Span<int> corner_verts,
const OffsetIndices<int> faces,
const Span<int> loop_to_face,
const int2 e2lfan_curr,
const int vert_pivot,
int *r_mlfan_curr_index,
int *r_mlfan_vert_index)
static void corner_manifold_fan_around_vert_next(const Span<int> corner_verts,
const OffsetIndices<int> faces,
const Span<int> corner_to_face,
const int2 e2lfan_curr,
const int vert_pivot,
int *r_mlfan_curr_index,
int *r_mlfan_vert_index)
{
const int mlfan_curr_orig = *r_mlfan_curr_index;
const int vert_fan_orig = corner_verts[mlfan_curr_orig];
/* WARNING: This is rather complex!
* We have to find our next edge around the vertex (fan mode).
* First we find the next loop, which is either previous or next to mlfan_curr_index, depending
* whether both loops using current edge are in the same direction or not, and whether
* First we find the next corner, which is either previous or next to mlfan_curr_index, depending
* whether both corners using current edge are in the same direction or not, and whether
* mlfan_curr_index actually uses the vertex we are fanning around!
* mlfan_curr_index is the index of mlfan_next here, and mlfan_next is not the real next one
* (i.e. not the future `mlfan_curr`). */
@@ -823,30 +822,30 @@ static void loop_manifold_fan_around_vert_next(const Span<int> corner_verts,
BLI_assert(*r_mlfan_curr_index >= 0);
const int vert_fan_next = corner_verts[*r_mlfan_curr_index];
const IndexRange face_fan_next = faces[loop_to_face[*r_mlfan_curr_index]];
const IndexRange face_fan_next = faces[corner_to_face[*r_mlfan_curr_index]];
if ((vert_fan_orig == vert_fan_next && vert_fan_orig == vert_pivot) ||
!ELEM(vert_fan_orig, vert_fan_next, vert_pivot))
{
/* We need the previous loop, but current one is our vertex's loop. */
/* We need the previous corner, but current one is our vertex's corner. */
*r_mlfan_vert_index = *r_mlfan_curr_index;
*r_mlfan_curr_index = face_corner_prev(face_fan_next, *r_mlfan_curr_index);
}
else {
/* We need the next loop, which is also our vertex's loop. */
/* We need the next corner, which is also our vertex's corner. */
*r_mlfan_curr_index = face_corner_next(face_fan_next, *r_mlfan_curr_index);
*r_mlfan_vert_index = *r_mlfan_curr_index;
}
}
static void lnor_space_for_single_fan(LoopSplitTaskDataCommon *common_data,
static void lnor_space_for_single_fan(CornerSplitTaskDataCommon *common_data,
const int ml_curr_index,
const int space_index)
{
const Span<int> loop_to_face = common_data->loop_to_face;
const Span<int> corner_to_face = common_data->corner_to_face;
const Span<float3> face_normals = common_data->face_normals;
MutableSpan<float3> loop_normals = common_data->loop_normals;
MutableSpan<float3> corner_normals = common_data->corner_normals;
loop_normals[ml_curr_index] = face_normals[loop_to_face[ml_curr_index]];
corner_normals[ml_curr_index] = face_normals[corner_to_face[ml_curr_index]];
if (CornerNormalSpaceArray *lnors_spacearr = common_data->lnors_spacearr) {
const Span<float3> positions = common_data->positions;
@@ -856,7 +855,7 @@ static void lnor_space_for_single_fan(LoopSplitTaskDataCommon *common_data,
const Span<int> corner_edges = common_data->corner_edges;
const Span<short2> clnors_data = common_data->clnors_data;
const int face_index = loop_to_face[ml_curr_index];
const int face_index = corner_to_face[ml_curr_index];
const int ml_prev_index = mesh::face_corner_prev(faces[face_index], ml_curr_index);
/* The vertex we are "fanning" around. */
@@ -867,13 +866,13 @@ static void lnor_space_for_single_fan(LoopSplitTaskDataCommon *common_data,
const float3 vec_curr = math::normalize(positions[vert_2] - positions[vert_pivot]);
const float3 vec_prev = math::normalize(positions[vert_3] - positions[vert_pivot]);
CornerNormalSpace &lnor_space = lnors_spacearr->spaces[space_index];
lnor_space = lnor_space_define(loop_normals[ml_curr_index], vec_curr, vec_prev, {});
CornerNormalSpace &space = lnors_spacearr->spaces[space_index];
space = corner_fan_space_define(corner_normals[ml_curr_index], vec_curr, vec_prev, {});
lnors_spacearr->corner_space_indices[ml_curr_index] = space_index;
if (!clnors_data.is_empty()) {
loop_normals[ml_curr_index] = lnor_space_custom_data_to_normal(lnor_space,
clnors_data[ml_curr_index]);
corner_normals[ml_curr_index] = corner_space_custom_data_to_normal(
space, clnors_data[ml_curr_index]);
}
if (!lnors_spacearr->corners_by_space.is_empty()) {
@@ -882,25 +881,25 @@ static void lnor_space_for_single_fan(LoopSplitTaskDataCommon *common_data,
}
}
static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data,
const int ml_curr_index,
const int space_index,
Vector<float3> *edge_vectors)
static void split_corner_normal_fan_do(CornerSplitTaskDataCommon *common_data,
const int ml_curr_index,
const int space_index,
Vector<float3> *edge_vectors)
{
CornerNormalSpaceArray *lnors_spacearr = common_data->lnors_spacearr;
MutableSpan<float3> loop_normals = common_data->loop_normals;
MutableSpan<float3> corner_normals = common_data->corner_normals;
const Span<float3> positions = common_data->positions;
const Span<int2> edges = common_data->edges;
const OffsetIndices faces = common_data->faces;
const Span<int> corner_verts = common_data->corner_verts;
const Span<int> corner_edges = common_data->corner_edges;
const Span<int2> edge_to_loops = common_data->edge_to_loops;
const Span<int> loop_to_face = common_data->loop_to_face;
const Span<int2> edge_to_corners = common_data->edge_to_corners;
const Span<int> corner_to_face = common_data->corner_to_face;
const Span<float3> face_normals = common_data->face_normals;
const Span<short2> clnors_data = common_data->clnors_data;
const int face_index = loop_to_face[ml_curr_index];
const int face_index = corner_to_face[ml_curr_index];
const int ml_prev_index = face_corner_prev(faces[face_index], ml_curr_index);
/* Sigh! we have to fan around current vertex, until we find the other non-smooth edge,
@@ -923,7 +922,8 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data,
Vector<int, 8> processed_corners;
/* `mlfan_vert_index` the loop of our current edge might not be the loop of our current vertex!
/* `mlfan_vert_index` the corner of our current edge might not be the corner of our current
* vertex!
*/
int mlfan_curr_index = ml_prev_index;
int mlfan_vert_index = ml_curr_index;
@@ -956,11 +956,12 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data,
vec_curr = math::normalize(positions[vert_2] - positions[vert_pivot]);
}
// printf("\thandling edge %d / loop %d\n", corner_edges[mlfan_curr_index], mlfan_curr_index);
// printf("\thandling edge %d / corner %d\n", corner_edges[mlfan_curr_index],
// mlfan_curr_index);
/* Code similar to accumulate_vertex_normals_poly_v3. */
/* Calculate angle between the two face edges incident on this vertex. */
lnor += face_normals[loop_to_face[mlfan_curr_index]] *
lnor += face_normals[corner_to_face[mlfan_curr_index]] *
math::safe_acos_approx(math::dot(vec_curr, vec_prev));
processed_corners.append(mlfan_vert_index);
@@ -978,7 +979,7 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data,
}
}
if (IS_EDGE_SHARP(edge_to_loops[corner_edges[mlfan_curr_index]]) || (edge == edge_orig)) {
if (IS_EDGE_SHARP(edge_to_corners[corner_edges[mlfan_curr_index]]) || (edge == edge_orig)) {
/* Current edge is sharp and we have finished with this fan of faces around this vert,
* or this vert is smooth, and we have completed a full turn around it. */
break;
@@ -986,14 +987,14 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data,
vec_prev = vec_curr;
/* Find next loop of the smooth fan. */
loop_manifold_fan_around_vert_next(corner_verts,
faces,
loop_to_face,
edge_to_loops[corner_edges[mlfan_curr_index]],
vert_pivot,
&mlfan_curr_index,
&mlfan_vert_index);
/* Find next corner of the smooth fan. */
corner_manifold_fan_around_vert_next(corner_verts,
faces,
corner_to_face,
edge_to_corners[corner_edges[mlfan_curr_index]],
vert_pivot,
&mlfan_curr_index,
&mlfan_vert_index);
}
float length;
@@ -1005,54 +1006,55 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data,
if (lnors_spacearr) {
if (UNLIKELY(length == 0.0f)) {
/* Use vertex normal as fallback! */
lnor = loop_normals[mlfan_vert_index];
lnor = corner_normals[mlfan_vert_index];
length = 1.0f;
}
CornerNormalSpace &lnor_space = lnors_spacearr->spaces[space_index];
lnor_space = lnor_space_define(lnor, vec_org, vec_curr, *edge_vectors);
lnor_space = corner_fan_space_define(lnor, vec_org, vec_curr, *edge_vectors);
lnors_spacearr->corner_space_indices.as_mutable_span().fill_indices(
processed_corners.as_span(), space_index);
edge_vectors->clear();
if (!clnors_data.is_empty()) {
clnors_avg /= processed_corners.size();
lnor = lnor_space_custom_data_to_normal(lnor_space, short2(clnors_avg));
lnor = corner_space_custom_data_to_normal(lnor_space, short2(clnors_avg));
}
}
/* In case we get a zero normal here, just use vertex normal already set! */
if (LIKELY(length != 0.0f)) {
/* Copy back the final computed normal into all related loop-normals. */
loop_normals.fill_indices(processed_corners.as_span(), lnor);
/* Copy back the final computed normal into all related corner-normals. */
corner_normals.fill_indices(processed_corners.as_span(), lnor);
}
}
/**
* Check whether given loop is part of an unknown-so-far cyclic smooth fan, or not.
* Check whether given corner is part of an unknown-so-far cyclic smooth fan, or not.
* Needed because cyclic smooth fans have no obvious 'entry point',
* and yet we need to walk them once, and only once.
*/
static bool loop_split_generator_check_cyclic_smooth_fan(const Span<int> corner_verts,
const Span<int> corner_edges,
const OffsetIndices<int> faces,
const Span<int2> edge_to_loops,
const Span<int> loop_to_face,
const int2 e2l_prev,
MutableBitSpan skip_loops,
const int ml_curr_index,
const int ml_prev_index)
static bool corner_split_generator_check_cyclic_smooth_fan(const Span<int> corner_verts,
const Span<int> corner_edges,
const OffsetIndices<int> faces,
const Span<int2> edge_to_corners,
const Span<int> corner_to_face,
const int2 e2l_prev,
MutableBitSpan skip_corners,
const int ml_curr_index,
const int ml_prev_index)
{
/* The vertex we are "fanning" around. */
const int vert_pivot = corner_verts[ml_curr_index];
int2 e2lfan_curr = e2l_prev;
if (IS_EDGE_SHARP(e2lfan_curr)) {
/* Sharp loop, so not a cyclic smooth fan. */
/* Sharp corner, so not a cyclic smooth fan. */
return false;
}
/* `mlfan_vert_index` the loop of our current edge might not be the loop of our current vertex!
/* `mlfan_vert_index` the corner of our current edge might not be the corner of our current
* vertex!
*/
int mlfan_curr_index = ml_prev_index;
int mlfan_vert_index = ml_curr_index;
@@ -1060,29 +1062,29 @@ static bool loop_split_generator_check_cyclic_smooth_fan(const Span<int> corner_
BLI_assert(mlfan_curr_index >= 0);
BLI_assert(mlfan_vert_index >= 0);
BLI_assert(!skip_loops[mlfan_vert_index]);
skip_loops[mlfan_vert_index].set();
BLI_assert(!skip_corners[mlfan_vert_index]);
skip_corners[mlfan_vert_index].set();
while (true) {
/* Find next loop of the smooth fan. */
loop_manifold_fan_around_vert_next(corner_verts,
faces,
loop_to_face,
e2lfan_curr,
vert_pivot,
&mlfan_curr_index,
&mlfan_vert_index);
/* Find next corner of the smooth fan. */
corner_manifold_fan_around_vert_next(corner_verts,
faces,
corner_to_face,
e2lfan_curr,
vert_pivot,
&mlfan_curr_index,
&mlfan_vert_index);
e2lfan_curr = edge_to_loops[corner_edges[mlfan_curr_index]];
e2lfan_curr = edge_to_corners[corner_edges[mlfan_curr_index]];
if (IS_EDGE_SHARP(e2lfan_curr)) {
/* Sharp loop/edge, so not a cyclic smooth fan. */
/* Sharp corner/edge, so not a cyclic smooth fan. */
return false;
}
/* Smooth loop/edge. */
if (skip_loops[mlfan_vert_index]) {
/* Smooth corner/edge. */
if (skip_corners[mlfan_vert_index]) {
if (mlfan_vert_index == ml_curr_index) {
/* We walked around a whole cyclic smooth fan without finding any already-processed loop,
/* We walked around a whole cyclic smooth fan without finding any already-processed corner,
* means we can use initial current / previous edge as start for this smooth fan. */
return true;
}
@@ -1091,27 +1093,27 @@ static bool loop_split_generator_check_cyclic_smooth_fan(const Span<int> corner_
}
/* We can skip it in future, and keep checking the smooth fan. */
skip_loops[mlfan_vert_index].set();
skip_corners[mlfan_vert_index].set();
}
}
static void loop_split_generator(LoopSplitTaskDataCommon *common_data,
Vector<int> &r_single_corners,
Vector<int> &r_fan_corners)
static void corner_split_generator(CornerSplitTaskDataCommon *common_data,
Vector<int> &r_single_corners,
Vector<int> &r_fan_corners)
{
const Span<int> corner_verts = common_data->corner_verts;
const Span<int> corner_edges = common_data->corner_edges;
const OffsetIndices faces = common_data->faces;
const Span<int> loop_to_face = common_data->loop_to_face;
const Span<int2> edge_to_loops = common_data->edge_to_loops;
const Span<int> corner_to_face = common_data->corner_to_face;
const Span<int2> edge_to_corners = common_data->edge_to_corners;
BitVector<> skip_loops(corner_verts.size(), false);
BitVector<> skip_corners(corner_verts.size(), false);
#ifdef DEBUG_TIME
SCOPED_TIMER_AVERAGED(__func__);
#endif
/* We now know edges that can be smoothed (with their vector, and their two loops),
/* We now know edges that can be smoothed (with their vector, and their two corners),
* and edges that will be hard! Now, time to generate the normals.
*/
for (const int face_index : faces.index_range()) {
@@ -1121,53 +1123,53 @@ static void loop_split_generator(LoopSplitTaskDataCommon *common_data,
const int ml_prev_index = mesh::face_corner_prev(face, ml_curr_index);
#if 0
printf("Checking loop %d / edge %u / vert %u (sharp edge: %d, skiploop: %d)",
printf("Checking corner %d / edge %u / vert %u (sharp edge: %d, skiploop: %d)",
ml_curr_index,
corner_edges[ml_curr_index],
corner_verts[ml_curr_index],
IS_EDGE_SHARP(edge_to_loops[corner_edges[ml_curr_index]]),
skip_loops[ml_curr_index]);
IS_EDGE_SHARP(edge_to_corners[corner_edges[ml_curr_index]]),
skip_corners[ml_curr_index]);
#endif
/* A smooth edge, we have to check for cyclic smooth fan case.
* If we find a new, never-processed cyclic smooth fan, we can do it now using that loop/edge
* as 'entry point', otherwise we can skip it. */
* If we find a new, never-processed cyclic smooth fan, we can do it now using that
* corner/edge as 'entry point', otherwise we can skip it. */
/* NOTE: In theory, we could make #loop_split_generator_check_cyclic_smooth_fan() store
/* NOTE: In theory, we could make #corner_split_generator_check_cyclic_smooth_fan() store
* mlfan_vert_index'es and edge indexes in two stacks, to avoid having to fan again around
* the vert during actual computation of `clnor` & `clnorspace`.
* However, this would complicate the code, add more memory usage, and despite its logical
* complexity, #loop_manifold_fan_around_vert_next() is quite cheap in term of CPU cycles,
* complexity, #corner_manifold_fan_around_vert_next() is quite cheap in term of CPU cycles,
* so really think it's not worth it. */
if (!IS_EDGE_SHARP(edge_to_loops[corner_edges[ml_curr_index]]) &&
(skip_loops[ml_curr_index] || !loop_split_generator_check_cyclic_smooth_fan(
corner_verts,
corner_edges,
faces,
edge_to_loops,
loop_to_face,
edge_to_loops[corner_edges[ml_prev_index]],
skip_loops,
ml_curr_index,
ml_prev_index)))
if (!IS_EDGE_SHARP(edge_to_corners[corner_edges[ml_curr_index]]) &&
(skip_corners[ml_curr_index] || !corner_split_generator_check_cyclic_smooth_fan(
corner_verts,
corner_edges,
faces,
edge_to_corners,
corner_to_face,
edge_to_corners[corner_edges[ml_prev_index]],
skip_corners,
ml_curr_index,
ml_prev_index)))
{
// printf("SKIPPING!\n");
}
else {
if (IS_EDGE_SHARP(edge_to_loops[corner_edges[ml_curr_index]]) &&
IS_EDGE_SHARP(edge_to_loops[corner_edges[ml_prev_index]]))
if (IS_EDGE_SHARP(edge_to_corners[corner_edges[ml_curr_index]]) &&
IS_EDGE_SHARP(edge_to_corners[corner_edges[ml_prev_index]]))
{
/* Simple case (both edges around that vertex are sharp in current face),
* this corner just takes its face normal. */
r_single_corners.append(ml_curr_index);
}
else {
/* We do not need to check/tag loops as already computed. Due to the fact that a loop
/* We do not need to check/tag corners as already computed. Due to the fact that a corner
* only points to one of its two edges, the same fan will never be walked more than once.
* Since we consider edges that have neighbor faces with inverted (flipped) normals as
* sharp, we are sure that no fan will be skipped, even only considering the case (sharp
* current edge, smooth previous edge), and not the alternative (smooth current edge,
* sharp previous edge). All this due/thanks to the link between normals and loop
* sharp previous edge). All this due/thanks to the link between normals and corner
* ordering (i.e. winding). */
r_fan_corners.append(ml_curr_index);
}
@@ -1176,35 +1178,35 @@ static void loop_split_generator(LoopSplitTaskDataCommon *common_data,
}
}
void normals_calc_loop(const Span<float3> vert_positions,
const Span<int2> edges,
const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int> corner_edges,
const Span<int> loop_to_face_map,
const Span<float3> vert_normals,
const Span<float3> face_normals,
const Span<bool> sharp_edges,
const Span<bool> sharp_faces,
const short2 *clnors_data,
CornerNormalSpaceArray *r_lnors_spacearr,
MutableSpan<float3> r_loop_normals)
void normals_calc_corners(const Span<float3> vert_positions,
const Span<int2> edges,
const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int> corner_edges,
const Span<int> corner_to_face_map,
const Span<float3> vert_normals,
const Span<float3> face_normals,
const Span<bool> sharp_edges,
const Span<bool> sharp_faces,
const short2 *clnors_data,
CornerNormalSpaceArray *r_lnors_spacearr,
MutableSpan<float3> r_corner_normals)
{
/**
* Mapping edge -> loops.
* If that edge is used by more than two loops (faces),
* Mapping edge -> corners.
* If that edge is used by more than two corners (faces),
* it is always sharp (and tagged as such, see below).
* We also use the second loop index as a kind of flag:
* We also use the second corner index as a kind of flag:
*
* - smooth edge: > 0.
* - sharp edge: < 0 (INDEX_INVALID || INDEX_UNSET).
* - unset: INDEX_UNSET.
*
* Note that currently we only have two values for second loop of sharp edges.
* However, if needed, we can store the negated value of loop index instead of INDEX_INVALID
* Note that currently we only have two values for second corner of sharp edges.
* However, if needed, we can store the negated value of corner index instead of INDEX_INVALID
* to retrieve the real value later in code).
* Note also that loose edges always have both values set to 0! */
Array<int2> edge_to_loops(edges.size(), int2(0));
Array<int2> edge_to_corners(edges.size(), int2(0));
CornerNormalSpaceArray _lnors_spacearr;
@@ -1218,31 +1220,31 @@ void normals_calc_loop(const Span<float3> vert_positions,
}
/* Init data common to all tasks. */
LoopSplitTaskDataCommon common_data;
CornerSplitTaskDataCommon common_data;
common_data.lnors_spacearr = r_lnors_spacearr;
common_data.loop_normals = r_loop_normals;
common_data.corner_normals = r_corner_normals;
common_data.clnors_data = {clnors_data, clnors_data ? corner_verts.size() : 0};
common_data.positions = vert_positions;
common_data.edges = edges;
common_data.faces = faces;
common_data.corner_verts = corner_verts;
common_data.corner_edges = corner_edges;
common_data.edge_to_loops = edge_to_loops;
common_data.loop_to_face = loop_to_face_map;
common_data.edge_to_corners = edge_to_corners;
common_data.corner_to_face = corner_to_face_map;
common_data.face_normals = face_normals;
common_data.vert_normals = vert_normals;
/* Pre-populate all loop normals as if their verts were all smooth.
/* Pre-populate all corner normals as if their verts were all smooth.
* This way we don't have to compute those later! */
array_utils::gather(vert_normals, corner_verts, r_loop_normals, 1024);
array_utils::gather(vert_normals, corner_verts, r_corner_normals, 1024);
/* This first loop check which edges are actually smooth, and compute edge vectors. */
build_edge_to_loop_map_with_flip_and_sharp(
faces, corner_verts, corner_edges, sharp_faces, sharp_edges, edge_to_loops);
/* This first corner check which edges are actually smooth, and compute edge vectors. */
build_edge_to_corner_map_with_flip_and_sharp(
faces, corner_verts, corner_edges, sharp_faces, sharp_edges, edge_to_corners);
Vector<int> single_corners;
Vector<int> fan_corners;
loop_split_generator(&common_data, single_corners, fan_corners);
corner_split_generator(&common_data, single_corners, fan_corners);
if (r_lnors_spacearr) {
r_lnors_spacearr->spaces.reinitialize(single_corners.size() + fan_corners.size());
@@ -1264,7 +1266,7 @@ void normals_calc_loop(const Span<float3> vert_positions,
for (const int i : range) {
const int corner = fan_corners[i];
const int space_index = single_corners.size() + i;
split_loop_nor_fan_do(&common_data, corner, space_index, &edge_vectors);
split_corner_normal_fan_do(&common_data, corner, space_index, &edge_vectors);
}
});
}
@@ -1276,90 +1278,91 @@ void normals_calc_loop(const Span<float3> vert_positions,
/**
* Compute internal representation of given custom normals (as an array of float[2]).
* It also makes sure the mesh matches those custom normals, by setting sharp edges flag as needed
* to get a same custom lnor for all loops sharing a same smooth fan.
* If use_vertices if true, r_custom_loop_normals is assumed to be per-vertex, not per-loop
* to get a same custom lnor for all corners sharing the same smooth fan.
* If use_vertices if true, r_custom_corner_normals is assumed to be per-vertex, not per-corner
* (this allows to set whole vert's normals at once, useful in some cases).
* r_custom_loop_normals is expected to have normalized normals, or zero ones,
* in which case they will be replaced by default loop/vertex normal.
* r_custom_corner_normals is expected to have normalized normals, or zero ones,
* in which case they will be replaced by default corner/vertex normal.
*/
static void mesh_normals_loop_custom_set(Span<float3> positions,
Span<int2> edges,
const OffsetIndices<int> faces,
Span<int> corner_verts,
Span<int> corner_edges,
Span<float3> vert_normals,
Span<float3> face_normals,
const Span<bool> sharp_faces,
const bool use_vertices,
MutableSpan<float3> r_custom_loop_normals,
MutableSpan<bool> sharp_edges,
MutableSpan<short2> r_clnors_data)
static void mesh_normals_corner_custom_set(const Span<float3> positions,
const Span<int2> edges,
const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int> corner_edges,
const Span<float3> vert_normals,
const Span<float3> face_normals,
const Span<bool> sharp_faces,
const bool use_vertices,
MutableSpan<float3> r_custom_corner_normals,
MutableSpan<bool> sharp_edges,
MutableSpan<short2> r_clnors_data)
{
/* We *may* make that poor #bke::mesh::normals_calc_loop() even more complex by making it
/* We *may* make that poor #bke::mesh::normals_calc_corners() even more complex by making it
* handling that feature too, would probably be more efficient in absolute. However, this
* function *is not* performance-critical, since it is mostly expected to be called by IO add-ons
* when importing custom normals, and modifier (and perhaps from some editing tools later?). So
* better to keep some simplicity here, and just call #bke::mesh::normals_calc_loop() twice! */
* better to keep some simplicity here, and just call #bke::mesh::normals_calc_corners() twice!
*/
CornerNormalSpaceArray lnors_spacearr;
lnors_spacearr.create_corners_by_space = true;
BitVector<> done_loops(corner_verts.size(), false);
Array<float3> loop_normals(corner_verts.size());
const Array<int> loop_to_face = build_loop_to_face_map(faces);
BitVector<> done_corners(corner_verts.size(), false);
Array<float3> corner_normals(corner_verts.size());
const Array<int> corner_to_face = build_corner_to_face_map(faces);
/* Compute current lnor spacearr. */
normals_calc_loop(positions,
edges,
faces,
corner_verts,
corner_edges,
loop_to_face,
vert_normals,
face_normals,
sharp_edges,
sharp_faces,
r_clnors_data.data(),
&lnors_spacearr,
loop_normals);
normals_calc_corners(positions,
edges,
faces,
corner_verts,
corner_edges,
corner_to_face,
vert_normals,
face_normals,
sharp_edges,
sharp_faces,
r_clnors_data.data(),
&lnors_spacearr,
corner_normals);
/* Set all given zero vectors to their default value. */
if (use_vertices) {
for (const int i : positions.index_range()) {
if (is_zero_v3(r_custom_loop_normals[i])) {
copy_v3_v3(r_custom_loop_normals[i], vert_normals[i]);
if (is_zero_v3(r_custom_corner_normals[i])) {
copy_v3_v3(r_custom_corner_normals[i], vert_normals[i]);
}
}
}
else {
for (const int i : corner_verts.index_range()) {
if (is_zero_v3(r_custom_loop_normals[i])) {
copy_v3_v3(r_custom_loop_normals[i], loop_normals[i]);
if (is_zero_v3(r_custom_corner_normals[i])) {
copy_v3_v3(r_custom_corner_normals[i], corner_normals[i]);
}
}
}
/* Now, check each current smooth fan (one lnor space per smooth fan!),
* and if all its matching custom loop_normals are not (enough) equal, add sharp edges as needed.
* This way, next time we run bke::mesh::normals_calc_loop(), we'll get lnor spacearr/smooth fans
* matching given custom loop_normals.
* Note this code *will never* unsharp edges! And quite obviously,
* when we set custom normals per vertices, running this is absolutely useless. */
* and if all its matching custom corner_normals are not (enough) equal, add sharp edges as
* needed. This way, next time we run bke::mesh::normals_calc_corners(), we'll get lnor
* spacearr/smooth fans matching given custom corner_normals. Note this code *will never* unsharp
* edges! And quite obviously, when we set custom normals per vertices, running this is
* absolutely useless. */
if (use_vertices) {
done_loops.fill(true);
done_corners.fill(true);
}
else {
for (const int i : corner_verts.index_range()) {
if (lnors_spacearr.corner_space_indices[i] == -1) {
/* This should not happen in theory, but in some rare case (probably ugly geometry)
* we can get some missing loopspacearr at this point. :/
* Maybe we should set those loops' edges as sharp? */
done_loops[i].set();
* Maybe we should set those corners' edges as sharp? */
done_corners[i].set();
if (G.debug & G_DEBUG) {
printf("WARNING! Getting invalid nullptr loop space for loop %d!\n", i);
printf("WARNING! Getting invalid nullptr corner space for corner %d!\n", i);
}
continue;
}
if (done_loops[i]) {
if (done_corners[i]) {
continue;
}
@@ -1367,16 +1370,16 @@ static void mesh_normals_loop_custom_set(Span<float3> positions,
const Span<int> fan_corners = lnors_spacearr.corners_by_space[space_index];
/* Notes:
* - In case of mono-loop smooth fan, we have nothing to do.
* - In case of mono-corner smooth fan, we have nothing to do.
* - Loops in this linklist are ordered (in reversed order compared to how they were
* discovered by bke::mesh::normals_calc_loop(), but this is not a problem).
* discovered by bke::mesh::normals_calc_corners(), but this is not a problem).
* Which means if we find a mismatching clnor,
* we know all remaining loops will have to be in a new, different smooth fan/lnor space.
* we know all remaining corners will have to be in a new, different smooth fan/lnor space.
* - In smooth fan case, we compare each clnor against a ref one,
* to avoid small differences adding up into a real big one in the end!
*/
if (fan_corners.is_empty()) {
done_loops[i].set();
done_corners[i].set();
continue;
}
@@ -1385,17 +1388,17 @@ static void mesh_normals_loop_custom_set(Span<float3> positions,
for (int i = fan_corners.index_range().last(); i >= 0; i--) {
const int lidx = fan_corners[i];
float *nor = r_custom_loop_normals[lidx];
float *nor = r_custom_corner_normals[lidx];
if (!org_nor) {
org_nor = nor;
}
else if (dot_v3v3(org_nor, nor) < LNOR_SPACE_TRIGO_THRESHOLD) {
/* Current normal differs too much from org one, we have to tag the edge between
* previous loop's face and current's one as sharp.
* We know those two loops do not point to the same edge,
* previous corner's face and current's one as sharp.
* We know those two corners do not point to the same edge,
* since we do not allow reversed winding in a same smooth fan. */
const IndexRange face = faces[loop_to_face[lidx]];
const IndexRange face = faces[corner_to_face[lidx]];
const int mlp = (lidx == face.start()) ? face.start() + face.size() - 1 : lidx - 1;
const int edge = corner_edges[lidx];
const int edge_p = corner_edges[mlp];
@@ -1406,19 +1409,19 @@ static void mesh_normals_loop_custom_set(Span<float3> positions,
}
prev_corner = lidx;
done_loops[lidx].set();
done_corners[lidx].set();
}
/* We also have to check between last and first loops,
/* We also have to check between last and first corners,
* otherwise we may miss some sharp edges here!
* This is just a simplified version of above while loop.
* See #45984. */
if (fan_corners.size() > 1 && org_nor) {
const int lidx = fan_corners.last();
float *nor = r_custom_loop_normals[lidx];
float *nor = r_custom_corner_normals[lidx];
if (dot_v3v3(org_nor, nor) < LNOR_SPACE_TRIGO_THRESHOLD) {
const IndexRange face = faces[loop_to_face[lidx]];
const IndexRange face = faces[corner_to_face[lidx]];
const int mlp = (lidx == face.start()) ? face.start() + face.size() - 1 : lidx - 1;
const int edge = corner_edges[lidx];
const int edge_p = corner_edges[mlp];
@@ -1428,34 +1431,33 @@ static void mesh_normals_loop_custom_set(Span<float3> positions,
}
}
/* And now, recompute our new auto `loop_normals` and lnor spacearr! */
normals_calc_loop(positions,
edges,
faces,
corner_verts,
corner_edges,
loop_to_face,
vert_normals,
face_normals,
sharp_edges,
sharp_faces,
r_clnors_data.data(),
&lnors_spacearr,
loop_normals);
/* And now, recompute our new auto `corner_normals` and lnor spacearr! */
normals_calc_corners(positions,
edges,
faces,
corner_verts,
corner_edges,
corner_to_face,
vert_normals,
face_normals,
sharp_edges,
sharp_faces,
r_clnors_data.data(),
&lnors_spacearr,
corner_normals);
}
/* And we just have to convert plain object-space custom normals to our
* lnor space-encoded ones. */
for (const int i : corner_verts.index_range()) {
if (lnors_spacearr.corner_space_indices[i] == -1) {
done_loops[i].reset();
done_corners[i].reset();
if (G.debug & G_DEBUG) {
printf("WARNING! Still getting invalid nullptr loop space in second loop for loop %d!\n",
i);
printf("WARNING! Still getting invalid nullptr corner space in second for loop %d!\n", i);
}
continue;
}
if (!done_loops[i]) {
if (!done_corners[i]) {
continue;
}
@@ -1467,77 +1469,77 @@ static void mesh_normals_loop_custom_set(Span<float3> positions,
* give rather huge differences in computed 2D factors). */
if (fan_corners.size() < 2) {
const int nidx = use_vertices ? corner_verts[i] : i;
r_clnors_data[i] = lnor_space_custom_normal_to_data(lnors_spacearr.spaces[space_index],
r_custom_loop_normals[nidx]);
done_loops[i].reset();
r_clnors_data[i] = corner_space_custom_normal_to_data(lnors_spacearr.spaces[space_index],
r_custom_corner_normals[nidx]);
done_corners[i].reset();
}
else {
float3 avg_nor(0.0f);
for (const int lidx : fan_corners) {
const int nidx = use_vertices ? corner_verts[lidx] : lidx;
avg_nor += r_custom_loop_normals[nidx];
done_loops[lidx].reset();
avg_nor += r_custom_corner_normals[nidx];
done_corners[lidx].reset();
}
mul_v3_fl(avg_nor, 1.0f / float(fan_corners.size()));
short2 clnor_data_tmp = lnor_space_custom_normal_to_data(lnors_spacearr.spaces[space_index],
avg_nor);
short2 clnor_data_tmp = corner_space_custom_normal_to_data(
lnors_spacearr.spaces[space_index], avg_nor);
r_clnors_data.fill_indices(fan_corners, clnor_data_tmp);
}
}
}
void normals_loop_custom_set(const Span<float3> vert_positions,
const Span<int2> edges,
const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int> corner_edges,
const Span<float3> vert_normals,
const Span<float3> face_normals,
const Span<bool> sharp_faces,
MutableSpan<bool> sharp_edges,
MutableSpan<float3> r_custom_loop_normals,
MutableSpan<short2> r_clnors_data)
void normals_corner_custom_set(const Span<float3> vert_positions,
const Span<int2> edges,
const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int> corner_edges,
const Span<float3> vert_normals,
const Span<float3> face_normals,
const Span<bool> sharp_faces,
MutableSpan<bool> sharp_edges,
MutableSpan<float3> r_custom_corner_normals,
MutableSpan<short2> r_clnors_data)
{
mesh_normals_loop_custom_set(vert_positions,
edges,
faces,
corner_verts,
corner_edges,
vert_normals,
face_normals,
sharp_faces,
false,
r_custom_loop_normals,
sharp_edges,
r_clnors_data);
mesh_normals_corner_custom_set(vert_positions,
edges,
faces,
corner_verts,
corner_edges,
vert_normals,
face_normals,
sharp_faces,
false,
r_custom_corner_normals,
sharp_edges,
r_clnors_data);
}
void normals_loop_custom_set_from_verts(const Span<float3> vert_positions,
const Span<int2> edges,
const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int> corner_edges,
const Span<float3> vert_normals,
const Span<float3> face_normals,
const Span<bool> sharp_faces,
MutableSpan<bool> sharp_edges,
MutableSpan<float3> r_custom_vert_normals,
MutableSpan<short2> r_clnors_data)
void normals_corner_custom_set_from_verts(const Span<float3> vert_positions,
const Span<int2> edges,
const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int> corner_edges,
const Span<float3> vert_normals,
const Span<float3> face_normals,
const Span<bool> sharp_faces,
MutableSpan<bool> sharp_edges,
MutableSpan<float3> r_custom_vert_normals,
MutableSpan<short2> r_clnors_data)
{
mesh_normals_loop_custom_set(vert_positions,
edges,
faces,
corner_verts,
corner_edges,
vert_normals,
face_normals,
sharp_faces,
true,
r_custom_vert_normals,
sharp_edges,
r_clnors_data);
mesh_normals_corner_custom_set(vert_positions,
edges,
faces,
corner_verts,
corner_edges,
vert_normals,
face_normals,
sharp_faces,
true,
r_custom_vert_normals,
sharp_edges,
r_clnors_data);
}
static void mesh_set_custom_normals(Mesh *mesh, float (*r_custom_nors)[3], const bool use_vertices)
@@ -1556,28 +1558,28 @@ static void mesh_set_custom_normals(Mesh *mesh, float (*r_custom_nors)[3], const
"sharp_edge", AttrDomain::Edge);
const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", AttrDomain::Face);
mesh_normals_loop_custom_set(mesh->vert_positions(),
mesh->edges(),
mesh->faces(),
mesh->corner_verts(),
mesh->corner_edges(),
mesh->vert_normals(),
mesh->face_normals(),
sharp_faces,
use_vertices,
{reinterpret_cast<float3 *>(r_custom_nors),
use_vertices ? mesh->verts_num : mesh->corners_num},
sharp_edges.span,
{clnors, mesh->corners_num});
mesh_normals_corner_custom_set(mesh->vert_positions(),
mesh->edges(),
mesh->faces(),
mesh->corner_verts(),
mesh->corner_edges(),
mesh->vert_normals(),
mesh->face_normals(),
sharp_faces,
use_vertices,
{reinterpret_cast<float3 *>(r_custom_nors),
use_vertices ? mesh->verts_num : mesh->corners_num},
sharp_edges.span,
{clnors, mesh->corners_num});
sharp_edges.finish();
}
} // namespace blender::bke::mesh
void BKE_mesh_set_custom_normals(Mesh *mesh, float (*r_custom_loop_normals)[3])
void BKE_mesh_set_custom_normals(Mesh *mesh, float (*r_custom_corner_normals)[3])
{
blender::bke::mesh::mesh_set_custom_normals(mesh, r_custom_loop_normals, false);
blender::bke::mesh::mesh_set_custom_normals(mesh, r_custom_corner_normals, false);
}
void BKE_mesh_set_custom_normals_from_verts(Mesh *mesh, float (*r_custom_vert_normals)[3])

View File

@@ -120,7 +120,7 @@ blender::Span<int> Mesh::corner_to_face_map() const
using namespace blender;
this->runtime->corner_to_face_map_cache.ensure([&](Array<int> &r_data) {
const OffsetIndices faces = this->faces();
r_data = bke::mesh::build_loop_to_face_map(faces);
r_data = bke::mesh::build_corner_to_face_map(faces);
});
return this->runtime->corner_to_face_map_cache.data();
}

View File

@@ -1062,7 +1062,7 @@ static void bm_mesh_loops_calc_normals_for_vert_without_clnors(
}
/**
* BMesh version of bke::mesh::normals_calc_loop() in `mesh_evaluate.cc`
* BMesh version of bke::mesh::normals_calc_corners() in `mesh_evaluate.cc`
* Will use first clnors_data array, and fallback to cd_loop_clnors_offset
* (use nullptr and -1 to not use clnors).
*
@@ -1413,7 +1413,7 @@ static bool bm_mesh_loops_split_lnor_fans(BMesh *bm,
/* Notes:
* * In case of mono-loop smooth fan, we have nothing to do.
* * Loops in this linklist are ordered (in reversed order compared to how they were
* discovered by bke::mesh::normals_calc_loop(), but this is not a problem).
* discovered by bke::mesh::normals_calc_corners(), but this is not a problem).
* Which means if we find a mismatching clnor,
* we know all remaining loops will have to be in a new, different smooth fan/lnor space.
* * In smooth fan case, we compare each clnor against a ref one,

View File

@@ -528,7 +528,7 @@ void split_edges(Mesh &mesh,
Array<int> edge_to_corner_offsets;
Array<int> edge_to_corner_indices;
const GroupedSpan<int> edge_to_corner_map = bke::mesh::build_edge_to_loop_map(
const GroupedSpan<int> edge_to_corner_map = bke::mesh::build_edge_to_corner_map(
mesh.corner_edges(), orig_edges.size(), edge_to_corner_offsets, edge_to_corner_indices);
Array<int> vert_to_edge_offsets;

View File

@@ -325,17 +325,17 @@ static void normalEditModifier_do_radial(NormalEditModifierData *enmd,
}
const bke::AttributeAccessor attributes = mesh->attributes();
const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", bke::AttrDomain::Face);
bke::mesh::normals_loop_custom_set(vert_positions,
edges,
faces,
corner_verts,
corner_edges,
mesh->vert_normals(),
mesh->face_normals(),
sharp_faces,
sharp_edges,
nos,
clnors);
bke::mesh::normals_corner_custom_set(vert_positions,
edges,
faces,
corner_verts,
corner_edges,
mesh->vert_normals(),
mesh->face_normals(),
sharp_faces,
sharp_edges,
nos,
clnors);
MEM_freeN(cos);
MEM_freeN(done_verts);
@@ -431,17 +431,17 @@ static void normalEditModifier_do_directional(NormalEditModifierData *enmd,
}
const bke::AttributeAccessor attributes = mesh->attributes();
const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", bke::AttrDomain::Face);
bke::mesh::normals_loop_custom_set(positions,
edges,
faces,
corner_verts,
corner_edges,
mesh->vert_normals(),
mesh->face_normals(),
sharp_faces,
sharp_edges,
nos,
clnors);
bke::mesh::normals_corner_custom_set(positions,
edges,
faces,
corner_verts,
corner_edges,
mesh->vert_normals(),
mesh->face_normals(),
sharp_faces,
sharp_edges,
nos,
clnors);
}
static bool is_valid_target(NormalEditModifierData *enmd)
@@ -515,19 +515,19 @@ static Mesh *normalEditModifier_do(NormalEditModifierData *enmd,
CustomData_get_layer_for_write(ldata, CD_CUSTOMLOOPNORMAL, corner_verts.size()));
loop_normals.reinitialize(corner_verts.size());
const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", bke::AttrDomain::Face);
blender::bke::mesh::normals_calc_loop(positions,
edges,
faces,
corner_verts,
corner_edges,
result->corner_to_face_map(),
result->vert_normals(),
result->face_normals(),
sharp_edges.span,
sharp_faces,
clnors,
nullptr,
loop_normals);
blender::bke::mesh::normals_calc_corners(positions,
edges,
faces,
corner_verts,
corner_edges,
result->corner_to_face_map(),
result->vert_normals(),
result->face_normals(),
sharp_edges.span,
sharp_faces,
clnors,
nullptr,
loop_normals);
}
if (clnors == nullptr) {

View File

@@ -221,19 +221,19 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd,
/* This will give us loop normal spaces,
* we do not actually care about computed loop_normals for now... */
loop_normals.reinitialize(corner_verts.size());
bke::mesh::normals_calc_loop(positions,
edges,
faces,
corner_verts,
corner_edges,
loop_to_face,
wn_data->vert_normals,
wn_data->face_normals,
wn_data->sharp_edges,
wn_data->sharp_faces,
has_clnors ? clnors.data() : nullptr,
&lnors_spacearr,
loop_normals);
bke::mesh::normals_calc_corners(positions,
edges,
faces,
corner_verts,
corner_edges,
loop_to_face,
wn_data->vert_normals,
wn_data->face_normals,
wn_data->sharp_edges,
wn_data->sharp_faces,
has_clnors ? clnors.data() : nullptr,
&lnors_spacearr,
loop_normals);
WeightedNormalDataAggregateItem start_item{};
start_item.curr_strength = FACE_STRENGTH_WEAK;
@@ -306,17 +306,17 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd,
}
}
blender::bke::mesh::normals_loop_custom_set(positions,
edges,
faces,
corner_verts,
corner_edges,
wn_data->vert_normals,
face_normals,
wn_data->sharp_faces,
wn_data->sharp_edges,
loop_normals,
clnors);
blender::bke::mesh::normals_corner_custom_set(positions,
edges,
faces,
corner_verts,
corner_edges,
wn_data->vert_normals,
face_normals,
wn_data->sharp_faces,
wn_data->sharp_edges,
loop_normals,
clnors);
}
else {
/* TODO: Ideally, we could add an option to `BKE_mesh_normals_loop_custom_[from_verts_]set()`
@@ -335,33 +335,33 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd,
copy_v3_v3(vert_normals[mv_index], items_data[mv_index].normal);
}
blender::bke::mesh::normals_loop_custom_set_from_verts(positions,
edges,
faces,
corner_verts,
corner_edges,
wn_data->vert_normals,
face_normals,
wn_data->sharp_faces,
wn_data->sharp_edges,
vert_normals,
clnors);
blender::bke::mesh::normals_corner_custom_set_from_verts(positions,
edges,
faces,
corner_verts,
corner_edges,
wn_data->vert_normals,
face_normals,
wn_data->sharp_faces,
wn_data->sharp_edges,
vert_normals,
clnors);
}
else {
loop_normals.reinitialize(corner_verts.size());
blender::bke::mesh::normals_calc_loop(positions,
edges,
faces,
corner_verts,
corner_edges,
loop_to_face,
wn_data->vert_normals,
face_normals,
wn_data->sharp_edges,
wn_data->sharp_faces,
has_clnors ? clnors.data() : nullptr,
nullptr,
loop_normals);
blender::bke::mesh::normals_calc_corners(positions,
edges,
faces,
corner_verts,
corner_edges,
loop_to_face,
wn_data->vert_normals,
face_normals,
wn_data->sharp_edges,
wn_data->sharp_faces,
has_clnors ? clnors.data() : nullptr,
nullptr,
loop_normals);
for (int corner = 0; corner < corner_verts.size(); corner++) {
const int item_index = corner_verts[corner];
@@ -369,17 +369,17 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd,
copy_v3_v3(loop_normals[corner], items_data[item_index].normal);
}
}
blender::bke::mesh::normals_loop_custom_set(positions,
edges,
faces,
corner_verts,
corner_edges,
wn_data->vert_normals,
face_normals,
wn_data->sharp_faces,
wn_data->sharp_edges,
loop_normals,
clnors);
blender::bke::mesh::normals_corner_custom_set(positions,
edges,
faces,
corner_verts,
corner_edges,
wn_data->vert_normals,
face_normals,
wn_data->sharp_faces,
wn_data->sharp_edges,
loop_normals,
clnors);
}
}
}

View File

@@ -53,7 +53,7 @@ class CornersOfEdgeInput final : public bke::MeshFieldInput {
Array<int> map_offsets;
Array<int> map_indices;
const Span<int> corner_edges = mesh.corner_edges();
const GroupedSpan<int> edge_to_loop_map = bke::mesh::build_edge_to_loop_map(
const GroupedSpan<int> edge_to_loop_map = bke::mesh::build_edge_to_corner_map(
mesh.corner_edges(), mesh.edges_num, map_offsets, map_indices);
const bke::MeshFieldContext context{mesh, domain};

View File

@@ -297,7 +297,7 @@ class TextureMarginMap {
void build_tables()
{
loop_to_face_map_ = blender::bke::mesh::build_loop_to_face_map(faces_);
loop_to_face_map_ = blender::bke::mesh::build_corner_to_face_map(faces_);
loop_adjacency_map_.resize(corner_edges_.size(), -1);