Blender V5.3
BKE_mesh.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include "BLI_index_mask_fwd.hh"
13#include "BLI_offset_indices.hh"
14#include "BLI_string_ref.hh"
15
16#include "BKE_mesh.h" // IWYU pragma: export
17#include "BKE_mesh_types.hh" // IWYU pragma: export
18
19namespace blender::bke {
20
21enum class AttrDomain : int8_t;
22enum class AttrType : int16_t;
25
26struct AttributeFilter;
27
28namespace mesh {
29/* -------------------------------------------------------------------- */
32
34float3 face_normal_calc(Span<float3> vert_positions, Span<int> face_verts);
35
36void corner_tris_calc(Span<float3> vert_positions,
38 Span<int> corner_verts,
39 MutableSpan<int3> corner_tris);
40
50 Span<int> corner_verts,
51 Span<float3> face_normals,
52 MutableSpan<int3> corner_tris);
53
55
60 Span<int3> corner_tris,
61 MutableSpan<int3> vert_tris);
62
65 Span<int> corner_verts,
66 Span<int> corner_edges,
67 const int3 &corner_tri);
68
70float3 face_center_calc(Span<float3> vert_positions, Span<int> face_verts);
71
73float face_area_calc(Span<float3> vert_positions, Span<int> face_verts);
74
76void face_angles_calc(Span<float3> vert_positions,
77 Span<int> face_verts,
78 MutableSpan<float> angles);
79
81
82/* -------------------------------------------------------------------- */
85
92void normals_calc_faces(Span<float3> vert_positions,
94 Span<int> corner_verts,
95 MutableSpan<float3> face_normals);
96
106void normals_calc_verts(Span<float3> vert_positions,
107 OffsetIndices<int> faces,
108 Span<int> corner_verts,
109 GroupedSpan<int> vert_to_face_map,
110 Span<float3> face_normals,
111 MutableSpan<float3> vert_normals);
112
114
115/* -------------------------------------------------------------------- */
118
152
181
183 const float3 &custom_lnor);
184
196void normals_calc_corners(Span<float3> vert_positions,
197 OffsetIndices<int> faces,
198 Span<int> corner_verts,
199 Span<int> corner_edges,
200 GroupedSpan<int> vert_to_face_map,
201 Span<float3> face_normals,
202 Span<bool> sharp_edges,
203 Span<bool> sharp_faces,
204 Span<short2> custom_normals,
205 CornerNormalSpaceArray *r_fan_spaces,
206 MutableSpan<float3> r_corner_normals);
207
211void normals_corner_custom_set(Span<float3> vert_positions,
212 OffsetIndices<int> faces,
213 Span<int> corner_verts,
214 Span<int> corner_edges,
215 GroupedSpan<int> vert_to_face_map,
216 Span<float3> vert_normals,
217 Span<float3> face_normals,
218 Span<bool> sharp_faces,
219 MutableSpan<bool> sharp_edges,
220 MutableSpan<float3> r_custom_corner_normals,
221 MutableSpan<short2> r_clnors_data);
222
227 OffsetIndices<int> faces,
228 Span<int> corner_verts,
229 Span<int> corner_edges,
230 GroupedSpan<int> vert_to_face_map,
231 Span<float3> vert_normals,
232 Span<float3> face_normals,
233 Span<bool> sharp_faces,
234 MutableSpan<bool> sharp_edges,
235 MutableSpan<float3> r_custom_vert_normals,
236 MutableSpan<short2> r_clnors_data);
237
247 Span<int> corner_verts,
248 Span<int> corner_edges,
249 Span<float3> face_normals,
250 Span<int> corner_to_face,
251 Span<bool> sharp_faces,
252 const float split_angle,
253 MutableSpan<bool> sharp_edges);
254
256bool is_corner_fan_normals(const AttributeMetaData &meta_data);
257
260 public:
261 enum class Output : int8_t { None, CornerFan, Free };
263 std::optional<bke::AttrDomain> result_domain;
264
267 void add_domain(bke::AttrDomain domain);
269 void add_mesh(const Mesh &mesh);
270};
271
272} // namespace mesh
273
283
293
295
296/* -------------------------------------------------------------------- */
299
300namespace mesh {
301
306inline int face_corner_prev(const IndexRange face, const int corner)
307{
308 return corner - 1 + (corner == face.start()) * face.size();
309}
310
315inline int face_corner_next(const IndexRange face, const int corner)
316{
317 if (corner == face.last()) {
318 return face.start();
319 }
320 return corner + 1;
321}
322
328 const Span<int> corner_verts,
329 const int vert)
330{
331 return face[corner_verts.slice(face).first_index(vert)];
332}
333
339 const Span<int> corner_verts,
340 const int vert)
341{
342 const int corner = face_find_corner_from_vert(face, corner_verts, vert);
343 return {corner_verts[face_corner_prev(face, corner)],
344 corner_verts[face_corner_next(face, corner)]};
345}
346
350inline int face_triangles_num(const int face_size)
351{
352 BLI_assert(face_size > 2);
353 return face_size - 2;
354}
355
360{
361 const IndexRange face = faces[face_i];
362 /* This is the same as #poly_to_tri_count which is not included here. */
363 const int start_triangle = face.start() - face_i * 2;
364 return IndexRange(start_triangle, face_triangles_num(face.size()));
365}
366
370inline int edge_other_vert(const int2 edge, const int vert)
371{
372 BLI_assert(ELEM(vert, edge[0], edge[1]));
373 BLI_assert(edge[0] >= 0);
374 BLI_assert(edge[1] >= 0);
375 /* Order is important to avoid overflow. */
376 return (edge[0] - vert) + edge[1];
377}
378
380
382bool is_uv_map(const AttributeMetaData &meta_data);
383bool is_uv_map(const std::optional<AttributeMetaData> &meta_data);
384
386bool is_color_attribute(const AttributeMetaData &meta_data);
387bool is_color_attribute(const std::optional<AttributeMetaData> &meta_data);
388
389} // namespace mesh
390
392Mesh *mesh_new_no_attributes(int verts_num, int edges_num, int faces_num, int corners_num);
393
395void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges);
396
398 bool keep_existing_edges,
399 bool select_new_edges,
400 const AttributeFilter &attribute_filter);
401
402void mesh_translate(Mesh &mesh, const float3 &translation, bool do_shape_keys);
403
404void mesh_transform(Mesh &mesh, const float4x4 &transform, bool do_shape_keys);
405
406void mesh_flip_faces(Mesh &mesh, const IndexMask &selection);
407
409
412
415
416void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges = false);
417void mesh_sharp_edges_set_from_angle(Mesh &mesh, float angle, bool keep_sharp_edges = false);
418
423void mesh_edge_hide_from_vert(Span<int2> edges, Span<bool> hide_vert, MutableSpan<bool> hide_edge);
424
425/* Hide faces when any of their vertices are hidden. */
427 Span<int> corner_verts,
428 Span<bool> hide_vert,
429 MutableSpan<bool> hide_poly);
430
435
442
445 StringRef name,
446 AttrDomain domain,
447 bke::AttrType data_type);
449 StringRef name,
450 AttrDomain domain,
451 bke::AttrType data_type);
452
457
458void mesh_data_update(Depsgraph &depsgraph,
459 const Scene &scene,
460 Object &ob,
461 const CustomData_MeshMasks &dataMask);
462
465
476bool mesh_validate(Mesh &mesh, bool verbose = false, bool allow_missing_edges = false);
477
482bool mesh_is_valid(const Mesh &mesh, bool verbose = true);
483
489
495
505 Span<int> corner_verts,
506 const IndexMask &mask,
507 IndexMaskMemory &memory,
508 MutableSpan<int> r_originals = {});
509
512
513} // namespace blender::bke
#define ELEM(...)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr int64_t start() const
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t first_index(const T &search_value) const
Definition BLI_span.hh:381
std::optional< bke::AttrDomain > result_domain
Definition BKE_mesh.hh:263
void add_domain(bke::AttrDomain domain)
void add_free_normals(bke::AttrDomain domain)
void add_no_custom_normals(bke::MeshNormalDomain domain)
int edge_other_vert(const int2 edge, const int vert)
Definition BKE_mesh.hh:370
void face_angles_calc(Span< float3 > vert_positions, Span< int > face_verts, MutableSpan< float > angles)
float3 face_normal_calc(Span< float3 > vert_positions, Span< int > face_verts)
float face_area_calc(Span< float3 > vert_positions, Span< int > face_verts)
int face_corner_prev(const IndexRange face, const int corner)
Definition BKE_mesh.hh:306
int face_triangles_num(const int face_size)
Definition BKE_mesh.hh:350
int face_find_corner_from_vert(const IndexRange face, const Span< int > corner_verts, const int vert)
Definition BKE_mesh.hh:327
bool is_uv_map(const AttributeMetaData &meta_data)
void corner_tris_calc(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, MutableSpan< int3 > corner_tris)
int3 corner_tri_get_real_edges(Span< int2 > edges, Span< int > corner_verts, Span< int > corner_edges, const int3 &corner_tri)
int2 face_find_adjacent_verts(const IndexRange face, const Span< int > corner_verts, const int vert)
Definition BKE_mesh.hh:338
bool is_corner_fan_normals(const AttributeMetaData &meta_data)
void 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 vert_tris_from_corner_tris(Span< int > corner_verts, Span< int3 > corner_tris, MutableSpan< int3 > vert_tris)
void 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)
IndexRange face_triangles_range(OffsetIndices< int > faces, int face_i)
Definition BKE_mesh.hh:359
float3 face_center_calc(Span< float3 > vert_positions, Span< int > face_verts)
void corner_tris_calc_with_normals(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< float3 > face_normals, MutableSpan< int3 > corner_tris)
void corner_tris_calc_face_indices(OffsetIndices< int > faces, MutableSpan< int > tri_faces)
short2 corner_space_custom_normal_to_data(const CornerNormalSpace &lnor_space, const float3 &custom_lnor)
void 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)
bool is_color_attribute(const AttributeMetaData &meta_data)
int face_corner_next(const IndexRange face, const int corner)
Definition BKE_mesh.hh:315
void 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)
void 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 normals_calc_faces(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, MutableSpan< float3 > face_normals)
void mesh_select_vert_flush(Mesh &mesh)
bool mesh_validate_material_indices(Mesh &mesh)
void mesh_sharp_edges_set_from_angle(Mesh &mesh, float angle, bool keep_sharp_edges=false)
IndexMask mesh_find_faces_duplicate_verts(const Mesh &mesh, IndexMaskMemory &memory)
const AttributeAccessorFunctions & mesh_attribute_accessor_functions()
void mesh_face_hide_from_vert(OffsetIndices< int > faces, Span< int > corner_verts, Span< bool > hide_vert, MutableSpan< bool > hide_poly)
void mesh_hide_vert_flush(Mesh &mesh)
bool mesh_is_valid(const Mesh &mesh, bool verbose=true)
void mesh_remove_invalid_attribute_strings(Mesh &mesh)
void mesh_hide_face_flush(Mesh &mesh)
IndexMask find_duplicate_faces(OffsetIndices< int > faces, Span< int > corner_verts, const IndexMask &mask, IndexMaskMemory &memory, MutableSpan< int > r_originals={})
void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges=false)
void mesh_apply_spatial_organization(Mesh &mesh)
void mesh_set_custom_normals_normalized(Mesh &mesh, MutableSpan< float3 > corner_normals)
void mesh_flip_faces(Mesh &mesh, const IndexMask &selection)
void mesh_select_edge_flush(Mesh &mesh)
Mesh * mesh_new_no_attributes(int verts_num, int edges_num, int faces_num, int corners_num)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
void mesh_transform(Mesh &mesh, const float4x4 &transform, bool do_shape_keys)
void mesh_ensure_default_uv_attribute_on_add(Mesh &mesh, StringRef name, AttrDomain domain, bke::AttrType data_type)
void mesh_set_custom_normals_from_verts(Mesh &mesh, MutableSpan< float3 > vert_normals)
void mesh_vert_normals_assign(Mesh &mesh, Span< float3 > vert_normals)
bool mesh_validate(Mesh &mesh, bool verbose=false, bool allow_missing_edges=false)
void mesh_set_custom_normals_from_verts_normalized(Mesh &mesh, MutableSpan< float3 > vert_normals)
void mesh_ensure_required_data_layers(Mesh &mesh)
void mesh_ensure_default_color_attribute_on_add(Mesh &mesh, StringRef name, AttrDomain domain, bke::AttrType data_type)
void mesh_data_update(Depsgraph &depsgraph, const Scene &scene, Object &ob, const CustomData_MeshMasks &dataMask)
void mesh_edge_hide_from_vert(Span< int2 > edges, Span< bool > hide_vert, MutableSpan< bool > hide_edge)
void mesh_set_custom_normals(Mesh &mesh, MutableSpan< float3 > corner_normals)
void mesh_select_face_flush(Mesh &mesh)
void mesh_translate(Mesh &mesh, const float3 &translation, bool do_shape_keys)
void mesh_ensure_active_uv_map(Mesh &mesh)
void mesh_ensure_default_uv_map(Mesh &mesh)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MatBase< float, 4, 4 > float4x4
VecBase< int32_t, 2 > int2
VecBase< int32_t, 3 > int3
VecBase< int16_t, 2 > short2
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
signed short int16_t
Definition BKE_text.h:77
Vector< Array< int > > corners_by_space
Definition BKE_mesh.hh:177
Vector< CornerNormalSpace > spaces
Definition BKE_mesh.hh:165