Blender V5.3
subdiv_ccg.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_subdiv_ccg.hh"
10
11#include "MEM_guardedalloc.h"
12
14#include "BLI_index_mask.hh"
15#include "BLI_math_bits.hh"
16#include "BLI_math_geom_c.hh"
17#include "BLI_math_vector_c.hh"
18#include "BLI_ordered_edge.hh"
19#include "BLI_set.hh"
20#include "BLI_task.hh"
21#include "BLI_vector_set.hh"
22
23#include "BKE_ccg.hh"
24#include "BKE_mesh.hh"
25#include "BKE_subdiv.hh"
26#include "BKE_subdiv_eval.hh"
27
28#ifdef WITH_OPENSUBDIV
30#endif
31
32namespace blender {
33
34using namespace bke::subdiv;
35using namespace bke::ccg;
36
37/* -------------------------------------------------------------------- */
40
41#ifdef WITH_OPENSUBDIV
42
43static void subdiv_ccg_average_inner_face_grids(SubdivCCG &subdiv_ccg,
44 const CCGKey &key,
45 const IndexRange face);
46
47void subdiv_ccg_average_faces_boundaries_and_corners(SubdivCCG &subdiv_ccg,
48 const CCGKey &key,
49 const IndexMask &face_mask);
50
52
53/* -------------------------------------------------------------------- */
56
57/* TODO(sergey): Make it more accessible function. */
58static int topology_refiner_count_face_corners(
59 const opensubdiv::TopologyRefinerImpl *topology_refiner)
60{
61 const int num_faces = topology_refiner->base_level().GetNumFaces();
62 int num_corners = 0;
63 for (int face_index = 0; face_index < num_faces; face_index++) {
64 num_corners += topology_refiner->base_level().GetFaceVertices(face_index).size();
65 }
66 return num_corners;
67}
68
69/* NOTE: Grid size and layer flags are to be filled in before calling this
70 * function. */
71static void subdiv_ccg_alloc_elements(SubdivCCG &subdiv_ccg,
73 const SubdivToCCGSettings &settings)
74{
75 const opensubdiv::TopologyRefinerImpl *topology_refiner = subdiv.topology_refiner;
76 /* Allocate memory for surface grids. */
77 const int64_t num_grids = topology_refiner_count_face_corners(topology_refiner);
78 const int64_t grid_size = grid_size_from_level(subdiv_ccg.level);
79 const int64_t grid_area = grid_size * grid_size;
80 subdiv_ccg.positions.reinitialize(num_grids * grid_area);
81 if (settings.need_normal) {
82 subdiv_ccg.normals.reinitialize(num_grids * grid_area);
83 }
84 if (settings.need_mask) {
85 subdiv_ccg.masks.reinitialize(num_grids * grid_area);
86 }
87 /* TODO(sergey): Allocate memory for loose elements. */
88}
89
91
92/* -------------------------------------------------------------------- */
95
96static void subdiv_ccg_eval_grid_element_limit(Subdiv &subdiv,
97 SubdivCCG &subdiv_ccg,
98 const int ptex_face_index,
99 const float u,
100 const float v,
101 const int element)
102{
103 if (subdiv.displacement_evaluator != nullptr) {
104 subdiv_ccg.positions[element] = eval_final_point(&subdiv, ptex_face_index, u, v);
105 }
106 else if (!subdiv_ccg.normals.is_empty()) {
108 ptex_face_index,
109 u,
110 v,
111 subdiv_ccg.positions[element],
112 subdiv_ccg.normals[element]);
113 }
114 else {
115 subdiv_ccg.positions[element] = eval_limit_point(&subdiv, ptex_face_index, u, v);
116 }
117}
118
119static void subdiv_ccg_eval_grid_element_mask(SubdivCCG &subdiv_ccg,
120 SubdivCCGMaskEvaluator *mask_evaluator,
121 const int ptex_face_index,
122 const float u,
123 const float v,
124 const int element)
125{
126 if (subdiv_ccg.masks.is_empty()) {
127 return;
128 }
129 if (mask_evaluator != nullptr) {
130 subdiv_ccg.masks[element] = mask_evaluator->eval_mask(mask_evaluator, ptex_face_index, u, v);
131 }
132 else {
133 subdiv_ccg.masks[element] = 0.0f;
134 }
135}
136
137static void subdiv_ccg_eval_grid_element(Subdiv &subdiv,
138 SubdivCCG &subdiv_ccg,
139 SubdivCCGMaskEvaluator *mask_evaluator,
140 const int ptex_face_index,
141 const float u,
142 const float v,
143 const int element)
144{
145 subdiv_ccg_eval_grid_element_limit(subdiv, subdiv_ccg, ptex_face_index, u, v, element);
146 subdiv_ccg_eval_grid_element_mask(subdiv_ccg, mask_evaluator, ptex_face_index, u, v, element);
147}
148
149static void subdiv_ccg_eval_regular_grid(Subdiv &subdiv,
150 SubdivCCG &subdiv_ccg,
151 const Span<int> face_ptex_offset,
152 SubdivCCGMaskEvaluator *mask_evaluator,
153 const int face_index)
154{
155 const int ptex_face_index = face_ptex_offset[face_index];
156 const int grid_size = subdiv_ccg.grid_size;
157 const int grid_area = subdiv_ccg.grid_area;
158 const float grid_size_1_inv = 1.0f / (grid_size - 1);
159 const IndexRange face = subdiv_ccg.faces[face_index];
160 for (int corner = 0; corner < face.size(); corner++) {
161 const int grid_index = face.start() + corner;
162 const IndexRange range = grid_range(grid_area, grid_index);
163 for (int y = 0; y < grid_size; y++) {
164 const float grid_v = y * grid_size_1_inv;
165 for (int x = 0; x < grid_size; x++) {
166 const float grid_u = x * grid_size_1_inv;
167 float u, v;
168 rotate_grid_to_quad(corner, grid_u, grid_v, &u, &v);
169 const int element = range[CCG_grid_xy_to_index(grid_size, x, y)];
170 subdiv_ccg_eval_grid_element(
171 subdiv, subdiv_ccg, mask_evaluator, ptex_face_index, u, v, element);
172 }
173 }
174 }
175}
176
177static void subdiv_ccg_eval_special_grid(Subdiv &subdiv,
178 SubdivCCG &subdiv_ccg,
179 const Span<int> face_ptex_offset,
180 SubdivCCGMaskEvaluator *mask_evaluator,
181 const int face_index)
182{
183 const int grid_size = subdiv_ccg.grid_size;
184 const int grid_area = subdiv_ccg.grid_area;
185 const float grid_size_1_inv = 1.0f / (grid_size - 1);
186 const IndexRange face = subdiv_ccg.faces[face_index];
187 for (int corner = 0; corner < face.size(); corner++) {
188 const int grid_index = face.start() + corner;
189 const int ptex_face_index = face_ptex_offset[face_index] + corner;
190 const IndexRange range = grid_range(grid_area, grid_index);
191 for (int y = 0; y < grid_size; y++) {
192 const float u = 1.0f - (y * grid_size_1_inv);
193 for (int x = 0; x < grid_size; x++) {
194 const float v = 1.0f - (x * grid_size_1_inv);
195 const int element = range[CCG_grid_xy_to_index(grid_size, x, y)];
196 subdiv_ccg_eval_grid_element(
197 subdiv, subdiv_ccg, mask_evaluator, ptex_face_index, u, v, element);
198 }
199 }
200 }
201}
202
203static bool subdiv_ccg_evaluate_grids(SubdivCCG &subdiv_ccg,
204 Subdiv &subdiv,
205 SubdivCCGMaskEvaluator *mask_evaluator)
206{
207 const opensubdiv::TopologyRefinerImpl *topology_refiner = subdiv.topology_refiner;
208 const int num_faces = topology_refiner->base_level().GetNumFaces();
209 const Span<int> face_ptex_offset = face_ptex_offset_get(&subdiv);
210 BLI_assert(face_ptex_offset.size() == subdiv_ccg.faces.size() + 1);
211 threading::parallel_for(IndexRange(num_faces), 1024, [&](const IndexRange range) {
212 for (const int face_index : range) {
213 if (subdiv_ccg.faces[face_index].size() == 4) {
214 subdiv_ccg_eval_regular_grid(
215 subdiv, subdiv_ccg, face_ptex_offset, mask_evaluator, face_index);
216 }
217 else {
218 subdiv_ccg_eval_special_grid(
219 subdiv, subdiv_ccg, face_ptex_offset, mask_evaluator, face_index);
220 }
221 }
222 });
223 /* If displacement is used, need to calculate normals after all final
224 * coordinates are known. */
225 if (subdiv.displacement_evaluator != nullptr) {
227 }
228 return true;
229}
230
231static void subdiv_ccg_allocate_adjacent_edges(SubdivCCG &subdiv_ccg, const int num_edges)
232{
233 subdiv_ccg.adjacent_edges = Array<SubdivCCGAdjacentEdge>(num_edges, SubdivCCGAdjacentEdge{});
234}
235
236static SubdivCCGCoord subdiv_ccg_coord(int grid_index, int x, int y)
237{
238 SubdivCCGCoord coord{};
239 coord.grid_index = grid_index;
240 coord.x = x;
241 coord.y = y;
242 return coord;
243}
244
245/* Returns storage where boundary elements are to be stored. */
246static MutableSpan<SubdivCCGCoord> subdiv_ccg_adjacent_edge_add_face(
247 const int num_elements, SubdivCCGAdjacentEdge &adjacent_edge)
248{
249 Array<SubdivCCGCoord> coords(num_elements);
250 adjacent_edge.boundary_coords.append(std::move(coords));
251 return adjacent_edge.boundary_coords[adjacent_edge.boundary_coords.size() - 1];
252}
253
254static void subdiv_ccg_init_faces_edge_neighborhood(SubdivCCG &subdiv_ccg)
255{
256 Subdiv *subdiv = subdiv_ccg.subdiv;
257 const OffsetIndices<int> faces = subdiv_ccg.faces;
258 const OpenSubdiv::Far::TopologyLevel &base_level = subdiv->topology_refiner->base_level();
259 const int num_edges = base_level.GetNumEdges();
260 const int grid_size = subdiv_ccg.grid_size;
261 if (num_edges == 0) {
262 /* Early output, nothing to do in this case. */
263 return;
264 }
265 subdiv_ccg_allocate_adjacent_edges(subdiv_ccg, num_edges);
266
267 /* Store adjacency for all faces. */
268 for (const int face_index : faces.index_range()) {
269 const IndexRange face = faces[face_index];
270 const int num_face_grids = face.size();
271 const OpenSubdiv::Far::ConstIndexArray face_vertices = base_level.GetFaceVertices(face_index);
272 /* Note that order of edges is same as order of MLoops, which also
273 * means it's the same as order of grids. */
274 const OpenSubdiv::Far::ConstIndexArray face_edges = base_level.GetFaceEdges(face_index);
275 /* Store grids adjacency for this edge. */
276 for (int corner = 0; corner < num_face_grids; corner++) {
277 const int vertex_index = face_vertices[corner];
278 const int edge_index = face_edges[corner];
279 const OpenSubdiv::Far::ConstIndexArray edge_vertices = base_level.GetEdgeVertices(
280 edge_index);
281 const bool is_edge_flipped = (edge_vertices[0] != vertex_index);
282 /* Grid which is adjacent to the current corner. */
283 const int current_grid_index = face.start() + corner;
284 /* Grid which is adjacent to the next corner. */
285 const int next_grid_index = face.start() + (corner + 1) % num_face_grids;
286 /* Add new face to the adjacent edge. */
287 SubdivCCGAdjacentEdge &adjacent_edge = subdiv_ccg.adjacent_edges[edge_index];
288 MutableSpan<SubdivCCGCoord> boundary_coords = subdiv_ccg_adjacent_edge_add_face(
289 grid_size * 2, adjacent_edge);
290 /* Fill CCG elements along the edge. */
291 int boundary_element_index = 0;
292 if (is_edge_flipped) {
293 for (int i = 0; i < grid_size; i++) {
294 boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
295 next_grid_index, grid_size - i - 1, grid_size - 1);
296 }
297 for (int i = 0; i < grid_size; i++) {
298 boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
299 current_grid_index, grid_size - 1, i);
300 }
301 }
302 else {
303 for (int i = 0; i < grid_size; i++) {
304 boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
305 current_grid_index, grid_size - 1, grid_size - i - 1);
306 }
307 for (int i = 0; i < grid_size; i++) {
308 boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
309 next_grid_index, i, grid_size - 1);
310 }
311 }
312 }
313 }
314}
315
316static void subdiv_ccg_allocate_adjacent_vertices(SubdivCCG &subdiv_ccg, const int num_vertices)
317{
318 subdiv_ccg.adjacent_verts = Array<SubdivCCGAdjacentVertex>(num_vertices,
320}
321
322/* Returns storage where corner elements are to be stored. This is a pointer
323 * to the actual storage. */
324static void subdiv_ccg_adjacent_vertex_add_face(SubdivCCGAdjacentVertex &adjacent_vertex,
325 const int grid_index,
326 const short x,
327 const short y)
328{
329 adjacent_vertex.corner_coords.append(SubdivCCGCoord{grid_index, x, y});
330}
331
332static void subdiv_ccg_init_faces_vertex_neighborhood(SubdivCCG &subdiv_ccg)
333{
334 Subdiv *subdiv = subdiv_ccg.subdiv;
335 const OffsetIndices<int> faces = subdiv_ccg.faces;
336 const opensubdiv::TopologyRefinerImpl *topology_refiner = subdiv->topology_refiner;
337 const int num_vertices = topology_refiner->base_level().GetNumVertices();
338 const int grid_size = subdiv_ccg.grid_size;
339 if (num_vertices == 0) {
340 /* Early output, nothing to do in this case. */
341 return;
342 }
343 subdiv_ccg_allocate_adjacent_vertices(subdiv_ccg, num_vertices);
344 /* Store adjacency for all faces. */
345 for (const int face_index : faces.index_range()) {
346 const IndexRange face = faces[face_index];
347 const int num_face_grids = face.size();
348 const OpenSubdiv::Far::ConstIndexArray face_vertices =
349 topology_refiner->base_level().GetFaceVertices(face_index);
350 for (int corner = 0; corner < num_face_grids; corner++) {
351 const int vertex_index = face_vertices[corner];
352 /* Grid which is adjacent to the current corner. */
353 const int grid_index = face.start() + corner;
354 /* Add new face to the adjacent edge. */
355 SubdivCCGAdjacentVertex &adjacent_vertex = subdiv_ccg.adjacent_verts[vertex_index];
356 subdiv_ccg_adjacent_vertex_add_face(
357 adjacent_vertex, grid_index, grid_size - 1, grid_size - 1);
358 }
359 }
360}
361
362static void subdiv_ccg_init_faces_neighborhood(SubdivCCG &subdiv_ccg)
363{
364 subdiv_ccg_init_faces_edge_neighborhood(subdiv_ccg);
365 subdiv_ccg_init_faces_vertex_neighborhood(subdiv_ccg);
366}
367
368#endif
369
371
372/* -------------------------------------------------------------------- */
375
376std::unique_ptr<SubdivCCG> BKE_subdiv_to_ccg(Subdiv &subdiv,
377 const SubdivToCCGSettings &settings,
378 const Mesh &coarse_mesh,
379 SubdivCCGMaskEvaluator *mask_evaluator)
380{
381#ifdef WITH_OPENSUBDIV
383 std::unique_ptr<SubdivCCG> subdiv_ccg = std::make_unique<SubdivCCG>();
384 subdiv_ccg->subdiv = &subdiv;
385 subdiv_ccg->level = bitscan_forward_i(settings.resolution - 1);
386 subdiv_ccg->grid_size = grid_size_from_level(subdiv_ccg->level);
387 subdiv_ccg->grid_area = subdiv_ccg->grid_size * subdiv_ccg->grid_size;
388 subdiv_ccg->faces = coarse_mesh.faces();
389 subdiv_ccg->grids_num = subdiv_ccg->faces.total_size();
390 subdiv_ccg->grid_to_face_map = coarse_mesh.corner_to_face_map();
391 if (coarse_mesh.corners_num) {
392 BLI_assert(subdiv.topology_refiner);
393 subdiv_ccg_alloc_elements(*subdiv_ccg, subdiv, settings);
394 subdiv_ccg_init_faces_neighborhood(*subdiv_ccg);
395 if (!subdiv_ccg_evaluate_grids(*subdiv_ccg, subdiv, mask_evaluator)) {
397 return nullptr;
398 }
399 }
401 return subdiv_ccg;
402#else
403 UNUSED_VARS(subdiv, settings, coarse_mesh, mask_evaluator);
404 return {};
405#endif
406}
407
409 const SubdivToCCGSettings &settings,
410 const Mesh &coarse_mesh)
411{
412 /* Make sure evaluator is ready. */
415 if (coarse_mesh.faces_num) {
416 return nullptr;
417 }
418 }
420 SubdivCCGMaskEvaluator mask_evaluator;
421 bool has_mask = BKE_subdiv_ccg_mask_init_from_paint(&mask_evaluator, &coarse_mesh);
422 std::unique_ptr<SubdivCCG> subdiv_ccg = BKE_subdiv_to_ccg(
423 subdiv, settings, coarse_mesh, has_mask ? &mask_evaluator : nullptr);
424 if (has_mask) {
425 mask_evaluator.free(&mask_evaluator);
426 }
427 if (!subdiv_ccg) {
428 return nullptr;
429 }
430 Mesh *result = BKE_mesh_copy_for_eval(coarse_mesh);
431 result->runtime->subdiv_ccg = std::move(subdiv_ccg);
432 return result;
433}
434
436{
437 if (this->subdiv != nullptr) {
438 free(this->subdiv);
439 }
440}
441
442CCGKey BKE_subdiv_ccg_key(const SubdivCCG & /*subdiv_ccg*/, int level)
443{
444#ifdef WITH_OPENSUBDIV
445 /* Most #CCGKey fields are unused for #SubdivCCG but still used in other areas of Blender.
446 * Initialize them to invalid values to catch mistaken use more easily. */
447 CCGKey key;
448 key.level = level;
449 key.elem_size = -1;
450 key.grid_size = grid_size_from_level(level);
451 key.grid_area = key.grid_size * key.grid_size;
452 key.grid_bytes = -1;
453
454 key.normal_offset = -1;
455 key.mask_offset = -1;
456
457 key.has_normals = false;
458 key.has_mask = false;
459 return key;
460#else
461 UNUSED_VARS(level);
462 return {};
463#endif
464}
465
467{
468 return BKE_subdiv_ccg_key(subdiv_ccg, subdiv_ccg.level);
469}
470
472
473/* -------------------------------------------------------------------- */
476
477#ifdef WITH_OPENSUBDIV
478
479/* Evaluate high-res face normals, for faces which corresponds to grid elements
480 *
481 * {(x, y), {x + 1, y}, {x + 1, y + 1}, {x, y + 1}}
482 *
483 * The result is stored in normals storage from TLS. */
484static void subdiv_ccg_recalc_inner_face_normals(const SubdivCCG &subdiv_ccg,
485 MutableSpan<float3> face_normals,
486 const int corner)
487{
488 const int grid_size = subdiv_ccg.grid_size;
489 const int grid_area = subdiv_ccg.grid_area;
490 const int grid_size_1 = grid_size - 1;
491 const Span grid_positions = subdiv_ccg.positions.as_span().slice(grid_range(grid_area, corner));
492 for (int y = 0; y < grid_size - 1; y++) {
493 for (int x = 0; x < grid_size - 1; x++) {
494 const int face_index = y * grid_size_1 + x;
495 float *face_normal = face_normals[face_index];
496 normal_quad_v3(face_normal,
497 grid_positions[CCG_grid_xy_to_index(grid_size, x, y + 1)],
498 grid_positions[CCG_grid_xy_to_index(grid_size, x + 1, y + 1)],
499 grid_positions[CCG_grid_xy_to_index(grid_size, x + 1, y)],
500 grid_positions[CCG_grid_xy_to_index(grid_size, x, y)]);
501 }
502 }
503}
504
505/* Average normals at every grid element, using adjacent faces normals. */
506static void subdiv_ccg_average_inner_face_normals(SubdivCCG &subdiv_ccg,
507 const Span<float3> face_normals,
508 const int corner)
509{
510 const int grid_size = subdiv_ccg.grid_size;
511 const int grid_area = subdiv_ccg.grid_area;
512 const int grid_size_1 = grid_size - 1;
513 MutableSpan grid_normals = subdiv_ccg.normals.as_mutable_span().slice(
514 grid_range(grid_area, corner));
515 for (int y = 0; y < grid_size; y++) {
516 for (int x = 0; x < grid_size; x++) {
517 float normal_acc[3] = {0.0f, 0.0f, 0.0f};
518 int counter = 0;
519 /* Accumulate normals of all adjacent faces. */
520 if (x < grid_size_1 && y < grid_size_1) {
521 add_v3_v3(normal_acc, face_normals[y * grid_size_1 + x]);
522 counter++;
523 }
524 if (x >= 1) {
525 if (y < grid_size_1) {
526 add_v3_v3(normal_acc, face_normals[y * grid_size_1 + (x - 1)]);
527 counter++;
528 }
529 if (y >= 1) {
530 add_v3_v3(normal_acc, face_normals[(y - 1) * grid_size_1 + (x - 1)]);
531 counter++;
532 }
533 }
534 if (y >= 1 && x < grid_size_1) {
535 add_v3_v3(normal_acc, face_normals[(y - 1) * grid_size_1 + x]);
536 counter++;
537 }
538 /* Normalize and store. */
539 mul_v3_v3fl(grid_normals[CCG_grid_xy_to_index(grid_size, x, y)], normal_acc, 1.0f / counter);
540 }
541 }
542}
543
544/* Recalculate normals which corresponds to non-boundaries elements of grids. */
545static void subdiv_ccg_recalc_inner_grid_normals(SubdivCCG &subdiv_ccg, const IndexMask &face_mask)
546{
547 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
548
549 const int grid_size_1 = subdiv_ccg.grid_size - 1;
551 [&]() { return Array<float3>(grid_size_1 * grid_size_1); });
552
553 const OffsetIndices<int> faces = subdiv_ccg.faces;
554 face_mask.foreach_segment(
555 [&](const IndexMaskSegment segment) {
556 MutableSpan<float3> face_normals = face_normals_tls.local();
557 for (const int face_index : segment) {
558 const IndexRange face = faces[face_index];
559 for (const int grid_index : face) {
560 subdiv_ccg_recalc_inner_face_normals(subdiv_ccg, face_normals, grid_index);
561 subdiv_ccg_average_inner_face_normals(subdiv_ccg, face_normals, grid_index);
562 }
563 subdiv_ccg_average_inner_face_grids(subdiv_ccg, key, face);
564 }
565 },
567}
568
569#endif
570
572{
573#ifdef WITH_OPENSUBDIV
574 if (subdiv_ccg.normals.is_empty()) {
575 /* Grids don't have normals, can do early output. */
576 return;
577 }
578 subdiv_ccg_recalc_inner_grid_normals(subdiv_ccg, subdiv_ccg.faces.index_range());
580#else
581 UNUSED_VARS(subdiv_ccg);
582#endif
583}
584
585void BKE_subdiv_ccg_update_normals(SubdivCCG &subdiv_ccg, const IndexMask &face_mask)
586{
587#ifdef WITH_OPENSUBDIV
588 if (subdiv_ccg.normals.is_empty()) {
589 /* Grids don't have normals, can do early output. */
590 return;
591 }
592 if (face_mask.is_empty()) {
593 /* No faces changed, so nothing to do here. */
594 return;
595 }
596 subdiv_ccg_recalc_inner_grid_normals(subdiv_ccg, face_mask);
597
598 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
599 subdiv_ccg_average_faces_boundaries_and_corners(subdiv_ccg, key, face_mask);
600#else
601 UNUSED_VARS(subdiv_ccg, face_mask);
602#endif
603}
604
606
607/* -------------------------------------------------------------------- */
610
611#ifdef WITH_OPENSUBDIV
612
613static void average_grid_element_value_v3(float a[3], float b[3])
614{
615 add_v3_v3(a, b);
616 mul_v3_fl(a, 0.5f);
617 copy_v3_v3(b, a);
618}
619
620static void average_grid_element(SubdivCCG &subdiv_ccg,
621 const int grid_element_a,
622 const int grid_element_b)
623{
624 average_grid_element_value_v3(subdiv_ccg.positions[grid_element_a],
625 subdiv_ccg.positions[grid_element_b]);
626 if (!subdiv_ccg.normals.is_empty()) {
627 average_grid_element_value_v3(subdiv_ccg.normals[grid_element_a],
628 subdiv_ccg.normals[grid_element_b]);
629 }
630 if (!subdiv_ccg.masks.is_empty()) {
631 float mask = (subdiv_ccg.masks[grid_element_a] + subdiv_ccg.masks[grid_element_b]) * 0.5f;
632 subdiv_ccg.masks[grid_element_a] = mask;
633 subdiv_ccg.masks[grid_element_b] = mask;
634 }
635}
636
637/* Accumulator to hold data during averaging. */
638struct GridElementAccumulator {
639 float3 co;
640 float3 no;
641 float mask;
642};
643
644static void element_accumulator_init(GridElementAccumulator &accumulator)
645{
646 zero_v3(accumulator.co);
647 zero_v3(accumulator.no);
648 accumulator.mask = 0.0f;
649}
650
651static void element_accumulator_add(GridElementAccumulator &accumulator,
652 const SubdivCCG &subdiv_ccg,
653 const int elem)
654{
655 accumulator.co += subdiv_ccg.positions[elem];
656 if (!subdiv_ccg.normals.is_empty()) {
657 accumulator.no += subdiv_ccg.normals[elem];
658 }
659 if (!subdiv_ccg.masks.is_empty()) {
660 accumulator.mask += subdiv_ccg.masks[elem];
661 }
662}
663
664static void element_accumulator_mul_fl(GridElementAccumulator &accumulator, const float f)
665{
666 mul_v3_fl(accumulator.co, f);
667 mul_v3_fl(accumulator.no, f);
668 accumulator.mask *= f;
669}
670
671static void element_accumulator_copy(SubdivCCG &subdiv_ccg,
672 const int destination,
673 const GridElementAccumulator &accumulator)
674{
675 subdiv_ccg.positions[destination] = accumulator.co;
676 if (!subdiv_ccg.normals.is_empty()) {
677 subdiv_ccg.normals[destination] = accumulator.no;
678 }
679 if (!subdiv_ccg.masks.is_empty()) {
680 subdiv_ccg.masks[destination] = accumulator.mask;
681 }
682}
683
684static void subdiv_ccg_average_inner_face_grids(SubdivCCG &subdiv_ccg,
685 const CCGKey &key,
686 const IndexRange face)
687{
688 const int num_face_grids = face.size();
689 const int grid_size = subdiv_ccg.grid_size;
690 int prev_grid = face.start() + num_face_grids - 1;
691 /* Average boundary between neighbor grid. */
692 for (const int grid : face) {
693 for (int i = 1; i < grid_size; i++) {
694 const int prev_grid_element = grid_xy_to_vert(key, prev_grid, i, 0);
695 const int grid_element = grid_xy_to_vert(key, grid, 0, i);
696 average_grid_element(subdiv_ccg, prev_grid_element, grid_element);
697 }
698 prev_grid = grid;
699 }
700 /* Average all grids centers into a single accumulator, and share it.
701 * Guarantees correct and smooth averaging in the center. */
702 GridElementAccumulator center_accumulator;
703 element_accumulator_init(center_accumulator);
704 for (const int grid : face) {
705 const int grid_center_element = grid_xy_to_vert(key, grid, 0, 0);
706 element_accumulator_add(center_accumulator, subdiv_ccg, grid_center_element);
707 }
708 element_accumulator_mul_fl(center_accumulator, 1.0f / num_face_grids);
709 for (const int grid : face) {
710 const int grid_center_element = grid_xy_to_vert(key, grid, 0, 0);
711 element_accumulator_copy(subdiv_ccg, grid_center_element, center_accumulator);
712 }
713}
714
715static void subdiv_ccg_average_grids_boundary(SubdivCCG &subdiv_ccg,
716 const CCGKey &key,
717 const SubdivCCGAdjacentEdge &adjacent_edge,
719{
720 const int num_adjacent_faces = adjacent_edge.boundary_coords.size();
721 const int grid_size2 = subdiv_ccg.grid_size * 2;
722 if (num_adjacent_faces == 1) {
723 /* Nothing to average with. */
724 return;
725 }
726 for (int i = 1; i < grid_size2 - 1; i++) {
727 element_accumulator_init(accumulators[i]);
728 }
729 for (int face_index = 0; face_index < num_adjacent_faces; face_index++) {
730 for (int i = 1; i < grid_size2 - 1; i++) {
731 const int grid_element = adjacent_edge.boundary_coords[face_index][i].to_index(key);
732 element_accumulator_add(accumulators[i], subdiv_ccg, grid_element);
733 }
734 }
735 for (int i = 1; i < grid_size2 - 1; i++) {
736 element_accumulator_mul_fl(accumulators[i], 1.0f / num_adjacent_faces);
737 }
738 /* Copy averaged value to all the other faces. */
739 for (int face_index = 0; face_index < num_adjacent_faces; face_index++) {
740 for (int i = 1; i < grid_size2 - 1; i++) {
741 const int grid_element = adjacent_edge.boundary_coords[face_index][i].to_index(key);
742 element_accumulator_copy(subdiv_ccg, grid_element, accumulators[i]);
743 }
744 }
745}
746
747static void subdiv_ccg_average_grids_corners(SubdivCCG &subdiv_ccg,
748 const CCGKey &key,
749 const SubdivCCGAdjacentVertex &adjacent_vertex)
750{
751 const int num_adjacent_faces = adjacent_vertex.corner_coords.size();
752 if (num_adjacent_faces == 1) {
753 /* Nothing to average with. */
754 return;
755 }
756 GridElementAccumulator accumulator;
757 element_accumulator_init(accumulator);
758 for (int face_index = 0; face_index < num_adjacent_faces; face_index++) {
759 const int grid_element = adjacent_vertex.corner_coords[face_index].to_index(key);
760 element_accumulator_add(accumulator, subdiv_ccg, grid_element);
761 }
762 element_accumulator_mul_fl(accumulator, 1.0f / num_adjacent_faces);
763 /* Copy averaged value to all the other faces. */
764 for (int face_index = 0; face_index < num_adjacent_faces; face_index++) {
765 const int grid_element = adjacent_vertex.corner_coords[face_index].to_index(key);
766 element_accumulator_copy(subdiv_ccg, grid_element, accumulator);
767 }
768}
769
770static void subdiv_ccg_average_boundaries(SubdivCCG &subdiv_ccg,
771 const CCGKey &key,
772 const IndexMask &adjacent_edge_mask)
773{
775 [&]() { return Array<GridElementAccumulator>(subdiv_ccg.grid_size * 2); });
776
777 adjacent_edge_mask.foreach_segment(
778 [&](const IndexMaskSegment segment) {
779 MutableSpan<GridElementAccumulator> accumulators = all_accumulators.local();
780 for (const int i : segment) {
781 const SubdivCCGAdjacentEdge &adjacent_edge = subdiv_ccg.adjacent_edges[i];
782 subdiv_ccg_average_grids_boundary(subdiv_ccg, key, adjacent_edge, accumulators);
783 }
784 },
786}
787
788static void subdiv_ccg_average_corners(SubdivCCG &subdiv_ccg,
789 const CCGKey &key,
790 const IndexMask &adjacent_vert_mask)
791{
792 adjacent_vert_mask.foreach_index(
793 [&](const int i) {
794 const SubdivCCGAdjacentVertex &adjacent_vert = subdiv_ccg.adjacent_verts[i];
795 subdiv_ccg_average_grids_corners(subdiv_ccg, key, adjacent_vert);
796 },
798}
799
800static void subdiv_ccg_average_inner_faces(SubdivCCG &subdiv_ccg,
801 const CCGKey &key,
802 const IndexMask &face_mask)
803{
804 face_mask.foreach_index(
805 [&](const int face_index) {
806 subdiv_ccg_average_inner_face_grids(subdiv_ccg, key, subdiv_ccg.faces[face_index]);
807 },
809}
810
811#endif
812
814{
815#ifdef WITH_OPENSUBDIV
816 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
817 /* Average inner boundaries of grids (within one face), across faces
818 * from different face-corners. */
819 subdiv_ccg_average_inner_faces(subdiv_ccg, key, subdiv_ccg.faces.index_range());
820 subdiv_ccg_average_boundaries(subdiv_ccg, key, subdiv_ccg.adjacent_edges.index_range());
821 subdiv_ccg_average_corners(subdiv_ccg, key, subdiv_ccg.adjacent_verts.index_range());
822#else
823 UNUSED_VARS(subdiv_ccg);
824#endif
825}
826
827#ifdef WITH_OPENSUBDIV
828
829static void subdiv_ccg_affected_face_adjacency(SubdivCCG &subdiv_ccg,
830 const IndexMask &face_mask,
831 Set<int> &adjacent_verts,
832 Set<int> &adjacent_edges)
833{
834 Subdiv *subdiv = subdiv_ccg.subdiv;
835 const opensubdiv::TopologyRefinerImpl *topology_refiner = subdiv->topology_refiner;
836
837 face_mask.foreach_index([&](const int face_index) {
838 const OpenSubdiv::Far::ConstIndexArray face_vertices =
839 topology_refiner->base_level().GetFaceVertices(face_index);
840 adjacent_verts.add_multiple({face_vertices.begin(), face_vertices.size()});
841
842 const OpenSubdiv::Far::ConstIndexArray face_edges =
843 topology_refiner->base_level().GetFaceEdges(face_index);
844 adjacent_edges.add_multiple({face_edges.begin(), face_edges.size()});
845 });
846}
847
848void subdiv_ccg_average_faces_boundaries_and_corners(SubdivCCG &subdiv_ccg,
849 const CCGKey &key,
850 const IndexMask &face_mask)
851{
852 if (face_mask.size() == subdiv_ccg.faces.size()) {
853 /* Avoid mask building for complete masks. */
854 subdiv_ccg_average_boundaries(subdiv_ccg, key, subdiv_ccg.adjacent_edges.index_range());
855 subdiv_ccg_average_corners(subdiv_ccg, key, subdiv_ccg.adjacent_verts.index_range());
856 return;
857 }
858
859 Set<int> adjacent_vert_set;
860 Set<int> adjacent_edge_set;
861 subdiv_ccg_affected_face_adjacency(subdiv_ccg, face_mask, adjacent_vert_set, adjacent_edge_set);
862
863 Vector<int> adjacent_verts(adjacent_vert_set.begin(), adjacent_vert_set.end());
864 Vector<int> adjacent_edges(adjacent_edge_set.begin(), adjacent_edge_set.end());
865
866 std::ranges::sort(adjacent_verts);
867 std::ranges::sort(adjacent_edges);
868
869 IndexMaskMemory memory;
870 subdiv_ccg_average_boundaries(
871 subdiv_ccg, key, IndexMask::from_indices(adjacent_edges.as_span(), memory));
872
873 subdiv_ccg_average_corners(
874 subdiv_ccg, key, IndexMask::from_indices(adjacent_verts.as_span(), memory));
875}
876
877#endif
878
879void BKE_subdiv_ccg_average_stitch_faces(SubdivCCG &subdiv_ccg, const IndexMask &face_mask)
880{
881#ifdef WITH_OPENSUBDIV
882 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
883 subdiv_ccg_average_inner_faces(subdiv_ccg, key, face_mask);
884 subdiv_ccg_average_faces_boundaries_and_corners(subdiv_ccg, key, face_mask);
885#else
886 UNUSED_VARS(subdiv_ccg, face_mask);
887#endif
888}
889
891 int &r_num_vertices,
892 int &r_num_edges,
893 int &r_num_faces,
894 int &r_num_loops)
895{
896 const int num_grids = subdiv_ccg.grids_num;
897 const int grid_size = subdiv_ccg.grid_size;
898 const int grid_area = grid_size * grid_size;
899 const int num_edges_per_grid = 2 * (grid_size * (grid_size - 1));
900 r_num_vertices = num_grids * grid_area;
901 r_num_edges = num_grids * num_edges_per_grid;
902 r_num_faces = num_grids * (grid_size - 1) * (grid_size - 1);
903 r_num_loops = r_num_faces * 4;
904}
905
907
908/* -------------------------------------------------------------------- */
911
912void BKE_subdiv_ccg_print_coord(const char *message, const SubdivCCGCoord &coord)
913{
914 printf("%s: grid index: %d, coord: (%d, %d)\n", message, coord.grid_index, coord.x, coord.y);
915}
916
917bool BKE_subdiv_ccg_check_coord_valid(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
918{
919 if (coord.grid_index < 0 || coord.grid_index >= subdiv_ccg.grids_num) {
920 return false;
921 }
922 const int grid_size = subdiv_ccg.grid_size;
923 if (coord.x < 0 || coord.x >= grid_size) {
924 return false;
925 }
926 if (coord.y < 0 || coord.y >= grid_size) {
927 return false;
928 }
929 return true;
930}
931
933 const int num_unique,
934 const int num_duplicates)
935{
936 const int size = num_unique + num_duplicates;
937 neighbors.coords.reinitialize(size);
938 neighbors.num_duplicates = num_duplicates;
939}
940
941/* Check whether given coordinate belongs to a grid corner. */
942BLI_INLINE bool is_corner_grid_coord(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
943{
944 const int grid_size_1 = subdiv_ccg.grid_size - 1;
945 return (coord.x == 0 && coord.y == 0) || (coord.x == 0 && coord.y == grid_size_1) ||
946 (coord.x == grid_size_1 && coord.y == grid_size_1) ||
947 (coord.x == grid_size_1 && coord.y == 0);
948}
949
950/* Check whether given coordinate belongs to a grid boundary. */
951BLI_INLINE bool is_boundary_grid_coord(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
952{
953 const int grid_size_1 = subdiv_ccg.grid_size - 1;
954 return coord.x == 0 || coord.y == 0 || coord.x == grid_size_1 || coord.y == grid_size_1;
955}
956
957/* Check whether coordinate is at the boundary between two grids of the same face. */
959 const SubdivCCGCoord &coord)
960{
961 const int grid_size_1 = subdiv_ccg.grid_size - 1;
962 if (coord.x == 0) {
963 return coord.y > 0 && coord.y < grid_size_1;
964 }
965 if (coord.y == 0) {
966 return coord.x > 0 && coord.x < grid_size_1;
967 }
968 return false;
969}
970
972 const SubdivCCGCoord &coord)
973{
974 BLI_assert(coord.y > 0);
975 SubdivCCGCoord result = coord;
976 result.y -= 1;
977 return result;
978}
980 const SubdivCCGCoord &coord)
981{
982 UNUSED_VARS_NDEBUG(subdiv_ccg);
983 BLI_assert(coord.y < subdiv_ccg.grid_size - 1);
984 SubdivCCGCoord result = coord;
985 result.y += 1;
986 return result;
987}
988
990 const SubdivCCGCoord &coord)
991{
992 BLI_assert(coord.x > 0);
993 SubdivCCGCoord result = coord;
994 result.x -= 1;
995 return result;
996}
998 const SubdivCCGCoord &coord)
999{
1000 UNUSED_VARS_NDEBUG(subdiv_ccg);
1001 BLI_assert(coord.x < subdiv_ccg.grid_size - 1);
1002 SubdivCCGCoord result = coord;
1003 result.x += 1;
1004 return result;
1005}
1006
1007#ifdef WITH_OPENSUBDIV
1008
1009/* For the input coordinate which is at the boundary of the grid do one step inside. */
1010static SubdivCCGCoord coord_step_inside_from_boundary(const SubdivCCG &subdiv_ccg,
1011 const SubdivCCGCoord &coord)
1012
1013{
1014 SubdivCCGCoord result = coord;
1015 const int grid_size_1 = subdiv_ccg.grid_size - 1;
1016 if (result.x == grid_size_1) {
1017 --result.x;
1018 }
1019 else if (result.y == grid_size_1) {
1020 --result.y;
1021 }
1022 else if (result.x == 0) {
1023 ++result.x;
1024 }
1025 else if (result.y == 0) {
1026 ++result.y;
1027 }
1028 else {
1029 BLI_assert_msg(0, "non-boundary element given");
1030 }
1031 return result;
1032}
1033
1035int next_grid_index_from_coord(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
1036{
1037 const IndexRange face = subdiv_ccg.faces[subdiv_ccg.grid_to_face_map[coord.grid_index]];
1038 const int face_grid_index = coord.grid_index;
1039 int next_face_grid_index = face_grid_index + 1 - face.start();
1040 if (next_face_grid_index == face.size()) {
1041 next_face_grid_index = 0;
1042 }
1043 return face.start() + next_face_grid_index;
1044}
1045BLI_INLINE int prev_grid_index_from_coord(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
1046{
1047 const IndexRange face = subdiv_ccg.faces[subdiv_ccg.grid_to_face_map[coord.grid_index]];
1048 const int face_grid_index = coord.grid_index;
1049 int prev_face_grid_index = face_grid_index - 1 - face.start();
1050 if (prev_face_grid_index < 0) {
1051 prev_face_grid_index = face.size() - 1;
1052 }
1053 return face.start() + prev_face_grid_index;
1054}
1055
1056/* Simple case of getting neighbors of a corner coordinate: the corner is a face center, so
1057 * can only iterate over grid of a single face, without looking into adjacency. */
1058static void neighbor_coords_corner_center_get(const SubdivCCG &subdiv_ccg,
1059 const SubdivCCGCoord &coord,
1060 const bool include_duplicates,
1061 SubdivCCGNeighbors &r_neighbors)
1062{
1063 const IndexRange face = subdiv_ccg.faces[subdiv_ccg.grid_to_face_map[coord.grid_index]];
1064 const int num_adjacent_grids = face.size();
1065
1067 r_neighbors, num_adjacent_grids, (include_duplicates) ? num_adjacent_grids - 1 : 0);
1068
1069 int duplicate_face_grid_index = num_adjacent_grids;
1070 for (int face_grid_index = 0; face_grid_index < num_adjacent_grids; ++face_grid_index) {
1071 SubdivCCGCoord neighbor_coord;
1072 neighbor_coord.grid_index = face.start() + face_grid_index;
1073 neighbor_coord.x = 1;
1074 neighbor_coord.y = 0;
1075 r_neighbors.coords[face_grid_index] = neighbor_coord;
1076
1077 if (include_duplicates && neighbor_coord.grid_index != coord.grid_index) {
1078 neighbor_coord.x = 0;
1079 r_neighbors.coords[duplicate_face_grid_index++] = neighbor_coord;
1080 }
1081 }
1082}
1083
1084/* Get index within adjacent_verts array for the given CCG coordinate. */
1085static int adjacent_vertex_index_from_coord(const SubdivCCG &subdiv_ccg,
1086 const SubdivCCGCoord &coord)
1087{
1088 Subdiv *subdiv = subdiv_ccg.subdiv;
1089 const opensubdiv::TopologyRefinerImpl *topology_refiner = subdiv->topology_refiner;
1090
1091 const int face_index = subdiv_ccg.grid_to_face_map[coord.grid_index];
1092 const IndexRange face = subdiv_ccg.faces[face_index];
1093 const int face_grid_index = coord.grid_index - face.start();
1094
1095 const OpenSubdiv::Far::ConstIndexArray face_vertices =
1096 topology_refiner->base_level().GetFaceVertices(face_index);
1097
1098 const int adjacent_vertex_index = face_vertices[face_grid_index];
1099 return adjacent_vertex_index;
1100}
1101
1102/* The corner is adjacent to a coarse vertex. */
1103static void neighbor_coords_corner_vertex_get(const SubdivCCG &subdiv_ccg,
1104 const SubdivCCGCoord &coord,
1105 const bool include_duplicates,
1106 SubdivCCGNeighbors &r_neighbors)
1107{
1108 Subdiv *subdiv = subdiv_ccg.subdiv;
1109 const opensubdiv::TopologyRefinerImpl *topology_refiner = subdiv->topology_refiner;
1110
1111 const int adjacent_vertex_index = adjacent_vertex_index_from_coord(subdiv_ccg, coord);
1112 const OpenSubdiv::Far::ConstIndexArray vertex_edges =
1113 topology_refiner->base_level().GetVertexEdges(adjacent_vertex_index);
1114
1115 const SubdivCCGAdjacentVertex &adjacent_vert = subdiv_ccg.adjacent_verts[adjacent_vertex_index];
1116 const int num_adjacent_faces = adjacent_vert.corner_coords.size();
1117
1119 r_neighbors, vertex_edges.size(), (include_duplicates) ? num_adjacent_faces - 1 : 0);
1120
1121 for (int i = 0; i < vertex_edges.size(); ++i) {
1122 const int edge_index = vertex_edges[i];
1123
1124 /* Use very first grid of every edge. */
1125 const int edge_face_index = 0;
1126
1127 /* Depending edge orientation we use first (zero-based) or previous-to-last point. */
1128 const OpenSubdiv::Far::ConstIndexArray edge_vertices_indices =
1129 topology_refiner->base_level().GetEdgeVertices(edge_index);
1130 int edge_point_index, duplicate_edge_point_index;
1131 if (edge_vertices_indices[0] == adjacent_vertex_index) {
1132 duplicate_edge_point_index = 0;
1133 edge_point_index = duplicate_edge_point_index + 1;
1134 }
1135 else {
1136 /* Edge "consists" of 2 grids, which makes it 2 * grid_size elements per edge.
1137 * The index of last edge element is 2 * grid_size - 1 (due to zero-based indices),
1138 * and we are interested in previous to last element. */
1139 duplicate_edge_point_index = subdiv_ccg.grid_size * 2 - 1;
1140 edge_point_index = duplicate_edge_point_index - 1;
1141 }
1142
1143 const SubdivCCGAdjacentEdge &adjacent_edge = subdiv_ccg.adjacent_edges[edge_index];
1144 r_neighbors.coords[i] = adjacent_edge.boundary_coords[edge_face_index][edge_point_index];
1145 }
1146
1147 if (include_duplicates) {
1148 /* Add duplicates of the current grid vertex in adjacent faces if requested. */
1149 for (int i = 0, duplicate_i = vertex_edges.size(); i < num_adjacent_faces; i++) {
1150 SubdivCCGCoord neighbor_coord = adjacent_vert.corner_coords[i];
1151 if (neighbor_coord.grid_index != coord.grid_index) {
1152 r_neighbors.coords[duplicate_i++] = neighbor_coord;
1153 }
1154 }
1155 }
1156}
1157
1158static int adjacent_edge_index_from_coord(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
1159{
1160 Subdiv *subdiv = subdiv_ccg.subdiv;
1161 const opensubdiv::TopologyRefinerImpl *topology_refiner = subdiv->topology_refiner;
1162
1163 const int face_index = subdiv_ccg.grid_to_face_map[coord.grid_index];
1164 const IndexRange face = subdiv_ccg.faces[face_index];
1165 const int face_grid_index = coord.grid_index - face.start();
1166
1167 const OpenSubdiv::Far::ConstIndexArray face_edges = topology_refiner->base_level().GetFaceEdges(
1168 face_index);
1169
1170 const int grid_size_1 = subdiv_ccg.grid_size - 1;
1171 int adjacent_edge_index = -1;
1172 if (coord.x == grid_size_1) {
1173 adjacent_edge_index = face_edges[face_grid_index];
1174 }
1175 else {
1176 BLI_assert(coord.y == grid_size_1);
1177 adjacent_edge_index = face_edges[face_grid_index == 0 ? face.size() - 1 : face_grid_index - 1];
1178 }
1179
1180 return adjacent_edge_index;
1181}
1182
1183static int adjacent_edge_point_index_from_coord(const SubdivCCG &subdiv_ccg,
1184 const SubdivCCGCoord &coord,
1185 const int adjacent_edge_index)
1186{
1187 Subdiv *subdiv = subdiv_ccg.subdiv;
1188 const opensubdiv::TopologyRefinerImpl *topology_refiner = subdiv->topology_refiner;
1189
1190 const int adjacent_vertex_index = adjacent_vertex_index_from_coord(subdiv_ccg, coord);
1191 const OpenSubdiv::Far::ConstIndexArray edge_vertices_indices =
1192 topology_refiner->base_level().GetEdgeVertices(adjacent_edge_index);
1193
1194 /* Vertex index of an edge which is used to see whether edge points in the right direction.
1195 * Tricky part here is that depending whether input coordinate is a maximum X or Y coordinate
1196 * of the grid we need to use different edge direction.
1197 * Basically, the edge adjacent to a previous loop needs to point opposite direction. */
1198 int directional_edge_vertex_index = -1;
1199
1200 const int grid_size_1 = subdiv_ccg.grid_size - 1;
1201 int adjacent_edge_point_index = -1;
1202 if (coord.x == grid_size_1) {
1203 adjacent_edge_point_index = subdiv_ccg.grid_size - coord.y - 1;
1204 directional_edge_vertex_index = edge_vertices_indices[0];
1205 }
1206 else {
1207 BLI_assert(coord.y == grid_size_1);
1208 adjacent_edge_point_index = subdiv_ccg.grid_size + coord.x;
1209 directional_edge_vertex_index = edge_vertices_indices[1];
1210 }
1211
1212 /* Flip the index if the edge points opposite direction. */
1213 if (adjacent_vertex_index != directional_edge_vertex_index) {
1214 const int num_edge_points = subdiv_ccg.grid_size * 2;
1215 adjacent_edge_point_index = num_edge_points - adjacent_edge_point_index - 1;
1216 }
1217
1218 return adjacent_edge_point_index;
1219}
1220
1221/* Adjacent edge has two points in the middle which corresponds to grid corners, but which are
1222 * the same point in the final geometry.
1223 * So need to use extra step when calculating next/previous points, so we don't go from a corner
1224 * of one grid to a corner of adjacent grid. */
1225static int next_adjacent_edge_point_index(const SubdivCCG &subdiv_ccg, const int point_index)
1226{
1227 if (point_index == subdiv_ccg.grid_size - 1) {
1228 return point_index + 2;
1229 }
1230 return point_index + 1;
1231}
1232static int prev_adjacent_edge_point_index(const SubdivCCG &subdiv_ccg, const int point_index)
1233{
1234 if (point_index == subdiv_ccg.grid_size) {
1235 return point_index - 2;
1236 }
1237 return point_index - 1;
1238}
1239
1240/* When the point index corresponds to a grid corner, returns the point index which corresponds to
1241 * the corner of the adjacent grid, as the adjacent edge has two separate points for each grid
1242 * corner at the middle of the edge. */
1243static int adjacent_grid_corner_point_index_on_edge(const SubdivCCG &subdiv_ccg,
1244 const int point_index)
1245{
1246 if (point_index == subdiv_ccg.grid_size) {
1247 return point_index - 1;
1248 }
1249 return point_index + 1;
1250}
1251
1252/* Common implementation of neighbor calculation when input coordinate is at the edge between two
1253 * coarse faces, but is not at the coarse vertex. */
1254static void neighbor_coords_edge_get(const SubdivCCG &subdiv_ccg,
1255 const SubdivCCGCoord &coord,
1256 const bool include_duplicates,
1257 SubdivCCGNeighbors &r_neighbors)
1258
1259{
1260 const bool is_corner = is_corner_grid_coord(subdiv_ccg, coord);
1261 const int adjacent_edge_index = adjacent_edge_index_from_coord(subdiv_ccg, coord);
1262 const SubdivCCGAdjacentEdge *adjacent_edge = &subdiv_ccg.adjacent_edges[adjacent_edge_index];
1263
1264 /* 2 neighbor points along the edge, plus one inner point per every adjacent grid. */
1265 const int num_adjacent_faces = adjacent_edge->boundary_coords.size();
1266 int num_duplicates = 0;
1267 if (include_duplicates) {
1268 num_duplicates += num_adjacent_faces - 1;
1269 if (is_corner) {
1270 /* When the coord is a grid corner, add an extra duplicate per adjacent grid in all adjacent
1271 * faces to the edge. */
1272 num_duplicates += num_adjacent_faces;
1273 }
1274 }
1275 subdiv_ccg_neighbors_init(r_neighbors, num_adjacent_faces + 2, num_duplicates);
1276
1277 const int point_index = adjacent_edge_point_index_from_coord(
1278 subdiv_ccg, coord, adjacent_edge_index);
1279 const int point_index_duplicate = adjacent_grid_corner_point_index_on_edge(subdiv_ccg,
1280 point_index);
1281
1282 const int next_point_index = next_adjacent_edge_point_index(subdiv_ccg, point_index);
1283 const int prev_point_index = prev_adjacent_edge_point_index(subdiv_ccg, point_index);
1284
1285 int duplicate_i = num_adjacent_faces;
1286 for (int i = 0; i < num_adjacent_faces; ++i) {
1287 const Span<SubdivCCGCoord> boundary_coords = adjacent_edge->boundary_coords[i];
1288 /* One step into the grid from the edge for each adjacent face. */
1289 SubdivCCGCoord grid_coord = boundary_coords[point_index];
1290 r_neighbors.coords[i + 2] = coord_step_inside_from_boundary(subdiv_ccg, grid_coord);
1291
1292 if (grid_coord.grid_index == coord.grid_index) {
1293 /* Previous and next along the edge for the current grid. */
1294 r_neighbors.coords[0] = boundary_coords[prev_point_index];
1295 r_neighbors.coords[1] = boundary_coords[next_point_index];
1296 }
1297 else if (include_duplicates) {
1298 /* Same coordinate on neighboring grids if requested. */
1299 r_neighbors.coords[duplicate_i + 2] = grid_coord;
1300 duplicate_i++;
1301 }
1302
1303 /* When it is a corner, add the duplicate of the adjacent grid in the same face. */
1304 if (include_duplicates && is_corner) {
1305 SubdivCCGCoord duplicate_corner_grid_coord = boundary_coords[point_index_duplicate];
1306 r_neighbors.coords[duplicate_i + 2] = duplicate_corner_grid_coord;
1307 duplicate_i++;
1308 }
1309 }
1310 BLI_assert(duplicate_i - num_adjacent_faces == num_duplicates);
1311}
1312
1313/* The corner is at the middle of edge between faces. */
1314static void neighbor_coords_corner_edge_get(const SubdivCCG &subdiv_ccg,
1315 const SubdivCCGCoord &coord,
1316 const bool include_duplicates,
1317 SubdivCCGNeighbors &r_neighbors)
1318{
1319 neighbor_coords_edge_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1320}
1321
1322/* Input coordinate is at one of 4 corners of its grid corners. */
1323static void neighbor_coords_corner_get(const SubdivCCG &subdiv_ccg,
1324 const SubdivCCGCoord &coord,
1325 const bool include_duplicates,
1326 SubdivCCGNeighbors &r_neighbors)
1327{
1328 if (coord.x == 0 && coord.y == 0) {
1329 neighbor_coords_corner_center_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1330 }
1331 else {
1332 const int grid_size_1 = subdiv_ccg.grid_size - 1;
1333 if (coord.x == grid_size_1 && coord.y == grid_size_1) {
1334 neighbor_coords_corner_vertex_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1335 }
1336 else {
1337 neighbor_coords_corner_edge_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1338 }
1339 }
1340}
1341
1342/* Simple case of getting neighbors of a boundary coordinate: the input coordinate is at the
1343 * boundary between two grids of the same face and there is no need to check adjacency with
1344 * other faces. */
1345static void neighbor_coords_boundary_inner_get(const SubdivCCG &subdiv_ccg,
1346 const SubdivCCGCoord &coord,
1347 const bool include_duplicates,
1348 SubdivCCGNeighbors &r_neighbors)
1349{
1350 subdiv_ccg_neighbors_init(r_neighbors, 4, (include_duplicates) ? 1 : 0);
1351
1352 if (coord.x == 0) {
1353 r_neighbors.coords[0] = coord_at_prev_row(subdiv_ccg, coord);
1354 r_neighbors.coords[1] = coord_at_next_row(subdiv_ccg, coord);
1355 r_neighbors.coords[2] = coord_at_next_col(subdiv_ccg, coord);
1356
1357 r_neighbors.coords[3].grid_index = prev_grid_index_from_coord(subdiv_ccg, coord);
1358 r_neighbors.coords[3].x = coord.y;
1359 r_neighbors.coords[3].y = 1;
1360
1361 if (include_duplicates) {
1362 r_neighbors.coords[4] = r_neighbors.coords[3];
1363 r_neighbors.coords[4].y = 0;
1364 }
1365 }
1366 else if (coord.y == 0) {
1367 r_neighbors.coords[0] = coord_at_prev_col(subdiv_ccg, coord);
1368 r_neighbors.coords[1] = coord_at_next_col(subdiv_ccg, coord);
1369 r_neighbors.coords[2] = coord_at_next_row(subdiv_ccg, coord);
1370
1371 r_neighbors.coords[3].grid_index = next_grid_index_from_coord(subdiv_ccg, coord);
1372 r_neighbors.coords[3].x = 1;
1373 r_neighbors.coords[3].y = coord.x;
1374
1375 if (include_duplicates) {
1376 r_neighbors.coords[4] = r_neighbors.coords[3];
1377 r_neighbors.coords[4].x = 0;
1378 }
1379 }
1380}
1381
1382/* Input coordinate is on an edge between two faces. Need to check adjacency. */
1383static void neighbor_coords_boundary_outer_get(const SubdivCCG &subdiv_ccg,
1384 const SubdivCCGCoord &coord,
1385 const bool include_duplicates,
1386 SubdivCCGNeighbors &r_neighbors)
1387{
1388 neighbor_coords_edge_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1389}
1390
1391/* Input coordinate is at one of 4 boundaries of its grid.
1392 * It could either be an inner boundary (which connects face center to the face edge) or could be
1393 * a part of coarse face edge. */
1394static void neighbor_coords_boundary_get(const SubdivCCG &subdiv_ccg,
1395 const SubdivCCGCoord &coord,
1396 const bool include_duplicates,
1397 SubdivCCGNeighbors &r_neighbors)
1398{
1399 if (is_inner_edge_grid_coordinate(subdiv_ccg, coord)) {
1400 neighbor_coords_boundary_inner_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1401 }
1402 else {
1403 neighbor_coords_boundary_outer_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1404 }
1405}
1406
1407/* Input coordinate is inside of its grid, all the neighbors belong to the same grid. */
1408static void neighbor_coords_inner_get(const SubdivCCG &subdiv_ccg,
1409 const SubdivCCGCoord &coord,
1410 SubdivCCGNeighbors &r_neighbors)
1411{
1412 subdiv_ccg_neighbors_init(r_neighbors, 4, 0);
1413
1414 r_neighbors.coords[0] = coord_at_prev_row(subdiv_ccg, coord);
1415 r_neighbors.coords[1] = coord_at_next_row(subdiv_ccg, coord);
1416 r_neighbors.coords[2] = coord_at_prev_col(subdiv_ccg, coord);
1417 r_neighbors.coords[3] = coord_at_next_col(subdiv_ccg, coord);
1418}
1419
1420#endif
1421
1423 const SubdivCCGCoord &coord,
1424 const bool include_duplicates,
1425 SubdivCCGNeighbors &r_neighbors)
1426{
1427#ifdef WITH_OPENSUBDIV
1428 BLI_assert(coord.grid_index >= 0);
1429 BLI_assert(coord.grid_index < subdiv_ccg.grids_num);
1430 BLI_assert(coord.x >= 0);
1431 BLI_assert(coord.x < subdiv_ccg.grid_size);
1432 BLI_assert(coord.y >= 0);
1433 BLI_assert(coord.y < subdiv_ccg.grid_size);
1434
1435 if (is_corner_grid_coord(subdiv_ccg, coord)) {
1436 neighbor_coords_corner_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1437 }
1438 else if (is_boundary_grid_coord(subdiv_ccg, coord)) {
1439 neighbor_coords_boundary_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1440 }
1441 else {
1442 neighbor_coords_inner_get(subdiv_ccg, coord, r_neighbors);
1443 }
1444
1445# ifndef NDEBUG
1446 for (const int i : r_neighbors.coords.index_range()) {
1447 BLI_assert(BKE_subdiv_ccg_check_coord_valid(subdiv_ccg, r_neighbors.coords[i]));
1448 }
1449# endif
1450#else
1451 UNUSED_VARS(subdiv_ccg, coord, include_duplicates, r_neighbors);
1452#endif
1453}
1454
1456 const SubdivCCGCoord &coord,
1457 const Span<int> corner_verts,
1458 const OffsetIndices<int> faces,
1459 int &r_v1,
1460 int &r_v2)
1461{
1462 const int grid_size_1 = subdiv_ccg.grid_size - 1;
1463 const int face_index = BKE_subdiv_ccg_grid_to_face_index(subdiv_ccg, coord.grid_index);
1464 const IndexRange face = faces[face_index];
1465 r_v1 = corner_verts[coord.grid_index];
1466
1467 const int corner = bke::mesh::face_find_corner_from_vert(face, corner_verts, r_v1);
1468 if (coord.x == grid_size_1) {
1469 const int next = bke::mesh::face_corner_next(face, corner);
1470 r_v2 = corner_verts[next];
1471 }
1472 if (coord.y == grid_size_1) {
1473 const int prev = bke::mesh::face_corner_prev(face, corner);
1474 r_v2 = corner_verts[prev];
1475 }
1476}
1477
1479 const SubdivCCG &subdiv_ccg,
1480 const SubdivCCGCoord &coord,
1481 const Span<int> corner_verts,
1482 const OffsetIndices<int> faces,
1483 int &r_v1,
1484 int &r_v2)
1485{
1486
1487 const int grid_size_1 = subdiv_ccg.grid_size - 1;
1488 if (is_corner_grid_coord(subdiv_ccg, coord)) {
1489 if (coord.x == 0 && coord.y == 0) {
1490 /* Grid corner in the center of a face. */
1492 }
1493 if (coord.x == grid_size_1 && coord.y == grid_size_1) {
1494 /* Grid corner adjacent to a coarse mesh vertex. */
1495 r_v1 = r_v2 = corner_verts[coord.grid_index];
1497 }
1498 /* Grid corner adjacent to the middle of a coarse mesh edge. */
1499 adjacent_vertices_index_from_adjacent_edge(subdiv_ccg, coord, corner_verts, faces, r_v1, r_v2);
1501 }
1502
1503 if (is_boundary_grid_coord(subdiv_ccg, coord)) {
1504 if (!is_inner_edge_grid_coordinate(subdiv_ccg, coord)) {
1505 /* Grid boundary adjacent to a coarse mesh edge. */
1507 subdiv_ccg, coord, corner_verts, faces, r_v1, r_v2);
1509 }
1510 }
1512}
1513
1515 const Span<int> corner_verts,
1516 const BitSpan boundary_verts,
1517 const Set<OrderedEdge> &boundary_edges,
1518 const SubdivCCG &subdiv_ccg,
1519 const SubdivCCGCoord coord)
1520{
1521 int v1, v2;
1523 subdiv_ccg, coord, corner_verts, faces, v1, v2);
1524 switch (adjacency) {
1526 return boundary_verts[v1];
1528 return boundary_edges.contains(OrderedEdge(v1, v2));
1530 return false;
1531 }
1533 return false;
1534}
1535
1537{
1538 if (subdiv_ccg.grid_hidden.is_empty()) {
1539 const int grid_area = subdiv_ccg.grid_area;
1540 subdiv_ccg.grid_hidden = BitGroupVector<>(subdiv_ccg.grids_num, grid_area, false);
1541 }
1542 return subdiv_ccg.grid_hidden;
1543}
1544
1546{
1547 subdiv_ccg.grid_hidden = {};
1548}
1549
1550static void subdiv_ccg_coord_to_ptex_coord(const SubdivCCG &subdiv_ccg,
1551 const SubdivCCGCoord &coord,
1552 int &r_ptex_face_index,
1553 float &r_u,
1554 float &r_v)
1555{
1556 Subdiv *subdiv = subdiv_ccg.subdiv;
1557
1558 const float grid_size = subdiv_ccg.grid_size;
1559 const float grid_size_1_inv = 1.0f / (grid_size - 1);
1560
1561 const float grid_u = coord.x * grid_size_1_inv;
1562 const float grid_v = coord.y * grid_size_1_inv;
1563
1564 const int face_index = BKE_subdiv_ccg_grid_to_face_index(subdiv_ccg, coord.grid_index);
1565 const OffsetIndices<int> faces = subdiv_ccg.faces;
1566 const IndexRange face = faces[face_index];
1567 const Span<int> face_ptex_offset = face_ptex_offset_get(subdiv);
1568 r_ptex_face_index = face_ptex_offset[face_index];
1569
1570 const float corner = coord.grid_index - face.start();
1571
1572 if (face.size() == 4) {
1573 rotate_grid_to_quad(corner, grid_u, grid_v, &r_u, &r_v);
1574 }
1575 else {
1576 r_ptex_face_index += corner;
1577 r_u = 1.0f - grid_v;
1578 r_v = 1.0f - grid_u;
1579 }
1580}
1581
1583 const CCGKey &key,
1584 const int grid_index,
1585 const MutableSpan<float3> r_limit_positions)
1586{
1587 SubdivCCGCoord coord{};
1588 coord.grid_index = grid_index;
1589 for (const int y : IndexRange(key.grid_size)) {
1590 for (const int x : IndexRange(key.grid_size)) {
1591 const int i = CCG_grid_xy_to_index(key.grid_size, x, y);
1592 coord.x = x;
1593 coord.y = y;
1594
1595 int ptex_face_index;
1596 float u, v;
1597 subdiv_ccg_coord_to_ptex_coord(subdiv_ccg, coord, ptex_face_index, u, v);
1598
1599 r_limit_positions[i] = eval_limit_point(subdiv_ccg.subdiv, ptex_face_index, u, v);
1600 }
1601 }
1602}
1603
1605
1606} // namespace blender
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define UNUSED_VARS_NDEBUG(...)
#define BLI_INLINE
#define BLI_assert_msg(a, msg)
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
Array(Allocator allocator={}) noexcept
Definition BLI_array.hh:78
bool is_empty() const
Definition BLI_array.hh:275
BitGroupVector(Allocator allocator={}) noexcept
static IndexMask from_indices(Span< T > indices, LinearAllocator<> &memory)
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr int64_t start() const
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
bool contains(const Key &key) const
Definition BLI_set.hh:310
IndexRange index_range() const
void reinitialize(const int64_t new_size)
VecBase< float, 3 > float3
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
ccl_device_inline void copy_v3_v3(ccl_private float *r, const float3 val)
IndexRange grid_range(const int grid_area, const int grid)
int grid_xy_to_vert(const CCGKey &key, const int grid, const int x, const int y)
int face_corner_prev(const IndexRange face, const int corner)
Definition BKE_mesh.hh:306
int face_find_corner_from_vert(const IndexRange face, const Span< int > corner_verts, const int vert)
Definition BKE_mesh.hh:327
int face_corner_next(const IndexRange face, const int corner)
Definition BKE_mesh.hh:315
void free(Subdiv *subdiv)
Definition subdiv.cc:192
float3 eval_final_point(Subdiv *subdiv, int ptex_face_index, float u, float v)
bool eval_begin_from_mesh(Subdiv *subdiv, const Mesh *mesh, eSubdivEvaluatorType evaluator_type, Span< float3 > coarse_vert_positions={}, OpenSubdiv_EvaluatorCache *evaluator_cache=nullptr)
float3 eval_limit_point(Subdiv *subdiv, int ptex_face_index, float u, float v)
BLI_INLINE void rotate_grid_to_quad(int corner, float grid_u, float grid_v, float *r_quad_u, float *r_quad_v)
void eval_limit_point_and_normal(Subdiv *subdiv, int ptex_face_index, float u, float v, float3 &r_P, float3 &r_N)
BLI_INLINE int grid_size_from_level(int level)
void stats_begin(SubdivStats *stats, StatsValue value)
void stats_end(SubdivStats *stats, StatsValue value)
Span< int > face_ptex_offset_get(Subdiv *subdiv)
Definition subdiv.cc:216
constexpr ParallelGrainSize grain_size(int grain_size)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool BKE_subdiv_ccg_mask_init_from_paint(SubdivCCGMaskEvaluator *mask_evaluator, const Mesh *mesh)
__int64 int64_t
Definition BKE_text.h:90
bool BKE_subdiv_ccg_check_coord_valid(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
static void adjacent_vertices_index_from_adjacent_edge(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, const Span< int > corner_verts, const OffsetIndices< int > faces, int &r_v1, int &r_v2)
BLI_INLINE SubdivCCGCoord coord_at_next_row(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void BKE_subdiv_ccg_average_grids(SubdivCCG &subdiv_ccg)
bool BKE_subdiv_ccg_coord_is_mesh_boundary(OffsetIndices< int > faces, Span< int > corner_verts, BitSpan boundary_verts, const Set< OrderedEdge > &boundary_edges, const SubdivCCG &subdiv_ccg, SubdivCCGCoord coord)
BLI_INLINE SubdivCCGCoord coord_at_prev_col(const SubdivCCG &, const SubdivCCGCoord &coord)
BitGroupVector & BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG &subdiv_ccg)
MINLINE void mul_v3_fl(float r[3], float f)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
void BKE_subdiv_ccg_grid_hidden_free(SubdivCCG &subdiv_ccg)
MINLINE int bitscan_forward_i(int a)
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, bool include_duplicates, SubdivCCGNeighbors &r_neighbors)
float normal_quad_v3(float n[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:62
BLI_INLINE bool is_inner_edge_grid_coordinate(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
void BKE_subdiv_ccg_update_normals(SubdivCCG &subdiv_ccg, const IndexMask &face_mask)
void BKE_subdiv_ccg_recalc_normals(SubdivCCG &subdiv_ccg)
BLI_INLINE SubdivCCGCoord coord_at_prev_row(const SubdivCCG &, const SubdivCCGCoord &coord)
BLI_INLINE SubdivCCGCoord coord_at_next_col(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
MINLINE void zero_v3(float r[3])
int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG &subdiv_ccg, const int grid_index)
int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
Definition BKE_ccg.hh:75
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void add_v3_v3(float r[3], const float a[3])
ATTR_WARN_UNUSED_RESULT const BMVert * v2
std::unique_ptr< SubdivCCG > BKE_subdiv_to_ccg(bke::subdiv::Subdiv &subdiv, const SubdivToCCGSettings &settings, const Mesh &coarse_mesh, SubdivCCGMaskEvaluator *mask_evaluator=nullptr)
CCGKey BKE_subdiv_ccg_key(const SubdivCCG &subdiv_ccg, int level)
SubdivCCGAdjacencyType BKE_subdiv_ccg_coarse_mesh_adjacency_info_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, Span< int > corner_verts, OffsetIndices< int > faces, int &r_v1, int &r_v2)
void BKE_subdiv_ccg_eval_limit_positions(const SubdivCCG &subdiv_ccg, const CCGKey &key, int grid_index, MutableSpan< float3 > r_limit_positions)
BLI_INLINE void subdiv_ccg_neighbors_init(SubdivCCGNeighbors &neighbors, const int num_unique, const int num_duplicates)
void BKE_subdiv_ccg_topology_counters(const SubdivCCG &subdiv_ccg, int &r_num_vertices, int &r_num_edges, int &r_num_faces, int &r_num_loops)
Mesh * BKE_subdiv_to_ccg_mesh(bke::subdiv::Subdiv &subdiv, const SubdivToCCGSettings &settings, const Mesh &coarse_mesh)
ATTR_WARN_UNUSED_RESULT const void * element
void BKE_subdiv_ccg_average_stitch_faces(SubdivCCG &subdiv_ccg, const IndexMask &face_mask)
BLI_INLINE bool is_boundary_grid_coord(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
static void subdiv_ccg_coord_to_ptex_coord(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, int &r_ptex_face_index, float &r_u, float &r_v)
BLI_INLINE bool is_corner_grid_coord(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
void BKE_subdiv_ccg_print_coord(const char *message, const SubdivCCGCoord &coord)
OrderedEdge(const int v1, const int v2)
Vector< Array< SubdivCCGCoord > > boundary_coords
Vector< SubdivCCGCoord > corner_coords
void(*) free(SubdivCCGMaskEvaluator *mask_evaluator)
SubdivCCGNeighborCoords coords
Array< SubdivCCGAdjacentEdge > adjacent_edges
bke::subdiv::Subdiv * subdiv
OffsetIndices< int > faces
Array< float3 > normals
BitGroupVector grid_hidden
Array< SubdivCCGAdjacentVertex > adjacent_verts