Blender  V3.3
subdiv_ccg.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2018 Blender Foundation. All rights reserved. */
3 
8 #include "BKE_subdiv_ccg.h"
9 
10 #include "DNA_mesh_types.h"
11 #include "DNA_meshdata_types.h"
12 
13 #include "MEM_guardedalloc.h"
14 
15 #include "BLI_ghash.h"
16 #include "BLI_math_bits.h"
17 #include "BLI_math_vector.h"
18 #include "BLI_task.h"
19 
20 #include "BKE_DerivedMesh.h"
21 #include "BKE_ccg.h"
22 #include "BKE_global.h"
23 #include "BKE_mesh.h"
24 #include "BKE_subdiv.h"
25 #include "BKE_subdiv_eval.h"
26 
28 
29 /* -------------------------------------------------------------------- */
34 
35 static void subdiv_ccg_average_inner_face_grids(SubdivCCG *subdiv_ccg,
36  CCGKey *key,
37  SubdivCCGFace *face);
38 
40  CCGKey *key,
41  struct CCGFace **effected_faces,
42  int num_effected_faces);
43 
46 /* -------------------------------------------------------------------- */
50 /* Number of floats in per-vertex elements. */
51 static int num_element_float_get(const SubdivCCG *subdiv_ccg)
52 {
53  /* We always have 3 floats for coordinate. */
54  int num_floats = 3;
55  if (subdiv_ccg->has_normal) {
56  num_floats += 3;
57  }
58  if (subdiv_ccg->has_mask) {
59  num_floats += 1;
60  }
61  return num_floats;
62 }
63 
64 /* Per-vertex element size in bytes. */
65 static int element_size_bytes_get(const SubdivCCG *subdiv_ccg)
66 {
67  return sizeof(float) * num_element_float_get(subdiv_ccg);
68 }
69 
72 /* -------------------------------------------------------------------- */
76 static void subdiv_ccg_init_layers(SubdivCCG *subdiv_ccg, const SubdivToCCGSettings *settings)
77 {
78  /* CCG always contains coordinates. Rest of layers are coming after them. */
79  int layer_offset = sizeof(float[3]);
80  /* Mask. */
81  if (settings->need_mask) {
82  subdiv_ccg->has_mask = true;
83  subdiv_ccg->mask_offset = layer_offset;
84  layer_offset += sizeof(float);
85  }
86  else {
87  subdiv_ccg->has_mask = false;
88  subdiv_ccg->mask_offset = -1;
89  }
90  /* Normals.
91  *
92  * NOTE: Keep them at the end, matching old CCGDM. Doesn't really matter
93  * here, but some other area might in theory depend memory layout. */
94  if (settings->need_normal) {
95  subdiv_ccg->has_normal = true;
96  subdiv_ccg->normal_offset = layer_offset;
97  layer_offset += sizeof(float[3]);
98  }
99  else {
100  subdiv_ccg->has_normal = false;
101  subdiv_ccg->normal_offset = -1;
102  }
103 }
104 
105 /* TODO(sergey): Make it more accessible function. */
107 {
108  const int num_faces = topology_refiner->getNumFaces(topology_refiner);
109  int num_corners = 0;
110  for (int face_index = 0; face_index < num_faces; face_index++) {
111  num_corners += topology_refiner->getNumFaceVertices(topology_refiner, face_index);
112  }
113  return num_corners;
114 }
115 
116 /* NOTE: Grid size and layer flags are to be filled in before calling this
117  * function. */
118 static void subdiv_ccg_alloc_elements(SubdivCCG *subdiv_ccg, Subdiv *subdiv)
119 {
120  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
121  const int element_size = element_size_bytes_get(subdiv_ccg);
122  /* Allocate memory for surface grids. */
123  const int num_faces = topology_refiner->getNumFaces(topology_refiner);
124  const int num_grids = topology_refiner_count_face_corners(topology_refiner);
125  const int grid_size = BKE_subdiv_grid_size_from_level(subdiv_ccg->level);
126  const int grid_area = grid_size * grid_size;
127  subdiv_ccg->grid_element_size = element_size;
128  subdiv_ccg->num_grids = num_grids;
129  subdiv_ccg->grids = MEM_calloc_arrayN(num_grids, sizeof(CCGElem *), "subdiv ccg grids");
130  subdiv_ccg->grids_storage = MEM_calloc_arrayN(
131  num_grids, ((size_t)grid_area) * element_size, "subdiv ccg grids storage");
132  const size_t grid_size_in_bytes = (size_t)grid_area * element_size;
133  for (int grid_index = 0; grid_index < num_grids; grid_index++) {
134  const size_t grid_offset = grid_size_in_bytes * grid_index;
135  subdiv_ccg->grids[grid_index] = (CCGElem *)&subdiv_ccg->grids_storage[grid_offset];
136  }
137  /* Grid material flags. */
138  subdiv_ccg->grid_flag_mats = MEM_calloc_arrayN(
139  num_grids, sizeof(DMFlagMat), "ccg grid material flags");
140  /* Grid hidden flags. */
141  subdiv_ccg->grid_hidden = MEM_calloc_arrayN(
142  num_grids, sizeof(BLI_bitmap *), "ccg grid material flags");
143  for (int grid_index = 0; grid_index < num_grids; grid_index++) {
144  subdiv_ccg->grid_hidden[grid_index] = BLI_BITMAP_NEW(grid_area, "ccg grid hidden");
145  }
146  /* TODO(sergey): Allocate memory for loose elements. */
147  /* Allocate memory for faces. */
148  subdiv_ccg->num_faces = num_faces;
149  if (num_faces) {
150  subdiv_ccg->faces = MEM_calloc_arrayN(num_faces, sizeof(SubdivCCGFace), "Subdiv CCG faces");
151  subdiv_ccg->grid_faces = MEM_calloc_arrayN(
152  num_grids, sizeof(SubdivCCGFace *), "Subdiv CCG grid faces");
153  }
154 }
155 
158 /* -------------------------------------------------------------------- */
162 typedef struct CCGEvalGridsData {
169 
171  const int ptex_face_index,
172  const float u,
173  const float v,
174  unsigned char *element)
175 {
176  Subdiv *subdiv = data->subdiv;
177  SubdivCCG *subdiv_ccg = data->subdiv_ccg;
178  if (subdiv->displacement_evaluator != NULL) {
179  BKE_subdiv_eval_final_point(subdiv, ptex_face_index, u, v, (float *)element);
180  }
181  else if (subdiv_ccg->has_normal) {
183  ptex_face_index,
184  u,
185  v,
186  (float *)element,
187  (float *)(element + subdiv_ccg->normal_offset));
188  }
189  else {
190  BKE_subdiv_eval_limit_point(subdiv, ptex_face_index, u, v, (float *)element);
191  }
192 }
193 
195  const int ptex_face_index,
196  const float u,
197  const float v,
198  unsigned char *element)
199 {
200  SubdivCCG *subdiv_ccg = data->subdiv_ccg;
201  if (!subdiv_ccg->has_mask) {
202  return;
203  }
204  float *mask_value_ptr = (float *)(element + subdiv_ccg->mask_offset);
205  if (data->mask_evaluator != NULL) {
206  *mask_value_ptr = data->mask_evaluator->eval_mask(data->mask_evaluator, ptex_face_index, u, v);
207  }
208  else {
209  *mask_value_ptr = 0.0f;
210  }
211 }
212 
214  const int ptex_face_index,
215  const float u,
216  const float v,
217  unsigned char *element)
218 {
219  subdiv_ccg_eval_grid_element_limit(data, ptex_face_index, u, v, element);
220  subdiv_ccg_eval_grid_element_mask(data, ptex_face_index, u, v, element);
221 }
222 
223 static void subdiv_ccg_eval_regular_grid(CCGEvalGridsData *data, const int face_index)
224 {
225  SubdivCCG *subdiv_ccg = data->subdiv_ccg;
226  const int ptex_face_index = data->face_ptex_offset[face_index];
227  const int grid_size = subdiv_ccg->grid_size;
228  const float grid_size_1_inv = 1.0f / (grid_size - 1);
229  const int element_size = element_size_bytes_get(subdiv_ccg);
230  SubdivCCGFace *faces = subdiv_ccg->faces;
231  SubdivCCGFace **grid_faces = subdiv_ccg->grid_faces;
232  const SubdivCCGFace *face = &faces[face_index];
233  for (int corner = 0; corner < face->num_grids; corner++) {
234  const int grid_index = face->start_grid_index + corner;
235  unsigned char *grid = (unsigned char *)subdiv_ccg->grids[grid_index];
236  for (int y = 0; y < grid_size; y++) {
237  const float grid_v = y * grid_size_1_inv;
238  for (int x = 0; x < grid_size; x++) {
239  const float grid_u = x * grid_size_1_inv;
240  float u, v;
241  BKE_subdiv_rotate_grid_to_quad(corner, grid_u, grid_v, &u, &v);
242  const size_t grid_element_index = (size_t)y * grid_size + x;
243  const size_t grid_element_offset = grid_element_index * element_size;
244  subdiv_ccg_eval_grid_element(data, ptex_face_index, u, v, &grid[grid_element_offset]);
245  }
246  }
247  /* Assign grid's face. */
248  grid_faces[grid_index] = &faces[face_index];
249  /* Assign material flags. */
250  subdiv_ccg->grid_flag_mats[grid_index] = data->material_flags_evaluator->eval_material_flags(
251  data->material_flags_evaluator, face_index);
252  }
253 }
254 
255 static void subdiv_ccg_eval_special_grid(CCGEvalGridsData *data, const int face_index)
256 {
257  SubdivCCG *subdiv_ccg = data->subdiv_ccg;
258  const int grid_size = subdiv_ccg->grid_size;
259  const float grid_size_1_inv = 1.0f / (grid_size - 1);
260  const int element_size = element_size_bytes_get(subdiv_ccg);
261  SubdivCCGFace *faces = subdiv_ccg->faces;
262  SubdivCCGFace **grid_faces = subdiv_ccg->grid_faces;
263  const SubdivCCGFace *face = &faces[face_index];
264  for (int corner = 0; corner < face->num_grids; corner++) {
265  const int grid_index = face->start_grid_index + corner;
266  const int ptex_face_index = data->face_ptex_offset[face_index] + corner;
267  unsigned char *grid = (unsigned char *)subdiv_ccg->grids[grid_index];
268  for (int y = 0; y < grid_size; y++) {
269  const float u = 1.0f - (y * grid_size_1_inv);
270  for (int x = 0; x < grid_size; x++) {
271  const float v = 1.0f - (x * grid_size_1_inv);
272  const size_t grid_element_index = (size_t)y * grid_size + x;
273  const size_t grid_element_offset = grid_element_index * element_size;
274  subdiv_ccg_eval_grid_element(data, ptex_face_index, u, v, &grid[grid_element_offset]);
275  }
276  }
277  /* Assign grid's face. */
278  grid_faces[grid_index] = &faces[face_index];
279  /* Assign material flags. */
280  subdiv_ccg->grid_flag_mats[grid_index] = data->material_flags_evaluator->eval_material_flags(
281  data->material_flags_evaluator, face_index);
282  }
283 }
284 
285 static void subdiv_ccg_eval_grids_task(void *__restrict userdata_v,
286  const int face_index,
287  const TaskParallelTLS *__restrict UNUSED(tls))
288 {
289  CCGEvalGridsData *data = userdata_v;
290  SubdivCCG *subdiv_ccg = data->subdiv_ccg;
291  SubdivCCGFace *face = &subdiv_ccg->faces[face_index];
292  if (face->num_grids == 4) {
293  subdiv_ccg_eval_regular_grid(data, face_index);
294  }
295  else {
296  subdiv_ccg_eval_special_grid(data, face_index);
297  }
298 }
299 
300 static bool subdiv_ccg_evaluate_grids(SubdivCCG *subdiv_ccg,
301  Subdiv *subdiv,
302  SubdivCCGMaskEvaluator *mask_evaluator,
303  SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator)
304 {
305  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
306  const int num_faces = topology_refiner->getNumFaces(topology_refiner);
307  /* Initialize data passed to all the tasks. */
309  data.subdiv_ccg = subdiv_ccg;
310  data.subdiv = subdiv;
311  data.face_ptex_offset = BKE_subdiv_face_ptex_offset_get(subdiv);
312  data.mask_evaluator = mask_evaluator;
313  data.material_flags_evaluator = material_flags_evaluator;
314  /* Threaded grids evaluation. */
315  TaskParallelSettings parallel_range_settings;
316  BLI_parallel_range_settings_defaults(&parallel_range_settings);
318  0, num_faces, &data, subdiv_ccg_eval_grids_task, &parallel_range_settings);
319  /* If displacement is used, need to calculate normals after all final
320  * coordinates are known. */
321  if (subdiv->displacement_evaluator != NULL) {
322  BKE_subdiv_ccg_recalc_normals(subdiv_ccg);
323  }
324  return true;
325 }
326 
327 /* Initialize face descriptors, assuming memory for them was already
328  * allocated. */
329 static void subdiv_ccg_init_faces(SubdivCCG *subdiv_ccg)
330 {
331  Subdiv *subdiv = subdiv_ccg->subdiv;
332  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
333  const int num_faces = subdiv_ccg->num_faces;
334  int corner_index = 0;
335  for (int face_index = 0; face_index < num_faces; face_index++) {
336  const int num_corners = topology_refiner->getNumFaceVertices(topology_refiner, face_index);
337  subdiv_ccg->faces[face_index].num_grids = num_corners;
338  subdiv_ccg->faces[face_index].start_grid_index = corner_index;
339  corner_index += num_corners;
340  }
341 }
342 
343 /* TODO(sergey): Consider making it generic enough to be fit into BLI. */
344 typedef struct StaticOrHeapIntStorage {
345  int static_storage[64];
350 
352 {
353  storage->static_storage_len = sizeof(storage->static_storage) / sizeof(*storage->static_storage);
354  storage->heap_storage = NULL;
355  storage->heap_storage_len = 0;
356 }
357 
358 static int *static_or_heap_storage_get(StaticOrHeapIntStorage *storage, int heap_len)
359 {
360  /* Requested size small enough to be fit into stack allocated memory. */
361  if (heap_len <= storage->static_storage_len) {
362  return storage->static_storage;
363  }
364  /* Make sure heap ius big enough. */
365  if (heap_len > storage->heap_storage_len) {
366  MEM_SAFE_FREE(storage->heap_storage);
367  storage->heap_storage = MEM_malloc_arrayN(heap_len, sizeof(int), "int storage");
368  storage->heap_storage_len = heap_len;
369  }
370  return storage->heap_storage;
371 }
372 
374 {
375  MEM_SAFE_FREE(storage->heap_storage);
376 }
377 
378 static void subdiv_ccg_allocate_adjacent_edges(SubdivCCG *subdiv_ccg, const int num_edges)
379 {
380  subdiv_ccg->num_adjacent_edges = num_edges;
381  subdiv_ccg->adjacent_edges = MEM_calloc_arrayN(
382  subdiv_ccg->num_adjacent_edges, sizeof(*subdiv_ccg->adjacent_edges), "ccg adjacent edges");
383 }
384 
385 static SubdivCCGCoord subdiv_ccg_coord(int grid_index, int x, int y)
386 {
387  SubdivCCGCoord coord = {.grid_index = grid_index, .x = x, .y = y};
388  return coord;
389 }
390 
392  const SubdivCCG *subdiv_ccg,
393  const SubdivCCGCoord *coord)
394 {
395  return CCG_grid_elem(key, subdiv_ccg->grids[coord->grid_index], coord->x, coord->y);
396 }
397 
398 /* Returns storage where boundary elements are to be stored. */
400  SubdivCCGAdjacentEdge *adjacent_edge)
401 {
402  const int grid_size = subdiv_ccg->grid_size * 2;
403  const int adjacent_face_index = adjacent_edge->num_adjacent_faces;
404  ++adjacent_edge->num_adjacent_faces;
405  /* Allocate memory for the boundary elements. */
406  adjacent_edge->boundary_coords = MEM_reallocN(adjacent_edge->boundary_coords,
407  adjacent_edge->num_adjacent_faces *
408  sizeof(*adjacent_edge->boundary_coords));
409  adjacent_edge->boundary_coords[adjacent_face_index] = MEM_malloc_arrayN(
410  grid_size * 2, sizeof(SubdivCCGCoord), "ccg adjacent boundary");
411  return adjacent_edge->boundary_coords[adjacent_face_index];
412 }
413 
415 {
416  Subdiv *subdiv = subdiv_ccg->subdiv;
417  SubdivCCGFace *faces = subdiv_ccg->faces;
418  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
419  const int num_edges = topology_refiner->getNumEdges(topology_refiner);
420  const int grid_size = subdiv_ccg->grid_size;
421  if (num_edges == 0) {
422  /* Early output, nothing to do in this case. */
423  return;
424  }
425  subdiv_ccg_allocate_adjacent_edges(subdiv_ccg, num_edges);
426  /* Initialize storage. */
427  StaticOrHeapIntStorage face_vertices_storage;
428  StaticOrHeapIntStorage face_edges_storage;
429  static_or_heap_storage_init(&face_vertices_storage);
430  static_or_heap_storage_init(&face_edges_storage);
431  /* Store adjacency for all faces. */
432  const int num_faces = subdiv_ccg->num_faces;
433  for (int face_index = 0; face_index < num_faces; face_index++) {
434  SubdivCCGFace *face = &faces[face_index];
435  const int num_face_grids = face->num_grids;
436  const int num_face_edges = num_face_grids;
437  int *face_vertices = static_or_heap_storage_get(&face_vertices_storage, num_face_edges);
438  topology_refiner->getFaceVertices(topology_refiner, face_index, face_vertices);
439  /* Note that order of edges is same as order of MLoops, which also
440  * means it's the same as order of grids. */
441  int *face_edges = static_or_heap_storage_get(&face_edges_storage, num_face_edges);
442  topology_refiner->getFaceEdges(topology_refiner, face_index, face_edges);
443  /* Store grids adjacency for this edge. */
444  for (int corner = 0; corner < num_face_edges; corner++) {
445  const int vertex_index = face_vertices[corner];
446  const int edge_index = face_edges[corner];
447  int edge_vertices[2];
448  topology_refiner->getEdgeVertices(topology_refiner, edge_index, edge_vertices);
449  const bool is_edge_flipped = (edge_vertices[0] != vertex_index);
450  /* Grid which is adjacent to the current corner. */
451  const int current_grid_index = face->start_grid_index + corner;
452  /* Grid which is adjacent to the next corner. */
453  const int next_grid_index = face->start_grid_index + (corner + 1) % num_face_grids;
454  /* Add new face to the adjacent edge. */
455  SubdivCCGAdjacentEdge *adjacent_edge = &subdiv_ccg->adjacent_edges[edge_index];
456  SubdivCCGCoord *boundary_coords = subdiv_ccg_adjacent_edge_add_face(subdiv_ccg,
457  adjacent_edge);
458  /* Fill CCG elements along the edge. */
459  int boundary_element_index = 0;
460  if (is_edge_flipped) {
461  for (int i = 0; i < grid_size; i++) {
462  boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
463  next_grid_index, grid_size - i - 1, grid_size - 1);
464  }
465  for (int i = 0; i < grid_size; i++) {
466  boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
467  current_grid_index, grid_size - 1, i);
468  }
469  }
470  else {
471  for (int i = 0; i < grid_size; i++) {
472  boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
473  current_grid_index, grid_size - 1, grid_size - i - 1);
474  }
475  for (int i = 0; i < grid_size; i++) {
476  boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
477  next_grid_index, i, grid_size - 1);
478  }
479  }
480  }
481  }
482  /* Free possibly heap-allocated storage. */
483  static_or_heap_storage_free(&face_vertices_storage);
484  static_or_heap_storage_free(&face_edges_storage);
485 }
486 
487 static void subdiv_ccg_allocate_adjacent_vertices(SubdivCCG *subdiv_ccg, const int num_vertices)
488 {
489  subdiv_ccg->num_adjacent_vertices = num_vertices;
491  sizeof(*subdiv_ccg->adjacent_vertices),
492  "ccg adjacent vertices");
493 }
494 
495 /* Returns storage where corner elements are to be stored. This is a pointer
496  * to the actual storage. */
498  SubdivCCGAdjacentVertex *adjacent_vertex)
499 {
500  const int adjacent_face_index = adjacent_vertex->num_adjacent_faces;
501  ++adjacent_vertex->num_adjacent_faces;
502  /* Allocate memory for the boundary elements. */
503  adjacent_vertex->corner_coords = MEM_reallocN(adjacent_vertex->corner_coords,
504  adjacent_vertex->num_adjacent_faces *
505  sizeof(*adjacent_vertex->corner_coords));
506  return &adjacent_vertex->corner_coords[adjacent_face_index];
507 }
508 
510 {
511  Subdiv *subdiv = subdiv_ccg->subdiv;
512  SubdivCCGFace *faces = subdiv_ccg->faces;
513  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
514  const int num_vertices = topology_refiner->getNumVertices(topology_refiner);
515  const int grid_size = subdiv_ccg->grid_size;
516  if (num_vertices == 0) {
517  /* Early output, nothing to do in this case. */
518  return;
519  }
520  subdiv_ccg_allocate_adjacent_vertices(subdiv_ccg, num_vertices);
521  /* Initialize storage. */
522  StaticOrHeapIntStorage face_vertices_storage;
523  static_or_heap_storage_init(&face_vertices_storage);
524  /* Key to access elements. */
525  CCGKey key;
526  BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
527  /* Store adjacency for all faces. */
528  const int num_faces = subdiv_ccg->num_faces;
529  for (int face_index = 0; face_index < num_faces; face_index++) {
530  SubdivCCGFace *face = &faces[face_index];
531  const int num_face_grids = face->num_grids;
532  const int num_face_edges = num_face_grids;
533  int *face_vertices = static_or_heap_storage_get(&face_vertices_storage, num_face_edges);
534  topology_refiner->getFaceVertices(topology_refiner, face_index, face_vertices);
535  for (int corner = 0; corner < num_face_edges; corner++) {
536  const int vertex_index = face_vertices[corner];
537  /* Grid which is adjacent to the current corner. */
538  const int grid_index = face->start_grid_index + corner;
539  /* Add new face to the adjacent edge. */
540  SubdivCCGAdjacentVertex *adjacent_vertex = &subdiv_ccg->adjacent_vertices[vertex_index];
541  SubdivCCGCoord *corner_coord = subdiv_ccg_adjacent_vertex_add_face(adjacent_vertex);
542  *corner_coord = subdiv_ccg_coord(grid_index, grid_size - 1, grid_size - 1);
543  }
544  }
545  /* Free possibly heap-allocated storage. */
546  static_or_heap_storage_free(&face_vertices_storage);
547 }
548 
550 {
553 }
554 
557 /* -------------------------------------------------------------------- */
562  const SubdivToCCGSettings *settings,
563  SubdivCCGMaskEvaluator *mask_evaluator,
564  SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator)
565 {
567  SubdivCCG *subdiv_ccg = MEM_callocN(sizeof(SubdivCCG), "subdiv ccg");
568  subdiv_ccg->subdiv = subdiv;
569  subdiv_ccg->level = bitscan_forward_i(settings->resolution - 1);
570  subdiv_ccg->grid_size = BKE_subdiv_grid_size_from_level(subdiv_ccg->level);
571  subdiv_ccg_init_layers(subdiv_ccg, settings);
572  subdiv_ccg_alloc_elements(subdiv_ccg, subdiv);
573  subdiv_ccg_init_faces(subdiv_ccg);
575  if (!subdiv_ccg_evaluate_grids(subdiv_ccg, subdiv, mask_evaluator, material_flags_evaluator)) {
576  BKE_subdiv_ccg_destroy(subdiv_ccg);
578  return NULL;
579  }
581  return subdiv_ccg;
582 }
583 
585  const SubdivToCCGSettings *settings,
586  const Mesh *coarse_mesh)
587 {
588  /* Make sure evaluator is ready. */
591  subdiv, coarse_mesh, NULL, SUBDIV_EVALUATOR_TYPE_CPU, NULL)) {
592  if (coarse_mesh->totpoly) {
593  return NULL;
594  }
595  }
597  SubdivCCGMaskEvaluator mask_evaluator;
598  bool has_mask = BKE_subdiv_ccg_mask_init_from_paint(&mask_evaluator, coarse_mesh);
599  SubdivCCGMaterialFlagsEvaluator material_flags_evaluator;
600  BKE_subdiv_ccg_material_flags_init_from_mesh(&material_flags_evaluator, coarse_mesh);
601  SubdivCCG *subdiv_ccg = BKE_subdiv_to_ccg(
602  subdiv, settings, has_mask ? &mask_evaluator : NULL, &material_flags_evaluator);
603  if (has_mask) {
604  mask_evaluator.free(&mask_evaluator);
605  }
606  material_flags_evaluator.free(&material_flags_evaluator);
607  if (subdiv_ccg == NULL) {
608  return NULL;
609  }
610  Mesh *result = BKE_mesh_new_nomain_from_template(coarse_mesh, 0, 0, 0, 0, 0);
611  result->runtime.subdiv_ccg = subdiv_ccg;
612  return result;
613 }
614 
616 {
617  const int num_grids = subdiv_ccg->num_grids;
618  MEM_SAFE_FREE(subdiv_ccg->grids);
619  MEM_SAFE_FREE(subdiv_ccg->grids_storage);
620  MEM_SAFE_FREE(subdiv_ccg->edges);
621  MEM_SAFE_FREE(subdiv_ccg->vertices);
622  MEM_SAFE_FREE(subdiv_ccg->grid_flag_mats);
623  if (subdiv_ccg->grid_hidden != NULL) {
624  for (int grid_index = 0; grid_index < num_grids; grid_index++) {
625  MEM_SAFE_FREE(subdiv_ccg->grid_hidden[grid_index]);
626  }
627  MEM_SAFE_FREE(subdiv_ccg->grid_hidden);
628  }
629  if (subdiv_ccg->subdiv != NULL) {
630  BKE_subdiv_free(subdiv_ccg->subdiv);
631  }
632  MEM_SAFE_FREE(subdiv_ccg->faces);
633  MEM_SAFE_FREE(subdiv_ccg->grid_faces);
634  /* Free map of adjacent edges. */
635  for (int i = 0; i < subdiv_ccg->num_adjacent_edges; i++) {
636  SubdivCCGAdjacentEdge *adjacent_edge = &subdiv_ccg->adjacent_edges[i];
637  for (int face_index = 0; face_index < adjacent_edge->num_adjacent_faces; face_index++) {
638  MEM_SAFE_FREE(adjacent_edge->boundary_coords[face_index]);
639  }
640  MEM_SAFE_FREE(adjacent_edge->boundary_coords);
641  }
642  MEM_SAFE_FREE(subdiv_ccg->adjacent_edges);
643  /* Free map of adjacent vertices. */
644  for (int i = 0; i < subdiv_ccg->num_adjacent_vertices; i++) {
645  SubdivCCGAdjacentVertex *adjacent_vertex = &subdiv_ccg->adjacent_vertices[i];
646  MEM_SAFE_FREE(adjacent_vertex->corner_coords);
647  }
648  MEM_SAFE_FREE(subdiv_ccg->adjacent_vertices);
650  MEM_freeN(subdiv_ccg);
651 }
652 
653 void BKE_subdiv_ccg_key(CCGKey *key, const SubdivCCG *subdiv_ccg, int level)
654 {
655  key->level = level;
656  key->elem_size = element_size_bytes_get(subdiv_ccg);
658  key->grid_area = key->grid_size * key->grid_size;
659  key->grid_bytes = key->elem_size * key->grid_area;
660 
661  key->normal_offset = subdiv_ccg->normal_offset;
662  key->mask_offset = subdiv_ccg->mask_offset;
663 
664  key->has_normals = subdiv_ccg->has_normal;
665  key->has_mask = subdiv_ccg->has_mask;
666 }
667 
668 void BKE_subdiv_ccg_key_top_level(CCGKey *key, const SubdivCCG *subdiv_ccg)
669 {
670  BKE_subdiv_ccg_key(key, subdiv_ccg, subdiv_ccg->level);
671 }
672 
675 /* -------------------------------------------------------------------- */
679 typedef struct RecalcInnerNormalsData {
683 
687 
688 /* Evaluate high-res face normals, for faces which corresponds to grid elements
689  *
690  * {(x, y), {x + 1, y}, {x + 1, y + 1}, {x, y + 1}}
691  *
692  * The result is stored in normals storage from TLS. */
694  CCGKey *key,
696  const int grid_index)
697 {
698  const int grid_size = subdiv_ccg->grid_size;
699  const int grid_size_1 = grid_size - 1;
700  CCGElem *grid = subdiv_ccg->grids[grid_index];
701  if (tls->face_normals == NULL) {
703  grid_size_1 * grid_size_1, sizeof(float[3]), "CCG TLS normals");
704  }
705  for (int y = 0; y < grid_size - 1; y++) {
706  for (int x = 0; x < grid_size - 1; x++) {
707  CCGElem *grid_elements[4] = {
708  CCG_grid_elem(key, grid, x, y + 1),
709  CCG_grid_elem(key, grid, x + 1, y + 1),
710  CCG_grid_elem(key, grid, x + 1, y),
711  CCG_grid_elem(key, grid, x, y),
712  };
713  float *co[4] = {
714  CCG_elem_co(key, grid_elements[0]),
715  CCG_elem_co(key, grid_elements[1]),
716  CCG_elem_co(key, grid_elements[2]),
717  CCG_elem_co(key, grid_elements[3]),
718  };
719  const int face_index = y * grid_size_1 + x;
720  float *face_normal = tls->face_normals[face_index];
721  normal_quad_v3(face_normal, co[0], co[1], co[2], co[3]);
722  }
723  }
724 }
725 
726 /* Average normals at every grid element, using adjacent faces normals. */
728  CCGKey *key,
730  const int grid_index)
731 {
732  const int grid_size = subdiv_ccg->grid_size;
733  const int grid_size_1 = grid_size - 1;
734  CCGElem *grid = subdiv_ccg->grids[grid_index];
735  const float(*face_normals)[3] = tls->face_normals;
736  for (int y = 0; y < grid_size; y++) {
737  for (int x = 0; x < grid_size; x++) {
738  float normal_acc[3] = {0.0f, 0.0f, 0.0f};
739  int counter = 0;
740  /* Accumulate normals of all adjacent faces. */
741  if (x < grid_size_1 && y < grid_size_1) {
742  add_v3_v3(normal_acc, face_normals[y * grid_size_1 + x]);
743  counter++;
744  }
745  if (x >= 1) {
746  if (y < grid_size_1) {
747  add_v3_v3(normal_acc, face_normals[y * grid_size_1 + (x - 1)]);
748  counter++;
749  }
750  if (y >= 1) {
751  add_v3_v3(normal_acc, face_normals[(y - 1) * grid_size_1 + (x - 1)]);
752  counter++;
753  }
754  }
755  if (y >= 1 && x < grid_size_1) {
756  add_v3_v3(normal_acc, face_normals[(y - 1) * grid_size_1 + x]);
757  counter++;
758  }
759  /* Normalize and store. */
760  mul_v3_v3fl(CCG_grid_elem_no(key, grid, x, y), normal_acc, 1.0f / counter);
761  }
762  }
763 }
764 
765 static void subdiv_ccg_recalc_inner_normal_task(void *__restrict userdata_v,
766  const int grid_index,
767  const TaskParallelTLS *__restrict tls_v)
768 {
769  RecalcInnerNormalsData *data = userdata_v;
770  RecalcInnerNormalsTLSData *tls = tls_v->userdata_chunk;
771  subdiv_ccg_recalc_inner_face_normals(data->subdiv_ccg, data->key, tls, grid_index);
772  subdiv_ccg_average_inner_face_normals(data->subdiv_ccg, data->key, tls, grid_index);
773 }
774 
775 static void subdiv_ccg_recalc_inner_normal_free(const void *__restrict UNUSED(userdata),
776  void *__restrict tls_v)
777 {
778  RecalcInnerNormalsTLSData *tls = tls_v;
780 }
781 
782 /* Recalculate normals which corresponds to non-boundaries elements of grids. */
784 {
785  CCGKey key;
786  BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
788  .subdiv_ccg = subdiv_ccg,
789  .key = &key,
790  };
791  RecalcInnerNormalsTLSData tls_data = {NULL};
792  TaskParallelSettings parallel_range_settings;
793  BLI_parallel_range_settings_defaults(&parallel_range_settings);
794  parallel_range_settings.userdata_chunk = &tls_data;
795  parallel_range_settings.userdata_chunk_size = sizeof(tls_data);
796  parallel_range_settings.func_free = subdiv_ccg_recalc_inner_normal_free;
798  subdiv_ccg->num_grids,
799  &data,
801  &parallel_range_settings);
802 }
803 
805 {
806  if (!subdiv_ccg->has_normal) {
807  /* Grids don't have normals, can do early output. */
808  return;
809  }
811  BKE_subdiv_ccg_average_grids(subdiv_ccg);
812 }
813 
819 
820 static void subdiv_ccg_recalc_modified_inner_normal_task(void *__restrict userdata_v,
821  const int face_index,
822  const TaskParallelTLS *__restrict tls_v)
823 {
824  RecalcModifiedInnerNormalsData *data = userdata_v;
825  SubdivCCG *subdiv_ccg = data->subdiv_ccg;
826  CCGKey *key = data->key;
827  RecalcInnerNormalsTLSData *tls = tls_v->userdata_chunk;
828  SubdivCCGFace **faces = data->effected_ccg_faces;
829  SubdivCCGFace *face = faces[face_index];
830  const int num_face_grids = face->num_grids;
831  for (int i = 0; i < num_face_grids; i++) {
832  const int grid_index = face->start_grid_index + i;
833  subdiv_ccg_recalc_inner_face_normals(data->subdiv_ccg, data->key, tls, grid_index);
834  subdiv_ccg_average_inner_face_normals(data->subdiv_ccg, data->key, tls, grid_index);
835  }
836  subdiv_ccg_average_inner_face_grids(subdiv_ccg, key, face);
837 }
838 
839 static void subdiv_ccg_recalc_modified_inner_normal_free(const void *__restrict UNUSED(userdata),
840  void *__restrict tls_v)
841 {
842  RecalcInnerNormalsTLSData *tls = tls_v;
844 }
845 
847  struct CCGFace **effected_faces,
848  int num_effected_faces)
849 {
850  CCGKey key;
851  BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
853  .subdiv_ccg = subdiv_ccg,
854  .key = &key,
855  .effected_ccg_faces = (SubdivCCGFace **)effected_faces,
856  };
857  RecalcInnerNormalsTLSData tls_data = {NULL};
858  TaskParallelSettings parallel_range_settings;
859  BLI_parallel_range_settings_defaults(&parallel_range_settings);
860  parallel_range_settings.userdata_chunk = &tls_data;
861  parallel_range_settings.userdata_chunk_size = sizeof(tls_data);
864  num_effected_faces,
865  &data,
867  &parallel_range_settings);
868 }
869 
871  struct CCGFace **effected_faces,
872  int num_effected_faces)
873 {
874  if (!subdiv_ccg->has_normal) {
875  /* Grids don't have normals, can do early output. */
876  return;
877  }
878  if (num_effected_faces == 0) {
879  /* No faces changed, so nothing to do here. */
880  return;
881  }
882  subdiv_ccg_recalc_modified_inner_grid_normals(subdiv_ccg, effected_faces, num_effected_faces);
883 
884  CCGKey key;
885  BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
886 
888  subdiv_ccg, &key, effected_faces, num_effected_faces);
889 }
890 
893 /* -------------------------------------------------------------------- */
897 typedef struct AverageInnerGridsData {
901 
902 static void average_grid_element_value_v3(float a[3], float b[3])
903 {
904  add_v3_v3(a, b);
905  mul_v3_fl(a, 0.5f);
906  copy_v3_v3(b, a);
907 }
908 
909 static void average_grid_element(SubdivCCG *subdiv_ccg,
910  CCGKey *key,
911  CCGElem *grid_element_a,
912  CCGElem *grid_element_b)
913 {
914  average_grid_element_value_v3(CCG_elem_co(key, grid_element_a),
915  CCG_elem_co(key, grid_element_b));
916  if (subdiv_ccg->has_normal) {
917  average_grid_element_value_v3(CCG_elem_no(key, grid_element_a),
918  CCG_elem_no(key, grid_element_b));
919  }
920  if (subdiv_ccg->has_mask) {
921  float mask = (*CCG_elem_mask(key, grid_element_a) + *CCG_elem_mask(key, grid_element_b)) *
922  0.5f;
923  *CCG_elem_mask(key, grid_element_a) = mask;
924  *CCG_elem_mask(key, grid_element_b) = mask;
925  }
926 }
927 
928 /* Accumulator to hold data during averaging. */
929 typedef struct GridElementAccumulator {
930  float co[3];
931  float no[3];
932  float mask;
934 
936 {
937  zero_v3(accumulator->co);
938  zero_v3(accumulator->no);
939  accumulator->mask = 0.0f;
940 }
941 
943  const SubdivCCG *subdiv_ccg,
944  CCGKey *key,
945  /*const*/ CCGElem *grid_element)
946 {
947  add_v3_v3(accumulator->co, CCG_elem_co(key, grid_element));
948  if (subdiv_ccg->has_normal) {
949  add_v3_v3(accumulator->no, CCG_elem_no(key, grid_element));
950  }
951  if (subdiv_ccg->has_mask) {
952  accumulator->mask += *CCG_elem_mask(key, grid_element);
953  }
954 }
955 
956 static void element_accumulator_mul_fl(GridElementAccumulator *accumulator, const float f)
957 {
958  mul_v3_fl(accumulator->co, f);
959  mul_v3_fl(accumulator->no, f);
960  accumulator->mask *= f;
961 }
962 
963 static void element_accumulator_copy(SubdivCCG *subdiv_ccg,
964  CCGKey *key,
965  CCGElem *destination,
966  const GridElementAccumulator *accumulator)
967 {
968  copy_v3_v3(CCG_elem_co(key, destination), accumulator->co);
969  if (subdiv_ccg->has_normal) {
970  copy_v3_v3(CCG_elem_no(key, destination), accumulator->no);
971  }
972  if (subdiv_ccg->has_mask) {
973  *CCG_elem_mask(key, destination) = accumulator->mask;
974  }
975 }
976 
978  CCGKey *key,
979  SubdivCCGFace *face)
980 {
981  CCGElem **grids = subdiv_ccg->grids;
982  const int num_face_grids = face->num_grids;
983  const int grid_size = subdiv_ccg->grid_size;
984  CCGElem *prev_grid = grids[face->start_grid_index + num_face_grids - 1];
985  /* Average boundary between neighbor grid. */
986  for (int corner = 0; corner < num_face_grids; corner++) {
987  CCGElem *grid = grids[face->start_grid_index + corner];
988  for (int i = 1; i < grid_size; i++) {
989  CCGElem *prev_grid_element = CCG_grid_elem(key, prev_grid, i, 0);
990  CCGElem *grid_element = CCG_grid_elem(key, grid, 0, i);
991  average_grid_element(subdiv_ccg, key, prev_grid_element, grid_element);
992  }
993  prev_grid = grid;
994  }
995  /* Average all grids centers into a single accumulator, and share it.
996  * Guarantees correct and smooth averaging in the center. */
997  GridElementAccumulator center_accumulator;
998  element_accumulator_init(&center_accumulator);
999  for (int corner = 0; corner < num_face_grids; corner++) {
1000  CCGElem *grid = grids[face->start_grid_index + corner];
1001  CCGElem *grid_center_element = CCG_grid_elem(key, grid, 0, 0);
1002  element_accumulator_add(&center_accumulator, subdiv_ccg, key, grid_center_element);
1003  }
1004  element_accumulator_mul_fl(&center_accumulator, 1.0f / num_face_grids);
1005  for (int corner = 0; corner < num_face_grids; corner++) {
1006  CCGElem *grid = grids[face->start_grid_index + corner];
1007  CCGElem *grid_center_element = CCG_grid_elem(key, grid, 0, 0);
1008  element_accumulator_copy(subdiv_ccg, key, grid_center_element, &center_accumulator);
1009  }
1010 }
1011 
1012 static void subdiv_ccg_average_inner_grids_task(void *__restrict userdata_v,
1013  const int face_index,
1014  const TaskParallelTLS *__restrict UNUSED(tls_v))
1015 {
1016  AverageInnerGridsData *data = userdata_v;
1017  SubdivCCG *subdiv_ccg = data->subdiv_ccg;
1018  CCGKey *key = data->key;
1019  SubdivCCGFace *faces = subdiv_ccg->faces;
1020  SubdivCCGFace *face = &faces[face_index];
1021  subdiv_ccg_average_inner_face_grids(subdiv_ccg, key, face);
1022 }
1023 
1027 
1028  /* Optional lookup table. Maps task index to index in `subdiv_ccg->adjacent_vertices`. */
1031 
1035 
1037  CCGKey *key,
1038  SubdivCCGAdjacentEdge *adjacent_edge,
1040 {
1041  const int num_adjacent_faces = adjacent_edge->num_adjacent_faces;
1042  const int grid_size2 = subdiv_ccg->grid_size * 2;
1043  if (num_adjacent_faces == 1) {
1044  /* Nothing to average with. */
1045  return;
1046  }
1047  if (tls->accumulators == NULL) {
1049  grid_size2, sizeof(GridElementAccumulator), "average accumulators");
1050  }
1051  else {
1052  for (int i = 1; i < grid_size2 - 1; i++) {
1054  }
1055  }
1056  for (int face_index = 0; face_index < num_adjacent_faces; face_index++) {
1057  for (int i = 1; i < grid_size2 - 1; i++) {
1058  CCGElem *grid_element = subdiv_ccg_coord_to_elem(
1059  key, subdiv_ccg, &adjacent_edge->boundary_coords[face_index][i]);
1060  element_accumulator_add(&tls->accumulators[i], subdiv_ccg, key, grid_element);
1061  }
1062  }
1063  for (int i = 1; i < grid_size2 - 1; i++) {
1064  element_accumulator_mul_fl(&tls->accumulators[i], 1.0f / num_adjacent_faces);
1065  }
1066  /* Copy averaged value to all the other faces. */
1067  for (int face_index = 0; face_index < num_adjacent_faces; face_index++) {
1068  for (int i = 1; i < grid_size2 - 1; i++) {
1069  CCGElem *grid_element = subdiv_ccg_coord_to_elem(
1070  key, subdiv_ccg, &adjacent_edge->boundary_coords[face_index][i]);
1071  element_accumulator_copy(subdiv_ccg, key, grid_element, &tls->accumulators[i]);
1072  }
1073  }
1074 }
1075 
1076 static void subdiv_ccg_average_grids_boundaries_task(void *__restrict userdata_v,
1077  const int n,
1078  const TaskParallelTLS *__restrict tls_v)
1079 {
1080  AverageGridsBoundariesData *data = userdata_v;
1081  const int adjacent_edge_index = data->adjacent_edge_index_map ?
1082  data->adjacent_edge_index_map[n] :
1083  n;
1084 
1085  AverageGridsBoundariesTLSData *tls = tls_v->userdata_chunk;
1086  SubdivCCG *subdiv_ccg = data->subdiv_ccg;
1087  CCGKey *key = data->key;
1088  SubdivCCGAdjacentEdge *adjacent_edge = &subdiv_ccg->adjacent_edges[adjacent_edge_index];
1089  subdiv_ccg_average_grids_boundary(subdiv_ccg, key, adjacent_edge, tls);
1090 }
1091 
1092 static void subdiv_ccg_average_grids_boundaries_free(const void *__restrict UNUSED(userdata),
1093  void *__restrict tls_v)
1094 {
1095  AverageGridsBoundariesTLSData *tls = tls_v;
1097 }
1098 
1099 typedef struct AverageGridsCornerData {
1102 
1103  /* Optional lookup table. Maps task range index to index in `subdiv_ccg->adjacent_vertices`. */
1106 
1108  CCGKey *key,
1109  SubdivCCGAdjacentVertex *adjacent_vertex)
1110 {
1111  const int num_adjacent_faces = adjacent_vertex->num_adjacent_faces;
1112  if (num_adjacent_faces == 1) {
1113  /* Nothing to average with. */
1114  return;
1115  }
1116  GridElementAccumulator accumulator;
1117  element_accumulator_init(&accumulator);
1118  for (int face_index = 0; face_index < num_adjacent_faces; face_index++) {
1119  CCGElem *grid_element = subdiv_ccg_coord_to_elem(
1120  key, subdiv_ccg, &adjacent_vertex->corner_coords[face_index]);
1121  element_accumulator_add(&accumulator, subdiv_ccg, key, grid_element);
1122  }
1123  element_accumulator_mul_fl(&accumulator, 1.0f / num_adjacent_faces);
1124  /* Copy averaged value to all the other faces. */
1125  for (int face_index = 0; face_index < num_adjacent_faces; face_index++) {
1126  CCGElem *grid_element = subdiv_ccg_coord_to_elem(
1127  key, subdiv_ccg, &adjacent_vertex->corner_coords[face_index]);
1128  element_accumulator_copy(subdiv_ccg, key, grid_element, &accumulator);
1129  }
1130 }
1131 
1132 static void subdiv_ccg_average_grids_corners_task(void *__restrict userdata_v,
1133  const int n,
1134  const TaskParallelTLS *__restrict UNUSED(tls_v))
1135 {
1136  AverageGridsCornerData *data = userdata_v;
1137  const int adjacent_vertex_index = data->adjacent_vert_index_map ?
1138  data->adjacent_vert_index_map[n] :
1139  n;
1140  SubdivCCG *subdiv_ccg = data->subdiv_ccg;
1141  CCGKey *key = data->key;
1142  SubdivCCGAdjacentVertex *adjacent_vertex = &subdiv_ccg->adjacent_vertices[adjacent_vertex_index];
1143  subdiv_ccg_average_grids_corners(subdiv_ccg, key, adjacent_vertex);
1144 }
1145 
1147  CCGKey *key,
1148  const int *adjacent_edge_index_map,
1149  int num_adjacent_edges)
1150 {
1151  TaskParallelSettings parallel_range_settings;
1152  BLI_parallel_range_settings_defaults(&parallel_range_settings);
1153  AverageGridsBoundariesData boundaries_data = {
1154  .subdiv_ccg = subdiv_ccg, .key = key, .adjacent_edge_index_map = adjacent_edge_index_map};
1155  AverageGridsBoundariesTLSData tls_data = {NULL};
1156  parallel_range_settings.userdata_chunk = &tls_data;
1157  parallel_range_settings.userdata_chunk_size = sizeof(tls_data);
1158  parallel_range_settings.func_free = subdiv_ccg_average_grids_boundaries_free;
1160  num_adjacent_edges,
1161  &boundaries_data,
1163  &parallel_range_settings);
1164 }
1165 
1167 {
1168  subdiv_ccg_average_boundaries(subdiv_ccg, key, NULL, subdiv_ccg->num_adjacent_edges);
1169 }
1170 
1171 static void subdiv_ccg_average_corners(SubdivCCG *subdiv_ccg,
1172  CCGKey *key,
1173  const int *adjacent_vert_index_map,
1174  int num_adjacent_vertices)
1175 {
1176  TaskParallelSettings parallel_range_settings;
1177  BLI_parallel_range_settings_defaults(&parallel_range_settings);
1178  AverageGridsCornerData corner_data = {
1179  .subdiv_ccg = subdiv_ccg, .key = key, .adjacent_vert_index_map = adjacent_vert_index_map};
1181  num_adjacent_vertices,
1182  &corner_data,
1184  &parallel_range_settings);
1185 }
1186 static void subdiv_ccg_average_all_corners(SubdivCCG *subdiv_ccg, CCGKey *key)
1187 {
1188  subdiv_ccg_average_corners(subdiv_ccg, key, NULL, subdiv_ccg->num_adjacent_vertices);
1189 }
1190 
1192 {
1193  subdiv_ccg_average_all_boundaries(subdiv_ccg, key);
1194  subdiv_ccg_average_all_corners(subdiv_ccg, key);
1195 }
1196 
1198 {
1199  CCGKey key;
1200  BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
1201  TaskParallelSettings parallel_range_settings;
1202  BLI_parallel_range_settings_defaults(&parallel_range_settings);
1203  /* Average inner boundaries of grids (within one face), across faces
1204  * from different face-corners. */
1205  AverageInnerGridsData inner_data = {
1206  .subdiv_ccg = subdiv_ccg,
1207  .key = &key,
1208  };
1210  subdiv_ccg->num_faces,
1211  &inner_data,
1213  &parallel_range_settings);
1215 }
1216 
1218  struct CCGFace **effected_faces,
1219  int num_effected_faces,
1220  GSet *r_adjacent_vertices,
1221  GSet *r_adjacent_edges)
1222 {
1223  Subdiv *subdiv = subdiv_ccg->subdiv;
1224  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
1225 
1226  StaticOrHeapIntStorage face_vertices_storage;
1227  StaticOrHeapIntStorage face_edges_storage;
1228 
1229  static_or_heap_storage_init(&face_vertices_storage);
1230  static_or_heap_storage_init(&face_edges_storage);
1231 
1232  for (int i = 0; i < num_effected_faces; i++) {
1233  SubdivCCGFace *face = (SubdivCCGFace *)effected_faces[i];
1234  int face_index = face - subdiv_ccg->faces;
1235  const int num_face_grids = face->num_grids;
1236  const int num_face_edges = num_face_grids;
1237  int *face_vertices = static_or_heap_storage_get(&face_vertices_storage, num_face_edges);
1238  topology_refiner->getFaceVertices(topology_refiner, face_index, face_vertices);
1239 
1240  /* Note that order of edges is same as order of MLoops, which also
1241  * means it's the same as order of grids. */
1242  int *face_edges = static_or_heap_storage_get(&face_edges_storage, num_face_edges);
1243  topology_refiner->getFaceEdges(topology_refiner, face_index, face_edges);
1244  for (int corner = 0; corner < num_face_edges; corner++) {
1245  const int vertex_index = face_vertices[corner];
1246  const int edge_index = face_edges[corner];
1247 
1248  int edge_vertices[2];
1249  topology_refiner->getEdgeVertices(topology_refiner, edge_index, edge_vertices);
1250 
1251  SubdivCCGAdjacentEdge *adjacent_edge = &subdiv_ccg->adjacent_edges[edge_index];
1252  BLI_gset_add(r_adjacent_edges, adjacent_edge);
1253 
1254  SubdivCCGAdjacentVertex *adjacent_vertex = &subdiv_ccg->adjacent_vertices[vertex_index];
1255  BLI_gset_add(r_adjacent_vertices, adjacent_vertex);
1256  }
1257  }
1258 
1259  static_or_heap_storage_free(&face_vertices_storage);
1260  static_or_heap_storage_free(&face_edges_storage);
1261 }
1262 
1264  CCGKey *key,
1265  struct CCGFace **effected_faces,
1266  int num_effected_faces)
1267 {
1268  GSet *adjacent_vertices = BLI_gset_ptr_new(__func__);
1269  GSet *adjacent_edges = BLI_gset_ptr_new(__func__);
1270  GSetIterator gi;
1271 
1273  subdiv_ccg, effected_faces, num_effected_faces, adjacent_vertices, adjacent_edges);
1274 
1275  int *adjacent_vertex_index_map;
1276  int *adjacent_edge_index_map;
1277 
1278  StaticOrHeapIntStorage index_heap;
1279  static_or_heap_storage_init(&index_heap);
1280 
1281  int i = 0;
1282 
1283  /* Average boundaries. */
1284 
1285  adjacent_edge_index_map = static_or_heap_storage_get(&index_heap, BLI_gset_len(adjacent_edges));
1286  GSET_ITER_INDEX (gi, adjacent_edges, i) {
1287  SubdivCCGAdjacentEdge *adjacent_edge = BLI_gsetIterator_getKey(&gi);
1288  adjacent_edge_index_map[i] = adjacent_edge - subdiv_ccg->adjacent_edges;
1289  }
1291  subdiv_ccg, key, adjacent_edge_index_map, BLI_gset_len(adjacent_edges));
1292 
1293  /* Average corners. */
1294 
1295  adjacent_vertex_index_map = static_or_heap_storage_get(&index_heap,
1296  BLI_gset_len(adjacent_vertices));
1297  GSET_ITER_INDEX (gi, adjacent_vertices, i) {
1298  SubdivCCGAdjacentVertex *adjacent_vertex = BLI_gsetIterator_getKey(&gi);
1299  adjacent_vertex_index_map[i] = adjacent_vertex - subdiv_ccg->adjacent_vertices;
1300  }
1302  subdiv_ccg, key, adjacent_vertex_index_map, BLI_gset_len(adjacent_vertices));
1303 
1304  BLI_gset_free(adjacent_vertices, NULL);
1305  BLI_gset_free(adjacent_edges, NULL);
1306  static_or_heap_storage_free(&index_heap);
1307 }
1308 
1314 
1316  void *__restrict userdata_v,
1317  const int face_index,
1318  const TaskParallelTLS *__restrict UNUSED(tls_v))
1319 {
1320  StitchFacesInnerGridsData *data = userdata_v;
1321  SubdivCCG *subdiv_ccg = data->subdiv_ccg;
1322  CCGKey *key = data->key;
1323  struct CCGFace **effected_ccg_faces = data->effected_ccg_faces;
1324  struct CCGFace *effected_ccg_face = effected_ccg_faces[face_index];
1325  SubdivCCGFace *face = (SubdivCCGFace *)effected_ccg_face;
1326  subdiv_ccg_average_inner_face_grids(subdiv_ccg, key, face);
1327 }
1328 
1330  struct CCGFace **effected_faces,
1331  int num_effected_faces)
1332 {
1333  CCGKey key;
1334  BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
1336  .subdiv_ccg = subdiv_ccg,
1337  .key = &key,
1338  .effected_ccg_faces = effected_faces,
1339  };
1340  TaskParallelSettings parallel_range_settings;
1341  BLI_parallel_range_settings_defaults(&parallel_range_settings);
1343  num_effected_faces,
1344  &data,
1346  &parallel_range_settings);
1347  /* TODO(sergey): Only average elements which are adjacent to modified
1348  * faces. */
1350 }
1351 
1353  int *r_num_vertices,
1354  int *r_num_edges,
1355  int *r_num_faces,
1356  int *r_num_loops)
1357 {
1358  const int num_grids = subdiv_ccg->num_grids;
1359  const int grid_size = subdiv_ccg->grid_size;
1360  const int grid_area = grid_size * grid_size;
1361  const int num_edges_per_grid = 2 * (grid_size * (grid_size - 1));
1362  *r_num_vertices = num_grids * grid_area;
1363  *r_num_edges = num_grids * num_edges_per_grid;
1364  *r_num_faces = num_grids * (grid_size - 1) * (grid_size - 1);
1365  *r_num_loops = *r_num_faces * 4;
1366 }
1367 
1370 /* -------------------------------------------------------------------- */
1374 void BKE_subdiv_ccg_print_coord(const char *message, const SubdivCCGCoord *coord)
1375 {
1376  printf("%s: grid index: %d, coord: (%d, %d)\n", message, coord->grid_index, coord->x, coord->y);
1377 }
1378 
1379 bool BKE_subdiv_ccg_check_coord_valid(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
1380 {
1381  if (coord->grid_index < 0 || coord->grid_index >= subdiv_ccg->num_grids) {
1382  return false;
1383  }
1384  const int grid_size = subdiv_ccg->grid_size;
1385  if (coord->x < 0 || coord->x >= grid_size) {
1386  return false;
1387  }
1388  if (coord->y < 0 || coord->y >= grid_size) {
1389  return false;
1390  }
1391  return true;
1392 }
1393 
1395  const int num_unique,
1396  const int num_duplicates)
1397 {
1398  const int size = num_unique + num_duplicates;
1399  neighbors->size = size;
1400  neighbors->num_duplicates = num_duplicates;
1401  if (size < ARRAY_SIZE(neighbors->coords_fixed)) {
1402  neighbors->coords = neighbors->coords_fixed;
1403  }
1404  else {
1405  neighbors->coords = MEM_mallocN(sizeof(*neighbors->coords) * size,
1406  "SubdivCCGNeighbors.coords");
1407  }
1408 }
1409 
1410 /* Check whether given coordinate belongs to a grid corner. */
1411 BLI_INLINE bool is_corner_grid_coord(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
1412 {
1413  const int grid_size_1 = subdiv_ccg->grid_size - 1;
1414  return (coord->x == 0 && coord->y == 0) || (coord->x == 0 && coord->y == grid_size_1) ||
1415  (coord->x == grid_size_1 && coord->y == grid_size_1) ||
1416  (coord->x == grid_size_1 && coord->y == 0);
1417 }
1418 
1419 /* Check whether given coordinate belongs to a grid boundary. */
1420 BLI_INLINE bool is_boundary_grid_coord(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
1421 {
1422  const int grid_size_1 = subdiv_ccg->grid_size - 1;
1423  return coord->x == 0 || coord->y == 0 || coord->x == grid_size_1 || coord->y == grid_size_1;
1424 }
1425 
1426 /* Check whether coordinate is at the boundary between two grids of the same face. */
1428  const SubdivCCGCoord *coord)
1429 {
1430  const int grid_size_1 = subdiv_ccg->grid_size - 1;
1431  if (coord->x == 0) {
1432  return coord->y > 0 && coord->y < grid_size_1;
1433  }
1434  if (coord->y == 0) {
1435  return coord->x > 0 && coord->x < grid_size_1;
1436  }
1437  return false;
1438 }
1439 
1441  const SubdivCCGCoord *coord)
1442 {
1443  BLI_assert(coord->y > 0);
1444  SubdivCCGCoord result = *coord;
1445  result.y -= 1;
1446  return result;
1447 }
1449  const SubdivCCGCoord *coord)
1450 {
1451  UNUSED_VARS_NDEBUG(subdiv_ccg);
1452  BLI_assert(coord->y < subdiv_ccg->grid_size - 1);
1453  SubdivCCGCoord result = *coord;
1454  result.y += 1;
1455  return result;
1456 }
1457 
1459  const SubdivCCGCoord *coord)
1460 {
1461  BLI_assert(coord->x > 0);
1462  SubdivCCGCoord result = *coord;
1463  result.x -= 1;
1464  return result;
1465 }
1467  const SubdivCCGCoord *coord)
1468 {
1469  UNUSED_VARS_NDEBUG(subdiv_ccg);
1470  BLI_assert(coord->x < subdiv_ccg->grid_size - 1);
1471  SubdivCCGCoord result = *coord;
1472  result.x += 1;
1473  return result;
1474 }
1475 
1476 /* For the input coordinate which is at the boundary of the grid do one step inside. */
1478  const SubdivCCGCoord *coord)
1479 
1480 {
1481  SubdivCCGCoord result = *coord;
1482  const int grid_size_1 = subdiv_ccg->grid_size - 1;
1483  if (result.x == grid_size_1) {
1484  --result.x;
1485  }
1486  else if (result.y == grid_size_1) {
1487  --result.y;
1488  }
1489  else if (result.x == 0) {
1490  ++result.x;
1491  }
1492  else if (result.y == 0) {
1493  ++result.y;
1494  }
1495  else {
1496  BLI_assert_msg(0, "non-boundary element given");
1497  }
1498  return result;
1499 }
1500 
1501 BLI_INLINE
1502 int next_grid_index_from_coord(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
1503 {
1504  SubdivCCGFace *face = subdiv_ccg->grid_faces[coord->grid_index];
1505  const int face_grid_index = coord->grid_index;
1506  int next_face_grid_index = face_grid_index + 1 - face->start_grid_index;
1507  if (next_face_grid_index == face->num_grids) {
1508  next_face_grid_index = 0;
1509  }
1510  return face->start_grid_index + next_face_grid_index;
1511 }
1513 {
1514  SubdivCCGFace *face = subdiv_ccg->grid_faces[coord->grid_index];
1515  const int face_grid_index = coord->grid_index;
1516  int prev_face_grid_index = face_grid_index - 1 - face->start_grid_index;
1517  if (prev_face_grid_index < 0) {
1518  prev_face_grid_index = face->num_grids - 1;
1519  }
1520  return face->start_grid_index + prev_face_grid_index;
1521 }
1522 
1523 /* Simple case of getting neighbors of a corner coordinate: the corner is a face center, so
1524  * can only iterate over grid of a single face, without looking into adjacency. */
1525 static void neighbor_coords_corner_center_get(const SubdivCCG *subdiv_ccg,
1526  const SubdivCCGCoord *coord,
1527  const bool include_duplicates,
1528  SubdivCCGNeighbors *r_neighbors)
1529 {
1530  SubdivCCGFace *face = subdiv_ccg->grid_faces[coord->grid_index];
1531  const int num_adjacent_grids = face->num_grids;
1532 
1534  r_neighbors, num_adjacent_grids, (include_duplicates) ? num_adjacent_grids - 1 : 0);
1535 
1536  int duplicate_face_grid_index = num_adjacent_grids;
1537  for (int face_grid_index = 0; face_grid_index < num_adjacent_grids; ++face_grid_index) {
1538  SubdivCCGCoord neighbor_coord;
1539  neighbor_coord.grid_index = face->start_grid_index + face_grid_index;
1540  neighbor_coord.x = 1;
1541  neighbor_coord.y = 0;
1542  r_neighbors->coords[face_grid_index] = neighbor_coord;
1543 
1544  if (include_duplicates && neighbor_coord.grid_index != coord->grid_index) {
1545  neighbor_coord.x = 0;
1546  r_neighbors->coords[duplicate_face_grid_index++] = neighbor_coord;
1547  }
1548  }
1549 }
1550 
1551 /* Get index within adjacent_vertices array for the given CCG coordinate. */
1552 static int adjacent_vertex_index_from_coord(const SubdivCCG *subdiv_ccg,
1553  const SubdivCCGCoord *coord)
1554 {
1555  Subdiv *subdiv = subdiv_ccg->subdiv;
1556  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
1557 
1558  const SubdivCCGFace *face = subdiv_ccg->grid_faces[coord->grid_index];
1559  const int face_index = face - subdiv_ccg->faces;
1560  const int face_grid_index = coord->grid_index - face->start_grid_index;
1561  const int num_face_grids = face->num_grids;
1562  const int num_face_vertices = num_face_grids;
1563 
1564  StaticOrHeapIntStorage face_vertices_storage;
1565  static_or_heap_storage_init(&face_vertices_storage);
1566 
1567  int *face_vertices = static_or_heap_storage_get(&face_vertices_storage, num_face_vertices);
1568  topology_refiner->getFaceVertices(topology_refiner, face_index, face_vertices);
1569 
1570  const int adjacent_vertex_index = face_vertices[face_grid_index];
1571  static_or_heap_storage_free(&face_vertices_storage);
1572  return adjacent_vertex_index;
1573 }
1574 
1575 /* The corner is adjacent to a coarse vertex. */
1576 static void neighbor_coords_corner_vertex_get(const SubdivCCG *subdiv_ccg,
1577  const SubdivCCGCoord *coord,
1578  const bool include_duplicates,
1579  SubdivCCGNeighbors *r_neighbors)
1580 {
1581  Subdiv *subdiv = subdiv_ccg->subdiv;
1582  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
1583 
1584  const int adjacent_vertex_index = adjacent_vertex_index_from_coord(subdiv_ccg, coord);
1585  BLI_assert(adjacent_vertex_index >= 0);
1586  BLI_assert(adjacent_vertex_index < subdiv_ccg->num_adjacent_vertices);
1587  const int num_vertex_edges = topology_refiner->getNumVertexEdges(topology_refiner,
1588  adjacent_vertex_index);
1589 
1590  SubdivCCGAdjacentVertex *adjacent_vertex = &subdiv_ccg->adjacent_vertices[adjacent_vertex_index];
1591  const int num_adjacent_faces = adjacent_vertex->num_adjacent_faces;
1592 
1594  r_neighbors, num_vertex_edges, (include_duplicates) ? num_adjacent_faces - 1 : 0);
1595 
1596  StaticOrHeapIntStorage vertex_edges_storage;
1597  static_or_heap_storage_init(&vertex_edges_storage);
1598 
1599  int *vertex_edges = static_or_heap_storage_get(&vertex_edges_storage, num_vertex_edges);
1600  topology_refiner->getVertexEdges(topology_refiner, adjacent_vertex_index, vertex_edges);
1601 
1602  for (int i = 0; i < num_vertex_edges; ++i) {
1603  const int edge_index = vertex_edges[i];
1604 
1605  /* Use very first grid of every edge. */
1606  const int edge_face_index = 0;
1607 
1608  /* Depending edge orientation we use first (zero-based) or previous-to-last point. */
1609  int edge_vertices_indices[2];
1610  topology_refiner->getEdgeVertices(topology_refiner, edge_index, edge_vertices_indices);
1611  int edge_point_index, duplicate_edge_point_index;
1612  if (edge_vertices_indices[0] == adjacent_vertex_index) {
1613  duplicate_edge_point_index = 0;
1614  edge_point_index = duplicate_edge_point_index + 1;
1615  }
1616  else {
1617  /* Edge "consists" of 2 grids, which makes it 2 * grid_size elements per edge.
1618  * The index of last edge element is 2 * grid_size - 1 (due to zero-based indices),
1619  * and we are interested in previous to last element. */
1620  duplicate_edge_point_index = subdiv_ccg->grid_size * 2 - 1;
1621  edge_point_index = duplicate_edge_point_index - 1;
1622  }
1623 
1624  SubdivCCGAdjacentEdge *adjacent_edge = &subdiv_ccg->adjacent_edges[edge_index];
1625  r_neighbors->coords[i] = adjacent_edge->boundary_coords[edge_face_index][edge_point_index];
1626  }
1627 
1628  if (include_duplicates) {
1629  /* Add duplicates of the current grid vertex in adjacent faces if requested. */
1630  for (int i = 0, duplicate_i = num_vertex_edges; i < num_adjacent_faces; i++) {
1631  SubdivCCGCoord neighbor_coord = adjacent_vertex->corner_coords[i];
1632  if (neighbor_coord.grid_index != coord->grid_index) {
1633  r_neighbors->coords[duplicate_i++] = neighbor_coord;
1634  }
1635  }
1636  }
1637 
1638  static_or_heap_storage_free(&vertex_edges_storage);
1639 }
1640 
1641 static int adjacent_edge_index_from_coord(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
1642 {
1643  Subdiv *subdiv = subdiv_ccg->subdiv;
1644  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
1645  SubdivCCGFace *face = subdiv_ccg->grid_faces[coord->grid_index];
1646 
1647  const int face_grid_index = coord->grid_index - face->start_grid_index;
1648  const int face_index = face - subdiv_ccg->faces;
1649  const int num_face_edges = topology_refiner->getNumFaceEdges(topology_refiner, face_index);
1650 
1651  StaticOrHeapIntStorage face_edges_storage;
1652  static_or_heap_storage_init(&face_edges_storage);
1653  int *face_edges_indices = static_or_heap_storage_get(&face_edges_storage, num_face_edges);
1654  topology_refiner->getFaceEdges(topology_refiner, face_index, face_edges_indices);
1655 
1656  const int grid_size_1 = subdiv_ccg->grid_size - 1;
1657  int adjacent_edge_index = -1;
1658  if (coord->x == grid_size_1) {
1659  adjacent_edge_index = face_edges_indices[face_grid_index];
1660  }
1661  else {
1662  BLI_assert(coord->y == grid_size_1);
1663  adjacent_edge_index =
1664  face_edges_indices[face_grid_index == 0 ? face->num_grids - 1 : face_grid_index - 1];
1665  }
1666 
1667  static_or_heap_storage_free(&face_edges_storage);
1668 
1669  return adjacent_edge_index;
1670 }
1671 
1673  const SubdivCCGCoord *coord,
1674  const int adjacent_edge_index)
1675 {
1676  Subdiv *subdiv = subdiv_ccg->subdiv;
1677  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
1678 
1679  const int adjacent_vertex_index = adjacent_vertex_index_from_coord(subdiv_ccg, coord);
1680  int edge_vertices_indices[2];
1681  topology_refiner->getEdgeVertices(topology_refiner, adjacent_edge_index, edge_vertices_indices);
1682 
1683  /* Vertex index of an edge which is used to see whether edge points in the right direction.
1684  * Tricky part here is that depending whether input coordinate is are maximum X or Y coordinate
1685  * of the grid we need to use different edge direction.
1686  * Basically, the edge adjacent to a previous loop needs to point opposite direction. */
1687  int directional_edge_vertex_index = -1;
1688 
1689  const int grid_size_1 = subdiv_ccg->grid_size - 1;
1690  int adjacent_edge_point_index = -1;
1691  if (coord->x == grid_size_1) {
1692  adjacent_edge_point_index = subdiv_ccg->grid_size - coord->y - 1;
1693  directional_edge_vertex_index = edge_vertices_indices[0];
1694  }
1695  else {
1696  BLI_assert(coord->y == grid_size_1);
1697  adjacent_edge_point_index = subdiv_ccg->grid_size + coord->x;
1698  directional_edge_vertex_index = edge_vertices_indices[1];
1699  }
1700 
1701  /* Flip the index if the edde points opposite direction. */
1702  if (adjacent_vertex_index != directional_edge_vertex_index) {
1703  const int num_edge_points = subdiv_ccg->grid_size * 2;
1704  adjacent_edge_point_index = num_edge_points - adjacent_edge_point_index - 1;
1705  }
1706 
1707  return adjacent_edge_point_index;
1708 }
1709 
1710 /* Adjacent edge has two points in the middle which corresponds to grid corners, but which are
1711  * the same point in the final geometry.
1712  * So need to use extra step when calculating next/previous points, so we don't go from a corner
1713  * of one grid to a corner of adjacent grid. */
1714 static int next_adjacent_edge_point_index(const SubdivCCG *subdiv_ccg, const int point_index)
1715 {
1716  if (point_index == subdiv_ccg->grid_size - 1) {
1717  return point_index + 2;
1718  }
1719  return point_index + 1;
1720 }
1721 static int prev_adjacent_edge_point_index(const SubdivCCG *subdiv_ccg, const int point_index)
1722 {
1723  if (point_index == subdiv_ccg->grid_size) {
1724  return point_index - 2;
1725  }
1726  return point_index - 1;
1727 }
1728 
1729 /* When the point index corresponds to a grid corner, returns the point index which corresponds to
1730  * the corner of the adjacent grid, as the adjacent edge has two separate points for each grid
1731  * corner at the middle of the edge. */
1733  const int point_index)
1734 {
1735  if (point_index == subdiv_ccg->grid_size) {
1736  return point_index - 1;
1737  }
1738  return point_index + 1;
1739 }
1740 
1741 /* Common implementation of neighbor calculation when input coordinate is at the edge between two
1742  * coarse faces, but is not at the coarse vertex. */
1743 static void neighbor_coords_edge_get(const SubdivCCG *subdiv_ccg,
1744  const SubdivCCGCoord *coord,
1745  const bool include_duplicates,
1746  SubdivCCGNeighbors *r_neighbors)
1747 
1748 {
1749  const bool is_corner = is_corner_grid_coord(subdiv_ccg, coord);
1750  const int adjacent_edge_index = adjacent_edge_index_from_coord(subdiv_ccg, coord);
1751  BLI_assert(adjacent_edge_index >= 0);
1752  BLI_assert(adjacent_edge_index < subdiv_ccg->num_adjacent_edges);
1753  const SubdivCCGAdjacentEdge *adjacent_edge = &subdiv_ccg->adjacent_edges[adjacent_edge_index];
1754 
1755  /* 2 neighbor points along the edge, plus one inner point per every adjacent grid. */
1756  const int num_adjacent_faces = adjacent_edge->num_adjacent_faces;
1757  int num_duplicates = 0;
1758  if (include_duplicates) {
1759  num_duplicates += num_adjacent_faces - 1;
1760  if (is_corner) {
1761  /* When the coord is a grid corner, add an extra duplicate per adjacent grid in all adjacent
1762  * faces to the edge. */
1763  num_duplicates += num_adjacent_faces;
1764  }
1765  }
1766  subdiv_ccg_neighbors_init(r_neighbors, num_adjacent_faces + 2, num_duplicates);
1767 
1769  subdiv_ccg, coord, adjacent_edge_index);
1770  const int point_index_duplicate = adjacent_grid_corner_point_index_on_edge(subdiv_ccg,
1771  point_index);
1772 
1773  const int next_point_index = next_adjacent_edge_point_index(subdiv_ccg, point_index);
1774  const int prev_point_index = prev_adjacent_edge_point_index(subdiv_ccg, point_index);
1775 
1776  int duplicate_i = num_adjacent_faces;
1777  for (int i = 0; i < num_adjacent_faces; ++i) {
1778  SubdivCCGCoord *boundary_coords = adjacent_edge->boundary_coords[i];
1779  /* One step into the grid from the edge for each adjacent face. */
1780  SubdivCCGCoord grid_coord = boundary_coords[point_index];
1781  r_neighbors->coords[i + 2] = coord_step_inside_from_boundary(subdiv_ccg, &grid_coord);
1782 
1783  if (grid_coord.grid_index == coord->grid_index) {
1784  /* Previous and next along the edge for the current grid. */
1785  r_neighbors->coords[0] = boundary_coords[prev_point_index];
1786  r_neighbors->coords[1] = boundary_coords[next_point_index];
1787  }
1788  else if (include_duplicates) {
1789  /* Same coordinate on neighboring grids if requested. */
1790  r_neighbors->coords[duplicate_i + 2] = grid_coord;
1791  duplicate_i++;
1792  }
1793 
1794  /* When it is a corner, add the duplicate of the adjacent grid in the same face. */
1795  if (include_duplicates && is_corner) {
1796  SubdivCCGCoord duplicate_corner_grid_coord = boundary_coords[point_index_duplicate];
1797  r_neighbors->coords[duplicate_i + 2] = duplicate_corner_grid_coord;
1798  duplicate_i++;
1799  }
1800  }
1801  BLI_assert(duplicate_i - num_adjacent_faces == num_duplicates);
1802 }
1803 
1804 /* The corner is at the middle of edge between faces. */
1805 static void neighbor_coords_corner_edge_get(const SubdivCCG *subdiv_ccg,
1806  const SubdivCCGCoord *coord,
1807  const bool include_duplicates,
1808  SubdivCCGNeighbors *r_neighbors)
1809 {
1810  neighbor_coords_edge_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1811 }
1812 
1813 /* Input coordinate is at one of 4 corners of its grid corners. */
1814 static void neighbor_coords_corner_get(const SubdivCCG *subdiv_ccg,
1815  const SubdivCCGCoord *coord,
1816  const bool include_duplicates,
1817  SubdivCCGNeighbors *r_neighbors)
1818 {
1819  if (coord->x == 0 && coord->y == 0) {
1820  neighbor_coords_corner_center_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1821  }
1822  else {
1823  const int grid_size_1 = subdiv_ccg->grid_size - 1;
1824  if (coord->x == grid_size_1 && coord->y == grid_size_1) {
1825  neighbor_coords_corner_vertex_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1826  }
1827  else {
1828  neighbor_coords_corner_edge_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1829  }
1830  }
1831 }
1832 
1833 /* Simple case of getting neighbors of a boundary coordinate: the input coordinate is at the
1834  * boundary between two grids of the same face and there is no need to check adjacency with
1835  * other faces. */
1836 static void neighbor_coords_boundary_inner_get(const SubdivCCG *subdiv_ccg,
1837  const SubdivCCGCoord *coord,
1838  const bool include_duplicates,
1839  SubdivCCGNeighbors *r_neighbors)
1840 {
1841  subdiv_ccg_neighbors_init(r_neighbors, 4, (include_duplicates) ? 1 : 0);
1842 
1843  if (coord->x == 0) {
1844  r_neighbors->coords[0] = coord_at_prev_row(subdiv_ccg, coord);
1845  r_neighbors->coords[1] = coord_at_next_row(subdiv_ccg, coord);
1846  r_neighbors->coords[2] = coord_at_next_col(subdiv_ccg, coord);
1847 
1848  r_neighbors->coords[3].grid_index = prev_grid_index_from_coord(subdiv_ccg, coord);
1849  r_neighbors->coords[3].x = coord->y;
1850  r_neighbors->coords[3].y = 1;
1851 
1852  if (include_duplicates) {
1853  r_neighbors->coords[4] = r_neighbors->coords[3];
1854  r_neighbors->coords[4].y = 0;
1855  }
1856  }
1857  else if (coord->y == 0) {
1858  r_neighbors->coords[0] = coord_at_prev_col(subdiv_ccg, coord);
1859  r_neighbors->coords[1] = coord_at_next_col(subdiv_ccg, coord);
1860  r_neighbors->coords[2] = coord_at_next_row(subdiv_ccg, coord);
1861 
1862  r_neighbors->coords[3].grid_index = next_grid_index_from_coord(subdiv_ccg, coord);
1863  r_neighbors->coords[3].x = 1;
1864  r_neighbors->coords[3].y = coord->x;
1865 
1866  if (include_duplicates) {
1867  r_neighbors->coords[4] = r_neighbors->coords[3];
1868  r_neighbors->coords[4].x = 0;
1869  }
1870  }
1871 }
1872 
1873 /* Input coordinate is on an edge between two faces. Need to check adjacency. */
1874 static void neighbor_coords_boundary_outer_get(const SubdivCCG *subdiv_ccg,
1875  const SubdivCCGCoord *coord,
1876  const bool include_duplicates,
1877  SubdivCCGNeighbors *r_neighbors)
1878 {
1879  neighbor_coords_edge_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1880 }
1881 
1882 /* Input coordinate is at one of 4 boundaries of its grid.
1883  * It could either be an inner boundary (which connects face center to the face edge) or could be
1884  * a part of coarse face edge. */
1885 static void neighbor_coords_boundary_get(const SubdivCCG *subdiv_ccg,
1886  const SubdivCCGCoord *coord,
1887  const bool include_duplicates,
1888  SubdivCCGNeighbors *r_neighbors)
1889 {
1890  if (is_inner_edge_grid_coordinate(subdiv_ccg, coord)) {
1891  neighbor_coords_boundary_inner_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1892  }
1893  else {
1894  neighbor_coords_boundary_outer_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1895  }
1896 }
1897 
1898 /* Input coordinate is inside of its grid, all the neighbors belong to the same grid. */
1899 static void neighbor_coords_inner_get(const SubdivCCG *subdiv_ccg,
1900  const SubdivCCGCoord *coord,
1901  SubdivCCGNeighbors *r_neighbors)
1902 {
1903  subdiv_ccg_neighbors_init(r_neighbors, 4, 0);
1904 
1905  r_neighbors->coords[0] = coord_at_prev_row(subdiv_ccg, coord);
1906  r_neighbors->coords[1] = coord_at_next_row(subdiv_ccg, coord);
1907  r_neighbors->coords[2] = coord_at_prev_col(subdiv_ccg, coord);
1908  r_neighbors->coords[3] = coord_at_next_col(subdiv_ccg, coord);
1909 }
1910 
1912  const SubdivCCGCoord *coord,
1913  const bool include_duplicates,
1914  SubdivCCGNeighbors *r_neighbors)
1915 {
1916  BLI_assert(coord->grid_index >= 0);
1917  BLI_assert(coord->grid_index < subdiv_ccg->num_grids);
1918  BLI_assert(coord->x >= 0);
1919  BLI_assert(coord->x < subdiv_ccg->grid_size);
1920  BLI_assert(coord->y >= 0);
1921  BLI_assert(coord->y < subdiv_ccg->grid_size);
1922 
1923  if (is_corner_grid_coord(subdiv_ccg, coord)) {
1924  neighbor_coords_corner_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1925  }
1926  else if (is_boundary_grid_coord(subdiv_ccg, coord)) {
1927  neighbor_coords_boundary_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1928  }
1929  else {
1930  neighbor_coords_inner_get(subdiv_ccg, coord, r_neighbors);
1931  }
1932 
1933 #ifndef NDEBUG
1934  for (int i = 0; i < r_neighbors->size; i++) {
1935  BLI_assert(BKE_subdiv_ccg_check_coord_valid(subdiv_ccg, &r_neighbors->coords[i]));
1936  }
1937 #endif
1938 }
1939 
1940 int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG *subdiv_ccg, const int grid_index)
1941 {
1942  const SubdivCCGFace *face = subdiv_ccg->grid_faces[grid_index];
1943  const int face_index = face - subdiv_ccg->faces;
1944  return face_index;
1945 }
1946 
1948 {
1949  if (subdiv_ccg->cache_.start_face_grid_index == NULL) {
1950  const Subdiv *subdiv = subdiv_ccg->subdiv;
1951  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
1952  if (topology_refiner == NULL) {
1953  return NULL;
1954  }
1955 
1956  const int num_coarse_faces = topology_refiner->getNumFaces(topology_refiner);
1957 
1959  num_coarse_faces, sizeof(int), "start_face_grid_index");
1960 
1961  int start_grid_index = 0;
1962  for (int face_index = 0; face_index < num_coarse_faces; face_index++) {
1963  const int num_face_grids = topology_refiner->getNumFaceVertices(topology_refiner,
1964  face_index);
1965  subdiv_ccg->cache_.start_face_grid_index[face_index] = start_grid_index;
1966  start_grid_index += num_face_grids;
1967  }
1968  }
1969 
1970  return subdiv_ccg->cache_.start_face_grid_index;
1971 }
1972 
1974 {
1975  return subdiv_ccg->cache_.start_face_grid_index;
1976 }
1977 
1979  const SubdivCCGCoord *coord,
1980  const MLoop *mloop,
1981  const MPoly *mpoly,
1982  int *r_v1,
1983  int *r_v2)
1984 {
1985  const int grid_size_1 = subdiv_ccg->grid_size - 1;
1986  const int poly_index = BKE_subdiv_ccg_grid_to_face_index(subdiv_ccg, coord->grid_index);
1987  const MPoly *p = &mpoly[poly_index];
1988  *r_v1 = mloop[coord->grid_index].v;
1989 
1990  const int corner = poly_find_loop_from_vert(p, &mloop[p->loopstart], *r_v1);
1991  if (coord->x == grid_size_1) {
1992  const MLoop *next = ME_POLY_LOOP_NEXT(mloop, p, corner);
1993  *r_v2 = next->v;
1994  }
1995  if (coord->y == grid_size_1) {
1996  const MLoop *prev = ME_POLY_LOOP_PREV(mloop, p, corner);
1997  *r_v2 = prev->v;
1998  }
1999 }
2000 
2002  const SubdivCCGCoord *coord,
2003  const MLoop *mloop,
2004  const MPoly *mpoly,
2005  int *r_v1,
2006  int *r_v2)
2007 {
2008 
2009  const int grid_size_1 = subdiv_ccg->grid_size - 1;
2010  if (is_corner_grid_coord(subdiv_ccg, coord)) {
2011  if (coord->x == 0 && coord->y == 0) {
2012  /* Grid corner in the center of a poly. */
2013  return SUBDIV_CCG_ADJACENT_NONE;
2014  }
2015  if (coord->x == grid_size_1 && coord->y == grid_size_1) {
2016  /* Grid corner adjacent to a coarse mesh vertex. */
2017  *r_v1 = *r_v2 = mloop[coord->grid_index].v;
2019  }
2020  /* Grid corner adjacent to the middle of a coarse mesh edge. */
2021  adjacet_vertices_index_from_adjacent_edge(subdiv_ccg, coord, mloop, mpoly, r_v1, r_v2);
2022  return SUBDIV_CCG_ADJACENT_EDGE;
2023  }
2024 
2025  if (is_boundary_grid_coord(subdiv_ccg, coord)) {
2026  if (!is_inner_edge_grid_coordinate(subdiv_ccg, coord)) {
2027  /* Grid boundary adjacent to a coarse mesh edge. */
2028  adjacet_vertices_index_from_adjacent_edge(subdiv_ccg, coord, mloop, mpoly, r_v1, r_v2);
2029  return SUBDIV_CCG_ADJACENT_EDGE;
2030  }
2031  }
2032  return SUBDIV_CCG_ADJACENT_NONE;
2033 }
2034 
2035 void BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG *subdiv_ccg, int grid_index)
2036 {
2037  if (subdiv_ccg->grid_hidden[grid_index] != NULL) {
2038  return;
2039  }
2040 
2041  CCGKey key;
2042  BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
2043  subdiv_ccg->grid_hidden[grid_index] = BLI_BITMAP_NEW(key.grid_area, __func__);
2044 }
2045 
2046 static void subdiv_ccg_coord_to_ptex_coord(const SubdivCCG *subdiv_ccg,
2047  const SubdivCCGCoord *coord,
2048  int *r_ptex_face_index,
2049  float *r_u,
2050  float *r_v)
2051 {
2052  Subdiv *subdiv = subdiv_ccg->subdiv;
2053 
2054  const float grid_size = subdiv_ccg->grid_size;
2055  const float grid_size_1_inv = 1.0f / (grid_size - 1);
2056 
2057  const float grid_u = coord->x * grid_size_1_inv;
2058  const float grid_v = coord->y * grid_size_1_inv;
2059 
2060  const int face_index = BKE_subdiv_ccg_grid_to_face_index(subdiv_ccg, coord->grid_index);
2061  const SubdivCCGFace *faces = subdiv_ccg->faces;
2062  const SubdivCCGFace *face = &faces[face_index];
2063  const int *face_ptex_offset = BKE_subdiv_face_ptex_offset_get(subdiv);
2064  *r_ptex_face_index = face_ptex_offset[face_index];
2065 
2066  const float corner = coord->grid_index - face->start_grid_index;
2067 
2068  if (face->num_grids == 4) {
2069  BKE_subdiv_rotate_grid_to_quad(corner, grid_u, grid_v, r_u, r_v);
2070  }
2071  else {
2072  *r_ptex_face_index += corner;
2073  *r_u = 1.0f - grid_v;
2074  *r_v = 1.0f - grid_u;
2075  }
2076 }
2077 
2079  const SubdivCCGCoord *coord,
2080  float r_point[3])
2081 {
2082  Subdiv *subdiv = subdiv_ccg->subdiv;
2083  int ptex_face_index;
2084  float u, v;
2085  subdiv_ccg_coord_to_ptex_coord(subdiv_ccg, coord, &ptex_face_index, &u, &v);
2086  BKE_subdiv_eval_limit_point(subdiv, ptex_face_index, u, v, r_point);
2087 }
2088 
typedef float(TangentPoint)[2]
BLI_INLINE CCGElem * CCG_grid_elem(const CCGKey *key, CCGElem *elem, int x, int y)
Definition: BKE_ccg.h:108
BLI_INLINE float * CCG_elem_mask(const CCGKey *key, CCGElem *elem)
Definition: BKE_ccg.h:97
BLI_INLINE float * CCG_grid_elem_no(const CCGKey *key, CCGElem *elem, int x, int y)
Definition: BKE_ccg.h:119
BLI_INLINE float * CCG_elem_no(const CCGKey *key, CCGElem *elem)
Definition: BKE_ccg.h:91
struct CCGElem CCGElem
Definition: BKE_ccg.h:30
BLI_INLINE float * CCG_elem_co(const CCGKey *key, CCGElem *elem)
struct Mesh * BKE_mesh_new_nomain_from_template(const struct Mesh *me_src, int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len)
int poly_find_loop_from_vert(const struct MPoly *poly, const struct MLoop *loopstart, uint vert)
@ SUBDIV_STATS_SUBDIV_TO_CCG
Definition: BKE_subdiv.h:81
void BKE_subdiv_stats_end(SubdivStats *stats, eSubdivStatsValue value)
Definition: subdiv_stats.c:31
BLI_INLINE int BKE_subdiv_grid_size_from_level(int level)
Definition: subdiv_inline.h:33
BLI_INLINE void BKE_subdiv_rotate_grid_to_quad(int corner, float grid_u, float grid_v, float *r_quad_u, float *r_quad_v)
Definition: subdiv_inline.h:68
void BKE_subdiv_stats_begin(SubdivStats *stats, eSubdivStatsValue value)
Definition: subdiv_stats.c:26
void BKE_subdiv_free(Subdiv *subdiv)
Definition: subdiv.c:184
int * BKE_subdiv_face_ptex_offset_get(Subdiv *subdiv)
Definition: subdiv.c:209
bool BKE_subdiv_ccg_mask_init_from_paint(SubdivCCGMaskEvaluator *mask_evaluator, const struct Mesh *mesh)
SubdivCCGAdjacencyType
@ SUBDIV_CCG_ADJACENT_EDGE
@ SUBDIV_CCG_ADJACENT_VERTEX
@ SUBDIV_CCG_ADJACENT_NONE
void BKE_subdiv_ccg_material_flags_init_from_mesh(SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator, const struct Mesh *mesh)
void BKE_subdiv_eval_limit_point_and_normal(struct Subdiv *subdiv, int ptex_face_index, float u, float v, float r_P[3], float r_N[3])
Definition: subdiv_eval.c:320
@ SUBDIV_EVALUATOR_TYPE_CPU
void BKE_subdiv_eval_final_point(struct Subdiv *subdiv, int ptex_face_index, float u, float v, float r_P[3])
Definition: subdiv_eval.c:366
void BKE_subdiv_eval_limit_point(struct Subdiv *subdiv, int ptex_face_index, float u, float v, float r_P[3])
Definition: subdiv_eval.c:280
bool BKE_subdiv_eval_begin_from_mesh(struct Subdiv *subdiv, const struct Mesh *mesh, const float(*coarse_vertex_cos)[3], eSubdivEvaluatorType evaluator_type, struct OpenSubdiv_EvaluatorCache *evaluator_cache)
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition: BLI_bitmap.h:40
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:16
#define BLI_INLINE
#define GSET_ITER_INDEX(gs_iter_, gset_, i_)
Definition: BLI_ghash.h:475
struct GSet GSet
Definition: BLI_ghash.h:340
GSet * BLI_gset_ptr_new(const char *info)
unsigned int BLI_gset_len(const GSet *gs) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:957
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1037
BLI_INLINE void * BLI_gsetIterator_getKey(GSetIterator *gsi)
Definition: BLI_ghash.h:458
bool BLI_gset_add(GSet *gs, void *key)
Definition: BLI_ghash.c:969
MINLINE int bitscan_forward_i(int a)
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.c:50
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void zero_v3(float r[3])
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])
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
Definition: task_range.cc:94
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
Definition: BLI_task.h:293
#define ARRAY_SIZE(arr)
#define UNUSED_VARS_NDEBUG(...)
#define UNUSED(x)
#define ME_POLY_LOOP_PREV(mloop, mp, i)
#define ME_POLY_LOOP_NEXT(mloop, mp, i)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define MEM_reallocN(vmemh, len)
ATTR_WARN_UNUSED_RESULT const void * element
ATTR_WARN_UNUSED_RESULT const BMVert * v
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
ccl_gpu_kernel_postfix ccl_global int * counter
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:34
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:32
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
static ulong * next
static char faces[256]
static unsigned a[3]
Definition: RandGen.cpp:78
SymEdge< T > * prev(const SymEdge< T > *se)
Definition: delaunay_2d.cc:105
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
const int * adjacent_edge_index_map
Definition: subdiv_ccg.c:1029
GridElementAccumulator * accumulators
Definition: subdiv_ccg.c:1033
SubdivCCG * subdiv_ccg
Definition: subdiv_ccg.c:1100
const int * adjacent_vert_index_map
Definition: subdiv_ccg.c:1104
SubdivCCG * subdiv_ccg
Definition: subdiv_ccg.c:898
SubdivCCGMaskEvaluator * mask_evaluator
Definition: subdiv_ccg.c:166
int * face_ptex_offset
Definition: subdiv_ccg.c:165
SubdivCCG * subdiv_ccg
Definition: subdiv_ccg.c:163
Subdiv * subdiv
Definition: subdiv_ccg.c:164
SubdivCCGMaterialFlagsEvaluator * material_flags_evaluator
Definition: subdiv_ccg.c:167
Definition: BKE_ccg.h:32
int has_mask
Definition: BKE_ccg.h:55
int mask_offset
Definition: BKE_ccg.h:52
int grid_size
Definition: BKE_ccg.h:40
int grid_bytes
Definition: BKE_ccg.h:44
int grid_area
Definition: BKE_ccg.h:42
int level
Definition: BKE_ccg.h:33
int normal_offset
Definition: BKE_ccg.h:48
int elem_size
Definition: BKE_ccg.h:37
int has_normals
Definition: BKE_ccg.h:54
unsigned int v
int totpoly
int(* getNumVertexEdges)(const struct OpenSubdiv_TopologyRefiner *topology_refiner, const int vertex_index)
int(* getNumFaceVertices)(const struct OpenSubdiv_TopologyRefiner *topology_refiner, const int face_index)
void(* getFaceEdges)(const struct OpenSubdiv_TopologyRefiner *topology_refiner, const int face_index, int *face_edges_indices)
void(* getFaceVertices)(const struct OpenSubdiv_TopologyRefiner *topology_refiner, const int face_index, int *face_vertices_indices)
void(* getVertexEdges)(const struct OpenSubdiv_TopologyRefiner *topology_refiner, const int vertex_index, int *vertex_edges_indices)
void(* getEdgeVertices)(const struct OpenSubdiv_TopologyRefiner *topology_refiner, const int edge_index, int edge_vertices_indices[2])
int(* getNumFaceEdges)(const struct OpenSubdiv_TopologyRefiner *topology_refiner, const int face_index)
int(* getNumFaces)(const struct OpenSubdiv_TopologyRefiner *topology_refiner)
int(* getNumEdges)(const struct OpenSubdiv_TopologyRefiner *topology_refiner)
int(* getNumVertices)(const struct OpenSubdiv_TopologyRefiner *topology_refiner)
SubdivCCG * subdiv_ccg
Definition: subdiv_ccg.c:680
SubdivCCGFace ** effected_ccg_faces
Definition: subdiv_ccg.c:817
struct CCGFace ** effected_ccg_faces
Definition: subdiv_ccg.c:1312
struct SubdivCCGCoord ** boundary_coords
struct SubdivCCGCoord * corner_coords
void(* free)(struct SubdivCCGMaskEvaluator *mask_evaluator)
void(* free)(struct SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator)
SubdivCCGCoord * coords
SubdivCCGCoord coords_fixed[256]
struct CCGElem ** grids
struct Subdiv * subdiv
unsigned char * grids_storage
SubdivCCGAdjacentVertex * adjacent_vertices
struct SubdivCCG::@70 cache_
SubdivCCGFace * faces
int grid_element_size
int * start_face_grid_index
int num_adjacent_vertices
SubdivCCGFace ** grid_faces
BLI_bitmap ** grid_hidden
SubdivCCGAdjacentEdge * adjacent_edges
struct DMFlagMat * grid_flag_mats
struct CCGElem * vertices
int num_adjacent_edges
struct CCGElem ** edges
SubdivStats stats
Definition: BKE_subdiv.h:170
struct SubdivDisplacement * displacement_evaluator
Definition: BKE_subdiv.h:168
struct OpenSubdiv_TopologyRefiner * topology_refiner
Definition: BKE_subdiv.h:164
TaskParallelFreeFunc func_free
Definition: BLI_task.h:183
size_t userdata_chunk_size
Definition: BLI_task.h:169
static void subdiv_ccg_recalc_modified_inner_normal_free(const void *__restrict UNUSED(userdata), void *__restrict tls_v)
Definition: subdiv_ccg.c:839
static void subdiv_ccg_stitch_face_inner_grids_task(void *__restrict userdata_v, const int face_index, const TaskParallelTLS *__restrict UNUSED(tls_v))
Definition: subdiv_ccg.c:1315
static void subdiv_ccg_average_boundaries(SubdivCCG *subdiv_ccg, CCGKey *key, const int *adjacent_edge_index_map, int num_adjacent_edges)
Definition: subdiv_ccg.c:1146
static void neighbor_coords_inner_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1899
BLI_INLINE int prev_grid_index_from_coord(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1512
void BKE_subdiv_ccg_recalc_normals(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:804
static void neighbor_coords_edge_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const bool include_duplicates, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1743
static int next_adjacent_edge_point_index(const SubdivCCG *subdiv_ccg, const int point_index)
Definition: subdiv_ccg.c:1714
static void subdiv_ccg_init_faces_edge_neighborhood(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:414
BLI_INLINE bool is_boundary_grid_coord(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1420
static void subdiv_ccg_eval_grids_task(void *__restrict userdata_v, const int face_index, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: subdiv_ccg.c:285
static void subdiv_ccg_alloc_elements(SubdivCCG *subdiv_ccg, Subdiv *subdiv)
Definition: subdiv_ccg.c:118
static void element_accumulator_copy(SubdivCCG *subdiv_ccg, CCGKey *key, CCGElem *destination, const GridElementAccumulator *accumulator)
Definition: subdiv_ccg.c:963
static void subdiv_ccg_eval_grid_element(CCGEvalGridsData *data, const int ptex_face_index, const float u, const float v, unsigned char *element)
Definition: subdiv_ccg.c:213
static void subdiv_ccg_eval_grid_element_limit(CCGEvalGridsData *data, const int ptex_face_index, const float u, const float v, unsigned char *element)
Definition: subdiv_ccg.c:170
static void subdiv_ccg_average_all_boundaries_and_corners(SubdivCCG *subdiv_ccg, CCGKey *key)
Definition: subdiv_ccg.c:1191
BLI_INLINE bool is_inner_edge_grid_coordinate(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1427
static void neighbor_coords_boundary_inner_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const bool include_duplicates, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1836
static void subdiv_ccg_recalc_inner_normal_task(void *__restrict userdata_v, const int grid_index, const TaskParallelTLS *__restrict tls_v)
Definition: subdiv_ccg.c:765
SubdivCCGAdjacencyType BKE_subdiv_ccg_coarse_mesh_adjacency_info_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const MLoop *mloop, const MPoly *mpoly, int *r_v1, int *r_v2)
Definition: subdiv_ccg.c:2001
static void static_or_heap_storage_free(StaticOrHeapIntStorage *storage)
Definition: subdiv_ccg.c:373
struct AverageGridsBoundariesTLSData AverageGridsBoundariesTLSData
struct StaticOrHeapIntStorage StaticOrHeapIntStorage
struct AverageGridsBoundariesData AverageGridsBoundariesData
struct RecalcInnerNormalsData RecalcInnerNormalsData
struct RecalcInnerNormalsTLSData RecalcInnerNormalsTLSData
const int * BKE_subdiv_ccg_start_face_grid_index_get(const SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:1973
BLI_INLINE int next_grid_index_from_coord(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1502
static int num_element_float_get(const SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:51
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const bool include_duplicates, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1911
static void element_accumulator_add(GridElementAccumulator *accumulator, const SubdivCCG *subdiv_ccg, CCGKey *key, CCGElem *grid_element)
Definition: subdiv_ccg.c:942
static void subdiv_ccg_init_faces(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:329
static void subdiv_ccg_affected_face_adjacency(SubdivCCG *subdiv_ccg, struct CCGFace **effected_faces, int num_effected_faces, GSet *r_adjacent_vertices, GSet *r_adjacent_edges)
Definition: subdiv_ccg.c:1217
BLI_INLINE SubdivCCGCoord coord_at_prev_col(const SubdivCCG *UNUSED(subdiv_ccg), const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1458
static SubdivCCGCoord * subdiv_ccg_adjacent_edge_add_face(SubdivCCG *subdiv_ccg, SubdivCCGAdjacentEdge *adjacent_edge)
Definition: subdiv_ccg.c:399
void BKE_subdiv_ccg_eval_limit_point(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, float r_point[3])
Definition: subdiv_ccg.c:2078
static void subdiv_ccg_allocate_adjacent_vertices(SubdivCCG *subdiv_ccg, const int num_vertices)
Definition: subdiv_ccg.c:487
static void element_accumulator_init(GridElementAccumulator *accumulator)
Definition: subdiv_ccg.c:935
static void subdiv_ccg_average_grids_boundaries_task(void *__restrict userdata_v, const int n, const TaskParallelTLS *__restrict tls_v)
Definition: subdiv_ccg.c:1076
void BKE_subdiv_ccg_average_grids(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:1197
struct GridElementAccumulator GridElementAccumulator
static void subdiv_ccg_eval_regular_grid(CCGEvalGridsData *data, const int face_index)
Definition: subdiv_ccg.c:223
static int prev_adjacent_edge_point_index(const SubdivCCG *subdiv_ccg, const int point_index)
Definition: subdiv_ccg.c:1721
static CCGElem * subdiv_ccg_coord_to_elem(const CCGKey *key, const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:391
static void subdiv_ccg_average_inner_face_grids(SubdivCCG *subdiv_ccg, CCGKey *key, SubdivCCGFace *face)
Definition: subdiv_ccg.c:977
struct AverageInnerGridsData AverageInnerGridsData
BLI_INLINE SubdivCCGCoord coord_at_next_row(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1448
void BKE_subdiv_ccg_destroy(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:615
static void subdiv_ccg_average_grids_boundaries_free(const void *__restrict UNUSED(userdata), void *__restrict tls_v)
Definition: subdiv_ccg.c:1092
BLI_INLINE bool is_corner_grid_coord(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1411
static SubdivCCGCoord coord_step_inside_from_boundary(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1477
static void neighbor_coords_corner_center_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const bool include_duplicates, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1525
void subdiv_ccg_average_faces_boundaries_and_corners(SubdivCCG *subdiv_ccg, CCGKey *key, struct CCGFace **effected_faces, int num_effected_faces)
Definition: subdiv_ccg.c:1263
static void subdiv_ccg_average_grids_boundary(SubdivCCG *subdiv_ccg, CCGKey *key, SubdivCCGAdjacentEdge *adjacent_edge, AverageGridsBoundariesTLSData *tls)
Definition: subdiv_ccg.c:1036
const int * BKE_subdiv_ccg_start_face_grid_index_ensure(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:1947
static int adjacent_vertex_index_from_coord(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1552
static void subdiv_ccg_average_corners(SubdivCCG *subdiv_ccg, CCGKey *key, const int *adjacent_vert_index_map, int num_adjacent_vertices)
Definition: subdiv_ccg.c:1171
static int adjacent_grid_corner_point_index_on_edge(const SubdivCCG *subdiv_ccg, const int point_index)
Definition: subdiv_ccg.c:1732
void BKE_subdiv_ccg_key_top_level(CCGKey *key, const SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:668
static void neighbor_coords_corner_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const bool include_duplicates, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1814
static void subdiv_ccg_eval_grid_element_mask(CCGEvalGridsData *data, const int ptex_face_index, const float u, const float v, unsigned char *element)
Definition: subdiv_ccg.c:194
static int adjacent_edge_point_index_from_coord(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const int adjacent_edge_index)
Definition: subdiv_ccg.c:1672
void BKE_subdiv_ccg_update_normals(SubdivCCG *subdiv_ccg, struct CCGFace **effected_faces, int num_effected_faces)
Definition: subdiv_ccg.c:870
void BKE_subdiv_ccg_key(CCGKey *key, const SubdivCCG *subdiv_ccg, int level)
Definition: subdiv_ccg.c:653
BLI_INLINE SubdivCCGCoord coord_at_prev_row(const SubdivCCG *UNUSED(subdiv_ccg), const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1440
static void element_accumulator_mul_fl(GridElementAccumulator *accumulator, const float f)
Definition: subdiv_ccg.c:956
struct StitchFacesInnerGridsData StitchFacesInnerGridsData
static void adjacet_vertices_index_from_adjacent_edge(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const MLoop *mloop, const MPoly *mpoly, int *r_v1, int *r_v2)
Definition: subdiv_ccg.c:1978
static void average_grid_element_value_v3(float a[3], float b[3])
Definition: subdiv_ccg.c:902
static void subdiv_ccg_recalc_inner_normal_free(const void *__restrict UNUSED(userdata), void *__restrict tls_v)
Definition: subdiv_ccg.c:775
static void subdiv_ccg_eval_special_grid(CCGEvalGridsData *data, const int face_index)
Definition: subdiv_ccg.c:255
static void subdiv_ccg_average_all_corners(SubdivCCG *subdiv_ccg, CCGKey *key)
Definition: subdiv_ccg.c:1186
static SubdivCCGCoord subdiv_ccg_coord(int grid_index, int x, int y)
Definition: subdiv_ccg.c:385
Mesh * BKE_subdiv_to_ccg_mesh(Subdiv *subdiv, const SubdivToCCGSettings *settings, const Mesh *coarse_mesh)
Definition: subdiv_ccg.c:584
static void subdiv_ccg_average_inner_grids_task(void *__restrict userdata_v, const int face_index, const TaskParallelTLS *__restrict UNUSED(tls_v))
Definition: subdiv_ccg.c:1012
void BKE_subdiv_ccg_average_stitch_faces(SubdivCCG *subdiv_ccg, struct CCGFace **effected_faces, int num_effected_faces)
Definition: subdiv_ccg.c:1329
void BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG *subdiv_ccg, int grid_index)
Definition: subdiv_ccg.c:2035
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)
Definition: subdiv_ccg.c:1352
static void subdiv_ccg_average_grids_corners(SubdivCCG *subdiv_ccg, CCGKey *key, SubdivCCGAdjacentVertex *adjacent_vertex)
Definition: subdiv_ccg.c:1107
static bool subdiv_ccg_evaluate_grids(SubdivCCG *subdiv_ccg, Subdiv *subdiv, SubdivCCGMaskEvaluator *mask_evaluator, SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator)
Definition: subdiv_ccg.c:300
static void neighbor_coords_corner_vertex_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const bool include_duplicates, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1576
static void neighbor_coords_boundary_outer_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const bool include_duplicates, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1874
int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG *subdiv_ccg, const int grid_index)
Definition: subdiv_ccg.c:1940
static void neighbor_coords_corner_edge_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const bool include_duplicates, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1805
static void average_grid_element(SubdivCCG *subdiv_ccg, CCGKey *key, CCGElem *grid_element_a, CCGElem *grid_element_b)
Definition: subdiv_ccg.c:909
static int topology_refiner_count_face_corners(OpenSubdiv_TopologyRefiner *topology_refiner)
Definition: subdiv_ccg.c:106
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)
Definition: subdiv_ccg.c:2046
void BKE_subdiv_ccg_print_coord(const char *message, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1374
BLI_INLINE void subdiv_ccg_neighbors_init(SubdivCCGNeighbors *neighbors, const int num_unique, const int num_duplicates)
Definition: subdiv_ccg.c:1394
static void subdiv_ccg_recalc_inner_grid_normals(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:783
static void subdiv_ccg_recalc_inner_face_normals(SubdivCCG *subdiv_ccg, CCGKey *key, RecalcInnerNormalsTLSData *tls, const int grid_index)
Definition: subdiv_ccg.c:693
static void subdiv_ccg_recalc_modified_inner_normal_task(void *__restrict userdata_v, const int face_index, const TaskParallelTLS *__restrict tls_v)
Definition: subdiv_ccg.c:820
static int adjacent_edge_index_from_coord(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1641
static void subdiv_ccg_average_all_boundaries(SubdivCCG *subdiv_ccg, CCGKey *key)
Definition: subdiv_ccg.c:1166
static void neighbor_coords_boundary_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const bool include_duplicates, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1885
SubdivCCG * BKE_subdiv_to_ccg(Subdiv *subdiv, const SubdivToCCGSettings *settings, SubdivCCGMaskEvaluator *mask_evaluator, SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator)
Definition: subdiv_ccg.c:561
struct CCGEvalGridsData CCGEvalGridsData
static void static_or_heap_storage_init(StaticOrHeapIntStorage *storage)
Definition: subdiv_ccg.c:351
static int * static_or_heap_storage_get(StaticOrHeapIntStorage *storage, int heap_len)
Definition: subdiv_ccg.c:358
bool BKE_subdiv_ccg_check_coord_valid(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1379
static void subdiv_ccg_average_grids_corners_task(void *__restrict userdata_v, const int n, const TaskParallelTLS *__restrict UNUSED(tls_v))
Definition: subdiv_ccg.c:1132
BLI_INLINE SubdivCCGCoord coord_at_next_col(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1466
static void subdiv_ccg_init_layers(SubdivCCG *subdiv_ccg, const SubdivToCCGSettings *settings)
Definition: subdiv_ccg.c:76
static void subdiv_ccg_average_inner_face_normals(SubdivCCG *subdiv_ccg, CCGKey *key, RecalcInnerNormalsTLSData *tls, const int grid_index)
Definition: subdiv_ccg.c:727
static SubdivCCGCoord * subdiv_ccg_adjacent_vertex_add_face(SubdivCCGAdjacentVertex *adjacent_vertex)
Definition: subdiv_ccg.c:497
static void subdiv_ccg_init_faces_vertex_neighborhood(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:509
static int element_size_bytes_get(const SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:65
static void subdiv_ccg_init_faces_neighborhood(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:549
struct RecalcModifiedInnerNormalsData RecalcModifiedInnerNormalsData
struct AverageGridsCornerData AverageGridsCornerData
static void subdiv_ccg_allocate_adjacent_edges(SubdivCCG *subdiv_ccg, const int num_edges)
Definition: subdiv_ccg.c:378
static void subdiv_ccg_recalc_modified_inner_grid_normals(SubdivCCG *subdiv_ccg, struct CCGFace **effected_faces, int num_effected_faces)
Definition: subdiv_ccg.c:846