Blender  V3.3
multires_reshape_vertcos.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2020 Blender Foundation. All rights reserved. */
3 
8 #include "multires_reshape.h"
9 
10 #include "DNA_mesh_types.h"
11 #include "DNA_meshdata_types.h"
12 
13 #include "BLI_math_vector.h"
14 
15 #include "BKE_subdiv_foreach.h"
16 #include "BKE_subdiv_mesh.h"
17 
20 
21  const float (*vert_coords)[3];
22  const int num_vert_coords;
24 
30  const SubdivForeachContext *foreach_context,
31  const GridCoord *grid_coord,
32  const int subdiv_vertex_index)
33 {
34  MultiresReshapeAssignVertcosContext *reshape_vertcos_context = foreach_context->user_data;
35  const float *coordinate = reshape_vertcos_context->vert_coords[subdiv_vertex_index];
36 
38  reshape_vertcos_context->reshape_context, grid_coord);
39  BLI_assert(grid_element.displacement != NULL);
40  copy_v3_v3(grid_element.displacement, coordinate);
41 }
42 
43 /* TODO(sergey): De-duplicate with similar function in multires_reshape_smooth.c */
45  const PTexCoord *ptex_coord,
46  const int subdiv_vertex_index)
47 {
48  const MultiresReshapeAssignVertcosContext *reshape_vertcos_context = foreach_context->user_data;
49  const MultiresReshapeContext *reshape_context = reshape_vertcos_context->reshape_context;
50 
51  const GridCoord grid_coord = multires_reshape_ptex_coord_to_grid(reshape_context, ptex_coord);
52  const int face_index = multires_reshape_grid_to_face_index(reshape_context,
53  grid_coord.grid_index);
54 
55  const Mesh *base_mesh = reshape_context->base_mesh;
56  const MPoly *base_poly = &base_mesh->mpoly[face_index];
57  const int num_corners = base_poly->totloop;
58  const int start_grid_index = reshape_context->face_start_grid_index[face_index];
59  const int corner = grid_coord.grid_index - start_grid_index;
60 
61  if (grid_coord.u == 0.0f && grid_coord.v == 0.0f) {
62  for (int current_corner = 0; current_corner < num_corners; ++current_corner) {
63  GridCoord corner_grid_coord = grid_coord;
64  corner_grid_coord.grid_index = start_grid_index + current_corner;
66  foreach_context, &corner_grid_coord, subdiv_vertex_index);
67  }
68  return;
69  }
70 
72  foreach_context, &grid_coord, subdiv_vertex_index);
73 
74  if (grid_coord.u == 0.0f) {
75  GridCoord prev_grid_coord;
76  prev_grid_coord.grid_index = start_grid_index + ((corner + num_corners - 1) % num_corners);
77  prev_grid_coord.u = grid_coord.v;
78  prev_grid_coord.v = 0.0f;
79 
81  foreach_context, &prev_grid_coord, subdiv_vertex_index);
82  }
83 
84  if (grid_coord.v == 0.0f) {
85  GridCoord next_grid_coord;
86  next_grid_coord.grid_index = start_grid_index + ((corner + 1) % num_corners);
87  next_grid_coord.u = 0.0f;
88  next_grid_coord.v = grid_coord.u;
89 
91  foreach_context, &next_grid_coord, subdiv_vertex_index);
92  }
93 }
94 
95 /* SubdivForeachContext::topology_info() */
97  const SubdivForeachContext *foreach_context,
98  const int num_vertices,
99  const int UNUSED(num_edges),
100  const int UNUSED(num_loops),
101  const int UNUSED(num_polygons),
102  const int *UNUSED(subdiv_polygon_offset))
103 {
104  MultiresReshapeAssignVertcosContext *reshape_vertcos_context = foreach_context->user_data;
105  if (num_vertices != reshape_vertcos_context->num_vert_coords) {
106  return false;
107  }
108  return true;
109 }
110 
111 /* SubdivForeachContext::vertex_inner() */
113  const SubdivForeachContext *foreach_context,
114  void *UNUSED(tls_v),
115  const int ptex_face_index,
116  const float ptex_face_u,
117  const float ptex_face_v,
118  const int UNUSED(coarse_face_index),
119  const int UNUSED(coarse_face_corner),
120  const int subdiv_vertex_index)
121 {
122  const PTexCoord ptex_coord = {
123  .ptex_face_index = ptex_face_index,
124  .u = ptex_face_u,
125  .v = ptex_face_v,
126  };
127  multires_reshape_vertcos_foreach_vertex(foreach_context, &ptex_coord, subdiv_vertex_index);
128 }
129 
130 /* SubdivForeachContext::vertex_every_corner() */
132  const struct SubdivForeachContext *foreach_context,
133  void *UNUSED(tls_v),
134  const int ptex_face_index,
135  const float ptex_face_u,
136  const float ptex_face_v,
137  const int UNUSED(coarse_vertex_index),
138  const int UNUSED(coarse_face_index),
139  const int UNUSED(coarse_face_corner),
140  const int subdiv_vertex_index)
141 {
142  const PTexCoord ptex_coord = {
143  .ptex_face_index = ptex_face_index,
144  .u = ptex_face_u,
145  .v = ptex_face_v,
146  };
147  multires_reshape_vertcos_foreach_vertex(foreach_context, &ptex_coord, subdiv_vertex_index);
148 }
149 
150 /* SubdivForeachContext::vertex_every_edge() */
152  const struct SubdivForeachContext *foreach_context,
153  void *UNUSED(tls_v),
154  const int ptex_face_index,
155  const float ptex_face_u,
156  const float ptex_face_v,
157  const int UNUSED(coarse_edge_index),
158  const int UNUSED(coarse_face_index),
159  const int UNUSED(coarse_face_corner),
160  const int subdiv_vertex_index)
161 {
162  const PTexCoord ptex_coord = {
163  .ptex_face_index = ptex_face_index,
164  .u = ptex_face_u,
165  .v = ptex_face_v,
166  };
167  multires_reshape_vertcos_foreach_vertex(foreach_context, &ptex_coord, subdiv_vertex_index);
168 }
169 
171  const MultiresReshapeContext *reshape_context,
172  const float (*vert_coords)[3],
173  const int num_vert_coords)
174 {
175  MultiresReshapeAssignVertcosContext reshape_vertcos_context = {
176  .reshape_context = reshape_context,
177  .vert_coords = vert_coords,
178  .num_vert_coords = num_vert_coords,
179  };
180 
181  SubdivForeachContext foreach_context = {
186  .user_data = &reshape_vertcos_context,
187  };
188 
189  SubdivToMeshSettings mesh_settings;
190  mesh_settings.resolution = (1 << reshape_context->reshape.level) + 1;
191  mesh_settings.use_optimal_display = false;
192 
194  reshape_context->subdiv, &foreach_context, &mesh_settings, reshape_context->base_mesh);
195 }
typedef float(TangentPoint)[2]
bool BKE_subdiv_foreach_subdiv_geometry(struct Subdiv *subdiv, const struct SubdivForeachContext *context, const struct SubdivToMeshSettings *mesh_settings, const struct Mesh *coarse_mesh)
#define BLI_assert(a)
Definition: BLI_assert.h:46
MINLINE void copy_v3_v3(float r[3], const float a[3])
#define UNUSED(x)
int multires_reshape_grid_to_face_index(const MultiresReshapeContext *reshape_context, int grid_index)
ReshapeGridElement multires_reshape_grid_element_for_grid_coord(const MultiresReshapeContext *reshape_context, const GridCoord *grid_coord)
GridCoord multires_reshape_ptex_coord_to_grid(const MultiresReshapeContext *reshape_context, const PTexCoord *ptex_coord)
static void multires_reshape_vertcos_foreach_single_vertex(const SubdivForeachContext *foreach_context, const GridCoord *grid_coord, const int subdiv_vertex_index)
static void multires_reshape_vertcos_foreach_vertex(const SubdivForeachContext *foreach_context, const PTexCoord *ptex_coord, const int subdiv_vertex_index)
static void multires_reshape_vertcos_foreach_vertex_every_edge(const struct SubdivForeachContext *foreach_context, void *UNUSED(tls_v), const int ptex_face_index, const float ptex_face_u, const float ptex_face_v, const int UNUSED(coarse_edge_index), const int UNUSED(coarse_face_index), const int UNUSED(coarse_face_corner), const int subdiv_vertex_index)
static void multires_reshape_vertcos_foreach_vertex_every_corner(const struct SubdivForeachContext *foreach_context, void *UNUSED(tls_v), const int ptex_face_index, const float ptex_face_u, const float ptex_face_v, const int UNUSED(coarse_vertex_index), const int UNUSED(coarse_face_index), const int UNUSED(coarse_face_corner), const int subdiv_vertex_index)
bool multires_reshape_assign_final_coords_from_vertcos(const MultiresReshapeContext *reshape_context, const float(*vert_coords)[3], const int num_vert_coords)
static bool multires_reshape_vertcos_foreach_topology_info(const SubdivForeachContext *foreach_context, const int num_vertices, const int UNUSED(num_edges), const int UNUSED(num_loops), const int UNUSED(num_polygons), const int *UNUSED(subdiv_polygon_offset))
static void multires_reshape_vertcos_foreach_vertex_inner(const SubdivForeachContext *foreach_context, void *UNUSED(tls_v), const int ptex_face_index, const float ptex_face_u, const float ptex_face_v, const int UNUSED(coarse_face_index), const int UNUSED(coarse_face_corner), const int subdiv_vertex_index)
struct MultiresReshapeAssignVertcosContext MultiresReshapeAssignVertcosContext
struct MPoly * mpoly
const MultiresReshapeContext * reshape_context
struct MultiresReshapeContext::@99 reshape
struct Subdiv * subdiv
SubdivForeachTopologyInformationCb topology_info