Blender V5.0
mesh_normals.cc File Reference
#include <climits>
#include "BLI_math_geom.h"
#include "BLI_math_vector.h"
#include "BLI_array_utils.hh"
#include "BLI_bit_vector.hh"
#include "BLI_enumerable_thread_specific.hh"
#include "BLI_linklist.h"
#include "BLI_math_base.hh"
#include "BLI_math_vector.hh"
#include "BLI_memarena.h"
#include "BLI_span.hh"
#include "BLI_task.hh"
#include "BLI_utildefines.h"
#include "BKE_attribute.hh"
#include "BKE_global.hh"
#include "BKE_mesh.hh"
#include "BKE_mesh_mapping.hh"

Go to the source code of this file.

Classes

struct  blender::bke::mesh::VertCornerInfo
struct  blender::bke::mesh::EdgeOneCorner
struct  blender::bke::mesh::EdgeTwoCorners
struct  blender::bke::mesh::EdgeSharp
struct  blender::bke::mesh::CornerSpaceGroup

Namespaces

namespace  blender
namespace  blender::bke
namespace  blender::bke::mesh

Macros

#define INDEX_UNSET   INT_MIN
#define INDEX_INVALID   -1
#define IS_EDGE_SHARP(_e2l)

Typedefs

using blender::bke::mesh::EdgeUninitialized = std::monostate
using blender::bke::mesh::VertEdgeInfo = std::variant<EdgeUninitialized, EdgeOneCorner, EdgeTwoCorners, EdgeSharp>
using blender::bke::mesh::LocalEdgeVectorSet

Functions

static void blender::bke::mesh::mix_normals_corner_to_vert (const Span< float3 > vert_positions, const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const Span< float3 > corner_normals, MutableSpan< float3 > vert_normals)
static void blender::bke::mesh::mix_normals_vert_to_face (const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< float3 > vert_normals, MutableSpan< float3 > face_normals)
static void blender::bke::mesh::mix_normals_corner_to_face (const OffsetIndices< int > faces, const Span< float3 > corner_normals, MutableSpan< float3 > face_normals)
static CornerNormalSpace blender::bke::mesh::corner_fan_space_define (const float3 &lnor, const float3 &vec_ref, const float3 &vec_other, const Span< float3 > edge_vectors)
static float3 blender::bke::mesh::corner_space_custom_data_to_normal (const CornerNormalSpace &lnor_space, const short2 clnor_data)
static void blender::bke::mesh::mesh_edges_sharp_tag (const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< int > corner_edges, 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_corners, MutableSpan< bool > r_sharp_edges)
static void blender::bke::mesh::collect_corner_info (const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< int > vert_faces, const int vert, MutableSpan< VertCornerInfo > r_corner_infos)
static void blender::bke::mesh::add_corner_to_edge (const Span< int > corner_edges, const Span< bool > sharp_edges, const int local_corner, const int corner, const int other_corner, const bool winding_torwards_vert, VertEdgeInfo &info)
static void blender::bke::mesh::calc_local_edge_indices (MutableSpan< VertCornerInfo > corner_infos, LocalEdgeVectorSet &r_other_vert_to_edge)
static void blender::bke::mesh::calc_connecting_edge_info (const Span< int > corner_edges, const Span< bool > sharp_edges, const Span< bool > sharp_faces, const Span< VertCornerInfo > corner_infos, MutableSpan< VertEdgeInfo > edge_infos)
static void blender::bke::mesh::traverse_fan_local_corners (const Span< VertCornerInfo > corner_infos, const Span< VertEdgeInfo > edge_infos, const int start_local_corner, Vector< int, 16 > &result_fan)
static void blender::bke::mesh::calc_edge_directions (const Span< float3 > vert_positions, const Span< int > local_edge_by_vert, const float3 &vert_position, MutableSpan< float3 > edge_dirs)
static float3 blender::bke::mesh::accumulate_fan_normal (const Span< VertCornerInfo > corner_infos, const Span< float3 > edge_dirs, const Span< float3 > face_normals, const Span< int > local_corners_in_fan)
static BLI_NOINLINE void blender::bke::mesh::handle_fan_result_and_custom_normals (const Span< short2 > custom_normals, const Span< VertCornerInfo > corner_infos, const Span< float3 > edge_dirs, const Span< int > local_corners_in_fan, float3 &fan_normal, CornerNormalSpaceArray *r_fan_spaces, Vector< CornerSpaceGroup, 0 > *r_local_space_groups)
static void blender::bke::mesh::mesh_normals_corner_custom_set (const Span< float3 > positions, const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< int > corner_edges, const GroupedSpan< int > vert_to_face_map, 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)
static void blender::bke::mesh::mesh_set_custom_normals (Mesh &mesh, MutableSpan< float3 > r_custom_nors, const bool use_vertices)
static void blender::bke::normalize_vecs (MutableSpan< float3 > normals)
void blender::bke::mesh_set_custom_normals (Mesh &mesh, MutableSpan< float3 > corner_normals)
void blender::bke::mesh_set_custom_normals_normalized (Mesh &mesh, MutableSpan< float3 > corner_normals)
void blender::bke::mesh_set_custom_normals_from_verts (Mesh &mesh, MutableSpan< float3 > vert_normals)
void blender::bke::mesh_set_custom_normals_from_verts_normalized (Mesh &mesh, MutableSpan< float3 > vert_normals)
static bke::AttrDomain blender::bke::mesh::normal_domain_to_domain (bke::MeshNormalDomain domain)
Topology Queries
void blender::bke::mesh_vert_normals_assign (Mesh &mesh, Span< float3 > vert_normals)
void blender::bke::mesh_vert_normals_assign (Mesh &mesh, Vector< float3 > vert_normals)
Public Utility Functions

