43static void subdiv_ccg_average_inner_face_grids(
SubdivCCG &subdiv_ccg,
47void subdiv_ccg_average_faces_boundaries_and_corners(
SubdivCCG &subdiv_ccg,
58static int topology_refiner_count_face_corners(
61 const int num_faces = topology_refiner->base_level().GetNumFaces();
63 for (
int face_index = 0; face_index < num_faces; face_index++) {
64 num_corners += topology_refiner->base_level().GetFaceVertices(face_index).size();
71static void subdiv_ccg_alloc_elements(
SubdivCCG &subdiv_ccg,
77 const int64_t num_grids = topology_refiner_count_face_corners(topology_refiner);
79 const int64_t grid_area = grid_size * grid_size;
80 subdiv_ccg.positions.reinitialize(num_grids * grid_area);
81 if (settings.need_normal) {
82 subdiv_ccg.normals.reinitialize(num_grids * grid_area);
84 if (settings.need_mask) {
85 subdiv_ccg.masks.reinitialize(num_grids * grid_area);
96static void subdiv_ccg_eval_grid_element_limit(
Subdiv &
subdiv,
98 const int ptex_face_index,
103 if (
subdiv.displacement_evaluator !=
nullptr) {
106 else if (!subdiv_ccg.normals.is_empty()) {
119static void subdiv_ccg_eval_grid_element_mask(
SubdivCCG &subdiv_ccg,
121 const int ptex_face_index,
126 if (subdiv_ccg.masks.is_empty()) {
129 if (mask_evaluator !=
nullptr) {
130 subdiv_ccg.masks[
element] = mask_evaluator->eval_mask(mask_evaluator, ptex_face_index, u,
v);
133 subdiv_ccg.masks[
element] = 0.0f;
140 const int ptex_face_index,
145 subdiv_ccg_eval_grid_element_limit(
subdiv, subdiv_ccg, ptex_face_index, u,
v,
element);
146 subdiv_ccg_eval_grid_element_mask(subdiv_ccg, mask_evaluator, ptex_face_index, u,
v,
element);
153 const int face_index)
155 const int ptex_face_index = face_ptex_offset[face_index];
156 const int grid_size = subdiv_ccg.grid_size;
157 const int grid_area = subdiv_ccg.grid_area;
158 const float grid_size_1_inv = 1.0f / (grid_size - 1);
159 const IndexRange face = subdiv_ccg.faces[face_index];
160 for (
int corner = 0; corner < face.size(); corner++) {
161 const int grid_index = face.start() + corner;
163 for (
int y = 0;
y < grid_size;
y++) {
164 const float grid_v =
y * grid_size_1_inv;
165 for (
int x = 0;
x < grid_size;
x++) {
166 const float grid_u =
x * grid_size_1_inv;
170 subdiv_ccg_eval_grid_element(
171 subdiv, subdiv_ccg, mask_evaluator, ptex_face_index, u,
v,
element);
181 const int face_index)
183 const int grid_size = subdiv_ccg.grid_size;
184 const int grid_area = subdiv_ccg.grid_area;
185 const float grid_size_1_inv = 1.0f / (grid_size - 1);
186 const IndexRange face = subdiv_ccg.faces[face_index];
187 for (
int corner = 0; corner < face.size(); corner++) {
188 const int grid_index = face.start() + corner;
189 const int ptex_face_index = face_ptex_offset[face_index] + corner;
191 for (
int y = 0;
y < grid_size;
y++) {
192 const float u = 1.0f - (
y * grid_size_1_inv);
193 for (
int x = 0;
x < grid_size;
x++) {
194 const float v = 1.0f - (
x * grid_size_1_inv);
196 subdiv_ccg_eval_grid_element(
197 subdiv, subdiv_ccg, mask_evaluator, ptex_face_index, u,
v,
element);
203static bool subdiv_ccg_evaluate_grids(
SubdivCCG &subdiv_ccg,
208 const int num_faces = topology_refiner->base_level().GetNumFaces();
210 BLI_assert(face_ptex_offset.
size() == subdiv_ccg.faces.size() + 1);
212 for (
const int face_index : range) {
213 if (subdiv_ccg.faces[face_index].size() == 4) {
214 subdiv_ccg_eval_regular_grid(
215 subdiv, subdiv_ccg, face_ptex_offset, mask_evaluator, face_index);
218 subdiv_ccg_eval_special_grid(
219 subdiv, subdiv_ccg, face_ptex_offset, mask_evaluator, face_index);
225 if (
subdiv.displacement_evaluator !=
nullptr) {
231static void subdiv_ccg_allocate_adjacent_edges(
SubdivCCG &subdiv_ccg,
const int num_edges)
250 adjacent_edge.boundary_coords.append(std::move(coords));
251 return adjacent_edge.boundary_coords[adjacent_edge.boundary_coords.size() - 1];
254static void subdiv_ccg_init_faces_edge_neighborhood(
SubdivCCG &subdiv_ccg)
258 const OpenSubdiv::Far::TopologyLevel &base_level =
subdiv->topology_refiner->base_level();
259 const int num_edges = base_level.GetNumEdges();
260 const int grid_size = subdiv_ccg.grid_size;
261 if (num_edges == 0) {
265 subdiv_ccg_allocate_adjacent_edges(subdiv_ccg, num_edges);
268 for (
const int face_index : faces.index_range()) {
270 const int num_face_grids = face.
size();
271 const OpenSubdiv::Far::ConstIndexArray face_vertices = base_level.GetFaceVertices(face_index);
274 const OpenSubdiv::Far::ConstIndexArray face_edges = base_level.GetFaceEdges(face_index);
276 for (
int corner = 0; corner < num_face_grids; corner++) {
277 const int vertex_index = face_vertices[corner];
278 const int edge_index = face_edges[corner];
279 const OpenSubdiv::Far::ConstIndexArray edge_vertices = base_level.GetEdgeVertices(
281 const bool is_edge_flipped = (edge_vertices[0] != vertex_index);
283 const int current_grid_index = face.start() + corner;
285 const int next_grid_index = face.start() + (corner + 1) % num_face_grids;
289 grid_size * 2, adjacent_edge);
291 int boundary_element_index = 0;
292 if (is_edge_flipped) {
293 for (
int i = 0; i < grid_size; i++) {
294 boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
295 next_grid_index, grid_size - i - 1, grid_size - 1);
297 for (
int i = 0; i < grid_size; i++) {
298 boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
299 current_grid_index, grid_size - 1, i);
303 for (
int i = 0; i < grid_size; i++) {
304 boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
305 current_grid_index, grid_size - 1, grid_size - i - 1);
307 for (
int i = 0; i < grid_size; i++) {
308 boundary_coords[boundary_element_index++] = subdiv_ccg_coord(
309 next_grid_index, i, grid_size - 1);
316static void subdiv_ccg_allocate_adjacent_vertices(
SubdivCCG &subdiv_ccg,
const int num_vertices)
325 const int grid_index,
332static void subdiv_ccg_init_faces_vertex_neighborhood(
SubdivCCG &subdiv_ccg)
337 const int num_vertices = topology_refiner->base_level().GetNumVertices();
338 const int grid_size = subdiv_ccg.grid_size;
339 if (num_vertices == 0) {
343 subdiv_ccg_allocate_adjacent_vertices(subdiv_ccg, num_vertices);
345 for (
const int face_index : faces.index_range()) {
347 const int num_face_grids = face.
size();
348 const OpenSubdiv::Far::ConstIndexArray face_vertices =
349 topology_refiner->base_level().GetFaceVertices(face_index);
350 for (
int corner = 0; corner < num_face_grids; corner++) {
351 const int vertex_index = face_vertices[corner];
353 const int grid_index = face.start() + corner;
356 subdiv_ccg_adjacent_vertex_add_face(
357 adjacent_vertex, grid_index, grid_size - 1, grid_size - 1);
362static void subdiv_ccg_init_faces_neighborhood(
SubdivCCG &subdiv_ccg)
364 subdiv_ccg_init_faces_edge_neighborhood(subdiv_ccg);
365 subdiv_ccg_init_faces_vertex_neighborhood(subdiv_ccg);
378 const Mesh &coarse_mesh,
381#ifdef WITH_OPENSUBDIV
383 std::unique_ptr<SubdivCCG> subdiv_ccg = std::make_unique<SubdivCCG>();
384 subdiv_ccg->subdiv = &
subdiv;
387 subdiv_ccg->grid_area = subdiv_ccg->grid_size * subdiv_ccg->grid_size;
388 subdiv_ccg->faces = coarse_mesh.faces();
389 subdiv_ccg->grids_num = subdiv_ccg->faces.total_size();
390 subdiv_ccg->grid_to_face_map = coarse_mesh.corner_to_face_map();
393 subdiv_ccg_alloc_elements(*subdiv_ccg,
subdiv, settings);
394 subdiv_ccg_init_faces_neighborhood(*subdiv_ccg);
395 if (!subdiv_ccg_evaluate_grids(*subdiv_ccg,
subdiv, mask_evaluator)) {
410 const Mesh &coarse_mesh)
423 subdiv, settings, coarse_mesh, has_mask ? &mask_evaluator :
nullptr);
425 mask_evaluator.
free(&mask_evaluator);
431 result->runtime->subdiv_ccg = std::move(subdiv_ccg);
437 if (this->subdiv !=
nullptr) {
444#ifdef WITH_OPENSUBDIV
477#ifdef WITH_OPENSUBDIV
484static void subdiv_ccg_recalc_inner_face_normals(
const SubdivCCG &subdiv_ccg,
485 MutableSpan<float3> face_normals,
488 const int grid_size = subdiv_ccg.grid_size;
489 const int grid_area = subdiv_ccg.grid_area;
490 const int grid_size_1 = grid_size - 1;
491 const Span grid_positions = subdiv_ccg.positions.as_span().
slice(
grid_range(grid_area, corner));
492 for (
int y = 0;
y < grid_size - 1;
y++) {
493 for (
int x = 0;
x < grid_size - 1;
x++) {
494 const int face_index =
y * grid_size_1 +
x;
495 float *face_normal = face_normals[face_index];
506static void subdiv_ccg_average_inner_face_normals(
SubdivCCG &subdiv_ccg,
510 const int grid_size = subdiv_ccg.grid_size;
511 const int grid_area = subdiv_ccg.grid_area;
512 const int grid_size_1 = grid_size - 1;
515 for (
int y = 0;
y < grid_size;
y++) {
516 for (
int x = 0;
x < grid_size;
x++) {
517 float normal_acc[3] = {0.0f, 0.0f, 0.0f};
520 if (
x < grid_size_1 &&
y < grid_size_1) {
521 add_v3_v3(normal_acc, face_normals[
y * grid_size_1 +
x]);
525 if (
y < grid_size_1) {
526 add_v3_v3(normal_acc, face_normals[
y * grid_size_1 + (
x - 1)]);
530 add_v3_v3(normal_acc, face_normals[(
y - 1) * grid_size_1 + (
x - 1)]);
534 if (
y >= 1 &&
x < grid_size_1) {
535 add_v3_v3(normal_acc, face_normals[(
y - 1) * grid_size_1 +
x]);
545static void subdiv_ccg_recalc_inner_grid_normals(
SubdivCCG &subdiv_ccg,
const IndexMask &face_mask)
549 const int grid_size_1 = subdiv_ccg.grid_size - 1;
554 face_mask.foreach_segment(
557 for (
const int face_index : segment) {
559 for (
const int grid_index : face) {
560 subdiv_ccg_recalc_inner_face_normals(subdiv_ccg, face_normals, grid_index);
561 subdiv_ccg_average_inner_face_normals(subdiv_ccg, face_normals, grid_index);
563 subdiv_ccg_average_inner_face_grids(subdiv_ccg, key, face);
573#ifdef WITH_OPENSUBDIV
578 subdiv_ccg_recalc_inner_grid_normals(subdiv_ccg, subdiv_ccg.
faces.index_range());
587#ifdef WITH_OPENSUBDIV
596 subdiv_ccg_recalc_inner_grid_normals(subdiv_ccg, face_mask);
599 subdiv_ccg_average_faces_boundaries_and_corners(subdiv_ccg, key, face_mask);
611#ifdef WITH_OPENSUBDIV
613static void average_grid_element_value_v3(
float a[3],
float b[3])
620static void average_grid_element(
SubdivCCG &subdiv_ccg,
621 const int grid_element_a,
622 const int grid_element_b)
624 average_grid_element_value_v3(subdiv_ccg.positions[grid_element_a],
625 subdiv_ccg.positions[grid_element_b]);
626 if (!subdiv_ccg.normals.is_empty()) {
627 average_grid_element_value_v3(subdiv_ccg.normals[grid_element_a],
628 subdiv_ccg.normals[grid_element_b]);
630 if (!subdiv_ccg.masks.is_empty()) {
631 float mask = (subdiv_ccg.masks[grid_element_a] + subdiv_ccg.masks[grid_element_b]) * 0.5f;
632 subdiv_ccg.masks[grid_element_a] =
mask;
633 subdiv_ccg.masks[grid_element_b] =
mask;
638struct GridElementAccumulator {
644static void element_accumulator_init(GridElementAccumulator &accumulator)
648 accumulator.mask = 0.0f;
651static void element_accumulator_add(GridElementAccumulator &accumulator,
655 accumulator.co += subdiv_ccg.positions[elem];
656 if (!subdiv_ccg.normals.is_empty()) {
657 accumulator.no += subdiv_ccg.normals[elem];
659 if (!subdiv_ccg.masks.is_empty()) {
660 accumulator.mask += subdiv_ccg.masks[elem];
664static void element_accumulator_mul_fl(GridElementAccumulator &accumulator,
const float f)
668 accumulator.mask *= f;
671static void element_accumulator_copy(
SubdivCCG &subdiv_ccg,
672 const int destination,
673 const GridElementAccumulator &accumulator)
675 subdiv_ccg.positions[destination] = accumulator.co;
676 if (!subdiv_ccg.normals.is_empty()) {
677 subdiv_ccg.normals[destination] = accumulator.no;
679 if (!subdiv_ccg.masks.is_empty()) {
680 subdiv_ccg.masks[destination] = accumulator.mask;
684static void subdiv_ccg_average_inner_face_grids(
SubdivCCG &subdiv_ccg,
688 const int num_face_grids = face.size();
689 const int grid_size = subdiv_ccg.grid_size;
690 int prev_grid = face.start() + num_face_grids - 1;
692 for (
const int grid : face) {
693 for (
int i = 1; i < grid_size; i++) {
696 average_grid_element(subdiv_ccg, prev_grid_element, grid_element);
702 GridElementAccumulator center_accumulator;
703 element_accumulator_init(center_accumulator);
704 for (
const int grid : face) {
706 element_accumulator_add(center_accumulator, subdiv_ccg, grid_center_element);
708 element_accumulator_mul_fl(center_accumulator, 1.0f / num_face_grids);
709 for (
const int grid : face) {
711 element_accumulator_copy(subdiv_ccg, grid_center_element, center_accumulator);
715static void subdiv_ccg_average_grids_boundary(
SubdivCCG &subdiv_ccg,
720 const int num_adjacent_faces = adjacent_edge.boundary_coords.size();
721 const int grid_size2 = subdiv_ccg.grid_size * 2;
722 if (num_adjacent_faces == 1) {
726 for (
int i = 1; i < grid_size2 - 1; i++) {
727 element_accumulator_init(accumulators[i]);
729 for (
int face_index = 0; face_index < num_adjacent_faces; face_index++) {
730 for (
int i = 1; i < grid_size2 - 1; i++) {
731 const int grid_element = adjacent_edge.boundary_coords[face_index][i].to_index(key);
732 element_accumulator_add(accumulators[i], subdiv_ccg, grid_element);
735 for (
int i = 1; i < grid_size2 - 1; i++) {
736 element_accumulator_mul_fl(accumulators[i], 1.0f / num_adjacent_faces);
739 for (
int face_index = 0; face_index < num_adjacent_faces; face_index++) {
740 for (
int i = 1; i < grid_size2 - 1; i++) {
741 const int grid_element = adjacent_edge.boundary_coords[face_index][i].to_index(key);
742 element_accumulator_copy(subdiv_ccg, grid_element, accumulators[i]);
747static void subdiv_ccg_average_grids_corners(
SubdivCCG &subdiv_ccg,
751 const int num_adjacent_faces = adjacent_vertex.corner_coords.size();
752 if (num_adjacent_faces == 1) {
756 GridElementAccumulator accumulator;
757 element_accumulator_init(accumulator);
758 for (
int face_index = 0; face_index < num_adjacent_faces; face_index++) {
759 const int grid_element = adjacent_vertex.corner_coords[face_index].to_index(key);
760 element_accumulator_add(accumulator, subdiv_ccg, grid_element);
762 element_accumulator_mul_fl(accumulator, 1.0f / num_adjacent_faces);
764 for (
int face_index = 0; face_index < num_adjacent_faces; face_index++) {
765 const int grid_element = adjacent_vertex.corner_coords[face_index].to_index(key);
766 element_accumulator_copy(subdiv_ccg, grid_element, accumulator);
770static void subdiv_ccg_average_boundaries(
SubdivCCG &subdiv_ccg,
777 adjacent_edge_mask.foreach_segment(
780 for (
const int i : segment) {
782 subdiv_ccg_average_grids_boundary(subdiv_ccg, key, adjacent_edge, accumulators);
788static void subdiv_ccg_average_corners(
SubdivCCG &subdiv_ccg,
792 adjacent_vert_mask.foreach_index(
795 subdiv_ccg_average_grids_corners(subdiv_ccg, key, adjacent_vert);
800static void subdiv_ccg_average_inner_faces(
SubdivCCG &subdiv_ccg,
804 face_mask.foreach_index(
805 [&](
const int face_index) {
806 subdiv_ccg_average_inner_face_grids(subdiv_ccg, key, subdiv_ccg.faces[face_index]);
815#ifdef WITH_OPENSUBDIV
819 subdiv_ccg_average_inner_faces(subdiv_ccg, key, subdiv_ccg.
faces.index_range());
820 subdiv_ccg_average_boundaries(subdiv_ccg, key, subdiv_ccg.
adjacent_edges.index_range());
821 subdiv_ccg_average_corners(subdiv_ccg, key, subdiv_ccg.
adjacent_verts.index_range());
827#ifdef WITH_OPENSUBDIV
829static void subdiv_ccg_affected_face_adjacency(SubdivCCG &subdiv_ccg,
830 const IndexMask &face_mask,
831 Set<int> &adjacent_verts,
832 Set<int> &adjacent_edges)
834 Subdiv *
subdiv = subdiv_ccg.subdiv;
835 const opensubdiv::TopologyRefinerImpl *topology_refiner =
subdiv->topology_refiner;
837 face_mask.foreach_index([&](
const int face_index) {
838 const OpenSubdiv::Far::ConstIndexArray face_vertices =
839 topology_refiner->base_level().GetFaceVertices(face_index);
840 adjacent_verts.add_multiple({face_vertices.begin(), face_vertices.size()});
842 const OpenSubdiv::Far::ConstIndexArray face_edges =
843 topology_refiner->base_level().GetFaceEdges(face_index);
844 adjacent_edges.add_multiple({face_edges.begin(), face_edges.size()});
848void subdiv_ccg_average_faces_boundaries_and_corners(
SubdivCCG &subdiv_ccg,
852 if (face_mask.size() == subdiv_ccg.faces.size()) {
854 subdiv_ccg_average_boundaries(subdiv_ccg, key, subdiv_ccg.adjacent_edges.index_range());
855 subdiv_ccg_average_corners(subdiv_ccg, key, subdiv_ccg.adjacent_verts.index_range());
861 subdiv_ccg_affected_face_adjacency(subdiv_ccg, face_mask, adjacent_vert_set, adjacent_edge_set);
863 Vector<int> adjacent_verts(adjacent_vert_set.begin(), adjacent_vert_set.end());
864 Vector<int> adjacent_edges(adjacent_edge_set.begin(), adjacent_edge_set.end());
866 std::ranges::sort(adjacent_verts);
867 std::ranges::sort(adjacent_edges);
870 subdiv_ccg_average_boundaries(
873 subdiv_ccg_average_corners(
881#ifdef WITH_OPENSUBDIV
883 subdiv_ccg_average_inner_faces(subdiv_ccg, key, face_mask);
884 subdiv_ccg_average_faces_boundaries_and_corners(subdiv_ccg, key, face_mask);
896 const int num_grids = subdiv_ccg.
grids_num;
897 const int grid_size = subdiv_ccg.
grid_size;
898 const int grid_area = grid_size * grid_size;
899 const int num_edges_per_grid = 2 * (grid_size * (grid_size - 1));
900 r_num_vertices = num_grids * grid_area;
901 r_num_edges = num_grids * num_edges_per_grid;
902 r_num_faces = num_grids * (grid_size - 1) * (grid_size - 1);
903 r_num_loops = r_num_faces * 4;
914 printf(
"%s: grid index: %d, coord: (%d, %d)\n", message, coord.
grid_index, coord.
x, coord.
y);
922 const int grid_size = subdiv_ccg.
grid_size;
923 if (coord.
x < 0 || coord.
x >= grid_size) {
926 if (coord.
y < 0 || coord.
y >= grid_size) {
933 const int num_unique,
934 const int num_duplicates)
936 const int size = num_unique + num_duplicates;
944 const int grid_size_1 = subdiv_ccg.
grid_size - 1;
945 return (coord.
x == 0 && coord.
y == 0) || (coord.
x == 0 && coord.
y == grid_size_1) ||
946 (coord.
x == grid_size_1 && coord.
y == grid_size_1) ||
947 (coord.
x == grid_size_1 && coord.
y == 0);
953 const int grid_size_1 = subdiv_ccg.
grid_size - 1;
954 return coord.
x == 0 || coord.
y == 0 || coord.
x == grid_size_1 || coord.
y == grid_size_1;
961 const int grid_size_1 = subdiv_ccg.
grid_size - 1;
963 return coord.
y > 0 && coord.
y < grid_size_1;
966 return coord.
x > 0 && coord.
x < grid_size_1;
1007#ifdef WITH_OPENSUBDIV
1010static SubdivCCGCoord coord_step_inside_from_boundary(
const SubdivCCG &subdiv_ccg,
1011 const SubdivCCGCoord &coord)
1014 SubdivCCGCoord
result = coord;
1015 const int grid_size_1 = subdiv_ccg.grid_size - 1;
1016 if (
result.x == grid_size_1) {
1019 else if (
result.y == grid_size_1) {
1022 else if (
result.x == 0) {
1025 else if (
result.y == 0) {
1037 const IndexRange face = subdiv_ccg.faces[subdiv_ccg.grid_to_face_map[coord.grid_index]];
1038 const int face_grid_index = coord.grid_index;
1039 int next_face_grid_index = face_grid_index + 1 - face.
start();
1040 if (next_face_grid_index == face.size()) {
1041 next_face_grid_index = 0;
1043 return face.start() + next_face_grid_index;
1047 const IndexRange face = subdiv_ccg.faces[subdiv_ccg.grid_to_face_map[coord.grid_index]];
1048 const int face_grid_index = coord.grid_index;
1049 int prev_face_grid_index = face_grid_index - 1 - face.
start();
1050 if (prev_face_grid_index < 0) {
1051 prev_face_grid_index = face.size() - 1;
1053 return face.start() + prev_face_grid_index;
1058static void neighbor_coords_corner_center_get(
const SubdivCCG &subdiv_ccg,
1060 const bool include_duplicates,
1063 const IndexRange face = subdiv_ccg.faces[subdiv_ccg.grid_to_face_map[coord.grid_index]];
1064 const int num_adjacent_grids = face.
size();
1067 r_neighbors, num_adjacent_grids, (include_duplicates) ? num_adjacent_grids - 1 : 0);
1069 int duplicate_face_grid_index = num_adjacent_grids;
1070 for (
int face_grid_index = 0; face_grid_index < num_adjacent_grids; ++face_grid_index) {
1072 neighbor_coord.
grid_index = face.start() + face_grid_index;
1073 neighbor_coord.x = 1;
1074 neighbor_coord.y = 0;
1075 r_neighbors.coords[face_grid_index] = neighbor_coord;
1077 if (include_duplicates && neighbor_coord.grid_index != coord.grid_index) {
1078 neighbor_coord.x = 0;
1079 r_neighbors.coords[duplicate_face_grid_index++] = neighbor_coord;
1085static int adjacent_vertex_index_from_coord(
const SubdivCCG &subdiv_ccg,
1091 const int face_index = subdiv_ccg.grid_to_face_map[coord.grid_index];
1092 const IndexRange face = subdiv_ccg.faces[face_index];
1093 const int face_grid_index = coord.grid_index - face.
start();
1095 const OpenSubdiv::Far::ConstIndexArray face_vertices =
1096 topology_refiner->base_level().GetFaceVertices(face_index);
1098 const int adjacent_vertex_index = face_vertices[face_grid_index];
1099 return adjacent_vertex_index;
1103static void neighbor_coords_corner_vertex_get(
const SubdivCCG &subdiv_ccg,
1105 const bool include_duplicates,
1111 const int adjacent_vertex_index = adjacent_vertex_index_from_coord(subdiv_ccg, coord);
1112 const OpenSubdiv::Far::ConstIndexArray vertex_edges =
1113 topology_refiner->base_level().GetVertexEdges(adjacent_vertex_index);
1116 const int num_adjacent_faces = adjacent_vert.
corner_coords.size();
1119 r_neighbors, vertex_edges.size(), (include_duplicates) ? num_adjacent_faces - 1 : 0);
1121 for (
int i = 0; i < vertex_edges.size(); ++i) {
1122 const int edge_index = vertex_edges[i];
1125 const int edge_face_index = 0;
1128 const OpenSubdiv::Far::ConstIndexArray edge_vertices_indices =
1129 topology_refiner->base_level().GetEdgeVertices(edge_index);
1130 int edge_point_index, duplicate_edge_point_index;
1131 if (edge_vertices_indices[0] == adjacent_vertex_index) {
1132 duplicate_edge_point_index = 0;
1133 edge_point_index = duplicate_edge_point_index + 1;
1139 duplicate_edge_point_index = subdiv_ccg.grid_size * 2 - 1;
1140 edge_point_index = duplicate_edge_point_index - 1;
1144 r_neighbors.coords[i] = adjacent_edge.
boundary_coords[edge_face_index][edge_point_index];
1147 if (include_duplicates) {
1149 for (
int i = 0, duplicate_i = vertex_edges.size(); i < num_adjacent_faces; i++) {
1151 if (neighbor_coord.grid_index != coord.grid_index) {
1152 r_neighbors.coords[duplicate_i++] = neighbor_coord;
1163 const int face_index = subdiv_ccg.grid_to_face_map[coord.grid_index];
1164 const IndexRange face = subdiv_ccg.faces[face_index];
1165 const int face_grid_index = coord.grid_index - face.
start();
1167 const OpenSubdiv::Far::ConstIndexArray face_edges = topology_refiner->base_level().GetFaceEdges(
1170 const int grid_size_1 = subdiv_ccg.grid_size - 1;
1171 int adjacent_edge_index = -1;
1172 if (coord.x == grid_size_1) {
1173 adjacent_edge_index = face_edges[face_grid_index];
1177 adjacent_edge_index = face_edges[face_grid_index == 0 ? face.size() - 1 : face_grid_index - 1];
1180 return adjacent_edge_index;
1183static int adjacent_edge_point_index_from_coord(
const SubdivCCG &subdiv_ccg,
1185 const int adjacent_edge_index)
1190 const int adjacent_vertex_index = adjacent_vertex_index_from_coord(subdiv_ccg, coord);
1191 const OpenSubdiv::Far::ConstIndexArray edge_vertices_indices =
1192 topology_refiner->base_level().GetEdgeVertices(adjacent_edge_index);
1198 int directional_edge_vertex_index = -1;
1200 const int grid_size_1 = subdiv_ccg.grid_size - 1;
1201 int adjacent_edge_point_index = -1;
1202 if (coord.x == grid_size_1) {
1203 adjacent_edge_point_index = subdiv_ccg.grid_size - coord.y - 1;
1204 directional_edge_vertex_index = edge_vertices_indices[0];
1208 adjacent_edge_point_index = subdiv_ccg.grid_size + coord.x;
1209 directional_edge_vertex_index = edge_vertices_indices[1];
1213 if (adjacent_vertex_index != directional_edge_vertex_index) {
1214 const int num_edge_points = subdiv_ccg.grid_size * 2;
1215 adjacent_edge_point_index = num_edge_points - adjacent_edge_point_index - 1;
1218 return adjacent_edge_point_index;
1225static int next_adjacent_edge_point_index(
const SubdivCCG &subdiv_ccg,
const int point_index)
1227 if (point_index == subdiv_ccg.grid_size - 1) {
1228 return point_index + 2;
1230 return point_index + 1;
1232static int prev_adjacent_edge_point_index(
const SubdivCCG &subdiv_ccg,
const int point_index)
1234 if (point_index == subdiv_ccg.grid_size) {
1235 return point_index - 2;
1237 return point_index - 1;
1243static int adjacent_grid_corner_point_index_on_edge(
const SubdivCCG &subdiv_ccg,
1244 const int point_index)
1246 if (point_index == subdiv_ccg.grid_size) {
1247 return point_index - 1;
1249 return point_index + 1;
1254static void neighbor_coords_edge_get(
const SubdivCCG &subdiv_ccg,
1256 const bool include_duplicates,
1261 const int adjacent_edge_index = adjacent_edge_index_from_coord(subdiv_ccg, coord);
1266 int num_duplicates = 0;
1267 if (include_duplicates) {
1268 num_duplicates += num_adjacent_faces - 1;
1272 num_duplicates += num_adjacent_faces;
1277 const int point_index = adjacent_edge_point_index_from_coord(
1278 subdiv_ccg, coord, adjacent_edge_index);
1279 const int point_index_duplicate = adjacent_grid_corner_point_index_on_edge(subdiv_ccg,
1282 const int next_point_index = next_adjacent_edge_point_index(subdiv_ccg, point_index);
1283 const int prev_point_index = prev_adjacent_edge_point_index(subdiv_ccg, point_index);
1285 int duplicate_i = num_adjacent_faces;
1286 for (
int i = 0; i < num_adjacent_faces; ++i) {
1290 r_neighbors.coords[i + 2] = coord_step_inside_from_boundary(subdiv_ccg, grid_coord);
1292 if (grid_coord.grid_index == coord.grid_index) {
1294 r_neighbors.coords[0] = boundary_coords[prev_point_index];
1295 r_neighbors.coords[1] = boundary_coords[next_point_index];
1297 else if (include_duplicates) {
1299 r_neighbors.coords[duplicate_i + 2] = grid_coord;
1304 if (include_duplicates && is_corner) {
1305 SubdivCCGCoord duplicate_corner_grid_coord = boundary_coords[point_index_duplicate];
1306 r_neighbors.coords[duplicate_i + 2] = duplicate_corner_grid_coord;
1310 BLI_assert(duplicate_i - num_adjacent_faces == num_duplicates);
1314static void neighbor_coords_corner_edge_get(
const SubdivCCG &subdiv_ccg,
1316 const bool include_duplicates,
1319 neighbor_coords_edge_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1323static void neighbor_coords_corner_get(
const SubdivCCG &subdiv_ccg,
1325 const bool include_duplicates,
1328 if (coord.x == 0 && coord.y == 0) {
1329 neighbor_coords_corner_center_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1332 const int grid_size_1 = subdiv_ccg.grid_size - 1;
1333 if (coord.x == grid_size_1 && coord.y == grid_size_1) {
1334 neighbor_coords_corner_vertex_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1337 neighbor_coords_corner_edge_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1345static void neighbor_coords_boundary_inner_get(
const SubdivCCG &subdiv_ccg,
1347 const bool include_duplicates,
1357 r_neighbors.coords[3].grid_index = prev_grid_index_from_coord(subdiv_ccg, coord);
1358 r_neighbors.coords[3].x = coord.y;
1359 r_neighbors.coords[3].y = 1;
1361 if (include_duplicates) {
1362 r_neighbors.coords[4] = r_neighbors.coords[3];
1363 r_neighbors.coords[4].y = 0;
1366 else if (coord.y == 0) {
1371 r_neighbors.coords[3].grid_index = next_grid_index_from_coord(subdiv_ccg, coord);
1372 r_neighbors.coords[3].x = 1;
1373 r_neighbors.coords[3].y = coord.x;
1375 if (include_duplicates) {
1376 r_neighbors.coords[4] = r_neighbors.coords[3];
1377 r_neighbors.coords[4].x = 0;
1383static void neighbor_coords_boundary_outer_get(
const SubdivCCG &subdiv_ccg,
1385 const bool include_duplicates,
1388 neighbor_coords_edge_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1394static void neighbor_coords_boundary_get(
const SubdivCCG &subdiv_ccg,
1396 const bool include_duplicates,
1400 neighbor_coords_boundary_inner_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1403 neighbor_coords_boundary_outer_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1408static void neighbor_coords_inner_get(
const SubdivCCG &subdiv_ccg,
1424 const bool include_duplicates,
1427#ifdef WITH_OPENSUBDIV
1436 neighbor_coords_corner_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1439 neighbor_coords_boundary_get(subdiv_ccg, coord, include_duplicates, r_neighbors);
1442 neighbor_coords_inner_get(subdiv_ccg, coord, r_neighbors);
1451 UNUSED_VARS(subdiv_ccg, coord, include_duplicates, r_neighbors);
1462 const int grid_size_1 = subdiv_ccg.
grid_size - 1;
1468 if (coord.
x == grid_size_1) {
1470 r_v2 = corner_verts[next];
1472 if (coord.
y == grid_size_1) {
1474 r_v2 = corner_verts[prev];
1487 const int grid_size_1 = subdiv_ccg.
grid_size - 1;
1489 if (coord.
x == 0 && coord.
y == 0) {
1493 if (coord.
x == grid_size_1 && coord.
y == grid_size_1) {
1495 r_v1 = r_v2 = corner_verts[coord.
grid_index];
1507 subdiv_ccg, coord, corner_verts, faces, r_v1, r_v2);
1523 subdiv_ccg, coord, corner_verts, faces, v1,
v2);
1524 switch (adjacency) {
1526 return boundary_verts[v1];
1539 const int grid_area = subdiv_ccg.
grid_area;
1552 int &r_ptex_face_index,
1558 const float grid_size = subdiv_ccg.
grid_size;
1559 const float grid_size_1_inv = 1.0f / (grid_size - 1);
1561 const float grid_u = coord.
x * grid_size_1_inv;
1562 const float grid_v = coord.
y * grid_size_1_inv;
1568 r_ptex_face_index = face_ptex_offset[face_index];
1572 if (face.
size() == 4) {
1576 r_ptex_face_index += corner;
1577 r_u = 1.0f - grid_v;
1578 r_v = 1.0f - grid_u;
1584 const int grid_index,
1595 int ptex_face_index;
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
constexpr Span slice(int64_t start, int64_t size) const
constexpr int64_t size() const
Array(Allocator allocator={}) noexcept
BitGroupVector(Allocator allocator={}) noexcept
static IndexMask from_indices(Span< T > indices, LinearAllocator<> &memory)
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr int64_t start() const
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
bool contains(const Key &key) const
IndexRange index_range() const
void reinitialize(const int64_t new_size)
VecBase< float, 3 > float3
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
ccl_device_inline void copy_v3_v3(ccl_private float *r, const float3 val)
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)
int face_corner_prev(const IndexRange face, const int corner)
int face_find_corner_from_vert(const IndexRange face, const Span< int > corner_verts, const int vert)
int face_corner_next(const IndexRange face, const int corner)
void free(Subdiv *subdiv)
float3 eval_final_point(Subdiv *subdiv, int ptex_face_index, float u, float v)
bool eval_begin_from_mesh(Subdiv *subdiv, const Mesh *mesh, eSubdivEvaluatorType evaluator_type, Span< float3 > coarse_vert_positions={}, OpenSubdiv_EvaluatorCache *evaluator_cache=nullptr)
float3 eval_limit_point(Subdiv *subdiv, int ptex_face_index, float u, float v)
BLI_INLINE void rotate_grid_to_quad(int corner, float grid_u, float grid_v, float *r_quad_u, float *r_quad_v)
void eval_limit_point_and_normal(Subdiv *subdiv, int ptex_face_index, float u, float v, float3 &r_P, float3 &r_N)
BLI_INLINE int grid_size_from_level(int level)
@ SUBDIV_EVALUATOR_TYPE_CPU
void stats_begin(SubdivStats *stats, StatsValue value)
void stats_end(SubdivStats *stats, StatsValue value)
@ SUBDIV_STATS_SUBDIV_TO_CCG
Span< int > face_ptex_offset_get(Subdiv *subdiv)
constexpr ParallelGrainSize grain_size(int grain_size)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
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)
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
static void adjacent_vertices_index_from_adjacent_edge(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, const Span< int > corner_verts, const OffsetIndices< int > faces, int &r_v1, int &r_v2)
BLI_INLINE SubdivCCGCoord coord_at_next_row(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)
BLI_INLINE SubdivCCGCoord coord_at_prev_col(const SubdivCCG &, const SubdivCCGCoord &coord)
BitGroupVector & BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG &subdiv_ccg)
MINLINE void mul_v3_fl(float r[3], float f)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
void BKE_subdiv_ccg_grid_hidden_free(SubdivCCG &subdiv_ccg)
MINLINE int bitscan_forward_i(int a)
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, bool include_duplicates, SubdivCCGNeighbors &r_neighbors)
float normal_quad_v3(float n[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3])
BLI_INLINE bool is_inner_edge_grid_coordinate(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
void BKE_subdiv_ccg_update_normals(SubdivCCG &subdiv_ccg, const IndexMask &face_mask)
void BKE_subdiv_ccg_recalc_normals(SubdivCCG &subdiv_ccg)
BLI_INLINE SubdivCCGCoord coord_at_prev_row(const SubdivCCG &, const SubdivCCGCoord &coord)
BLI_INLINE SubdivCCGCoord coord_at_next_col(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
MINLINE void zero_v3(float r[3])
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)
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])
ATTR_WARN_UNUSED_RESULT const BMVert * v2
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)
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)
BLI_INLINE void subdiv_ccg_neighbors_init(SubdivCCGNeighbors &neighbors, const int num_unique, const int num_duplicates)
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)
ATTR_WARN_UNUSED_RESULT const void * element
void BKE_subdiv_ccg_average_stitch_faces(SubdivCCG &subdiv_ccg, const IndexMask &face_mask)
BLI_INLINE bool is_boundary_grid_coord(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
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)
BLI_INLINE bool is_corner_grid_coord(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord)
void BKE_subdiv_ccg_print_coord(const char *message, const SubdivCCGCoord &coord)
OrderedEdge(const int v1, const int v2)
Vector< Array< SubdivCCGCoord > > boundary_coords
Vector< SubdivCCGCoord > corner_coords
void(*) free(SubdivCCGMaskEvaluator *mask_evaluator)
SubdivCCGNeighborCoords coords
Array< SubdivCCGAdjacentEdge > adjacent_edges
bke::subdiv::Subdiv * subdiv
OffsetIndices< int > faces
BitGroupVector grid_hidden
Array< SubdivCCGAdjacentVertex > adjacent_verts