151 const int target_start,
152 const int target_verts_num,
153 const int source_start,
154 const int source_verts_num,
158 int i_source, i_target, i_target_low_bound, target_end, source_end;
159 SortVertsElem *sve_source, *sve_target, *sve_target_low_bound;
160 bool target_scan_completed;
162 target_end = target_start + target_verts_num;
163 source_end = source_start + source_verts_num;
167 size_t(target_verts_num), __func__);
169 size_t(source_verts_num), __func__);
172 svert_from_mvert(sorted_verts_target, vert_positions, target_start, target_end);
175 svert_from_mvert(sorted_verts_source, vert_positions, source_start, source_end);
181 sve_target_low_bound = sorted_verts_target;
182 i_target_low_bound = 0;
183 target_scan_completed =
false;
187 for (i_source = 0, sve_source = sorted_verts_source; i_source < source_verts_num;
188 i_source++, sve_source++)
190 int best_target_vertex = -1;
191 float best_dist_sq = dist * dist;
192 float sve_source_sumco;
195 if (doubles_map[sve_source->
vertex_num] != -1) {
200 if (target_scan_completed) {
205 sve_source_sumco =
sum_v3(sve_source->
co);
209 while ((i_target_low_bound < target_verts_num) &&
210 (sve_target_low_bound->
sum_co < sve_source_sumco - dist3))
212 i_target_low_bound++;
213 sve_target_low_bound++;
216 if (i_target_low_bound >= target_verts_num) {
218 target_scan_completed =
true;
223 i_target = i_target_low_bound;
224 sve_target = sve_target_low_bound;
229 while ((i_target < target_verts_num) && (sve_target->
sum_co <= sve_source_sumco + dist3)) {
233 if (dist_sq <= best_dist_sq) {
235 best_dist_sq = dist_sq;
243 while (best_target_vertex != -1 &&
244 !
ELEM(doubles_map[best_target_vertex], -1, best_target_vertex))
247 vert_positions[doubles_map[best_target_vertex]],
250 best_target_vertex = doubles_map[best_target_vertex];
253 best_target_vertex = -1;
261 doubles_map[sve_source->
vertex_num] = best_target_vertex;
264 MEM_delete(sorted_verts_source);
265 MEM_delete(sorted_verts_target);
270 const float cap_offset[4][4],
271 uint cap_verts_index,
272 uint cap_edges_index,
286 const Span<int> cap_face_offsets = cap_mesh->face_offsets();
299 vert_interp.
copy(0, cap_verts_index, cap_nverts);
300 edge_interp.
copy(0, cap_edges_index, cap_nedges);
301 face_interp.
copy(0, cap_faces_index, cap_nfaces);
302 corner_interp.
copy(0, cap_loops_index, cap_nloops);
304 for (i = 0; i < cap_nverts; i++) {
305 mul_m4_v3(cap_offset, result_positions[cap_verts_index + i]);
310 for (i = 0; i < cap_nverts; i++) {
311 mul_mat3_m4_v3(cap_offset, dst_vert_normals[cap_verts_index + i]);
317 if (!
result->deform_verts().is_empty()) {
323 edge = &result_edges[cap_edges_index];
324 for (i = 0; i < cap_nedges; i++, edge++) {
325 (*edge) += cap_verts_index;
329 for (i = 0; i < cap_nfaces; i++) {
330 result_face_offsets[cap_faces_index + i] = cap_face_offsets[i] + cap_loops_index;
334 for (i = 0; i < cap_nloops; i++) {
335 result_corner_verts[cap_loops_index + i] += cap_verts_index;
336 result_corner_edges[cap_loops_index + i] += cap_edges_index;
340 if (
const VArray cap_material_indices = *cap_attributes.
lookup<
int>(
"material_index",
346 cap_material_indices.materialize(
347 result_material_indices.
span.slice(cap_faces_index, cap_nfaces));
348 result_material_indices.
finish();
352 index_orig =
static_cast<int *
>(
355 std::fill_n(index_orig + cap_verts_index, cap_nverts,
ORIGINDEX_NONE);
358 index_orig =
static_cast<int *
>(
361 std::fill_n(index_orig + cap_edges_index, cap_nedges,
ORIGINDEX_NONE);
364 index_orig =
static_cast<int *
>(
367 std::fill_n(index_orig + cap_faces_index, cap_nfaces,
ORIGINDEX_NONE);
370 index_orig =
static_cast<int *
>(
373 std::fill_n(index_orig + cap_loops_index, cap_nloops,
ORIGINDEX_NONE);
392 bool offset_has_scale;
393 float current_offset[4][4];
394 float final_offset[4][4];
395 int *full_doubles_map =
nullptr;
402 int start_cap_nverts = 0, start_cap_nedges = 0, start_cap_nfaces = 0, start_cap_nloops = 0;
403 int end_cap_nverts = 0, end_cap_nedges = 0, end_cap_nfaces = 0, end_cap_nloops = 0;
404 int result_nverts = 0, result_nedges = 0, result_nfaces = 0, result_nloops = 0;
405 int chunk_nverts, chunk_nedges, chunk_nloops, chunk_nfaces;
406 int first_chunk_start, first_chunk_nverts, last_chunk_start, last_chunk_nverts;
408 Mesh *
result, *start_cap_mesh =
nullptr, *end_cap_mesh =
nullptr;
410 int *vgroup_start_cap_remap =
nullptr;
411 int vgroup_start_cap_remap_len = 0;
412 int *vgroup_end_cap_remap =
nullptr;
413 int vgroup_end_cap_remap_len = 0;
415 chunk_nverts =
mesh->verts_num;
416 chunk_nedges =
mesh->edges_num;
417 chunk_nloops =
mesh->corners_num;
418 chunk_nfaces =
mesh->faces_num;
423 if (start_cap_ob && start_cap_ob != ctx->
object) {
426 start_cap_ob, ctx->
object, &vgroup_start_cap_remap_len);
430 if (start_cap_mesh) {
432 start_cap_nverts = start_cap_mesh->
verts_num;
433 start_cap_nedges = start_cap_mesh->
edges_num;
435 start_cap_nfaces = start_cap_mesh->
faces_num;
439 if (end_cap_ob && end_cap_ob != ctx->
object) {
442 end_cap_ob, ctx->
object, &vgroup_end_cap_remap_len);
448 end_cap_nverts = end_cap_mesh->verts_num;
449 end_cap_nedges = end_cap_mesh->edges_num;
450 end_cap_nloops = end_cap_mesh->corners_num;
451 end_cap_nfaces = end_cap_mesh->faces_num;
473 float result_mat[4][4];
494 float scale_fac =
mat4_to_scale(curve_ob->object_to_world().ptr());
500 const size_t max_verts_num = 1 << 26;
505 const float float_epsilon = 1e-6f;
506 bool offset_is_too_small =
false;
507 float dist =
len_v3(offset[3]);
509 if (dist > float_epsilon) {
512 count = (
length + float_epsilon) / dist + 1;
517 if ((
size_t(count) *
size_t(chunk_nverts) +
size_t(start_cap_nverts) +
518 size_t(end_cap_nverts)) > max_verts_num)
521 offset_is_too_small =
true;
527 offset_is_too_small =
true;
530 if (offset_is_too_small) {
534 "The offset is too small, we cannot generate the amount of geometry it would require");
540 else if ((
size_t(count) *
size_t(chunk_nverts) +
size_t(start_cap_nverts) +
541 size_t(end_cap_nverts)) > max_verts_num)
546 "The amount of copies is too high, we cannot generate the amount of "
547 "geometry it would require");
550 count = std::max(count, 1);
553 result_nverts = chunk_nverts * count + start_cap_nverts + end_cap_nverts;
554 result_nedges = chunk_nedges * count + start_cap_nedges + end_cap_nedges;
555 result_nloops = chunk_nloops * count + start_cap_nloops + end_cap_nloops;
556 result_nfaces = chunk_nfaces * count + start_cap_nfaces + end_cap_nfaces;
560 mesh, result_nverts, result_nedges, result_nfaces, result_nloops);
571 std::fill_n(full_doubles_map, result_nverts, -1);
579 vert_interp.
copy(0, 0, chunk_nverts);
580 edge_interp.
copy(0, 0, chunk_nedges);
581 face_interp.
copy(0, 0, chunk_nfaces);
582 corner_interp.
copy(0, 0, chunk_nloops);
587 first_chunk_start = 0;
588 first_chunk_nverts = chunk_nverts;
593 if (!use_recalc_normals) {
594 src_vert_normals =
mesh->vert_normals();
597 .take_front(src_vert_normals.
size())
598 .copy_from(src_vert_normals);
601 for (c = 1; c < count; c++) {
603 vert_interp.
copy(0, c * chunk_nverts, chunk_nverts);
604 edge_interp.
copy(0, c * chunk_nedges, chunk_nedges);
605 face_interp.
copy(0, c * chunk_nfaces, chunk_nfaces);
606 corner_interp.
copy(0, c * chunk_nloops, chunk_nloops);
609 mul_m4_m4m4(current_offset, current_offset, offset);
613 const int vert_offset = c * chunk_nverts;
614 for (i = 0; i < chunk_nverts; i++) {
615 const int i_dst = vert_offset + i;
616 mul_m4_v3(current_offset, result_positions[i_dst]);
620 copy_v3_v3(dst_vert_normals[i_dst], src_vert_normals[i]);
629 edge = &result_edges[c * chunk_nedges];
630 for (i = 0; i < chunk_nedges; i++, edge++) {
631 (*edge) += c * chunk_nverts;
636 for (i = 0; i < chunk_nfaces; i++) {
637 result_face_offsets[c * chunk_nfaces + i] = result_face_offsets[i] + c * chunk_nloops;
643 const int chunk_corner_start = c * chunk_nloops;
644 for (i = 0; i < chunk_nloops; i++) {
645 result_corner_verts[chunk_corner_start + i] += c * chunk_nverts;
646 result_corner_edges[chunk_corner_start + i] += c * chunk_nedges;
651 if (use_merge && (c >= 1)) {
652 if (!offset_has_scale && (c >= 2)) {
656 int this_chunk_index = c * chunk_nverts;
657 int prev_chunk_index = (c - 1) * chunk_nverts;
658 for (k = 0; k < chunk_nverts; k++, this_chunk_index++, prev_chunk_index++) {
659 int target = full_doubles_map[prev_chunk_index];
661 target += chunk_nverts;
662 while (target != -1 && !
ELEM(full_doubles_map[target], -1, target)) {
666 result_positions[full_doubles_map[target]],
669 target = full_doubles_map[target];
676 full_doubles_map[this_chunk_index] = target;
682 (c - 1) * chunk_nverts,
694 for (i = 0; i < uv_map_names.
size(); i++) {
698 uv_map += chunk_nloops;
699 for (c = 1; c < count; c++) {
700 const float uv_offset[2] = {
704 int l_index = chunk_nloops;
705 for (; l_index-- != 0; uv_map++) {
706 (*uv_map)[0] += uv_offset[0];
707 (*uv_map)[1] += uv_offset[1];
714 if (!use_merge && !
mesh->runtime->subsurf_optimal_display_edges.is_empty()) {
717 result->runtime->subsurf_optimal_display_edges.resize(
result->edges_num);
720 dst.
slice({i *
mesh->edges_num,
mesh->edges_num}).copy_from(src);
723 if (start_cap_mesh) {
725 {result_nedges - start_cap_nedges - end_cap_nedges, start_cap_mesh->
edges_num});
735 {result_nedges - end_cap_nedges, end_cap_mesh->edges_num});
736 if (end_cap_mesh->runtime->subsurf_optimal_display_edges.is_empty()) {
740 cap_bits.
copy_from(end_cap_mesh->runtime->subsurf_optimal_display_edges);
745 last_chunk_start = (count - 1) * chunk_nverts;
746 last_chunk_nverts = chunk_nverts;
762 if (start_cap_mesh) {
763 float start_offset[4][4];
764 int start_cap_start = result_nverts - start_cap_nverts - end_cap_nverts;
769 result_nverts - start_cap_nverts - end_cap_nverts,
770 result_nedges - start_cap_nedges - end_cap_nedges,
771 result_nloops - start_cap_nloops - end_cap_nloops,
772 result_nfaces - start_cap_nfaces - end_cap_nfaces,
777 vgroup_start_cap_remap,
778 vgroup_start_cap_remap_len,
793 float end_offset[4][4];
794 int end_cap_start = result_nverts - end_cap_nverts;
799 result_nverts - end_cap_nverts,
800 result_nedges - end_cap_nedges,
801 result_nloops - end_cap_nloops,
802 result_nfaces - end_cap_nfaces,
807 vgroup_end_cap_remap,
808 vgroup_end_cap_remap_len,
830 for (i = 0; i < result_nverts; i++) {
831 int new_i = full_doubles_map[i];
836 while (!
ELEM(full_doubles_map[new_i], -1, new_i)) {
837 new_i = full_doubles_map[new_i];
840 full_doubles_map[i] = -1;
843 full_doubles_map[i] = new_i;
848 if (tot_doubles > 0) {
850 for (i = 0; i < result_nverts; i++) {
851 if (full_doubles_map[i] == -1) {
852 full_doubles_map[i] = i;
857 *tmp,
Span<int>{full_doubles_map,
result->verts_num}, tot_doubles,
false);
860 MEM_delete(full_doubles_map);
863 if (vgroup_start_cap_remap) {
864 MEM_delete(vgroup_start_cap_remap);
866 if (vgroup_end_cap_remap) {
867 MEM_delete(vgroup_end_cap_remap);