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