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