Blender  V3.3
subdiv_ccg_mask.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2018 Blender Foundation. All rights reserved. */
3 
8 #include <math.h>
9 
10 #include "BKE_subdiv_ccg.h"
11 
12 #include "DNA_mesh_types.h"
13 #include "DNA_meshdata_types.h"
14 #include "DNA_modifier_types.h"
15 #include "DNA_object_types.h"
16 
17 #include "BLI_utildefines.h"
18 
19 #include "BKE_customdata.h"
20 #include "BKE_subdiv.h"
21 
22 #include "MEM_guardedalloc.h"
23 
24 typedef struct PolyCornerIndex {
26  int corner;
28 
29 typedef struct GridPaintMaskData {
30  // int grid_size;
31  const MPoly *mpoly;
33  /* Indexed by ptex face index, contains polygon/corner which corresponds
34  * to it.
35  *
36  * NOTE: For quad polygon this is an index of first corner only, since
37  * there we only have one ptex.
38  */
41 
43  const int ptex_face_index,
44  const float u,
45  const float v,
46  const GridPaintMask **r_mask_grid,
47  float *grid_u,
48  float *grid_v)
49 {
50  GridPaintMaskData *data = mask_evaluator->user_data;
51  const PolyCornerIndex *poly_corner = &data->ptex_poly_corner[ptex_face_index];
52  const MPoly *poly = &data->mpoly[poly_corner->poly_index];
53  const int start_grid_index = poly->loopstart + poly_corner->corner;
54  int corner = 0;
55  if (poly->totloop == 4) {
56  float corner_u, corner_v;
57  corner = BKE_subdiv_rotate_quad_to_corner(u, v, &corner_u, &corner_v);
58  *r_mask_grid = &data->grid_paint_mask[start_grid_index + corner];
59  BKE_subdiv_ptex_face_uv_to_grid_uv(corner_u, corner_v, grid_u, grid_v);
60  }
61  else {
62  *r_mask_grid = &data->grid_paint_mask[start_grid_index];
63  BKE_subdiv_ptex_face_uv_to_grid_uv(u, v, grid_u, grid_v);
64  }
65  return corner;
66 }
67 
68 BLI_INLINE float read_mask_grid(const GridPaintMask *mask_grid,
69  const float grid_u,
70  const float grid_v)
71 {
72  if (mask_grid->data == NULL) {
73  return 0;
74  }
75  const int grid_size = BKE_subdiv_grid_size_from_level(mask_grid->level);
76  const int x = roundf(grid_u * (grid_size - 1));
77  const int y = roundf(grid_v * (grid_size - 1));
78  return mask_grid->data[y * grid_size + x];
79 }
80 
81 static float eval_mask(SubdivCCGMaskEvaluator *mask_evaluator,
82  const int ptex_face_index,
83  const float u,
84  const float v)
85 {
86  const GridPaintMask *mask_grid;
87  float grid_u, grid_v;
88  mask_get_grid_and_coord(mask_evaluator, ptex_face_index, u, v, &mask_grid, &grid_u, &grid_v);
89  return read_mask_grid(mask_grid, grid_u, grid_v);
90 }
91 
92 static void free_mask_data(SubdivCCGMaskEvaluator *mask_evaluator)
93 {
94  GridPaintMaskData *data = mask_evaluator->user_data;
95  MEM_freeN(data->ptex_poly_corner);
96  MEM_freeN(data);
97 }
98 
99 /* TODO(sergey): This seems to be generally used information, which almost
100  * worth adding to a subdiv itself, with possible cache of the value.
101  */
102 static int count_num_ptex_faces(const Mesh *mesh)
103 {
104  int num_ptex_faces = 0;
105  const MPoly *mpoly = mesh->mpoly;
106  for (int poly_index = 0; poly_index < mesh->totpoly; poly_index++) {
107  const MPoly *poly = &mpoly[poly_index];
108  num_ptex_faces += (poly->totloop == 4) ? 1 : poly->totloop;
109  }
110  return num_ptex_faces;
111 }
112 
113 static void mask_data_init_mapping(SubdivCCGMaskEvaluator *mask_evaluator, const Mesh *mesh)
114 {
115  GridPaintMaskData *data = mask_evaluator->user_data;
116  const MPoly *mpoly = mesh->mpoly;
117  const int num_ptex_faces = count_num_ptex_faces(mesh);
118  /* Allocate memory. */
119  data->ptex_poly_corner = MEM_malloc_arrayN(
120  num_ptex_faces, sizeof(*data->ptex_poly_corner), "ptex poly corner");
121  /* Fill in offsets. */
122  int ptex_face_index = 0;
123  PolyCornerIndex *ptex_poly_corner = data->ptex_poly_corner;
124  for (int poly_index = 0; poly_index < mesh->totpoly; poly_index++) {
125  const MPoly *poly = &mpoly[poly_index];
126  if (poly->totloop == 4) {
127  ptex_poly_corner[ptex_face_index].poly_index = poly_index;
128  ptex_poly_corner[ptex_face_index].corner = 0;
129  ptex_face_index++;
130  }
131  else {
132  for (int corner = 0; corner < poly->totloop; corner++) {
133  ptex_poly_corner[ptex_face_index].poly_index = poly_index;
134  ptex_poly_corner[ptex_face_index].corner = corner;
135  ptex_face_index++;
136  }
137  }
138  }
139 }
140 
141 static void mask_init_data(SubdivCCGMaskEvaluator *mask_evaluator, const Mesh *mesh)
142 {
143  GridPaintMaskData *data = mask_evaluator->user_data;
144  data->mpoly = mesh->mpoly;
145  data->grid_paint_mask = CustomData_get_layer(&mesh->ldata, CD_GRID_PAINT_MASK);
146  mask_data_init_mapping(mask_evaluator, mesh);
147 }
148 
149 static void mask_init_functions(SubdivCCGMaskEvaluator *mask_evaluator)
150 {
151  mask_evaluator->eval_mask = eval_mask;
152  mask_evaluator->free = free_mask_data;
153 }
154 
156  const struct Mesh *mesh)
157 {
159  return false;
160  }
161  /* Allocate all required memory. */
162  mask_evaluator->user_data = MEM_callocN(sizeof(GridPaintMaskData), "mask from grid data");
163  mask_init_data(mask_evaluator, mesh);
164  mask_init_functions(mask_evaluator);
165  return true;
166 }
CustomData interface, see also DNA_customdata_types.h.
void * CustomData_get_layer(const struct CustomData *data, int type)
BLI_INLINE void BKE_subdiv_ptex_face_uv_to_grid_uv(float ptex_u, float ptex_v, float *r_grid_u, float *r_grid_v)
Definition: subdiv_inline.h:15
BLI_INLINE int BKE_subdiv_grid_size_from_level(int level)
Definition: subdiv_inline.h:33
BLI_INLINE int BKE_subdiv_rotate_quad_to_corner(float quad_u, float quad_v, float *r_corner_u, float *r_corner_v)
Definition: subdiv_inline.h:38
#define BLI_INLINE
@ CD_GRID_PAINT_MASK
Object is a sort of wrapper for general info.
_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.
ATTR_WARN_UNUSED_RESULT const BMVert * v
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:34
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
PolyCornerIndex * ptex_poly_corner
const MPoly * mpoly
const GridPaintMask * grid_paint_mask
unsigned int level
int totpoly
struct MPoly * mpoly
CustomData ldata
float(* eval_mask)(struct SubdivCCGMaskEvaluator *mask_evaluator, int ptex_face_index, float u, float v)
void(* free)(struct SubdivCCGMaskEvaluator *mask_evaluator)
bool BKE_subdiv_ccg_mask_init_from_paint(SubdivCCGMaskEvaluator *mask_evaluator, const struct Mesh *mesh)
struct GridPaintMaskData GridPaintMaskData
static float eval_mask(SubdivCCGMaskEvaluator *mask_evaluator, const int ptex_face_index, const float u, const float v)
static void mask_init_data(SubdivCCGMaskEvaluator *mask_evaluator, const Mesh *mesh)
static int mask_get_grid_and_coord(SubdivCCGMaskEvaluator *mask_evaluator, const int ptex_face_index, const float u, const float v, const GridPaintMask **r_mask_grid, float *grid_u, float *grid_v)
static void mask_data_init_mapping(SubdivCCGMaskEvaluator *mask_evaluator, const Mesh *mesh)
static void mask_init_functions(SubdivCCGMaskEvaluator *mask_evaluator)
static int count_num_ptex_faces(const Mesh *mesh)
BLI_INLINE float read_mask_grid(const GridPaintMask *mask_grid, const float grid_u, const float grid_v)
struct PolyCornerIndex PolyCornerIndex
static void free_mask_data(SubdivCCGMaskEvaluator *mask_evaluator)