Blender V5.3
geometry_component_mesh.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 "BLI_math_vector.hh"
10
12#include "BKE_geometry_set.hh"
13#include "BKE_lib_id.hh"
14#include "BKE_mesh.hh"
15
16namespace blender {
17
18namespace bke {
19
20/* -------------------------------------------------------------------- */
23
25
27 : GeometryComponent(Type::Mesh), mesh_(mesh), ownership_(ownership)
28{
29}
30
35
37{
38 MeshComponent *new_component = new MeshComponent();
39 if (mesh_ != nullptr) {
40 new_component->mesh_ = BKE_mesh_copy_for_eval(*mesh_);
41 new_component->ownership_ = GeometryOwnershipType::Owned;
42 }
43 return GeometryComponentPtr(new_component);
44}
45
47{
48 BLI_assert(this->is_mutable() || this->is_expired());
49 if (mesh_ != nullptr) {
50 if (ownership_ == GeometryOwnershipType::Owned) {
51 BKE_id_free(nullptr, mesh_);
52 }
53 mesh_ = nullptr;
54 }
55}
56
58{
59 return mesh_ != nullptr;
60}
61
63{
64 BLI_assert(this->is_mutable());
65 this->clear();
66 mesh_ = mesh;
67 ownership_ = ownership;
68}
69
71{
72 BLI_assert(this->is_mutable());
73 Mesh *mesh = mesh_;
74 mesh_ = nullptr;
75 return mesh;
76}
77
79{
80 return mesh_;
81}
82
84{
85 BLI_assert(this->is_mutable());
86 if (ownership_ == GeometryOwnershipType::ReadOnly) {
87 mesh_ = BKE_mesh_copy_for_eval(*mesh_);
89 }
90 return mesh_;
91}
92
94{
95 return mesh_ == nullptr;
96}
97
99{
100 return ownership_ == GeometryOwnershipType::Owned;
101}
102
104{
105 BLI_assert(this->is_mutable());
106 if (ownership_ != GeometryOwnershipType::Owned) {
107 if (mesh_) {
108 mesh_ = BKE_mesh_copy_for_eval(*mesh_);
109 }
110 ownership_ = GeometryOwnershipType::Owned;
111 }
112}
113
115{
116 if (mesh_) {
117 mesh_->count_memory(memory);
118 }
119}
120
122
123/* -------------------------------------------------------------------- */
126
128 const IndexMask &mask,
129 const AttrDomain domain,
130 const bool no_corner_normals,
131 const bool true_normals)
132{
133 switch (domain) {
134 case AttrDomain::Face: {
135 return VArray<float3>::from_span(true_normals ? mesh.face_normals_true() :
136 mesh.face_normals());
137 }
138 case AttrDomain::Point: {
139 return VArray<float3>::from_span(true_normals ? mesh.vert_normals_true() :
140 mesh.vert_normals());
141 }
142 case AttrDomain::Edge: {
143 /* In this case, start with vertex normals and convert to the edge domain, since the
144 * conversion from edges to vertices is very simple. Use "manual" domain interpolation
145 * instead of the GeometryComponent API to avoid calculating unnecessary values and to
146 * allow normalizing the result more simply. */
147 Span<float3> vert_normals = true_normals ? mesh.vert_normals_true() : mesh.vert_normals();
148 const Span<int2> edges = mesh.edges();
149 Array<float3> edge_normals(mask.min_array_size());
150 mask.foreach_index([&](const int i) {
151 const int2 &edge = edges[i];
152 edge_normals[i] = math::normalize(
153 math::interpolate(vert_normals[edge[0]], vert_normals[edge[1]], 0.5f));
154 });
155
156 return VArray<float3>::from_container(std::move(edge_normals));
157 }
158 case AttrDomain::Corner: {
159 if (no_corner_normals || true_normals) {
160 return mesh.attributes().adapt_domain(
161 VArray<float3>::from_span(true_normals ? mesh.face_normals_true() :
162 mesh.face_normals()),
165 }
166 return VArray<float3>::from_span(mesh.corner_normals());
167 }
168 default:
169 return {};
170 }
171}
172
174
175} // namespace bke
176
177namespace bke {
178
179/* -------------------------------------------------------------------- */
182
183std::optional<AttributeAccessor> MeshComponent::attributes() const
184{
186}
187
188std::optional<MutableAttributeAccessor> MeshComponent::attributes_for_write()
189{
190 Mesh *mesh = this->get_for_write();
192}
193
195
196} // namespace bke
197} // namespace blender
static VArray from_span(Span< T > values)
static VArray from_container(ContainerT container)
AttributeAccessor(const void *owner, const AttributeAccessorFunctions &fn)
void replace(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GeometryComponentPtr copy() const override
void count_memory(MemoryCounter &memory) const override
std::optional< AttributeAccessor > attributes() const final
std::optional< MutableAttributeAccessor > attributes_for_write() final
MutableAttributeAccessor(void *owner, const AttributeAccessorFunctions &fn)
ImplicitSharingPtr< GeometryComponent > GeometryComponentPtr
const AttributeAccessorFunctions & mesh_attribute_accessor_functions()
VArray< float3 > mesh_normals_varray(const Mesh &mesh, const IndexMask &mask, AttrDomain domain, bool no_corner_normals=false, bool true_normals=false)
T interpolate(const T &a, const T &b, const FactorT &t)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
VecBase< int32_t, 2 > int2
void BKE_id_free(Main *bmain, void *idv)