Blender V5.3
mesh_flip_faces.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_mesh_types.h"
10#include "DNA_meshdata_types.h"
11
12#include "BLI_task.hh"
13
14#include "BKE_attribute.hh"
15#include "BKE_attribute_math.hh"
16#include "BKE_customdata.hh"
17#include "BKE_mesh.hh"
18
19namespace blender::bke {
20
21template<typename T>
22static void flip_corner_data(const OffsetIndices<int> faces,
23 const IndexMask &face_selection,
25{
26 face_selection.foreach_index([&](const int i) { data.slice(faces[i].drop_front(1)).reverse(); },
28}
29
30template<typename T>
32 CustomData &corner_data,
33 const IndexMask &face_selection,
34 const eCustomDataType data_type)
35{
36 BLI_assert(sizeof(T) == CustomData_sizeof(data_type));
37 for (const int i : IndexRange(CustomData_number_of_layers(&corner_data, data_type))) {
38 T *data = static_cast<T *>(
39 CustomData_get_layer_n_for_write(&corner_data, data_type, i, faces.total_size()));
40 flip_corner_data(faces, face_selection, MutableSpan(data, faces.total_size()));
41 }
42}
43
44void mesh_flip_faces(Mesh &mesh, const IndexMask &selection)
45{
46 if (mesh.faces_num == 0 || selection.is_empty()) {
47 return;
48 }
49
50 const OffsetIndices faces = mesh.faces();
51 MutableSpan<int> corner_verts = mesh.corner_verts_for_write();
52 MutableSpan<int> corner_edges = mesh.corner_edges_for_write();
53
54 selection.foreach_index(
55 [&](const int i) {
56 const IndexRange face = faces[i];
57 for (const int j : IndexRange(face.size() / 2)) {
58 const int a = face[j + 1];
59 const int b = face.last(j);
60 std::swap(corner_verts[a], corner_verts[b]);
61 std::swap(corner_edges[a - 1], corner_edges[b]);
62 }
63 },
65
66 flip_custom_data_type<float4>(faces, mesh.corner_data, selection, CD_MLOOPTANGENT);
67 flip_custom_data_type<GridPaintMask>(faces, mesh.corner_data, selection, CD_GRID_PAINT_MASK);
68 flip_custom_data_type<OrigSpaceLoop>(faces, mesh.corner_data, selection, CD_ORIGSPACE_MLOOP);
69 flip_custom_data_type<MDisps>(faces, mesh.corner_data, selection, CD_MDISPS);
70 if (MDisps *mdisp = static_cast<MDisps *>(
71 CustomData_get_layer_for_write(&mesh.corner_data, CD_MDISPS, mesh.corners_num)))
72 {
73 selection.foreach_index(
74 [&](const int i) {
75 for (const int corner : faces[i]) {
76 BKE_mesh_mdisp_flip(&mdisp[corner], true);
77 }
78 },
80 }
81
82 MutableAttributeAccessor attributes = mesh.attributes_for_write();
83 attributes.foreach_attribute([&](const AttributeIter &iter) {
84 if (iter.data_type == bke::AttrType::String) {
85 return;
86 }
87 if (iter.domain != AttrDomain::Corner) {
88 return;
89 }
91 return;
92 }
93 if (ELEM(iter.name, ".corner_vert", ".corner_edge")) {
94 return;
95 }
97 attribute_math::to_static_type(iter.data_type, [&]<typename T>() {
98 flip_corner_data(faces, selection, attribute.span.typed<T>());
99 });
100 attribute.finish();
101 });
102
103 mesh.tag_face_winding_changed();
104}
105
106} // namespace blender::bke
CustomData interface, see also DNA_customdata_types.h.
#define ELEM(...)
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr MutableSpan()=default
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
std::optional< AttrStorageType > storage_type
GSpanAttributeWriter lookup_for_write_span(StringRef name)
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define attribute(slot)
#define T
void to_static_type(const CPPType &cpp_type, Fn &&fn)
static void flip_corner_data(const OffsetIndices< int > faces, const IndexMask &face_selection, MutableSpan< T > data)
static void flip_custom_data_type(const OffsetIndices< int > faces, CustomData &corner_data, const IndexMask &face_selection, const eCustomDataType data_type)
void mesh_flip_faces(Mesh &mesh, const IndexMask &selection)
constexpr ParallelGrainSize grain_size(int grain_size)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int CustomData_number_of_layers(const CustomData *data, eCustomDataType type)
void * CustomData_get_layer_n_for_write(CustomData *data, eCustomDataType type, int n, int totelem)
int CustomData_sizeof(eCustomDataType type)
BMesh const char void * data
void BKE_mesh_mdisp_flip(MDisps *md, bool use_loop_mdisp_flip)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)