Related to managing normals but not directly related to calculating normals.

bool BKE_mesh_vert_normals_are_dirty (const Mesh *mesh)
bool BKE_mesh_face_normals_are_dirty (const Mesh *mesh)
Mesh Normal Calculation (Polygons)
static float3 blender::bke::mesh::normal_calc_ngon (const Span< float3 > vert_positions, const Span< int > face_verts)
Polygon Data Evaluation
float3 blender::bke::mesh::face_normal_calc (Span< float3 > vert_positions, Span< int > face_verts)
Medium-Level Normals Calculation
void blender::bke::mesh::normals_calc_faces (Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, MutableSpan< float3 > face_normals)
void blender::bke::mesh::normals_calc_verts (Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face_map, Span< float3 > face_normals, MutableSpan< float3 > vert_normals)
Face Corner Normal Calculation
short2 blender::bke::mesh::corner_space_custom_normal_to_data (const CornerNormalSpace &lnor_space, const float3 &custom_lnor)
void blender::bke::mesh::edges_sharp_from_angle_set (OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, Span< float3 > face_normals, Span< int > corner_to_face, Span< bool > sharp_faces, const float split_angle, MutableSpan< bool > sharp_edges)
void blender::bke::mesh::normals_calc_corners (Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, GroupedSpan< int > vert_to_face_map, Span< float3 > face_normals, Span< bool > sharp_edges, Span< bool > sharp_faces, Span< short2 > custom_normals, CornerNormalSpaceArray *r_fan_spaces, MutableSpan< float3 > r_corner_normals)
void blender::bke::mesh::normals_corner_custom_set (Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, GroupedSpan< int > vert_to_face_map, 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)
void blender::bke::mesh::normals_corner_custom_set_from_verts (Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, GroupedSpan< int > vert_to_face_map, 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)
bool blender::bke::mesh::is_corner_fan_normals (const AttributeMetaData &meta_data)

Variables

constexpr AttributeMetaData blender::bke::mesh::CORNER_FAN_META_DATA {AttrDomain::Corner, AttrType::Int16_2D}

Mesh Normal Calculation

#define LNOR_SPACE_TRIGO_THRESHOLD   (1.0f - 1e-4f)
void BKE_lnor_spacearr_init (MLoopNorSpaceArray *lnors_spacearr, const int numLoops, const char data_type)
void BKE_lnor_spacearr_tls_init (MLoopNorSpaceArray *lnors_spacearr, MLoopNorSpaceArray *lnors_spacearr_tls)
void BKE_lnor_spacearr_tls_join (MLoopNorSpaceArray *lnors_spacearr, MLoopNorSpaceArray *lnors_spacearr_tls)
void BKE_lnor_spacearr_clear (MLoopNorSpaceArray *lnors_spacearr)
void BKE_lnor_spacearr_free (MLoopNorSpaceArray *lnors_spacearr)
MLoopNorSpaceBKE_lnor_space_create (MLoopNorSpaceArray *lnors_spacearr)
void BKE_lnor_space_define (MLoopNorSpace *lnor_space, const float lnor[3], const float vec_ref[3], const float vec_other[3], const blender::Span< blender::float3 > edge_vectors)
void BKE_lnor_space_add_loop (MLoopNorSpaceArray *lnors_spacearr, MLoopNorSpace *lnor_space, const int corner, void *bm_loop, const bool is_single)
MINLINE float unit_short_to_float (const short val)
MINLINE short unit_float_to_short (const float val)
void BKE_lnor_space_custom_data_to_normal (const MLoopNorSpace *lnor_space, const short clnor_data[2], float r_custom_lnor[3])
void BKE_lnor_space_custom_normal_to_data (const MLoopNorSpace *lnor_space, const float custom_lnor[3], short r_clnor_data[2])

Detailed Description

Mesh normal calculation functions.

See also
bmesh_mesh_normals.cc for the equivalent BMesh functionality.

Definition in file mesh_normals.cc.

Macro Definition Documentation

◆ INDEX_INVALID

#define INDEX_INVALID   -1

Definition at line 830 of file mesh_normals.cc.

Referenced by blender::bke::mesh::mesh_edges_sharp_tag().

◆ INDEX_UNSET

