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