Blender V4.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
9#include <cctype>
10#include <cfloat>
11#include <cmath>
12#include <cstdlib>
13#include <cstring>
14#include <ctime>
15
16#include "MEM_guardedalloc.h"
17
18#include "BLI_array.hh"
19#include "BLI_array_utils.hh"
21#include "BLI_index_range.hh"
22#include "BLI_math_vector.h"
23#include "BLI_span.hh"
24#include "BLI_task.hh"
25
26#include "BKE_attribute.hh"
27#include "BKE_attribute_math.hh"
28#include "BKE_bvhutils.hh"
29#include "BKE_customdata.hh"
30#include "BKE_editmesh.hh"
31#include "BKE_lib_id.hh"
32#include "BKE_mesh.hh"
33#include "BKE_mesh_mapping.hh"
34#include "BKE_mesh_remesh_voxel.hh" /* own include */
35#include "BKE_mesh_runtime.hh"
36#include "BKE_mesh_sample.hh"
37
38#include "bmesh_tools.hh"
39
40#ifdef WITH_OPENVDB
41# include <openvdb/openvdb.h>
42# include <openvdb/tools/MeshToVolume.h>
43# include <openvdb/tools/VolumeToMesh.h>
44#endif
45
46#ifdef WITH_QUADRIFLOW
47# include "quadriflow_capi.hpp"
48#endif
49
50using blender::Array;
51using blender::float3;
53using blender::int3;
55using blender::Span;
56
57#ifdef WITH_QUADRIFLOW
58static Mesh *remesh_quadriflow(const Mesh *input_mesh,
59 int target_faces,
60 int seed,
61 bool preserve_sharp,
62 bool preserve_boundary,
63 bool adaptive_scale,
64 void (*update_cb)(void *, float progress, int *cancel),
65 void *update_cb_data)
66{
67 using namespace blender;
68 using namespace blender::bke;
69 const Span<float3> input_positions = input_mesh->vert_positions();
70 const Span<int> input_corner_verts = input_mesh->corner_verts();
71 const Span<int3> corner_tris = input_mesh->corner_tris();
72
73 /* Gather the required data for export to the internal quadriflow mesh format. */
74 Array<int3> vert_tris(corner_tris.size());
75 mesh::vert_tris_from_corner_tris(input_corner_verts, corner_tris, vert_tris);
76
77 /* Fill out the required input data */
79
80 qrd.totfaces = corner_tris.size();
81 qrd.totverts = input_positions.size();
82 qrd.verts = input_positions.cast<float>().data();
83 qrd.faces = vert_tris.as_span().cast<int>().data();
84 qrd.target_faces = target_faces;
85
86 qrd.preserve_sharp = preserve_sharp;
87 qrd.preserve_boundary = preserve_boundary;
88 qrd.adaptive_scale = adaptive_scale;
89 qrd.minimum_cost_flow = false;
90 qrd.aggresive_sat = false;
91 qrd.rng_seed = seed;
92
93 qrd.out_faces = nullptr;
94
95 /* Run the remesher */
96 QFLOW_quadriflow_remesh(&qrd, update_cb, update_cb_data);
97
98 if (qrd.out_faces == nullptr) {
99 /* The remeshing was canceled */
100 return nullptr;
101 }
102
103 if (qrd.out_totfaces == 0) {
104 /* Meshing failed */
105 MEM_freeN(qrd.out_faces);
106 MEM_freeN(qrd.out_verts);
107 return nullptr;
108 }
109
110 /* Construct the new output mesh */
111 Mesh *mesh = BKE_mesh_new_nomain(qrd.out_totverts, 0, qrd.out_totfaces, qrd.out_totfaces * 4);
112 BKE_mesh_copy_parameters(mesh, input_mesh);
113 MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
114 MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
115
117
118 mesh->vert_positions_for_write().copy_from(
119 Span(reinterpret_cast<float3 *>(qrd.out_verts), qrd.out_totverts));
120
121 for (const int i : IndexRange(qrd.out_totfaces)) {
122 const int loopstart = i * 4;
123 corner_verts[loopstart] = qrd.out_faces[loopstart];
124 corner_verts[loopstart + 1] = qrd.out_faces[loopstart + 1];
125 corner_verts[loopstart + 2] = qrd.out_faces[loopstart + 2];
126 corner_verts[loopstart + 3] = qrd.out_faces[loopstart + 3];
127 }
128
129 mesh_calc_edges(*mesh, false, false);
130
131 MEM_freeN(qrd.out_faces);
132 MEM_freeN(qrd.out_verts);
133
134 return mesh;
135}
136#endif
137
139 int target_faces,
140 int seed,
141 bool preserve_sharp,
142 bool preserve_boundary,
143 bool adaptive_scale,
144 void (*update_cb)(void *, float progress, int *cancel),
145 void *update_cb_data)
146{
147#ifdef WITH_QUADRIFLOW
148 if (target_faces <= 0) {
149 target_faces = -1;
150 }
151 return remesh_quadriflow(mesh,
152 target_faces,
153 seed,
154 preserve_sharp,
155 preserve_boundary,
156 adaptive_scale,
157 update_cb,
158 update_cb_data);
159#else
160 UNUSED_VARS(mesh,
161 target_faces,
162 seed,
163 preserve_sharp,
164 preserve_boundary,
165 adaptive_scale,
166 update_cb,
167 update_cb_data);
168 return nullptr;
169#endif
170}
171
172#ifdef WITH_OPENVDB
173static openvdb::FloatGrid::Ptr remesh_voxel_level_set_create(const Mesh *mesh,
174 const float voxel_size)
175{
176 const Span<float3> positions = mesh->vert_positions();
177 const Span<int> corner_verts = mesh->corner_verts();
178 const Span<int3> corner_tris = mesh->corner_tris();
179
180 std::vector<openvdb::Vec3s> points(mesh->verts_num);
181 std::vector<openvdb::Vec3I> triangles(corner_tris.size());
182
183 for (const int i : IndexRange(mesh->verts_num)) {
184 const float3 &co = positions[i];
185 points[i] = openvdb::Vec3s(co.x, co.y, co.z);
186 }
187
188 for (const int i : IndexRange(corner_tris.size())) {
189 const int3 &tri = corner_tris[i];
190 triangles[i] = openvdb::Vec3I(
191 corner_verts[tri[0]], corner_verts[tri[1]], corner_verts[tri[2]]);
192 }
193
194 openvdb::math::Transform::Ptr transform = openvdb::math::Transform::createLinearTransform(
195 voxel_size);
196 openvdb::FloatGrid::Ptr grid = openvdb::tools::meshToLevelSet<openvdb::FloatGrid>(
197 *transform, points, triangles, 1.0f);
198
199 return grid;
200}
201
202static Mesh *remesh_voxel_volume_to_mesh(const openvdb::FloatGrid::Ptr level_set_grid,
203 const float isovalue,
204 const float adaptivity,
205 const bool relax_disoriented_triangles)
206{
207 using namespace blender;
208 using namespace blender::bke;
209 std::vector<openvdb::Vec3s> vertices;
210 std::vector<openvdb::Vec4I> quads;
211 std::vector<openvdb::Vec3I> tris;
212 openvdb::tools::volumeToMesh<openvdb::FloatGrid>(
213 *level_set_grid, vertices, tris, quads, isovalue, adaptivity, relax_disoriented_triangles);
214
216 vertices.size(), 0, quads.size() + tris.size(), quads.size() * 4 + tris.size() * 3);
217 MutableSpan<float3> vert_positions = mesh->vert_positions_for_write();
218 MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
219 MutableSpan<int> mesh_corner_verts = mesh->corner_verts_for_write();
220
221 const int triangle_loop_start = quads.size() * 4;
222 if (!face_offsets.is_empty()) {
223 blender::offset_indices::fill_constant_group_size(4, 0, face_offsets.take_front(quads.size()));
225 3, triangle_loop_start, face_offsets.drop_front(quads.size()));
226 }
227
228 for (const int i : vert_positions.index_range()) {
229 vert_positions[i] = float3(vertices[i].x(), vertices[i].y(), vertices[i].z());
230 }
231
232 for (const int i : IndexRange(quads.size())) {
233 const int loopstart = i * 4;
234 mesh_corner_verts[loopstart] = quads[i][0];
235 mesh_corner_verts[loopstart + 1] = quads[i][3];
236 mesh_corner_verts[loopstart + 2] = quads[i][2];
237 mesh_corner_verts[loopstart + 3] = quads[i][1];
238 }
239
240 for (const int i : IndexRange(tris.size())) {
241 const int loopstart = triangle_loop_start + i * 3;
242 mesh_corner_verts[loopstart] = tris[i][2];
243 mesh_corner_verts[loopstart + 1] = tris[i][1];
244 mesh_corner_verts[loopstart + 2] = tris[i][0];
245 }
246
247 mesh_calc_edges(*mesh, false, false);
248
249 return mesh;
250}
251#endif
252
254 const float voxel_size,
255 const float adaptivity,
256 const float isovalue)
257{
258#ifdef WITH_OPENVDB
259 openvdb::FloatGrid::Ptr level_set = remesh_voxel_level_set_create(mesh, voxel_size);
260 Mesh *result = remesh_voxel_volume_to_mesh(level_set, isovalue, adaptivity, false);
261 BKE_mesh_copy_parameters(result, mesh);
262 return result;
263#else
264 UNUSED_VARS(mesh, voxel_size, adaptivity, isovalue);
265 return nullptr;
266#endif
267}
268
269namespace blender::bke {
270
271static void calc_edge_centers(const Span<float3> positions,
272 const Span<int2> edges,
273 MutableSpan<float3> edge_centers)
274{
275 for (const int i : edges.index_range()) {
276 edge_centers[i] = math::midpoint(positions[edges[i][0]], positions[edges[i][1]]);
277 }
278}
279
280static void calc_face_centers(const Span<float3> positions,
281 const OffsetIndices<int> faces,
282 const Span<int> corner_verts,
283 MutableSpan<float3> face_centers)
284{
285 for (const int i : faces.index_range()) {
286 face_centers[i] = mesh::face_center_calc(positions, corner_verts.slice(faces[i]));
287 }
288}
289
290static void find_nearest_tris(const Span<float3> positions,
291 BVHTreeFromMesh &bvhtree,
292 MutableSpan<int> tris)
293{
294 for (const int i : positions.index_range()) {
295 BVHTreeNearest nearest;
296 nearest.index = -1;
297 nearest.dist_sq = FLT_MAX;
299 bvhtree.tree, positions[i], &nearest, bvhtree.nearest_callback, &bvhtree);
300 tris[i] = nearest.index;
301 }
302}
303
304static void find_nearest_tris_parallel(const Span<float3> positions,
305 BVHTreeFromMesh &bvhtree,
306 MutableSpan<int> tris)
307{
308 threading::parallel_for(tris.index_range(), 512, [&](const IndexRange range) {
309 find_nearest_tris(positions.slice(range), bvhtree, tris.slice(range));
310 });
311}
312
313static void find_nearest_verts(const Span<float3> positions,
314 const Span<int> corner_verts,
315 const Span<int3> src_corner_tris,
316 const Span<float3> dst_positions,
317 const Span<int> nearest_vert_tris,
318 MutableSpan<int> nearest_verts)
319{
320 threading::parallel_for(dst_positions.index_range(), 512, [&](const IndexRange range) {
321 for (const int dst_vert : range) {
322 const float3 &dst_position = dst_positions[dst_vert];
323 const int3 &src_tri = src_corner_tris[nearest_vert_tris[dst_vert]];
324
325 std::array<float, 3> distances;
326 for (const int i : IndexRange(3)) {
327 const int src_vert = corner_verts[src_tri[i]];
328 distances[i] = math::distance_squared(positions[src_vert], dst_position);
329 }
330
331 const int min = std::min_element(distances.begin(), distances.end()) - distances.begin();
332 nearest_verts[dst_vert] = corner_verts[src_tri[min]];
333 }
334 });
335}
336
337static void find_nearest_faces(const Span<int> src_tri_faces,
338 const Span<float3> dst_positions,
339 const OffsetIndices<int> dst_faces,
340 const Span<int> dst_corner_verts,
341 BVHTreeFromMesh &bvhtree,
342 MutableSpan<int> nearest_faces)
343{
344 struct TLS {
345 Vector<float3> face_centers;
346 Vector<int> tri_indices;
347 };
349 threading::parallel_for(dst_faces.index_range(), 512, [&](const IndexRange range) {
350 threading::isolate_task([&] {
351 TLS &tls = all_tls.local();
352 Vector<float3> &face_centers = tls.face_centers;
353 face_centers.reinitialize(range.size());
354 calc_face_centers(dst_positions, dst_faces.slice(range), dst_corner_verts, face_centers);
355
356 Vector<int> &tri_indices = tls.tri_indices;
357 tri_indices.reinitialize(range.size());
358 find_nearest_tris(face_centers, bvhtree, tri_indices);
359
360 array_utils::gather(src_tri_faces, tri_indices.as_span(), nearest_faces.slice(range));
361 });
362 });
363}
364
365static void find_nearest_corners(const Span<float3> src_positions,
366 const OffsetIndices<int> src_faces,
367 const Span<int> src_corner_verts,
368 const Span<int> src_tri_faces,
369 const Span<float3> dst_positions,
370 const Span<int> dst_corner_verts,
371 const Span<int> nearest_vert_tris,
372 MutableSpan<int> nearest_corners)
373{
374 threading::parallel_for(nearest_corners.index_range(), 512, [&](const IndexRange range) {
375 Vector<float, 64> distances;
376 for (const int dst_corner : range) {
377 const int dst_vert = dst_corner_verts[dst_corner];
378 const float3 &dst_position = dst_positions[dst_vert];
379
380 const int src_tri = nearest_vert_tris[dst_vert];
381 const IndexRange src_face = src_faces[src_tri_faces[src_tri]];
382 const Span<int> src_face_verts = src_corner_verts.slice(src_face);
383
384 /* Find the corner in the face that's closest in the closest face. */
385 distances.reinitialize(src_face_verts.size());
386 for (const int i : src_face_verts.index_range()) {
387 const int src_vert = src_face_verts[i];
388 distances[i] = math::distance_squared(src_positions[src_vert], dst_position);
389 }
390
391 const int min = std::min_element(distances.begin(), distances.end()) - distances.begin();
392 nearest_corners[dst_corner] = src_face[min];
393 }
394 });
395}
396
397static void find_nearest_edges(const Span<float3> src_positions,
398 const Span<int2> src_edges,
399 const OffsetIndices<int> src_faces,
400 const Span<int> src_corner_edges,
401 const Span<int> src_tri_faces,
402 const Span<float3> dst_positions,
403 const Span<int2> dst_edges,
404 BVHTreeFromMesh &bvhtree,
405 MutableSpan<int> nearest_edges)
406{
407 struct TLS {
408 Vector<float3> edge_centers;
409 Vector<int> tri_indices;
410 Vector<int> face_indices;
411 Vector<float> distances;
412 };
414 threading::parallel_for(nearest_edges.index_range(), 512, [&](const IndexRange range) {
415 threading::isolate_task([&] {
416 TLS &tls = all_tls.local();
417 Vector<float3> &edge_centers = tls.edge_centers;
418 edge_centers.reinitialize(range.size());
419 calc_edge_centers(dst_positions, dst_edges.slice(range), edge_centers);
420
421 Vector<int> &tri_indices = tls.tri_indices;
422 tri_indices.reinitialize(range.size());
423 find_nearest_tris_parallel(edge_centers, bvhtree, tri_indices);
424
425 Vector<int> &face_indices = tls.face_indices;
426 face_indices.reinitialize(range.size());
427 array_utils::gather(src_tri_faces, tri_indices.as_span(), face_indices.as_mutable_span());
428
429 /* Find the source edge that's closest to the destination edge in the nearest face. Search
430 * through the whole face instead of just the triangle because the triangle has edges that
431 * might not be actual mesh edges. */
432 Vector<float, 64> distances;
433 for (const int i : range.index_range()) {
434 const int dst_edge = range[i];
435 const float3 &dst_position = edge_centers[i];
436
437 const int src_face = face_indices[i];
438 const Span<int> src_face_edges = src_corner_edges.slice(src_faces[src_face]);
439
440 distances.reinitialize(src_face_edges.size());
441 for (const int i : src_face_edges.index_range()) {
442 const int2 src_edge = src_edges[src_face_edges[i]];
443 const float3 src_center = math::midpoint(src_positions[src_edge[0]],
444 src_positions[src_edge[1]]);
445 distances[i] = math::distance_squared(src_center, dst_position);
446 }
447
448 const int min = std::min_element(distances.begin(), distances.end()) - distances.begin();
449 nearest_edges[dst_edge] = src_face_edges[min];
450 }
451 });
452 });
453}
454
455static void gather_attributes(const Span<StringRef> ids,
456 const AttributeAccessor src_attributes,
457 const AttrDomain domain,
458 const Span<int> index_map,
459 MutableAttributeAccessor dst_attributes)
460{
461 for (const StringRef id : ids) {
462 const GVArraySpan src = *src_attributes.lookup(id, domain);
464 GSpanAttributeWriter dst = dst_attributes.lookup_or_add_for_write_only_span(id, domain, type);
465 attribute_math::gather(src, index_map, dst.span);
466 dst.finish();
467 }
468}
469
471{
472 /* Gather attributes to transfer for each domain. This makes it possible to skip
473 * building index maps and even the main BVH tree if there are no attributes. */
474 const AttributeAccessor src_attributes = src.attributes();
475 Vector<StringRef> point_ids;
476 Vector<StringRef> edge_ids;
477 Vector<StringRef> face_ids;
478 Vector<StringRef> corner_ids;
479 src_attributes.foreach_attribute([&](const AttributeIter &iter) {
480 if (ELEM(iter.name, "position", ".edge_verts", ".corner_vert", ".corner_edge")) {
481 return;
482 }
483 switch (iter.domain) {
484 case AttrDomain::Point:
485 point_ids.append(iter.name);
486 break;
487 case AttrDomain::Edge:
488 edge_ids.append(iter.name);
489 break;
490 case AttrDomain::Face:
491 face_ids.append(iter.name);
492 break;
493 case AttrDomain::Corner:
494 corner_ids.append(iter.name);
495 break;
496 default:
497 BLI_assert_unreachable();
498 break;
499 }
500 });
501
502 if (point_ids.is_empty() && edge_ids.is_empty() && face_ids.is_empty() && corner_ids.is_empty())
503 {
504 return;
505 }
506
507 const Span<float3> src_positions = src.vert_positions();
508 const OffsetIndices src_faces = src.faces();
509 const Span<int> src_corner_verts = src.corner_verts();
510 const Span<int3> src_corner_tris = src.corner_tris();
511
512 /* The main idea in the following code is to trade some complexity in sampling for the benefit of
513 * only using and building a single BVH tree. Since sculpt mode doesn't generally deal with loose
514 * vertices and edges, we use the standard "triangles" BVH which won't contain them. Also, only
515 * relying on a single BVH should reduce memory usage, and work better if the BVH and #pbvh::Tree
516 * are ever merged.
517 *
518 * One key decision is separating building transfer index maps from actually transferring any
519 * attribute data. This is important to keep attribute storage independent from the specifics of
520 * the decisions made here, which mainly results in easier refactoring, more generic code, and
521 * possibly improved performance from lower cache usage in the "complex" sampling part of the
522 * algorithm and the copying itself. */
523 BVHTreeFromMesh bvhtree{};
525
526 const Span<float3> dst_positions = dst.vert_positions();
527 const OffsetIndices dst_faces = dst.faces();
528 const Span<int> dst_corner_verts = dst.corner_verts();
529
530 MutableAttributeAccessor dst_attributes = dst.attributes_for_write();
531
532 if (!point_ids.is_empty() || !corner_ids.is_empty()) {
533 Array<int> vert_nearest_tris(dst_positions.size());
534 find_nearest_tris_parallel(dst_positions, bvhtree, vert_nearest_tris);
535
536 if (!point_ids.is_empty()) {
537 Array<int> map(dst.verts_num);
539 src_positions, src_corner_verts, src_corner_tris, dst_positions, vert_nearest_tris, map);
540 gather_attributes(point_ids, src_attributes, AttrDomain::Point, map, dst_attributes);
541 }
542
543 if (!corner_ids.is_empty()) {
544 const Span<int> src_tri_faces = src.corner_tri_faces();
545 Array<int> map(dst.corners_num);
546 find_nearest_corners(src_positions,
547 src_faces,
548 src_corner_verts,
549 src_tri_faces,
550 dst_positions,
551 dst_corner_verts,
552 vert_nearest_tris,
553 map);
554 gather_attributes(corner_ids, src_attributes, AttrDomain::Corner, map, dst_attributes);
555 }
556 }
557
558 if (!edge_ids.is_empty()) {
559 const Span<int2> src_edges = src.edges();
560 const Span<int> src_corner_edges = src.corner_edges();
561 const Span<int> src_tri_faces = src.corner_tri_faces();
562 const Span<int2> dst_edges = dst.edges();
563 Array<int> map(dst.edges_num);
564 find_nearest_edges(src_positions,
565 src_edges,
566 src_faces,
567 src_corner_edges,
568 src_tri_faces,
569 dst_positions,
570 dst_edges,
571 bvhtree,
572 map);
573 gather_attributes(edge_ids, src_attributes, AttrDomain::Edge, map, dst_attributes);
574 }
575
576 if (!face_ids.is_empty()) {
577 const Span<int> src_tri_faces = src.corner_tri_faces();
578 Array<int> map(dst.faces_num);
579 find_nearest_faces(src_tri_faces, dst_positions, dst_faces, dst_corner_verts, bvhtree, map);
580 gather_attributes(face_ids, src_attributes, AttrDomain::Face, map, dst_attributes);
581 }
582
583 if (src.active_color_attribute) {
585 }
586 if (src.default_color_attribute) {
588 }
589
590 free_bvhtree_from_mesh(&bvhtree);
591}
592
593} // namespace blender::bke
594
596{
597 const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_ME(mesh);
598
599 BMeshCreateParams bmesh_create_params{};
600 bmesh_create_params.use_toolflags = true;
601 BMesh *bm = BM_mesh_create(&allocsize, &bmesh_create_params);
602
603 BMeshFromMeshParams bmesh_from_mesh_params{};
604 bmesh_from_mesh_params.calc_face_normal = true;
605 bmesh_from_mesh_params.calc_vert_normal = true;
606 BM_mesh_bm_from_me(bm, mesh, &bmesh_from_mesh_params);
607
608 BMVert *v;
609 BMEdge *ed, *ed_next;
610 BMFace *f, *f_next;
611 BMIter iter_a, iter_b;
612
613 /* Merge 3 edge poles vertices that exist in the same face */
615 BM_ITER_MESH_MUTABLE (f, f_next, &iter_a, bm, BM_FACES_OF_MESH) {
616 BMVert *v1, *v2;
617 v1 = nullptr;
618 v2 = nullptr;
619 BM_ITER_ELEM (v, &iter_b, f, BM_VERTS_OF_FACE) {
620 if (BM_vert_edge_count(v) == 3) {
621 if (v1) {
622 v2 = v;
623 }
624 else {
625 v1 = v;
626 }
627 }
628 }
629 if (v1 && v2 && (v1 != v2) && !BM_edge_exists(v1, v2)) {
630 BM_face_kill(bm, f);
631 BMEdge *e = BM_edge_create(bm, v1, v2, nullptr, BM_CREATE_NOP);
633 }
634 }
635
636 BM_ITER_MESH_MUTABLE (ed, ed_next, &iter_a, bm, BM_EDGES_OF_MESH) {
638 float co[3];
639 mid_v3_v3v3(co, ed->v1->co, ed->v2->co);
640 BMVert *vc = BM_edge_collapse(bm, ed, ed->v1, true, true);
641 copy_v3_v3(vc->co, co);
642 }
643 }
644
645 /* Delete faces with a 3 edge pole in all their vertices */
647 BM_ITER_MESH (f, &iter_a, bm, BM_FACES_OF_MESH) {
648 bool dissolve = true;
649 BM_ITER_ELEM (v, &iter_b, f, BM_VERTS_OF_FACE) {
650 if (BM_vert_edge_count(v) != 3) {
651 dissolve = false;
652 }
653 }
654 if (dissolve) {
655 BM_ITER_ELEM (v, &iter_b, f, BM_VERTS_OF_FACE) {
657 }
658 }
659 }
661
662 BM_ITER_MESH (ed, &iter_a, bm, BM_EDGES_OF_MESH) {
663 if (BM_edge_face_count(ed) != 2) {
664 BM_elem_flag_set(ed, BM_ELEM_TAG, true);
665 }
666 }
667 BM_mesh_edgenet(bm, false, true);
668
669 /* Smooth the result */
670 for (int i = 0; i < 4; i++) {
671 BM_ITER_MESH (v, &iter_a, bm, BM_VERTS_OF_MESH) {
672 float co[3];
673 zero_v3(co);
674 BM_ITER_ELEM (ed, &iter_b, v, BM_EDGES_OF_VERT) {
675 BMVert *vert = BM_edge_other_vert(ed, v);
676 add_v3_v3(co, vert->co);
677 }
678 mul_v3_fl(co, 1.0f / float(BM_vert_edge_count(v)));
679 mid_v3_v3v3(v->co, v->co, co);
680 }
681 }
682
684
689 "recalc_face_normals faces=%hf",
692
693 BMeshToMeshParams bmesh_to_mesh_params{};
694 bmesh_to_mesh_params.calc_object_remap = false;
695 Mesh *result = BKE_mesh_from_bmesh_nomain(bm, &bmesh_to_mesh_params, mesh);
696
698 return result;
699}
void BKE_id_attributes_default_color_set(struct ID *id, const char *name)
Definition attribute.cc:994
void BKE_id_attributes_active_color_set(struct ID *id, const char *name)
Definition attribute.cc:965
void free_bvhtree_from_mesh(BVHTreeFromMesh *data)
Definition bvhutils.cc:1160
BVHTree * BKE_bvhtree_from_mesh_get(BVHTreeFromMesh *data, const Mesh *mesh, BVHCacheType bvh_cache_type, int tree_type)
Definition bvhutils.cc:899
@ BVHTREE_FROM_CORNER_TRIS
CustomData interface, see also DNA_customdata_types.h.
Mesh * BKE_mesh_from_bmesh_nomain(BMesh *bm, const BMeshToMeshParams *params, const Mesh *me_settings)
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
void BKE_mesh_copy_parameters(Mesh *me_dst, const Mesh *me_src)
int BLI_bvhtree_find_nearest(const BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void zero_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
#define UNUSED_VARS(...)
#define ELEM(...)
Read Guarded memory(de)allocation.
@ BM_ELEM_SELECT
@ BM_ELEM_TAG
void BM_face_kill(BMesh *bm, BMFace *f)
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.
@ BM_CREATE_NOP
Definition bmesh_core.hh:24
void BM_mesh_delete_hflag_context(BMesh *bm, const char hflag, const int type)
void BM_mesh_edgenet(BMesh *bm, const bool use_edge_tag, const bool use_new_face_tag)
#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)
@ BM_EDGES_OF_MESH
@ BM_VERTS_OF_MESH
@ BM_VERTS_OF_FACE
@ BM_FACES_OF_MESH
@ BM_EDGES_OF_VERT
#define BM_ITER_MESH_MUTABLE(ele, ele_next, iter, bm, itype)
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_mesh_elem_hflag_enable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const BMeshCreateParams *params)
BMesh Make Mesh.
#define BMALLOC_TEMPLATE_FROM_ME(...)
void BM_mesh_bm_from_me(BMesh *bm, const Mesh *mesh, const BMeshFromMeshParams *params)
void BM_mesh_normals_update(BMesh *bm)
BMVert * BM_edge_collapse(BMesh *bm, BMEdge *e_kill, BMVert *v_kill, const bool do_del, const bool kill_degenerate_faces)
#define BM_FACE
#define BM_EDGE
#define BM_VERT
bool BMO_op_callf(BMesh *bm, int flag, const char *fmt,...)
#define BMO_FLAG_DEFAULTS
@ BMO_FLAG_RESPECT_HIDE
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
int BM_edge_face_count(const BMEdge *e)
int BM_vert_edge_count(const BMVert *v)
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
static unsigned long seed
Definition btSoftBody.h:39
AttributeSet attributes
const CPPType & type() const
constexpr int64_t size() const
Definition BLI_span.hh:494
constexpr bool is_empty() const
Definition BLI_span.hh:510
constexpr MutableSpan drop_front(const int64_t n) const
Definition BLI_span.hh:608
constexpr IndexRange index_range() const
Definition BLI_span.hh:671
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:630
Span< NewT > constexpr cast() const
Definition BLI_span.hh:419
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:138
constexpr int64_t size() const
Definition BLI_span.hh:253
constexpr IndexRange index_range() const
Definition BLI_span.hh:402
bool is_empty() const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
GAttributeReader lookup(const StringRef attribute_id) const
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
Mesh * BKE_mesh_remesh_voxel_fix_poles(const Mesh *mesh)
Mesh * BKE_mesh_remesh_voxel(const Mesh *mesh, const float voxel_size, const float adaptivity, const float isovalue)
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)
float3 face_center_calc(Span< float3 > vert_positions, Span< int > face_verts)
static void find_nearest_tris_parallel(const Span< float3 > positions, BVHTreeFromMesh &bvhtree, MutableSpan< int > tris)
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, BVHTreeFromMesh &bvhtree, MutableSpan< int > nearest_faces)
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)
eCustomDataType cpp_type_to_custom_data_type(const CPPType &type)
static void find_nearest_verts(const Span< float3 > positions, const Span< int > corner_verts, const Span< int3 > src_corner_tris, const Span< float3 > dst_positions, const Span< int > nearest_vert_tris, MutableSpan< int > nearest_verts)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
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, BVHTreeFromMesh &bvhtree, MutableSpan< int > nearest_edges)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
static void find_nearest_corners(const Span< float3 > src_positions, const OffsetIndices< int > src_faces, const Span< int > src_corner_verts, const Span< int > src_tri_faces, const Span< float3 > dst_positions, const Span< int > dst_corner_verts, const Span< int > nearest_vert_tris, MutableSpan< int > nearest_corners)
static void calc_face_centers(const Span< float3 > positions, const OffsetIndices< int > faces, const Span< int > corner_verts, MutableSpan< float3 > face_centers)
static void find_nearest_tris(const Span< float3 > positions, BVHTreeFromMesh &bvhtree, MutableSpan< int > tris)
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:95
void QFLOW_quadriflow_remesh(QuadriflowRemeshData *qrd, void(*update_cb)(void *, float progress, int *cancel), void *update_cb_data)
#define FLT_MAX
Definition stdcycles.h:14
BMVert * v1
BMVert * v2
float co[3]
BVHTree_NearestPointCallback nearest_callback
int corners_num
int edges_num
char * default_color_attribute
int faces_num
int verts_num
char * active_color_attribute
float z
Definition sky_float3.h:27
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27