Blender V5.3
mesh_remesh_voxel.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cctype>
10#include <cfloat>
11#include <cmath>
12#include <cstdlib>
13#include <cstring>
14#include <ctime>
15
16#include "BLI_array.hh"
17#include "BLI_array_utils.hh"
19#include "BLI_index_range.hh"
20#include "BLI_math_vector_c.hh"
21#include "BLI_span.hh"
22#include "BLI_task.hh"
23
24#include "BKE_attribute.h"
25#include "BKE_attribute.hh"
26#include "BKE_attribute_math.hh"
27#include "BKE_bvhutils.hh"
28#include "BKE_deform.hh"
29#include "BKE_mesh.hh"
30#include "BKE_mesh_remesh_voxel.hh" /* own include */
31#include "BKE_mesh_sample.hh"
32#include "BKE_modifier.hh"
33#include "BKE_report.hh"
34
35#include "bmesh.hh"
36#include "bmesh_tools.hh"
37
38#ifdef WITH_OPENVDB
39# include <openvdb/openvdb.h>
40# include <openvdb/tools/MeshToVolume.h>
41# include <openvdb/tools/VolumeToMesh.h>
42#endif
43
44#ifdef WITH_QUADRIFLOW
45# include "quadriflow_capi.hpp"
46#endif
47
48namespace blender {
49
50#ifdef WITH_QUADRIFLOW
51static Mesh *remesh_quadriflow(const Mesh *input_mesh,
52 int target_faces,
53 int seed,
54 bool preserve_sharp,
55 bool preserve_boundary,
56 bool adaptive_scale,
57 void (*update_cb)(void *, float progress, int *cancel),
58 void *update_cb_data)
59{
60 using namespace blender::bke;
61 const Span<float3> input_positions = input_mesh->vert_positions();
62 const Span<int> input_corner_verts = input_mesh->corner_verts();
63 const Span<int3> corner_tris = input_mesh->corner_tris();
64
65 /* Gather the required data for export to the internal quadriflow mesh format. */
66 Array<int3> vert_tris(corner_tris.size());
67 mesh::vert_tris_from_corner_tris(input_corner_verts, corner_tris, vert_tris);
68
69 /* Fill out the required input data */
70 QuadriflowRemeshData qrd;
71
72 qrd.totfaces = corner_tris.size();
73 qrd.totverts = input_positions.size();
74 qrd.verts = input_positions.cast<float>().data();
75 qrd.faces = vert_tris.as_span().cast<int>().data();
76 qrd.target_faces = target_faces;
77
78 qrd.preserve_sharp = preserve_sharp;
79 qrd.preserve_boundary = preserve_boundary;
80 qrd.adaptive_scale = adaptive_scale;
81 qrd.minimum_cost_flow = false;
82 qrd.aggresive_sat = false;
83 qrd.rng_seed = seed;
84
85 qrd.out_faces = nullptr;
86
87 /* Run the remesher */
88 QFLOW_quadriflow_remesh(&qrd, update_cb, update_cb_data);
89
90 if (qrd.out_faces == nullptr) {
91 /* The remeshing was canceled */
92 return nullptr;
93 }
94
95 if (qrd.out_totfaces == 0) {
96 /* Meshing failed */
97 MEM_delete(qrd.out_faces);
98 MEM_delete(qrd.out_verts);
99 return nullptr;
100 }
101
102 /* Construct the new output mesh */
104 BKE_mesh_copy_parameters(mesh, input_mesh);
105 MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
106 MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
107
109
110 mesh->vert_positions_for_write().copy_from(
111 Span(reinterpret_cast<float3 *>(qrd.out_verts), qrd.out_totverts));
112
113 for (const int i : IndexRange(qrd.out_totfaces)) {
114 const int loopstart = i * 4;
115 corner_verts[loopstart] = qrd.out_faces[loopstart];
116 corner_verts[loopstart + 1] = qrd.out_faces[loopstart + 1];
117 corner_verts[loopstart + 2] = qrd.out_faces[loopstart + 2];
118 corner_verts[loopstart + 3] = qrd.out_faces[loopstart + 3];
119 }
120
121 mesh_calc_edges(*mesh, false, false);
122
123 MEM_delete(qrd.out_faces);
124 MEM_delete(qrd.out_verts);
125
126 return mesh;
127}
128#endif
129
131 int target_faces,
132 int seed,
133 bool preserve_sharp,
134 bool preserve_boundary,
135 bool adaptive_scale,
136 void (*update_cb)(void *, float progress, int *cancel),
137 void *update_cb_data)
138{
139#ifdef WITH_QUADRIFLOW
140 if (target_faces <= 0) {
141 target_faces = -1;
142 }
143 return remesh_quadriflow(mesh,
144 target_faces,
145 seed,
146 preserve_sharp,
147 preserve_boundary,
148 adaptive_scale,
149 update_cb,
150 update_cb_data);
151#else
153 target_faces,
154 seed,
155 preserve_sharp,
156 preserve_boundary,
157 adaptive_scale,
158 update_cb,
159 update_cb_data);
160 return nullptr;
161#endif
162}
163
164#ifdef WITH_OPENVDB
165static openvdb::FloatGrid::Ptr remesh_voxel_level_set_create(
166 const Mesh *mesh, openvdb::math::Transform::Ptr transform)
167{
168 const Span<float3> positions = mesh->vert_positions();
169 const Span<int> corner_verts = mesh->corner_verts();
170 const Span<int3> corner_tris = mesh->corner_tris();
171
172 std::vector<openvdb::Vec3s> points(mesh->verts_num);
173 std::vector<openvdb::Vec3I> triangles(corner_tris.size());
174
175 for (const int i : IndexRange(mesh->verts_num)) {
176 const float3 &co = positions[i];
177 points[i] = openvdb::Vec3s(co.x, co.y, co.z);
178 }
179
180 for (const int i : IndexRange(corner_tris.size())) {
181 const int3 &tri = corner_tris[i];
182 triangles[i] = openvdb::Vec3I(
183 corner_verts[tri[0]], corner_verts[tri[1]], corner_verts[tri[2]]);
184 }
185
186 openvdb::FloatGrid::Ptr grid = openvdb::tools::meshToLevelSet<openvdb::FloatGrid>(
187 *transform, points, triangles, 1.0f);
188
189 return grid;
190}
191
192static Mesh *remesh_voxel_volume_to_mesh(const openvdb::FloatGrid::Ptr level_set_grid,
193 const float isovalue,
194 const float adaptivity,
195 const bool relax_disoriented_triangles)
196{
197 using namespace blender::bke;
198 std::vector<openvdb::Vec3s> vertices;
199 std::vector<openvdb::Vec4I> quads;
200 std::vector<openvdb::Vec3I> tris;
201 openvdb::tools::volumeToMesh<openvdb::FloatGrid>(
202 *level_set_grid, vertices, tris, quads, isovalue, adaptivity, relax_disoriented_triangles);
203
204 if (vertices.empty() || quads.size() + tris.size() == 0) {
205 return nullptr;
206 }
207
209 vertices.size(), 0, quads.size() + tris.size(), quads.size() * 4 + tris.size() * 3);
210 MutableSpan<float3> vert_positions = mesh->vert_positions_for_write();
211 MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
212 MutableSpan<int> mesh_corner_verts = mesh->corner_verts_for_write();
213
214 const int triangle_loop_start = quads.size() * 4;
215 if (!face_offsets.is_empty()) {
216 offset_indices::fill_constant_group_size(4, 0, face_offsets.take_front(quads.size()));
218 3, triangle_loop_start, face_offsets.drop_front(quads.size()));
219 }
220
221 for (const int i : vert_positions.index_range()) {
222 vert_positions[i] = float3(vertices[i].x(), vertices[i].y(), vertices[i].z());
223 }
224
225 for (const int i : IndexRange(quads.size())) {
226 const int loopstart = i * 4;
227 mesh_corner_verts[loopstart] = quads[i][0];
228 mesh_corner_verts[loopstart + 1] = quads[i][3];
229 mesh_corner_verts[loopstart + 2] = quads[i][2];
230 mesh_corner_verts[loopstart + 3] = quads[i][1];
231 }
232
233 for (const int i : IndexRange(tris.size())) {
234 const int loopstart = triangle_loop_start + i * 3;
235 mesh_corner_verts[loopstart] = tris[i][2];
236 mesh_corner_verts[loopstart + 1] = tris[i][1];
237 mesh_corner_verts[loopstart + 2] = tris[i][0];
238 }
239
240 mesh_calc_edges(*mesh, false, false);
241
242 return mesh;
243}
244#endif
245
247 const float voxel_size,
248 const float adaptivity,
249 const float isovalue,
250 const Object *object,
251 ModifierData *modifier_data)
252{
253#ifdef WITH_OPENVDB
254 openvdb::math::Transform::Ptr transform;
255 try {
256 transform = openvdb::math::Transform::createLinearTransform(voxel_size);
257 }
258 catch (const openvdb::ArithmeticError & /*e*/) {
259 /* OpenVDB internally has a limit of 3e-15 for the matrix's determinant and throws
260 * ArithmeticError if the provided value is too low.
261 * See #136637 for more details. */
263 object, modifier_data, "Voxel size of %f too small to be solved", voxel_size);
264 return nullptr;
265 }
266 openvdb::FloatGrid::Ptr level_set = remesh_voxel_level_set_create(mesh, transform);
267 Mesh *result = remesh_voxel_volume_to_mesh(level_set, isovalue, adaptivity, false);
268 if (result != nullptr) {
270 }
271 return result;
272#else
273 UNUSED_VARS(mesh, voxel_size, adaptivity, isovalue, object, modifier_data);
274 return nullptr;
275#endif
276}
277
279 const float voxel_size,
280 const float adaptivity,
281 const float isovalue,
282 ReportList *reports)
283{
284#ifdef WITH_OPENVDB
285 openvdb::math::Transform::Ptr transform;
286 try {
287 transform = openvdb::math::Transform::createLinearTransform(voxel_size);
288 }
289 catch (const openvdb::ArithmeticError & /*e*/) {
290 /* OpenVDB internally has a limit of 3e-15 for the matrix's determinant and throws
291 * ArithmeticError if the provided value is too low.
292 * See #136637 for more details. */
293 BKE_reportf(reports, RPT_ERROR, "Voxel size of %f too small to be solved", voxel_size);
294 return nullptr;
295 }
296 openvdb::FloatGrid::Ptr level_set = remesh_voxel_level_set_create(mesh, transform);
297 Mesh *result = remesh_voxel_volume_to_mesh(level_set, isovalue, adaptivity, false);
298 if (result != nullptr) {
300 }
301 return result;
302#else
303 UNUSED_VARS(mesh, voxel_size, adaptivity, isovalue, reports);
304 return nullptr;
305#endif
306}
307
308namespace bke {
309
310static void calc_edge_centers(const Span<float3> positions,
311 const Span<int2> edges,
312 MutableSpan<float3> edge_centers)
313{
314 for (const int i : edges.index_range()) {
315 edge_centers[i] = math::midpoint(positions[edges[i][0]], positions[edges[i][1]]);
316 }
317}
318
319static void calc_face_centers(const Span<float3> positions,
320 const OffsetIndices<int> faces,
321 const Span<int> corner_verts,
322 MutableSpan<float3> face_centers)
323{
324 for (const int i : faces.index_range()) {
325 face_centers[i] = mesh::face_center_calc(positions, corner_verts.slice(faces[i]));
326 }
327}
328
329static void find_nearest_tris(const Span<float3> positions,
330 const bvh::Tree &bvhtree,
331 MutableSpan<int> tris,
332 MutableSpan<float3> bary_coords)
333{
334 for (const int i : positions.index_range()) {
335 const std::optional<bvh::ClosestPointResult> nearest = bvhtree.closest_point(positions[i]);
336 BLI_assert(nearest.has_value());
337 tris[i] = nearest->index;
338 if (!bary_coords.is_empty()) {
339 bary_coords[i] = nearest->bary_coord;
340 }
341 }
342}
343
344static void find_nearest_tris_parallel(const Span<float3> positions,
345 const bvh::Tree &bvhtree,
346 MutableSpan<int> tris,
347 MutableSpan<float3> bary_coords)
348{
349 threading::parallel_for(tris.index_range(), 512, [&](const IndexRange range) {
350 find_nearest_tris(positions.slice(range),
351 bvhtree,
352 tris.slice(range),
353 bary_coords.is_empty() ? MutableSpan<float3>() : bary_coords.slice(range));
354 });
355}
356
357static void find_nearest_faces(const Span<int> src_tri_faces,
358 const Span<float3> dst_positions,
359 const OffsetIndices<int> dst_faces,
360 const Span<int> dst_corner_verts,
361 const bvh::Tree &bvhtree,
362 MutableSpan<int> nearest_faces)
363{
364 struct TLS {
365 Vector<float3> face_centers;
366 Vector<int> tri_indices;
367 };
369 threading::parallel_for(dst_faces.index_range(), 512, [&](const IndexRange range) {
370 threading::isolate_task([&] {
371 TLS &tls = all_tls.local();
372 Vector<float3> &face_centers = tls.face_centers;
373 face_centers.reinitialize(range.size());
374 calc_face_centers(dst_positions, dst_faces.slice(range), dst_corner_verts, face_centers);
375
376 Vector<int> &tri_indices = tls.tri_indices;
377 tri_indices.reinitialize(range.size());
378 find_nearest_tris(face_centers, bvhtree, tri_indices, {});
379
380 array_utils::gather(src_tri_faces, tri_indices.as_span(), nearest_faces.slice(range));
381 });
382 });
383}
384
385static void find_nearest_edges(const Span<float3> src_positions,
386 const Span<int2> src_edges,
387 const OffsetIndices<int> src_faces,
388 const Span<int> src_corner_edges,
389 const Span<int> src_tri_faces,
390 const Span<float3> dst_positions,
391 const Span<int2> dst_edges,
392 const bvh::Tree &bvhtree,
393 MutableSpan<int> nearest_edges)
394{
395 struct TLS {
396 Vector<float3> edge_centers;
397 Vector<int> tri_indices;
398 Vector<int> face_indices;
399 Vector<float> distances;
400 };
402 threading::parallel_for(nearest_edges.index_range(), 512, [&](const IndexRange range) {
403 threading::isolate_task([&] {
404 TLS &tls = all_tls.local();
405 Vector<float3> &edge_centers = tls.edge_centers;
406 edge_centers.reinitialize(range.size());
407 calc_edge_centers(dst_positions, dst_edges.slice(range), edge_centers);
408
409 Vector<int> &tri_indices = tls.tri_indices;
410 tri_indices.reinitialize(range.size());
411 find_nearest_tris_parallel(edge_centers, bvhtree, tri_indices, {});
412
413 Vector<int> &face_indices = tls.face_indices;
414 face_indices.reinitialize(range.size());
415 array_utils::gather(src_tri_faces, tri_indices.as_span(), face_indices.as_mutable_span());
416
417 /* Find the source edge that's closest to the destination edge in the nearest face. Search
418 * through the whole face instead of just the triangle because the triangle has edges that
419 * might not be actual mesh edges. */
420 Vector<float, 64> distances;
421 for (const int i : range.index_range()) {
422 const int dst_edge = range[i];
423 const float3 &dst_position = edge_centers[i];
424
425 const int src_face = face_indices[i];
426 const Span<int> src_face_edges = src_corner_edges.slice(src_faces[src_face]);
427
428 distances.reinitialize(src_face_edges.size());
429 for (const int i : src_face_edges.index_range()) {
430 const int2 src_edge = src_edges[src_face_edges[i]];
431 const float3 src_center = math::midpoint(src_positions[src_edge[0]],
432 src_positions[src_edge[1]]);
433 distances[i] = math::distance_squared(src_center, dst_position);
434 }
435
436 const int min = std::min_element(distances.begin(), distances.end()) - distances.begin();
437 nearest_edges[dst_edge] = src_face_edges[min];
438 }
439 });
440 });
441}
442
443static void gather_attributes(const Span<StringRef> names,
444 const AttributeAccessor src_attributes,
445 const AttrDomain domain,
446 const Span<int> index_map,
447 MutableAttributeAccessor dst_attributes)
448{
449 for (const StringRef name : names) {
450 const GVArraySpan src = *src_attributes.lookup(name, domain);
451 const AttrType type = cpp_type_to_attribute_type(src.type());
453 name, domain, type);
454 attribute_math::gather(src, index_map, dst.span);
455 dst.finish();
456 }
457}
458
460 Span<int> corner_verts,
461 Span<int3> corner_tris,
462 Span<int> tri_indices,
463 Span<float3> bary_coords,
464 const AttributeAccessor src_attributes,
465 MutableAttributeAccessor dst_attributes)
466{
467 for (const StringRef name : names) {
468 const GVArray src = *src_attributes.lookup(name, AttrDomain::Point);
469 const AttrType type = cpp_type_to_attribute_type(src.type());
471 name, AttrDomain::Point, type);
473 corner_tris,
474 tri_indices,
475 bary_coords,
476 src,
477 IndexMask(dst.span.size()),
478 dst.span);
479 dst.finish();
480 }
481}
482
484 Span<int3> corner_tris,
485 Span<int> tri_indices,
486 Span<float3> bary_coords,
487 const AttributeAccessor src_attributes,
488 MutableAttributeAccessor dst_attributes)
489{
490 for (const StringRef name : names) {
491 const GVArray src = *src_attributes.lookup(name, AttrDomain::Corner);
492 const AttrType type = cpp_type_to_attribute_type(src.type());
493
494 GArray<> dst_point(src.type(), bary_coords.size());
496 corner_tris, tri_indices, bary_coords, src, IndexMask(dst_point.size()), dst_point);
497
498 GVArray dst_corner = dst_attributes.adapt_domain(
500 dst_attributes.add(name, AttrDomain::Corner, type, AttributeInitVArray(std::move(dst_corner)));
501 }
502}
503
505{
506 BLI_assert(src.faces_num > 0);
507 MutableAttributeAccessor dst_attributes = dst.attributes_for_write();
508
509 /* Gather attributes to transfer for each domain. This makes it possible to skip
510 * building index maps and even the main BVH tree if there are no attributes. */
511 const AttributeAccessor src_attributes = src.attributes();
512 Vector<StringRef> point_ids;
513 Vector<StringRef> edge_ids;
514 Vector<StringRef> face_ids;
515 Vector<StringRef> corner_ids;
516 src_attributes.foreach_attribute([&](const AttributeIter &iter) {
517 if (ELEM(iter.name, "position", ".edge_verts", ".corner_vert", ".corner_edge")) {
518 return;
519 }
521 const GVArray src_attr = *iter.get();
522 const CommonVArrayInfo info = src_attr.common_info();
524 const bke::AttributeInitValue init(GPointer(src_attr.type(), info.data));
525 if (dst_attributes.add(iter.name, iter.domain, iter.data_type, init)) {
526 return;
527 }
528 }
529 }
530 switch (iter.domain) {
532 point_ids.append(iter.name);
533 break;
534 case AttrDomain::Edge:
535 edge_ids.append(iter.name);
536 break;
537 case AttrDomain::Face:
538 face_ids.append(iter.name);
539 break;
541 corner_ids.append(iter.name);
542 break;
543 default:
545 break;
546 }
547 });
548
549 if (point_ids.is_empty() && edge_ids.is_empty() && face_ids.is_empty() && corner_ids.is_empty())
550 {
551 return;
552 }
553
554 const Span<float3> src_positions = src.vert_positions();
555 const OffsetIndices src_faces = src.faces();
556 const Span<int> src_corner_verts = src.corner_verts();
557 const Span<int3> src_corner_tris = src.corner_tris();
558
559 /* The main idea in the following code is to trade some complexity in sampling for the benefit of
560 * only using and building a single BVH tree. Since sculpt mode doesn't generally deal with loose
561 * vertices and edges, we use the standard "triangles" BVH which won't contain them. Also, only
562 * relying on a single BVH should reduce memory usage, and work better if the BVH and #pbvh::Tree
563 * are ever merged.
564 *
565 * One key decision is separating building transfer index maps from actually transferring any
566 * attribute data. This is important to keep attribute storage independent from the specifics of
567 * the decisions made here, which mainly results in easier refactoring, more generic code, and
568 * possibly improved performance from lower cache usage in the "complex" sampling part of the
569 * algorithm and the copying itself. */
570 const bvh::Tree &bvhtree = src.bvh_tris();
571
572 const Span<float3> dst_positions = dst.vert_positions();
573 const OffsetIndices dst_faces = dst.faces();
574 const Span<int> dst_corner_verts = dst.corner_verts();
575
576 if (!point_ids.is_empty() || !corner_ids.is_empty()) {
577 Array<int> vert_nearest_tris(dst_positions.size());
578 Array<float3> bary_coords(dst_positions.size());
579 find_nearest_tris_parallel(dst_positions, bvhtree, vert_nearest_tris, bary_coords);
580
581 if (!point_ids.is_empty()) {
582 /* Copy vertex group names (otherwise `MeshVertexGroupsAttributeProvider` wont find them -
583 * and these would show up as regular attributes afterwards). "vertex_group_active_index" is
584 * taken care of via #BKE_mesh_copy_parameters(). */
586 sample_vertex_attributes(point_ids,
587 src_corner_verts,
588 src_corner_tris,
589 vert_nearest_tris,
590 bary_coords,
591 src_attributes,
592 dst_attributes);
593 }
594
595 if (!corner_ids.is_empty()) {
596 sample_corner_attributes(corner_ids,
597 src_corner_tris,
598 vert_nearest_tris,
599 bary_coords,
600 src_attributes,
601 dst_attributes);
602 }
603 }
604
605 if (!edge_ids.is_empty()) {
606 const Span<int2> src_edges = src.edges();
607 const Span<int> src_corner_edges = src.corner_edges();
608 const Span<int> src_tri_faces = src.corner_tri_faces();
609 const Span<int2> dst_edges = dst.edges();
610 Array<int> map(dst.edges_num);
611 find_nearest_edges(src_positions,
612 src_edges,
613 src_faces,
614 src_corner_edges,
615 src_tri_faces,
616 dst_positions,
617 dst_edges,
618 bvhtree,
619 map);
620 gather_attributes(edge_ids, src_attributes, AttrDomain::Edge, map, dst_attributes);
621 }
622
623 if (!face_ids.is_empty()) {
624 const Span<int> src_tri_faces = src.corner_tri_faces();
625 Array<int> map(dst.faces_num);
626 find_nearest_faces(src_tri_faces, dst_positions, dst_faces, dst_corner_verts, bvhtree, map);
627 gather_attributes(face_ids, src_attributes, AttrDomain::Face, map, dst_attributes);
628 }
629
630 if (src.active_color_attribute) {
632 }
633 if (src.default_color_attribute) {
635 }
636 if (!src.active_uv_map_name().is_empty()) {
637 dst.uv_maps_active_set(src.active_uv_map_name());
638 }
639 if (!src.default_uv_map_name().is_empty()) {
640 dst.uv_maps_default_set(src.default_uv_map_name());
641 }
642}
643
644} // namespace bke
645
647{
649
650 BMeshCreateParams bmesh_create_params{};
651 bmesh_create_params.use_toolflags = true;
652 BMesh *bm = BM_mesh_create(&allocsize, &bmesh_create_params);
653
654 BMeshFromMeshParams bmesh_from_mesh_params{};
655 bmesh_from_mesh_params.calc_face_normal = true;
656 bmesh_from_mesh_params.calc_vert_normal = true;
657 BM_mesh_bm_from_me(bm, mesh, &bmesh_from_mesh_params);
658
659 BMVert *v;
660 BMEdge *ed, *ed_next;
661 BMFace *f, *f_next;
662 BMIter iter_a, iter_b;
663
664 /* Merge 3 edge poles vertices that exist in the same face */
666 BM_ITER_MESH_MUTABLE (f, f_next, &iter_a, bm, BM_FACES_OF_MESH) {
667 BMVert *v1, *v2;
668 v1 = nullptr;
669 v2 = nullptr;
670 BM_ITER_ELEM (v, &iter_b, f, BM_VERTS_OF_FACE) {
671 if (BM_vert_edge_count(v) == 3) {
672 if (v1) {
673 v2 = v;
674 }
675 else {
676 v1 = v;
677 }
678 }
679 }
680 if (v1 && v2 && (v1 != v2) && !BM_edge_exists(v1, v2)) {
681 BM_face_kill(bm, f);
682 BMEdge *e = BM_edge_create(bm, v1, v2, nullptr, BM_CREATE_NOP);
684 }
685 }
686
687 BM_ITER_MESH_MUTABLE (ed, ed_next, &iter_a, bm, BM_EDGES_OF_MESH) {
689 float co[3];
690 mid_v3_v3v3(co, ed->v1->co, ed->v2->co);
691 BMVert *vc = BM_edge_collapse(bm, ed, ed->v1, true, true);
692 copy_v3_v3(vc->co, co);
693 }
694 }
695
696 /* Delete faces with a 3 edge pole in all their vertices */
698 BM_ITER_MESH (f, &iter_a, bm, BM_FACES_OF_MESH) {
699 bool dissolve = true;
700 BM_ITER_ELEM (v, &iter_b, f, BM_VERTS_OF_FACE) {
701 if (BM_vert_edge_count(v) != 3) {
702 dissolve = false;
703 }
704 }
705 if (dissolve) {
706 BM_ITER_ELEM (v, &iter_b, f, BM_VERTS_OF_FACE) {
708 }
709 }
710 }
712
713 BM_ITER_MESH (ed, &iter_a, bm, BM_EDGES_OF_MESH) {
714 if (BM_edge_face_count(ed) != 2) {
716 }
717 }
718 BM_mesh_edgenet(bm, false, true);
719
720 /* Smooth the result */
721 for (int i = 0; i < 4; i++) {
722 BM_ITER_MESH (v, &iter_a, bm, BM_VERTS_OF_MESH) {
723 float co[3];
724 zero_v3(co);
725 BM_ITER_ELEM (ed, &iter_b, v, BM_EDGES_OF_VERT) {
726 BMVert *vert = BM_edge_other_vert(ed, v);
727 add_v3_v3(co, vert->co);
728 }
729 mul_v3_fl(co, 1.0f / float(BM_vert_edge_count(v)));
730 mid_v3_v3v3(v->co, v->co, co);
731 }
732 }
733
735
740 "recalc_face_normals faces=%hf",
743
744 BMeshToMeshParams bmesh_to_mesh_params{};
745 bmesh_to_mesh_params.calc_object_remap = false;
746 Mesh *result = BKE_mesh_from_bmesh_nomain(bm, &bmesh_to_mesh_params, mesh);
747
749 return result;
750}
751
752} // namespace blender
Generic geometry attributes built on CustomData.
support for deformation groups and hooks.
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define ELEM(...)
#define BM_elem_flag_set(ele, hflag, val)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_MESH_MUTABLE(ele, ele_next, iter, bm, itype)
#define BMALLOC_TEMPLATE_FROM_ME(...)
#define BMO_FLAG_DEFAULTS
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
Span< NewT > constexpr cast() const
Definition BLI_span.hh:422
constexpr int64_t size() const
Definition BLI_span.hh:256
int64_t size() const
GSpan as_span() const
const CPPType & type() const
CommonVArrayInfo common_info() const
static GVArray from_span(GSpan span)
constexpr IndexRange()=default
constexpr bool is_empty() const
Definition BLI_span.hh:515
constexpr MutableSpan drop_front(const int64_t n) const
Definition BLI_span.hh:613
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void append(const T &value)
bool is_empty() const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
GVArray adapt_domain(const GVArray &varray, const AttrDomain from_domain, const AttrDomain to_domain) const
GAttributeReader lookup(const StringRef name) const
std::optional< AttrStorageType > storage_type
GAttributeReader get() const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
std::optional< ClosestPointResult > closest_point(const float3 &point, float radius=std::numeric_limits< float >::max()) const
Definition bvh.cc:709
void gather(GSpan src, Span< int > map, GMutableSpan dst)
void sample_point_attribute(Span< int > corner_verts, Span< int3 > corner_tris, Span< int > tri_indices, Span< float3 > bary_coords, const GVArray &src, const IndexMask &mask, GMutableSpan dst)
void sample_corner_attribute(Span< int3 > corner_tris, Span< int > tri_indices, Span< float3 > bary_coords, const GVArray &src, const IndexMask &mask, GMutableSpan dst)
void vert_tris_from_corner_tris(Span< int > corner_verts, Span< int3 > corner_tris, MutableSpan< int3 > vert_tris)
float3 face_center_calc(Span< float3 > vert_positions, Span< int > face_verts)
static void find_nearest_tris_parallel(const Span< float3 > positions, const bvh::Tree &bvhtree, MutableSpan< int > tris, MutableSpan< float3 > bary_coords)
static void find_nearest_tris(const Span< float3 > positions, const bvh::Tree &bvhtree, MutableSpan< int > tris, MutableSpan< float3 > bary_coords)
static void calc_edge_centers(const Span< float3 > positions, const Span< int2 > edges, MutableSpan< float3 > edge_centers)
void mesh_remesh_reproject_attributes(const Mesh &src, Mesh &dst)
static void find_nearest_edges(const Span< float3 > src_positions, const Span< int2 > src_edges, const OffsetIndices< int > src_faces, const Span< int > src_corner_edges, const Span< int > src_tri_faces, const Span< float3 > dst_positions, const Span< int2 > dst_edges, const bvh::Tree &bvhtree, MutableSpan< int > nearest_edges)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
static void sample_corner_attributes(const Span< StringRef > names, Span< int3 > corner_tris, Span< int > tri_indices, Span< float3 > bary_coords, const AttributeAccessor src_attributes, MutableAttributeAccessor dst_attributes)
static void calc_face_centers(const Span< float3 > positions, const OffsetIndices< int > faces, const Span< int > corner_verts, MutableSpan< float3 > face_centers)
static void sample_vertex_attributes(const Span< StringRef > names, Span< int > corner_verts, Span< int3 > corner_tris, Span< int > tri_indices, Span< float3 > bary_coords, const AttributeAccessor src_attributes, MutableAttributeAccessor dst_attributes)
static void find_nearest_faces(const Span< int > src_tri_faces, const Span< float3 > dst_positions, const OffsetIndices< int > dst_faces, const Span< int > dst_corner_verts, const bvh::Tree &bvhtree, MutableSpan< int > nearest_faces)
AttrType cpp_type_to_attribute_type(const CPPType &type)
T midpoint(const T &a, const T &b)
void fill_constant_group_size(int size, int start_offset, MutableSpan< int > offsets)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
void BM_mesh_delete_hflag_context(BMesh *bm, const char hflag, const int type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_mesh_copy_parameters(Mesh *me_dst, const Mesh *me_src)
int BM_edge_face_count(const BMEdge *e)
void BM_mesh_elem_hflag_enable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
void BKE_modifier_set_error(const Object *ob, ModifierData *md, const char *format,...) ATTR_PRINTF_FORMAT(3
void BKE_id_attributes_default_color_set(struct ID *id, std::optional< StringRef > name)
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
MINLINE void copy_v3_v3(float r[3], const float a[3])
Mesh * BKE_mesh_remesh_quadriflow(const Mesh *mesh, int target_faces, int seed, bool preserve_sharp, bool preserve_boundary, bool adaptive_scale, void(*update_cb)(void *, float progress, int *cancel), void *update_cb_data)
Mesh * BKE_mesh_from_bmesh_nomain(BMesh *bm, const BMeshToMeshParams *params, const Mesh *me_settings)
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
ATTR_WARN_UNUSED_RESULT const BMVert * v
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BMVert * BM_edge_collapse(BMesh *bm, BMEdge *e_kill, BMVert *v_kill, const bool do_del, const bool kill_degenerate_faces)
MINLINE void mul_v3_fl(float r[3], float f)
void BM_mesh_bm_from_me(BMesh *bm, const Mesh *mesh, const BMeshFromMeshParams *params)
Mesh -> BMesh.
void BM_mesh_edgenet(BMesh *bm, const bool use_edge_tag, const bool use_new_face_tag)
VecBase< int32_t, 3 > int3
@ BM_CREATE_NOP
Definition bmesh_core.hh:34
BMesh const char void * data
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
bool BMO_op_callf(BMesh *bm, int flag, const char *fmt,...)
Mesh * BKE_mesh_remesh_voxel(const Mesh *mesh, float voxel_size, float adaptivity, float isovalue, const Object *object, ModifierData *modifier_data)
MINLINE void zero_v3(float r[3])
Mesh * BKE_mesh_remesh_voxel_fix_poles(const Mesh *mesh)
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
MINLINE void add_v3_v3(float r[3], const float a[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void BM_face_kill(BMesh *bm, BMFace *f)
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< StringRef > name)
void BKE_defgroup_copy_list(ListBaseT< bDeformGroup > *outbase, const ListBaseT< bDeformGroup > *inbase)
Definition deform.cc:75
VecBase< float, 3 > float3
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const BMeshCreateParams *params)
BMesh Make Mesh.
BMEdge * BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *e_example, const eBMCreateFlag create_flag)
Main function for creating a new edge.
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void BM_mesh_normals_update(BMesh *bm)
int BM_vert_edge_count(const BMVert *v)
void QFLOW_quadriflow_remesh(QuadriflowRemeshData *qrd, void(*update_cb)(void *, float progress, int *cancel), void *update_cb_data)
ListBaseT< bDeformGroup > vertex_group_names
char * active_color_attribute
char * default_color_attribute
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136