Blender V4.5
multires_unsubdivide.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include "MEM_guardedalloc.h"
13
14#include "DNA_mesh_types.h"
15#include "DNA_meshdata_types.h"
16#include "DNA_modifier_types.h"
17#include "DNA_object_types.h"
18
19#include "BLI_gsqueue.h"
20#include "BLI_math_vector.h"
21
22#include "BKE_customdata.hh"
23#include "BKE_mesh.hh"
24#include "BKE_multires.hh"
25#include "BKE_subsurf.hh"
26
27#include "bmesh.hh"
28
29#include "multires_reshape.hh"
31
32/* This is done in the following steps:
33 *
34 * - If there are already grids in the original mesh,
35 * convert them from tangent displacement to object space coordinates.
36 * - Assign data-layers to the original mesh to map vertices to a new base mesh.
37 * These data-layers store the indices of the elements in the original mesh.
38 * This way the original indices are
39 * preserved when doing mesh modifications (removing and dissolving vertices)
40 * when building the new base mesh.
41 * - Try to find a lower resolution base mesh. This is done by flood fill operation that tags the
42 * center vertices of the lower level grid.
43 * If the algorithm can tag all vertices correctly,
44 * the lower level base mesh is generated by dissolving the tagged vertices.
45 * - Use the data-layers to map vertices from the base mesh to the original mesh and original to
46 * base mesh.
47 * - Find two adjacent vertices on the base mesh to a given vertex to map that loop from base mesh
48 * to original mesh
49 * - Extract the grid from the original mesh from that loop. If there are no grids in the original
50 * mesh, build the new grid directly from the vertex coordinates by iterating in a grid pattern
51 * over them. If there are grids in the original mesh, iterate in a grid pattern over the faces,
52 * reorder all the coordinates of the grid in that face and copy those coordinates to the new
53 * base mesh grid.
54 * - Copy the new grid data over to a new allocated MDISP layer with the appropriate size to store
55 * the new levels.
56 * - Convert the grid data from object space to tangent displacement.
57 */
58
62static bool is_vertex_in_id(BMVert *v, const int *elem_id, int elem)
63{
64 const int v_index = BM_elem_index_get(v);
65 return elem_id[v_index] == elem;
66}
67
72
78
85static BMVert *unsubdivide_find_any_pole(BMesh *bm, int *elem_id, int elem)
86{
87 BMIter iter;
88 BMVert *v;
89 BMVert *pole = nullptr;
91 if (is_vertex_in_id(v, elem_id, elem) && is_vertex_pole_three(v)) {
92 return v;
93 }
94 if (is_vertex_in_id(v, elem_id, elem) && is_vertex_pole(v)) {
95 pole = v;
96 }
97 }
98 return pole;
99}
100
108{
109 BMIter iter;
110 BMFace *f;
111 BMVert *v;
112 if (bm->totface < 3) {
113 return false;
114 }
115
116 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
117 if (f->len != 4) {
118 return false;
119 }
120 }
121
122 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
123 if (BM_vert_is_wire(v) || (v->e == nullptr)) {
124 return false;
125 }
126 }
127
128 return true;
129}
130
134static bool is_vertex_diagonal(BMVert *from_v, BMVert *to_v)
135{
136 return !BM_edge_exists(from_v, to_v);
137}
138
148static void unsubdivide_face_center_vertex_tag(BMesh *bm, BMVert *initial_vertex)
149{
150 blender::BitVector<> visited_verts(bm->totvert);
151 GSQueue *queue;
152 queue = BLI_gsqueue_new(sizeof(BMVert *));
153
154 /* Add and tag the vertices connected by a diagonal to initial_vertex to the flood fill queue. If
155 * initial_vertex is a pole and there is a valid solution, those vertices should be the (0,0) of
156 * the grids for the loops of initial_vertex. */
157 BMIter iter;
158 BMIter iter_a;
159 BMFace *f;
160 BMVert *neighbor_v;
161 BM_ITER_ELEM (f, &iter, initial_vertex, BM_FACES_OF_VERT) {
162 BM_ITER_ELEM (neighbor_v, &iter_a, f, BM_VERTS_OF_FACE) {
163 int neighbor_vertex_index = BM_elem_index_get(neighbor_v);
164 if (neighbor_v != initial_vertex && is_vertex_diagonal(neighbor_v, initial_vertex)) {
165 BLI_gsqueue_push(queue, &neighbor_v);
166 visited_verts[neighbor_vertex_index].set();
167 BM_elem_flag_set(neighbor_v, BM_ELEM_TAG, true);
168 }
169 }
170 }
171
172 /* Repeat a similar operation for all vertices in the queue. */
173 /* In this case, add to the queue the vertices connected by 2 steps using the diagonals in any
174 * direction. If a solution exists and `initial_vertex` was a pole, this is guaranteed that will
175 * tag all the (0,0) vertices of the grids, and nothing else. */
176 /* If it was not a pole, it may or may not find a solution, even if the solution exists. */
177 while (!BLI_gsqueue_is_empty(queue)) {
178 BMVert *from_v;
179 BLI_gsqueue_pop(queue, &from_v);
180
181 /* Get the diagonals (first connected step) */
182 GSQueue *diagonals;
183 diagonals = BLI_gsqueue_new(sizeof(BMVert *));
184 BM_ITER_ELEM (f, &iter, from_v, BM_FACES_OF_VERT) {
185 BM_ITER_ELEM (neighbor_v, &iter_a, f, BM_VERTS_OF_FACE) {
186 if (neighbor_v != from_v && is_vertex_diagonal(neighbor_v, from_v)) {
187 BLI_gsqueue_push(diagonals, &neighbor_v);
188 }
189 }
190 }
191
192 /* Do the second connected step. This vertices are the ones that are added to the flood fill
193 * queue. */
194 while (!BLI_gsqueue_is_empty(diagonals)) {
195 BMVert *diagonal_v;
196 BLI_gsqueue_pop(diagonals, &diagonal_v);
197 BM_ITER_ELEM (f, &iter, diagonal_v, BM_FACES_OF_VERT) {
198 BM_ITER_ELEM (neighbor_v, &iter_a, f, BM_VERTS_OF_FACE) {
199 int neighbor_vertex_index = BM_elem_index_get(neighbor_v);
200 if (!visited_verts[neighbor_vertex_index] && neighbor_v != diagonal_v &&
201 is_vertex_diagonal(neighbor_v, diagonal_v))
202 {
203 BLI_gsqueue_push(queue, &neighbor_v);
204 visited_verts[neighbor_vertex_index].set(true);
205 BM_elem_flag_set(neighbor_v, BM_ELEM_TAG, true);
206 }
207 }
208 }
209 }
210 BLI_gsqueue_free(diagonals);
211 }
212
213 BLI_gsqueue_free(queue);
214}
215
227static bool unsubdivide_is_center_vertex_tag_valid(BMesh *bm, int *elem_id, int elem)
228{
229 BMVert *v, *neighbor_v;
230 BMIter iter, iter_a, iter_b;
231 BMFace *f;
232
233 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
234 if (is_vertex_in_id(v, elem_id, elem)) {
236 /* Tagged vertex in boundary */
237 if (BM_vert_is_boundary(v)) {
238 return false;
239 }
240 /* Tagged vertex with connected tagged vertex. */
241 BM_ITER_ELEM (f, &iter_a, v, BM_FACES_OF_VERT) {
242 BM_ITER_ELEM (neighbor_v, &iter_b, f, BM_VERTS_OF_FACE) {
243 if (neighbor_v != v && BM_elem_flag_test(neighbor_v, BM_ELEM_TAG)) {
244 return false;
245 }
246 }
247 }
248 }
249 if (BM_vert_is_boundary(v)) {
250 /* Un-tagged vertex in boundary without connected tagged vertices. */
251 bool any_tagged = false;
252 BM_ITER_ELEM (f, &iter_a, v, BM_FACES_OF_VERT) {
253 BM_ITER_ELEM (neighbor_v, &iter_b, f, BM_VERTS_OF_FACE) {
254 if (neighbor_v != v && BM_elem_flag_test(neighbor_v, BM_ELEM_TAG)) {
255 any_tagged = true;
256 }
257 }
258 }
259 if (!any_tagged) {
260 return false;
261 }
262 }
263 }
264 }
265
266 return true;
267}
268
272static bool unsubdivide_tag_disconnected_mesh_element(BMesh *bm, int *elem_id, int elem)
273{
274 /* First, get vertex candidates to try to generate possible un-subdivide solution. */
275 /* Find a vertex pole. If there is a solution on an all quad base mesh, this vertex should be
276 * part of the base mesh. If it isn't, then there is no solution. */
277 GSQueue *initial_vertex = BLI_gsqueue_new(sizeof(BMVert *));
278 BMVert *initial_vertex_pole = unsubdivide_find_any_pole(bm, elem_id, elem);
279 if (initial_vertex_pole != nullptr) {
280 BLI_gsqueue_push(initial_vertex, &initial_vertex_pole);
281 }
282
283 /* Also try from the different 4 vertices of a quad in the current
284 * disconnected element ID. If a solution exists the search should return a valid solution from
285 * one of these vertices. */
286 BMFace *f, *init_face = nullptr;
287 BMVert *v;
288 BMIter iter_a, iter_b;
289 BM_ITER_MESH (f, &iter_a, bm, BM_FACES_OF_MESH) {
290 BM_ITER_ELEM (v, &iter_b, f, BM_VERTS_OF_FACE) {
291 if (is_vertex_in_id(v, elem_id, elem)) {
292 init_face = f;
293 break;
294 }
295 }
296 if (init_face != nullptr) {
297 break;
298 }
299 }
300
301 BM_ITER_ELEM (v, &iter_a, init_face, BM_VERTS_OF_FACE) {
302 BLI_gsqueue_push(initial_vertex, &v);
303 }
304
305 bool valid_tag_found = false;
306
307 /* Check all vertex candidates to a solution. */
308 while (!BLI_gsqueue_is_empty(initial_vertex)) {
309
310 BMVert *iv;
311 BLI_gsqueue_pop(initial_vertex, &iv);
312
313 /* Generate a possible solution. */
315
316 /* Check if the solution is valid. If it is, stop searching. */
317 if (unsubdivide_is_center_vertex_tag_valid(bm, elem_id, elem)) {
318 valid_tag_found = true;
319 break;
320 }
321
322 /* If the solution is not valid, reset the state of all tags in this disconnected element ID
323 * and try again. */
324 BMVert *v_reset;
325 BMIter iter;
326 BM_ITER_MESH (v_reset, &iter, bm, BM_VERTS_OF_MESH) {
327 if (is_vertex_in_id(v_reset, elem_id, elem)) {
328 BM_elem_flag_set(v_reset, BM_ELEM_TAG, false);
329 }
330 }
331 }
332 BLI_gsqueue_free(initial_vertex);
333 return valid_tag_found;
334}
335
339static int unsubdivide_init_elem_ids(BMesh *bm, int *elem_id)
340{
341 bool *visited_verts = MEM_calloc_arrayN<bool>(bm->totvert, "visited vertices");
342 int current_id = 0;
343 for (int i = 0; i < bm->totvert; i++) {
344 if (!visited_verts[i]) {
345 GSQueue *queue;
346 queue = BLI_gsqueue_new(sizeof(BMVert *));
347
348 visited_verts[i] = true;
349 elem_id[i] = current_id;
350 BMVert *iv = BM_vert_at_index(bm, i);
351 BLI_gsqueue_push(queue, &iv);
352
353 while (!BLI_gsqueue_is_empty(queue)) {
354 BMIter iter;
355 BMVert *current_v, *neighbor_v;
356 BMEdge *ed;
357 BLI_gsqueue_pop(queue, &current_v);
358 BM_ITER_ELEM (ed, &iter, current_v, BM_EDGES_OF_VERT) {
359 neighbor_v = BM_edge_other_vert(ed, current_v);
360 const int neighbor_index = BM_elem_index_get(neighbor_v);
361 if (!visited_verts[neighbor_index]) {
362 visited_verts[neighbor_index] = true;
363 elem_id[neighbor_index] = current_id;
364 BLI_gsqueue_push(queue, &neighbor_v);
365 }
366 }
367 }
368 current_id++;
369 BLI_gsqueue_free(queue);
370 }
371 }
372 MEM_freeN(visited_verts);
373 return current_id;
374}
375
381{
382 BMVert *v;
383 BMIter iter;
384
385 /* Stores the vertices which correspond to (1, 0) and (0, 1) of the grids in the select flag. */
387 BMVert *v_neighbor;
388 BMIter iter_a;
389 BMEdge *ed;
390 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
391 BM_ITER_ELEM (ed, &iter_a, v, BM_EDGES_OF_VERT) {
392 v_neighbor = BM_edge_other_vert(ed, v);
393 if (BM_elem_flag_test(v_neighbor, BM_ELEM_TAG)) {
395 }
396 }
397 }
398
399 /* Dissolves the (0,0) vertices of the grids. */
402 "dissolve_verts verts=%hv use_face_split=%b use_boundary_tear=%b",
404 false,
405 true);
406
408
409 /* Copy the select flag to the tag flag. */
410 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
413 }
414 }
415
416 /* Dissolves the (1,0) and (0,1) vertices of the grids. */
419 "dissolve_verts verts=%hv use_face_split=%b use_boundary_tear=%b",
421 false,
422 true);
423}
424
435{
436
437 /* Do a first check to make sure that it makes sense to search for un-subdivision in this mesh.
438 */
440 return false;
441 };
442
443 /* Initialize the vertex table. */
446
447 /* Build disconnected elements IDs. Each disconnected mesh element is evaluated separately. */
448 int *elem_id = MEM_calloc_arrayN<int>(bm->totvert, " ELEM ID");
449 const int tot_ids = unsubdivide_init_elem_ids(bm, elem_id);
450
451 bool valid_tag_found = true;
452
453 /* Reset the #BMesh flags as they are used to store data during the un-subdivide process.
454 * Un-hiding all faces is important so the entire mesh is handled, see: #126633. */
457
458 /* For each disconnected mesh element ID, search if an un-subdivide solution is possible. The
459 * whole un-subdivide process fails if a single disconnected mesh element fails. */
460 for (int id = 0; id < tot_ids; id++) {
461 /* Try to the #BMesh vertex flag tags corresponding to an un-subdivide solution. */
463 valid_tag_found = false;
464 break;
465 }
466 }
467
468 /* If a solution was found for all elements IDs, build the new base mesh using the solution
469 * stored in the BMVert tags. */
470 if (valid_tag_found) {
472 }
473
474 MEM_freeN(elem_id);
475 return valid_tag_found;
476}
477
481static BMEdge *edge_step(BMVert *v, BMEdge *edge, BMVert **r_next_vertex)
482{
483 BMIter iter;
484 BMEdge *test_edge;
485 if (edge == nullptr) {
486 (*r_next_vertex) = v;
487 return edge;
488 }
489 (*r_next_vertex) = BM_edge_other_vert(edge, v);
490 BM_ITER_ELEM (test_edge, &iter, (*r_next_vertex), BM_EDGES_OF_VERT) {
491 if (!BM_edge_share_quad_check(test_edge, edge)) {
492 return test_edge;
493 }
494 }
495 return nullptr;
496}
497
498static BMFace *face_step(BMEdge *edge, BMFace *f)
499{
500 BMIter iter;
501 BMFace *face_iter;
502
503 BM_ITER_ELEM (face_iter, &iter, edge, BM_FACES_OF_EDGE) {
504 if (BM_face_share_edge_check(face_iter, f)) {
505 return face_iter;
506 }
507 }
508 return f;
509}
510
515static BMEdge *get_initial_edge_y(BMFace *f, BMEdge *edge_x, BMVert *initial_vertex)
516{
517 BMIter iter;
518 BMEdge *test_edge;
519 BM_ITER_ELEM (test_edge, &iter, f, BM_EDGES_OF_FACE) {
520 if (edge_x != test_edge) {
521 if (test_edge->v1 != initial_vertex && test_edge->v2 == initial_vertex) {
522 return test_edge;
523 }
524 if (test_edge->v2 != initial_vertex && test_edge->v1 == initial_vertex) {
525 return test_edge;
526 }
527 }
528 }
529 return nullptr;
530}
531
536 float (*face_grid)[3], MDisps *mdisp, int face_grid_size, int orig_grid_size, int loop)
537{
538 int origin[2];
539 int step_x[2];
540 int step_y[2];
541
542 const int grid_offset = orig_grid_size - 1;
543 origin[0] = grid_offset;
544 origin[1] = grid_offset;
545
546 switch (loop) {
547 case 0:
548 step_x[0] = -1;
549 step_x[1] = 0;
550
551 step_y[0] = 0;
552 step_y[1] = -1;
553
554 break;
555 case 1:
556 step_x[0] = 0;
557 step_x[1] = 1;
558
559 step_y[0] = -1;
560 step_y[1] = -0;
561 break;
562 case 2:
563 step_x[0] = 1;
564 step_x[1] = 0;
565
566 step_y[0] = 0;
567 step_y[1] = 1;
568 break;
569 case 3:
570 step_x[0] = 0;
571 step_x[1] = -1;
572
573 step_y[0] = 1;
574 step_y[1] = 0;
575 break;
576 default:
577 BLI_assert_msg(0, "Should never happen");
578 break;
579 }
580
581 for (int y = 0; y < orig_grid_size; y++) {
582 for (int x = 0; x < orig_grid_size; x++) {
583 const int remap_x = origin[1] + (step_x[1] * x) + (step_y[1] * y);
584 const int remap_y = origin[0] + (step_x[0] * x) + (step_y[0] * y);
585
586 const int final_index = remap_x + remap_y * face_grid_size;
587 copy_v3_v3(face_grid[final_index], mdisp->disps[x + y * orig_grid_size]);
588 }
589 }
590}
591
597 float (*face_grid)[3],
598 int face_grid_size,
599 int gunsub_x,
600 int gunsub_y)
601{
602 const int grid_it = face_grid_size - 1;
603 for (int y = 0; y < face_grid_size; y++) {
604 for (int x = 0; x < face_grid_size; x++) {
605 const int remap_x = (grid_it * gunsub_x) + x;
606 const int remap_y = (grid_it * gunsub_y) + y;
607
608 const int remap_index_y = grid->grid_size - remap_x - 1;
609 const int remap_index_x = grid->grid_size - remap_y - 1;
610 const int grid_index = remap_index_x + (remap_index_y * grid->grid_size);
611 copy_v3_v3(grid->grid_co[grid_index], face_grid[x + y * face_grid_size]);
612 }
613 }
614}
615
624 BMVert *v,
625 BMFace *f,
626 int grid_x,
627 int grid_y)
628{
629 Mesh *original_mesh = context->original_mesh;
630 const blender::OffsetIndices faces = original_mesh->faces();
631 const blender::Span<int> corner_verts = original_mesh->corner_verts();
633
634 const int corner_vertex_index = BM_elem_index_get(v);
635
636 /* Calculates an offset to write the grids correctly oriented in the main
637 * #MultiresUnsubdivideGrid. */
638 int loop_offset = 0;
639 for (int i = 0; i < face.size(); i++) {
640 const int loop_index = face[i];
641 if (corner_verts[loop_index] == corner_vertex_index) {
642 loop_offset = i;
643 break;
644 }
645 }
646
647 /* Write the 4 grids of the current quad with the right orientation into the face_grid buffer. */
648 const int grid_size = BKE_ccg_gridsize(context->num_original_levels);
649 const int face_grid_size = BKE_ccg_gridsize(context->num_original_levels + 1);
650 const int face_grid_area = face_grid_size * face_grid_size;
651 float(*face_grid)[3] = MEM_calloc_arrayN<float[3]>(face_grid_area, "face_grid");
652
653 for (int i = 0; i < face.size(); i++) {
654 const int loop_index = face[i];
655 MDisps *mdisp = &context->original_mdisp[loop_index];
656 int quad_loop = i - loop_offset;
657 if (quad_loop < 0) {
658 quad_loop += 4;
659 }
660 if (quad_loop >= 4) {
661 quad_loop -= 4;
662 }
663 write_loop_in_face_grid(face_grid, mdisp, face_grid_size, grid_size, quad_loop);
664 }
665
666 /* Write the face_grid buffer in the correct position in the #MultiresUnsubdivideGrids that is
667 * being extracted. */
668 write_face_grid_in_unsubdivide_grid(grid, face_grid, face_grid_size, grid_x, grid_y);
669
670 MEM_freeN(face_grid);
671}
672
677static void store_vertex_data(MultiresUnsubdivideGrid *grid, BMVert *v, int grid_x, int grid_y)
678{
679 const int remap_index_y = grid->grid_size - 1 - grid_x;
680 const int remap_index_x = grid->grid_size - 1 - grid_y;
681
682 const int grid_index = remap_index_x + (remap_index_y * grid->grid_size);
683
684 copy_v3_v3(grid->grid_co[grid_index], v->co);
685}
686
692 BMFace *f1,
693 BMEdge *e1,
694 bool flip_grid,
696{
697 BMVert *initial_vertex;
698 BMEdge *initial_edge_x;
699 BMEdge *initial_edge_y;
700
701 const int grid_size = BKE_ccg_gridsize(context->num_new_levels);
702 const int unsubdiv_grid_size = grid->grid_size = BKE_ccg_gridsize(context->num_total_levels);
703 BLI_assert(grid->grid_co == nullptr);
704 grid->grid_size = unsubdiv_grid_size;
706 size_t(unsubdiv_grid_size) * size_t(unsubdiv_grid_size), "grids coordinates");
707
708 /* Get the vertex on the corner of the grid. This vertex was tagged previously as it also exist
709 * on the base mesh. */
710 initial_edge_x = e1;
711 if (BM_elem_flag_test(initial_edge_x->v1, BM_ELEM_TAG)) {
712 initial_vertex = initial_edge_x->v1;
713 }
714 else {
715 initial_vertex = initial_edge_x->v2;
716 }
717
718 /* From that vertex, get the edge that defines the grid Y axis for extraction. */
719 initial_edge_y = get_initial_edge_y(f1, initial_edge_x, initial_vertex);
720
721 if (flip_grid) {
722 BMEdge *edge_temp;
723 edge_temp = initial_edge_x;
724 initial_edge_x = initial_edge_y;
725 initial_edge_y = edge_temp;
726 }
727
728 int grid_x = 0;
729 int grid_y = 0;
730
731 BMVert *current_vertex_x = initial_vertex;
732 BMEdge *edge_x = initial_edge_x;
733
734 BMVert *current_vertex_y = initial_vertex;
735 BMEdge *edge_y = initial_edge_y;
736 BMEdge *prev_edge_y = initial_edge_y;
737
738 BMFace *current_face = f1;
739 BMFace *grid_face = f1;
740
741 /* If the data is going to be extracted from the already existing grids, there is no need to go
742 * to the last vertex of the iteration as that coordinate is also included in the grids
743 * corresponding to the loop of the face of the previous iteration. */
744 int grid_iteration_max_steps = grid_size;
745 if (context->num_original_levels > 0) {
746 grid_iteration_max_steps = grid_size - 1;
747 }
748
749 /* Iterate over the mesh vertices in a grid pattern using the axis defined by the two initial
750 * edges. */
751 while (grid_y < grid_iteration_max_steps) {
752
753 grid_face = current_face;
754
755 while (grid_x < grid_iteration_max_steps) {
756 if (context->num_original_levels == 0) {
757 /* If there were no grids on the original mesh, extract the data directly from the
758 * vertices. */
759 store_vertex_data(grid, current_vertex_x, grid_x, grid_y);
760 edge_x = edge_step(current_vertex_x, edge_x, &current_vertex_x);
761 }
762 else {
763 /* If there were grids in the original mesh, extract the data from the grids and iterate
764 * over the faces. */
765 store_grid_data(context, grid, current_vertex_x, grid_face, grid_x, grid_y);
766 edge_x = edge_step(current_vertex_x, edge_x, &current_vertex_x);
767 grid_face = face_step(edge_x, grid_face);
768 }
769
770 grid_x++;
771 }
772 grid_x = 0;
773
774 edge_y = edge_step(current_vertex_y, edge_y, &current_vertex_y);
775 current_vertex_x = current_vertex_y;
776
777 /* Get the next edge_x to extract the next row of the grid. This needs to be done because there
778 * may be two edges connected to current_vertex_x that belong to two different grids. */
779 BMIter iter;
780 BMEdge *ed;
781 BMFace *f;
782 BM_ITER_ELEM (ed, &iter, current_vertex_x, BM_EDGES_OF_VERT) {
783 if (ed != prev_edge_y && BM_edge_in_face(ed, current_face)) {
784 edge_x = ed;
785 break;
786 }
787 }
788 BM_ITER_ELEM (f, &iter, edge_x, BM_FACES_OF_EDGE) {
789 if (f != current_face) {
790 current_face = f;
791 break;
792 }
793 }
794
795 prev_edge_y = edge_y;
796 grid_y++;
797 }
798}
799
807 BMEdge *e1,
808 BMVert **r_corner_x,
809 BMVert **r_corner_y)
810{
811 BMVert *initial_vertex;
812 BMEdge *initial_edge_x;
813 BMEdge *initial_edge_y;
814
815 initial_edge_x = e1;
816 if (BM_elem_flag_test(initial_edge_x->v1, BM_ELEM_TAG)) {
817 initial_vertex = initial_edge_x->v1;
818 }
819 else {
820 initial_vertex = initial_edge_x->v2;
821 }
822
823 /* From that vertex, get the edge that defines the grid Y axis for extraction. */
824 initial_edge_y = get_initial_edge_y(f1, initial_edge_x, initial_vertex);
825
826 BMVert *current_vertex_x = initial_vertex;
827 BMEdge *edge_x = initial_edge_x;
828
829 BMVert *current_vertex_y = initial_vertex;
830 BMEdge *edge_y = initial_edge_y;
831
832 /* Do an edge step until it finds a tagged vertex, which is part of the base mesh. */
833 /* x axis */
834 edge_x = edge_step(current_vertex_x, edge_x, &current_vertex_x);
835 while (!BM_elem_flag_test(current_vertex_x, BM_ELEM_TAG)) {
836 edge_x = edge_step(current_vertex_x, edge_x, &current_vertex_x);
837 }
838 *r_corner_x = current_vertex_x;
839
840 /* Same for y axis */
841 edge_y = edge_step(current_vertex_y, edge_y, &current_vertex_y);
842 while (!BM_elem_flag_test(current_vertex_y, BM_ELEM_TAG)) {
843 edge_y = edge_step(current_vertex_y, edge_y, &current_vertex_y);
844 }
845 *r_corner_y = current_vertex_y;
846}
847
849{
850 const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_ME(mesh);
851
852 BMeshCreateParams bm_create_params{};
853 bm_create_params.use_toolflags = true;
854 BMesh *bm = BM_mesh_create(&allocsize, &bm_create_params);
855
856 BMeshFromMeshParams bm_from_me_params{};
857 bm_from_me_params.calc_face_normal = true;
858 bm_from_me_params.calc_vert_normal = true;
859 BM_mesh_bm_from_me(bm, mesh, &bm_from_me_params);
860
861 return bm;
862}
863
864/* Data-layer names to store the original indices of the elements before modifying the mesh. */
865static const char lname[] = "l_remap_index";
866static const char vname[] = "v_remap_index";
867
869{
870 const int l_layer_index = CustomData_get_named_layer_index(
872 if (l_layer_index != -1) {
873 CustomData_free_layer(&mesh->corner_data, CD_PROP_INT32, l_layer_index);
874 }
875
876 const int v_layer_index = CustomData_get_named_layer_index(
877 &mesh->vert_data, CD_PROP_INT32, vname);
878 if (v_layer_index != -1) {
879 CustomData_free_layer(&mesh->vert_data, CD_PROP_INT32, v_layer_index);
880 }
881}
882
888{
890
891 int *l_index = static_cast<int *>(CustomData_add_layer_named(
893
894 int *v_index = static_cast<int *>(CustomData_add_layer_named(
896
897 /* Initialize these data-layer with the indices in the current mesh. */
898 for (int i = 0; i < mesh->corners_num; i++) {
899 l_index[i] = i;
900 }
901 for (int i = 0; i < mesh->verts_num; i++) {
902 v_index[i] = i;
903 }
904}
905
908{
909 Mesh *original_mesh = context->original_mesh;
910
911 Mesh *base_mesh = context->base_mesh;
912
913 BMesh *bm_original_mesh = context->bm_original_mesh = get_bmesh_from_mesh(original_mesh);
914
915 /* Initialize the elem tables. */
916 BM_mesh_elem_table_ensure(bm_original_mesh, BM_VERT | BM_EDGE | BM_FACE);
917
918 /* Disable all flags. */
919 BM_mesh_elem_hflag_disable_all(bm_original_mesh,
922 false);
923
924 /* Get the mapping data-layer. */
925 context->base_to_orig_vmap = static_cast<const int *>(
927
928 /* Tag the base mesh vertices in the original mesh. */
929 for (int i = 0; i < base_mesh->verts_num; i++) {
930 int vert_basemesh_index = context->base_to_orig_vmap[i];
931 BMVert *v = BM_vert_at_index(bm_original_mesh, vert_basemesh_index);
933 }
934
935 context->loop_to_face_map = original_mesh->corner_to_face_map();
936}
937
943 const blender::Span<int> corner_verts,
944 int face_index,
945 int loop,
946 int v_x)
947{
948 const blender::IndexRange face = faces[face_index];
949
950 const int v_first = corner_verts[face.start()];
951 if ((loop == (face.start() + (face.size() - 1))) && v_first == v_x) {
952 return true;
953 }
954
955 int next_l_index = loop + 1;
956 if (next_l_index < face.start() + face.size()) {
957 const int v_next = corner_verts[next_l_index];
958 if (v_next == v_x) {
959 return true;
960 }
961 }
962
963 return false;
964}
965
967{
968 Mesh *original_mesh = context->original_mesh;
969 Mesh *base_mesh = context->base_mesh;
970
971 BMesh *bm_original_mesh = context->bm_original_mesh;
972
973 context->num_grids = base_mesh->corners_num;
974 context->base_mesh_grids = MEM_calloc_arrayN<MultiresUnsubdivideGrid>(
975 size_t(base_mesh->corners_num), "grids");
976
977 /* Based on the existing indices in the data-layers, generate two vertex indices maps. */
978 /* From vertex index in original to vertex index in base and from vertex index in base to vertex
979 * index in original. */
980 int *orig_to_base_vmap = MEM_calloc_arrayN<int>(bm_original_mesh->totvert, "orig vmap");
981 int *base_to_orig_vmap = MEM_calloc_arrayN<int>(base_mesh->verts_num, "base vmap");
982
983 context->base_to_orig_vmap = static_cast<const int *>(
985 for (int i = 0; i < base_mesh->verts_num; i++) {
986 base_to_orig_vmap[i] = context->base_to_orig_vmap[i];
987 }
988
989 /* If an index in original does not exist in base (it was dissolved when creating the new base
990 * mesh, return -1. */
991 for (int i = 0; i < original_mesh->verts_num; i++) {
992 orig_to_base_vmap[i] = -1;
993 }
994
995 for (int i = 0; i < base_mesh->verts_num; i++) {
996 const int orig_vertex_index = context->base_to_orig_vmap[i];
997 orig_to_base_vmap[orig_vertex_index] = i;
998 }
999
1000 /* Add the original data-layers to the base mesh to have the loop indices stored in a data-layer,
1001 * so they can be used from #BMesh. */
1003
1004 BMesh *bm_base_mesh = get_bmesh_from_mesh(base_mesh);
1005 BMIter iter, iter_a, iter_b;
1006 BMVert *v;
1007 BMLoop *l, *lb;
1008
1009 BM_mesh_elem_table_ensure(bm_base_mesh, BM_VERT | BM_FACE);
1010
1011 /* Get the data-layer that contains the loops indices. */
1012 const int base_l_offset = CustomData_get_offset_named(
1013 &bm_base_mesh->ldata, CD_PROP_INT32, lname);
1014
1015 const blender::OffsetIndices faces = base_mesh->faces();
1016 const blender::Span<int> corner_verts = base_mesh->corner_verts();
1017
1018 /* Main loop for extracting the grids. Iterates over the base mesh vertices. */
1019 BM_ITER_MESH (v, &iter, bm_base_mesh, BM_VERTS_OF_MESH) {
1020
1021 /* For each base mesh vertex, get the corresponding #BMVert of the original mesh using the
1022 * vertex map. */
1023 const int orig_vertex_index = base_to_orig_vmap[BM_elem_index_get(v)];
1024 BMVert *vert_original = BM_vert_at_index(bm_original_mesh, orig_vertex_index);
1025
1026 /* Iterate over the loops of that vertex in the original mesh. */
1027 BM_ITER_ELEM (l, &iter_a, vert_original, BM_LOOPS_OF_VERT) {
1028 /* For each loop, get the two vertices that should map to the l+1 and l-1 vertices in the
1029 * base mesh of the face of grid that is going to be extracted. */
1030 BMVert *corner_x, *corner_y;
1031 multires_unsubdivide_get_grid_corners_on_base_mesh(l->f, l->e, &corner_x, &corner_y);
1032
1033 /* Map the two obtained vertices to the base mesh. */
1034 const int corner_x_index = orig_to_base_vmap[BM_elem_index_get(corner_x)];
1035 const int corner_y_index = orig_to_base_vmap[BM_elem_index_get(corner_y)];
1036 if (corner_x_index < 0 || corner_y_index < 0) {
1037 continue;
1038 }
1039
1040 /* Iterate over the loops of the same vertex in the base mesh. With the previously obtained
1041 * vertices and the current vertex it is possible to get the index of the loop in the base
1042 * mesh the grid that is going to be extracted belongs to. */
1043 BM_ITER_ELEM (lb, &iter_b, v, BM_LOOPS_OF_VERT) {
1044 BMFace *base_face = lb->f;
1045 BMVert *base_corner_x = BM_vert_at_index(bm_base_mesh, corner_x_index);
1046 BMVert *base_corner_y = BM_vert_at_index(bm_base_mesh, corner_y_index);
1047 /* If this is the correct loop in the base mesh, the original vertex and the two corners
1048 * should be in the loop's face. */
1049 if (BM_vert_in_face(base_corner_x, base_face) && BM_vert_in_face(base_corner_y, base_face))
1050 {
1051 /* Get the index of the loop. */
1052 const int base_mesh_loop_index = BM_ELEM_CD_GET_INT(lb, base_l_offset);
1053 const int base_mesh_face_index = BM_elem_index_get(base_face);
1054
1055 /* Check the orientation of the loops in case that is needed to flip the x and y axis
1056 * when extracting the grid. */
1057 const bool flip_grid = multires_unsubdivide_flip_grid_x_axis(
1058 faces, corner_verts, base_mesh_face_index, base_mesh_loop_index, corner_x_index);
1059
1060 /* Extract the grid for that loop. */
1061 MultiresUnsubdivideGrid *grid = &context->base_mesh_grids[base_mesh_loop_index];
1062 if (UNLIKELY(grid->grid_co != nullptr)) {
1063 /* It's possible this grid has already been initialized which occurs when quads
1064 * share two edge, while not so common it happens with "Suzanne's" nose,
1065 * see: #126633 & run un-subdivide.
1066 *
1067 * Continue here instead of breaking as logically: quads sharing 2 edges
1068 * will share 3 vertices and those 3 vertices may be attached to any number of quads.
1069 * So in this case, continue scanning instead of breaking out of the loop
1070 * because the `lb` to extract a grid from has not yet been encountered. */
1071 continue;
1072 }
1073
1074 grid->grid_index = base_mesh_loop_index;
1076 context, l->f, l->e, !flip_grid, grid);
1077
1078 break;
1079 }
1080 }
1081 }
1082 }
1083
1084 MEM_freeN(orig_to_base_vmap);
1085 MEM_freeN(base_to_orig_vmap);
1086
1087 BM_mesh_free(bm_base_mesh);
1089}
1090
1092{
1093 if (context->bm_original_mesh != nullptr) {
1094 BM_mesh_free(context->bm_original_mesh);
1095 }
1096}
1097
1099 Mesh *original_mesh,
1101{
1102 context->original_mesh = original_mesh;
1103 context->num_new_levels = 0;
1104 context->num_total_levels = 0;
1105 context->num_original_levels = mmd->totlvl;
1106}
1107
1109{
1110 Mesh *original_mesh = context->original_mesh;
1111
1112 /* Prepare the data-layers to map base to original. */
1114 BMesh *bm_base_mesh = get_bmesh_from_mesh(original_mesh);
1115
1116 /* Un-subdivide as many iterations as possible. */
1117 context->num_new_levels = 0;
1118 int num_levels_left = context->max_new_levels;
1119 while (num_levels_left > 0 && multires_unsubdivide_single_level(bm_base_mesh)) {
1120 context->num_new_levels++;
1121 num_levels_left--;
1122 }
1123
1124 /* If no un-subdivide steps were possible, free the bmesh, the map data-layers and stop. */
1125 if (context->num_new_levels == 0) {
1127 BM_mesh_free(bm_base_mesh);
1128 return false;
1129 }
1130
1131 /* Calculate the final levels for the new grids over base mesh. */
1132 context->num_total_levels = context->num_new_levels + context->num_original_levels;
1133
1134 /* Store the new base-mesh as a mesh in context, free bmesh.
1135 * NOTE(@ideasman42): passing the original mesh in the template is important so mesh settings &
1136 * vertex groups are kept, see: #93911. */
1137 context->base_mesh = BKE_mesh_new_nomain_from_template(original_mesh, 0, 0, 0, 0);
1138
1139 /* De-select all.
1140 * The user-selection has been overwritten and this selection has not been flushed. */
1142
1143 BMeshToMeshParams bm_to_me_params{};
1144 bm_to_me_params.calc_object_remap = true;
1145 BM_mesh_bm_to_me(nullptr, bm_base_mesh, context->base_mesh, &bm_to_me_params);
1146 BM_mesh_free(bm_base_mesh);
1147
1148 /* Initialize bmesh and maps for the original mesh and extract the grids. */
1149
1152
1153 return true;
1154}
1155
1157{
1159 for (int i = 0; i < context->num_grids; i++) {
1160 if (context->base_mesh_grids[i].grid_size > 0) {
1161 MEM_SAFE_FREE(context->base_mesh_grids[i].grid_co);
1162 }
1163 }
1164 MEM_SAFE_FREE(context->base_mesh_grids);
1165}
1166
1172 Mesh *base_mesh)
1173{
1174 /* Free the current MDISPS and create a new ones. */
1175 if (CustomData_has_layer(&base_mesh->corner_data, CD_MDISPS)) {
1177 }
1178 MDisps *mdisps = static_cast<MDisps *>(CustomData_add_layer(
1179 &base_mesh->corner_data, CD_MDISPS, CD_SET_DEFAULT, base_mesh->corners_num));
1180
1181 const int totdisp = pow_i(BKE_ccg_gridsize(context->num_total_levels), 2);
1182 const int totloop = base_mesh->corners_num;
1183
1184 BLI_assert(base_mesh->corners_num == context->num_grids);
1185
1186 /* Allocate the MDISPS grids and copy the extracted data from context. */
1187 for (int i = 0; i < totloop; i++) {
1188 float(*disps)[3] = MEM_calloc_arrayN<float[3]>(totdisp, __func__);
1189
1190 if (mdisps[i].disps) {
1191 MEM_freeN(mdisps[i].disps);
1192 }
1193
1194 for (int j = 0; j < totdisp; j++) {
1195 if (context->base_mesh_grids[i].grid_co) {
1196 copy_v3_v3(disps[j], context->base_mesh_grids[i].grid_co[j]);
1197 }
1198 }
1199
1200 mdisps[i].disps = disps;
1201 mdisps[i].totdisp = totdisp;
1202 mdisps[i].level = context->num_total_levels;
1203 }
1204}
1205
1207 Object *object,
1209 int rebuild_limit,
1210 bool switch_view_to_lower_level)
1211{
1212 Mesh *mesh = static_cast<Mesh *>(object->data);
1213
1215
1216 MultiresUnsubdivideContext unsubdiv_context{};
1217 MultiresReshapeContext reshape_context{};
1218
1219 multires_unsubdivide_context_init(&unsubdiv_context, mesh, mmd);
1220
1221 /* Convert and store the existing grids in object space if available. */
1222 if (mmd->totlvl != 0) {
1223 if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
1224 return 0;
1225 }
1226
1227 multires_reshape_store_original_grids(&reshape_context);
1229 unsubdiv_context.original_mdisp = reshape_context.mdisps;
1230 }
1231
1232 /* Set the limit for the levels that should be rebuild. */
1233 unsubdiv_context.max_new_levels = rebuild_limit;
1234
1235 /* Un-subdivide and create the data for the new grids. */
1236 if (multires_unsubdivide_to_basemesh(&unsubdiv_context) == 0) {
1237 /* If there was no possible to rebuild any level, free the data and return. */
1238 if (mmd->totlvl != 0) {
1240 multires_unsubdivide_context_free(&unsubdiv_context);
1241 }
1242 multires_reshape_context_free(&reshape_context);
1243 return 0;
1244 }
1245
1246 /* Free the reshape context used to convert the data from the original grids to object space. */
1247 if (mmd->totlvl != 0) {
1248 multires_reshape_context_free(&reshape_context);
1249 }
1250
1251 /* Copy the new base mesh to the original mesh. */
1252 Mesh *base_mesh = static_cast<Mesh *>(object->data);
1253 BKE_mesh_nomain_to_mesh(unsubdiv_context.base_mesh, base_mesh, object);
1254 multires_create_grids_in_unsubdivided_base_mesh(&unsubdiv_context, base_mesh);
1255
1256 /* Update the levels in the modifier. Force always to display at level 0 as it contains the new
1257 * created level. */
1258 mmd->totlvl = char(unsubdiv_context.num_total_levels);
1259
1260 if (switch_view_to_lower_level) {
1261 mmd->sculptlvl = 0;
1262 mmd->lvl = 0;
1263 }
1264 else {
1265 mmd->sculptlvl = char(mmd->sculptlvl + unsubdiv_context.num_new_levels);
1266 mmd->lvl = char(mmd->lvl + unsubdiv_context.num_new_levels);
1267 }
1268
1269 mmd->renderlvl = char(mmd->renderlvl + unsubdiv_context.num_new_levels);
1270
1271 /* Create a reshape context to convert the MDISPS data to tangent displacement. It can be the
1272 * same as the previous one as a new Subdivision needs to be created for the new base mesh. */
1273 if (!multires_reshape_context_create_from_base_mesh(&reshape_context, depsgraph, object, mmd)) {
1274 return 0;
1275 }
1277 multires_reshape_context_free(&reshape_context);
1278
1279 /* Free the un-subdivide context and return the total number of levels that were rebuild. */
1280 const int rebuild_subdvis = unsubdiv_context.num_new_levels;
1281 multires_unsubdivide_context_free(&unsubdiv_context);
1282
1283 return rebuild_subdvis;
1284}
CustomData interface, see also DNA_customdata_types.h.
@ CD_SET_DEFAULT
const void * CustomData_get_layer_named(const CustomData *data, eCustomDataType type, blender::StringRef name)
void * CustomData_add_layer_named(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem, blender::StringRef name)
bool CustomData_free_layer(CustomData *data, eCustomDataType type, int index)
int CustomData_get_named_layer_index(const CustomData *data, eCustomDataType type, blender::StringRef name)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, blender::StringRef name)
void CustomData_free_layers(CustomData *data, eCustomDataType type)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
Mesh * BKE_mesh_new_nomain_from_template(const Mesh *me_src, int verts_num, int edges_num, int faces_num, int corners_num)
void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, Mesh *mesh_dst, Object *ob)
void multires_force_sculpt_rebuild(Object *object)
Definition multires.cc:445
int BKE_ccg_gridsize(int level)
Definition CCGSubSurf.cc:24
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
void BLI_gsqueue_free(GSQueue *queue)
Definition gsqueue.cc:93
void BLI_gsqueue_push(GSQueue *queue, const void *item)
Definition gsqueue.cc:100
void BLI_gsqueue_pop(GSQueue *queue, void *r_item)
Definition gsqueue.cc:135
GSQueue * BLI_gsqueue_new(size_t elem_size)
Definition gsqueue.cc:72
struct _GSQueue GSQueue
Definition BLI_gsqueue.h:13
bool BLI_gsqueue_is_empty(const GSQueue *queue)
Definition gsqueue.cc:163
int pow_i(int base, int exp)
Definition math_base.cc:13
MINLINE void copy_v3_v3(float r[3], const float a[3])
#define UNLIKELY(x)
@ CD_PROP_INT32
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
@ BM_ELEM_HIDDEN
@ BM_ELEM_SELECT
@ BM_ELEM_TAG
#define BM_ELEM_CD_GET_INT(ele, offset)
#define BM_elem_index_get(ele)
#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_FACES_OF_EDGE
@ BM_FACES_OF_VERT
@ BM_VERTS_OF_MESH
@ BM_VERTS_OF_FACE
@ BM_FACES_OF_MESH
@ BM_LOOPS_OF_VERT
@ BM_EDGES_OF_VERT
@ BM_EDGES_OF_FACE
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.
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
void BM_mesh_elem_table_init(BMesh *bm, const char htype)
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const BMeshCreateParams *params)
BMesh Make Mesh.
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
#define BMALLOC_TEMPLATE_FROM_ME(...)
void BM_mesh_bm_from_me(BMesh *bm, const Mesh *mesh, const BMeshFromMeshParams *params)
void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *mesh, const BMeshToMeshParams *params)
#define BM_FACE
#define BM_EDGE
#define BM_VERT
@ BMO_FLAG_RESPECT_HIDE
bool BMO_op_callf(BMesh *bm, int flag, const char *fmt,...)
#define BMO_FLAG_DEFAULTS
bool BM_vert_is_wire(const BMVert *v)
bool BM_edge_share_quad_check(BMEdge *e1, BMEdge *e2)
bool BM_edge_in_face(const BMEdge *e, const BMFace *f)
bool BM_face_share_edge_check(BMFace *f_a, BMFace *f_b)
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
bool BM_vert_in_face(BMVert *v, BMFace *f)
bool BM_vert_is_boundary(const BMVert *v)
#define BM_vert_edge_count_is_equal(v, n)
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
#define BM_vert_edge_count_is_over(v, n)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert * v
BPy_StructRNA * depsgraph
constexpr int64_t size() const
constexpr int64_t start() const
#define MEM_SAFE_FREE(v)
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
static char faces[256]
void multires_reshape_assign_final_coords_from_mdisps(const MultiresReshapeContext *reshape_context)
bool multires_reshape_context_create_from_object(MultiresReshapeContext *reshape_context, Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
void multires_reshape_context_free(MultiresReshapeContext *reshape_context)
void multires_reshape_store_original_grids(MultiresReshapeContext *reshape_context)
void multires_reshape_object_grids_to_tangent_displacement(const MultiresReshapeContext *reshape_context)
bool multires_reshape_context_create_from_base_mesh(MultiresReshapeContext *reshape_context, Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
static BMFace * face_step(BMEdge *edge, BMFace *f)
static bool unsubdivide_tag_disconnected_mesh_element(BMesh *bm, int *elem_id, int elem)
static void store_vertex_data(MultiresUnsubdivideGrid *grid, BMVert *v, int grid_x, int grid_y)
static void write_loop_in_face_grid(float(*face_grid)[3], MDisps *mdisp, int face_grid_size, int orig_grid_size, int loop)
static bool is_vertex_in_id(BMVert *v, const int *elem_id, int elem)
static void multires_create_grids_in_unsubdivided_base_mesh(MultiresUnsubdivideContext *context, Mesh *base_mesh)
static bool multires_unsubdivide_single_level(BMesh *bm)
static void multires_unsubdivide_prepare_original_bmesh_for_extract(MultiresUnsubdivideContext *context)
static BMEdge * edge_step(BMVert *v, BMEdge *edge, BMVert **r_next_vertex)
static void multires_unsubdivide_private_extract_data_free(MultiresUnsubdivideContext *context)
static BMVert * unsubdivide_find_any_pole(BMesh *bm, int *elem_id, int elem)
static bool is_vertex_pole_three(BMVert *v)
static void multires_unsubdivide_add_original_index_datalayers(Mesh *mesh)
bool multires_unsubdivide_to_basemesh(MultiresUnsubdivideContext *context)
static void unsubdivide_face_center_vertex_tag(BMesh *bm, BMVert *initial_vertex)
static bool is_vertex_pole(BMVert *v)
static int unsubdivide_init_elem_ids(BMesh *bm, int *elem_id)
static void store_grid_data(MultiresUnsubdivideContext *context, MultiresUnsubdivideGrid *grid, BMVert *v, BMFace *f, int grid_x, int grid_y)
static const char lname[]
static bool unsubdivide_is_center_vertex_tag_valid(BMesh *bm, int *elem_id, int elem)
static bool unsubdivide_is_all_quads(BMesh *bm)
void multires_unsubdivide_context_init(MultiresUnsubdivideContext *context, Mesh *original_mesh, MultiresModifierData *mmd)
static bool multires_unsubdivide_flip_grid_x_axis(const blender::OffsetIndices< int > faces, const blender::Span< int > corner_verts, int face_index, int loop, int v_x)
static void multires_unsubdivide_get_grid_corners_on_base_mesh(BMFace *f1, BMEdge *e1, BMVert **r_corner_x, BMVert **r_corner_y)
static void unsubdivide_build_base_mesh_from_tags(BMesh *bm)
static void multires_unsubdivide_free_original_datalayers(Mesh *mesh)
static bool is_vertex_diagonal(BMVert *from_v, BMVert *to_v)
static BMEdge * get_initial_edge_y(BMFace *f, BMEdge *edge_x, BMVert *initial_vertex)
static void multires_unsubdivide_extract_grids(MultiresUnsubdivideContext *context)
static void multires_unsubdivide_extract_single_grid_from_face_edge(MultiresUnsubdivideContext *context, BMFace *f1, BMEdge *e1, bool flip_grid, MultiresUnsubdivideGrid *grid)
static const char vname[]
int multiresModifier_rebuild_subdiv(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd, int rebuild_limit, bool switch_view_to_lower_level)
static void write_face_grid_in_unsubdivide_grid(MultiresUnsubdivideGrid *grid, float(*face_grid)[3], int face_grid_size, int gunsub_x, int gunsub_y)
void multires_unsubdivide_context_free(MultiresUnsubdivideContext *context)
static BMesh * get_bmesh_from_mesh(Mesh *mesh)
BMVert * v1
BMVert * v2
struct BMFace * f
int totvert
CustomData ldata
float(* disps)[3]
int corners_num
CustomData corner_data
CustomData vert_data
int verts_num
i
Definition text_draw.cc:230