#define INDEX_UNSET   INT_MIN

◆ IS_EDGE_SHARP

#define IS_EDGE_SHARP ( _e2l)
Value:
#define ELEM(...)
#define INDEX_INVALID
#define INDEX_UNSET

Definition at line 832 of file mesh_normals.cc.

Referenced by blender::bke::mesh::mesh_edges_sharp_tag().

◆ LNOR_SPACE_TRIGO_THRESHOLD

Function Documentation

◆ BKE_lnor_space_add_loop()

void BKE_lnor_space_add_loop ( MLoopNorSpaceArray * lnors_spacearr,
MLoopNorSpace * lnor_space,
int corner,
void * bm_loop,
bool is_single )

Add a new given loop to given lnor_space. Depending on lnor_space->data_type, we expect bm_loop to be a pointer to BMLoop struct (in case of BMLOOP_PTR), or nullptr (in case of LOOP_INDEX), loop index is then stored in pointer. If is_single is set, the BMLoop or loop index is directly stored in lnor_space->loops pointer (since there is only one loop in this fan), else it is added to the linked list of loops in the fan.

Definition at line 672 of file mesh_normals.cc.

References BLI_assert, BLI_linklist_prepend_nlink(), MLoopNorSpaceArray::data_type, MLoopNorSpace::flags, MLoopNorSpace::loops, MLoopNorSpaceArray::loops_pool, MLoopNorSpaceArray::lspacearr, MLNOR_SPACE_IS_SINGLE, MLNOR_SPACEARR_BMLOOP_PTR, MLNOR_SPACEARR_LOOP_INDEX, and POINTER_FROM_INT.

Referenced by bm_mesh_loops_calc_normals_for_loop().

◆ BKE_lnor_space_create()

MLoopNorSpace * BKE_lnor_space_create ( MLoopNorSpaceArray * lnors_spacearr)

◆ BKE_lnor_space_custom_data_to_normal()

◆ BKE_lnor_space_custom_normal_to_data()

◆ BKE_lnor_space_define()

void BKE_lnor_space_define ( MLoopNorSpace * lnor_space,
const float lnor[3],
const float vec_ref[3],
const float vec_other[3],
blender::Span< blender::float3 > edge_vectors )

◆ BKE_lnor_spacearr_clear()

◆ BKE_lnor_spacearr_free()

◆ BKE_lnor_spacearr_init()

◆ BKE_lnor_spacearr_tls_init()

void BKE_lnor_spacearr_tls_init ( MLoopNorSpaceArray * lnors_spacearr,
MLoopNorSpaceArray * lnors_spacearr_tls )

Utility for multi-threaded calculation that ensures lnors_spacearr_tls doesn't share memory with lnors_spacearr that would cause it not to be thread safe.

Note
This works as long as threads never operate on the same loops at once.

Definition at line 544 of file mesh_normals.cc.

References BLI_memarena_new(), BLI_MEMARENA_STD_BUFSIZE, and MLoopNorSpaceArray::mem.

Referenced by bm_mesh_loops_calc_normals_for_vert_init_fn().

◆ BKE_lnor_spacearr_tls_join()

void BKE_lnor_spacearr_tls_join ( MLoopNorSpaceArray * lnors_spacearr,
MLoopNorSpaceArray * lnors_spacearr_tls )

Utility for multi-threaded calculation that merges lnors_spacearr_tls into lnors_spacearr.

Definition at line 551 of file mesh_normals.cc.

References BKE_lnor_spacearr_clear(), BLI_assert, BLI_memarena_free(), BLI_memarena_merge(), MLoopNorSpaceArray::data_type, MLoopNorSpaceArray::mem, and MLoopNorSpaceArray::spaces_num.

Referenced by bm_mesh_loops_calc_normals_for_vert_reduce_fn().

◆ BKE_mesh_face_normals_are_dirty()

bool BKE_mesh_face_normals_are_dirty ( const Mesh * mesh)

Return true if the mesh face normals either are not stored or are dirty.

Definition at line 101 of file mesh_normals.cc.

References Mesh::runtime.

Referenced by C_BVHTree_FromObject(), and mesh_calc_tri_tessface().

◆ BKE_mesh_vert_normals_are_dirty()

bool BKE_mesh_vert_normals_are_dirty ( const Mesh * mesh)

Return true if the mesh vertex normals either are not stored or are dirty.

Definition at line 96 of file mesh_normals.cc.

References Mesh::runtime.

Referenced by arrayModifier_doArray().

◆ unit_float_to_short()

MINLINE short unit_float_to_short ( const float val)

Definition at line 701 of file mesh_normals.cc.

References floorf, and MINLINE.

Referenced by blender::bke::mesh::corner_space_custom_normal_to_data().

◆ unit_short_to_float()

MINLINE float unit_short_to_float ( const short val)

Definition at line 696 of file mesh_normals.cc.

References float, and MINLINE.

Referenced by blender::bke::mesh::corner_space_custom_data_to_normal().