Blender V5.3
BKE_subdiv_ccg.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <memory>
12
13#include "BLI_array.hh"
15#include "BLI_bit_span_ops.hh"
16#include "BLI_index_mask_fwd.hh"
17#include "BLI_offset_indices.hh"
18#include "BLI_ordered_edge.hh"
19#include "BLI_set.hh"
20#include "BLI_span.hh"
21#include "BLI_utility_mixins.hh"
22#include "BLI_vector.hh"
23
24#include "BKE_ccg.hh"
25
26namespace blender {
27
28struct Mesh;
29namespace bke::subdiv {
30struct Subdiv;
31}
32
33/* -------------------------------------------------------------------- */
36
40 int ptex_face_index,
41 float u,
42 float v);
43
45 void (*free)(SubdivCCGMaskEvaluator *mask_evaluator);
46
47 void *user_data;
48};
49
52
54
55/* -------------------------------------------------------------------- */
58
72
76
78 short x, y;
79
84 static SubdivCCGCoord from_index(const CCGKey &key, int index)
85 {
86 const int grid_index = index / key.grid_area;
87 const int index_in_grid = index - grid_index * key.grid_area;
88
89 SubdivCCGCoord coord{};
90 coord.grid_index = grid_index;
91 coord.x = index_in_grid % key.grid_size;
92 coord.y = index_in_grid / key.grid_size;
93
94 return coord;
95 }
96
101 int to_index(const CCGKey &key) const
102 {
103 return key.grid_area * this->grid_index +
104 CCG_grid_xy_to_index(key.grid_size, this->x, this->y);
105 }
106};
107
116
122
136 int level = -1;
141 int grid_size = -1;
143 int grid_area = -1;
145 int grids_num = -1;
155
160
170
179
182
183 /* TODO(sergey): Consider adding some accessors to a "decoded" geometry,
184 * to make integration with draw manager and such easy.
185 */
186
187 /* TODO(sergey): Consider adding CD layers here, so we can draw final mesh
188 * from grids, and have UVs and such work.
189 */
190
191 /* Integration with sculpting. */
192 /* TODO(sergey): Is this really best way to go? Kind of annoying to have
193 * such use-related flags in a more or less generic structure. */
194 struct {
196 bool coords = false;
198 bool hidden = false;
200
201 ~SubdivCCG();
202};
203
217std::unique_ptr<SubdivCCG> BKE_subdiv_to_ccg(bke::subdiv::Subdiv &subdiv,
218 const SubdivToCCGSettings &settings,
219 const Mesh &coarse_mesh,
220 SubdivCCGMaskEvaluator *mask_evaluator = nullptr);
221
226Mesh *BKE_subdiv_to_ccg_mesh(bke::subdiv::Subdiv &subdiv,
227 const SubdivToCCGSettings &settings,
228 const Mesh &coarse_mesh);
229
231CCGKey BKE_subdiv_ccg_key(const SubdivCCG &subdiv_ccg, int level);
232CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg);
233
235void BKE_subdiv_ccg_recalc_normals(SubdivCCG &subdiv_ccg);
236
238void BKE_subdiv_ccg_update_normals(SubdivCCG &subdiv_ccg, const IndexMask &face_mask);
239
241void BKE_subdiv_ccg_average_grids(SubdivCCG &subdiv_ccg);
242
244void BKE_subdiv_ccg_average_stitch_faces(SubdivCCG &subdiv_ccg, const IndexMask &face_mask);
245
247void BKE_subdiv_ccg_topology_counters(const SubdivCCG &subdiv_ccg,
248 int &r_num_vertices,
249 int &r_num_edges,
250 int &r_num_faces,
251 int &r_num_loops);
252
257
259 {
260 return this->coords.as_span().drop_back(num_duplicates);
261 }
262
264 {
265 return this->coords.as_span().take_back(num_duplicates);
266 }
267};
268
269void BKE_subdiv_ccg_print_coord(const char *message, const SubdivCCGCoord &coord);
270bool BKE_subdiv_ccg_check_coord_valid(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord);
271
272/* CCG element neighbors.
273 *
274 * Neighbors are considered:
275 *
276 * - For an inner elements of a grid other elements which are sharing same row or column (4
277 * neighbor elements in total).
278 *
279 * - For the corner element a single neighboring element on every adjacent edge, single from
280 * every grid.
281 *
282 * - For the boundary element two neighbor elements on the boundary (from same grid) and one
283 * element inside of every neighboring grid. */
284
291void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg,
292 const SubdivCCGCoord &coord,
293 bool include_duplicates,
294 SubdivCCGNeighbors &r_neighbors);
295
296inline int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG &subdiv_ccg, const int grid_index)
297{
298 return subdiv_ccg.grid_to_face_map[grid_index];
299}
300
301void BKE_subdiv_ccg_eval_limit_positions(const SubdivCCG &subdiv_ccg,
302 const CCGKey &key,
303 int grid_index,
304 MutableSpan<float3> r_limit_positions);
305
311
318 const SubdivCCGCoord &coord,
319 Span<int> corner_verts,
320 OffsetIndices<int> faces,
321 int &r_v1,
322 int &r_v2);
323
325bool BKE_subdiv_ccg_coord_is_mesh_boundary(OffsetIndices<int> faces,
326 Span<int> corner_verts,
327 BitSpan boundary_verts,
328 const Set<OrderedEdge> &boundary_edges,
329 const SubdivCCG &subdiv_ccg,
330 SubdivCCGCoord coord);
331
332BitGroupVector<> &BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG &subdiv_ccg);
333void BKE_subdiv_ccg_grid_hidden_free(SubdivCCG &subdiv_ccg);
334
335template<typename Fn>
337 const BitGroupVector<> &grid_hidden,
338 const int grid,
339 const Fn &fn)
340{
341 if (grid_hidden.is_empty()) {
342 for (const int i : IndexRange(key.grid_area)) {
343 fn(i);
344 }
345 }
346 else {
347 bits::foreach_0_index(grid_hidden[grid], fn);
348 }
349}
350
351namespace bke::ccg {
352
354inline IndexRange grid_range(const int grid_area, const int grid)
355{
356 return IndexRange(grid * grid_area, grid_area);
357}
358inline IndexRange grid_range(const CCGKey &key, const int grid)
359{
360 return grid_range(key.grid_area, grid);
361}
362
364inline IndexRange face_range(const OffsetIndices<int> faces, const int grid_area, const int face)
365{
366 const IndexRange corners = faces[face];
367 return IndexRange(corners.start() * grid_area, corners.size() * grid_area);
368}
369inline IndexRange face_range(const OffsetIndices<int> faces, const CCGKey &key, const int face)
370{
371 return face_range(faces, key.grid_area, face);
372}
373
375inline int grid_xy_to_vert(const CCGKey &key, const int grid, const int x, const int y)
376{
377 return key.grid_area * grid + CCG_grid_xy_to_index(key.grid_size, x, y);
378}
379
380} // namespace bke::ccg
381
383
384} // namespace blender
constexpr IndexRange()=default
Span< T > as_span() const
NonCopyable(const NonCopyable &other)=delete
void foreach_0_index(const BitSpanT &data, Fn &&fn)
IndexRange face_range(const OffsetIndices< int > faces, const int grid_area, const int face)
IndexRange grid_range(const int grid_area, const int grid)
int grid_xy_to_vert(const CCGKey &key, const int grid, const int x, const int y)
BMesh Iterators.
bool BKE_subdiv_ccg_mask_init_from_paint(SubdivCCGMaskEvaluator *mask_evaluator, const Mesh *mesh)
bool BKE_subdiv_ccg_check_coord_valid(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void BKE_subdiv_ccg_average_grids(SubdivCCG &subdiv_ccg)
bool BKE_subdiv_ccg_coord_is_mesh_boundary(OffsetIndices< int > faces, Span< int > corner_verts, BitSpan boundary_verts, const Set< OrderedEdge > &boundary_edges, const SubdivCCG &subdiv_ccg, SubdivCCGCoord coord)
BitGroupVector & BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG &subdiv_ccg)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
void BKE_subdiv_ccg_grid_hidden_free(SubdivCCG &subdiv_ccg)
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, bool include_duplicates, SubdivCCGNeighbors &r_neighbors)
Vector< SubdivCCGCoord, 256 > SubdivCCGNeighborCoords
void BKE_subdiv_ccg_update_normals(SubdivCCG &subdiv_ccg, const IndexMask &face_mask)
void BKE_subdiv_ccg_recalc_normals(SubdivCCG &subdiv_ccg)
int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG &subdiv_ccg, const int grid_index)
int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
Definition BKE_ccg.hh:75
std::unique_ptr< SubdivCCG > BKE_subdiv_to_ccg(bke::subdiv::Subdiv &subdiv, const SubdivToCCGSettings &settings, const Mesh &coarse_mesh, SubdivCCGMaskEvaluator *mask_evaluator=nullptr)
CCGKey BKE_subdiv_ccg_key(const SubdivCCG &subdiv_ccg, int level)
void BKE_subdiv_ccg_foreach_visible_grid_vert(const CCGKey &key, const BitGroupVector<> &grid_hidden, const int grid, const Fn &fn)
SubdivCCGAdjacencyType BKE_subdiv_ccg_coarse_mesh_adjacency_info_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, Span< int > corner_verts, OffsetIndices< int > faces, int &r_v1, int &r_v2)
void BKE_subdiv_ccg_eval_limit_positions(const SubdivCCG &subdiv_ccg, const CCGKey &key, int grid_index, MutableSpan< float3 > r_limit_positions)
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)
Mesh * BKE_subdiv_to_ccg_mesh(bke::subdiv::Subdiv &subdiv, const SubdivToCCGSettings &settings, const Mesh &coarse_mesh)
void BKE_subdiv_ccg_average_stitch_faces(SubdivCCG &subdiv_ccg, const IndexMask &face_mask)
signed char int8_t
Definition BKE_text.h:76
void BKE_subdiv_ccg_print_coord(const char *message, const SubdivCCGCoord &coord)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Vector< Array< SubdivCCGCoord > > boundary_coords
Vector< SubdivCCGCoord > corner_coords
static SubdivCCGCoord from_index(const CCGKey &key, int index)
int to_index(const CCGKey &key) const
void(*) free(SubdivCCGMaskEvaluator *mask_evaluator)
float(*) eval_mask(SubdivCCGMaskEvaluator *mask_evaluator, int ptex_face_index, float u, float v)
Span< SubdivCCGCoord > duplicates() const
SubdivCCGNeighborCoords coords
Span< SubdivCCGCoord > unique() const
Array< float3 > positions
Array< SubdivCCGAdjacentEdge > adjacent_edges
OffsetIndices< int > faces
Array< float3 > normals
BitGroupVector grid_hidden
struct blender::SubdivCCG::@356361271106267377043074237104234372210112141040 dirty
Array< float > masks
Span< int > grid_to_face_map
Array< SubdivCCGAdjacentVertex > adjacent_verts