index-of-nearest-104619 #2

Merged
Iliya Katushenock merged 62 commits from HooglyBoogly/blender:index-of-nearest-104619 into index_of_nearest 2023-04-20 21:19:53 +02:00
Showing only changes of commit 98ccee78fe - Show all commits

View File

@ -19,7 +19,7 @@
namespace blender::geometry { namespace blender::geometry {
bke::CurvesGeometry create_curve_from_vert_indices( bke::CurvesGeometry create_curve_from_vert_indices(
const Mesh &mesh, const bke::AttributeAccessor &mesh_attributes,
const Span<int> vert_indices, const Span<int> vert_indices,
const Span<int> curve_offsets, const Span<int> curve_offsets,
const IndexRange cyclic_curves, const IndexRange cyclic_curves,
@ -30,7 +30,6 @@ bke::CurvesGeometry create_curve_from_vert_indices(
curves.offsets_for_write().last() = vert_indices.size(); curves.offsets_for_write().last() = vert_indices.size();
curves.fill_curve_types(CURVE_TYPE_POLY); curves.fill_curve_types(CURVE_TYPE_POLY);
const bke::AttributeAccessor mesh_attributes = mesh.attributes();
bke::MutableAttributeAccessor curves_attributes = curves.attributes_for_write(); bke::MutableAttributeAccessor curves_attributes = curves.attributes_for_write();
if (!cyclic_curves.is_empty()) { if (!cyclic_curves.is_empty()) {
@ -82,7 +81,7 @@ struct CurveFromEdgesOutput {
}; };
static CurveFromEdgesOutput edges_to_curve_point_indices(const int verts_num, static CurveFromEdgesOutput edges_to_curve_point_indices(const int verts_num,
Span<std::pair<int, int>> edges) const Span<int2> edges)
{ {
Vector<int> vert_indices; Vector<int> vert_indices;
vert_indices.reserve(edges.size()); vert_indices.reserve(edges.size());
@ -90,9 +89,9 @@ static CurveFromEdgesOutput edges_to_curve_point_indices(const int verts_num,
/* Compute the number of edges connecting to each vertex. */ /* Compute the number of edges connecting to each vertex. */
Array<int> neighbor_count(verts_num, 0); Array<int> neighbor_count(verts_num, 0);
for (const std::pair<int, int> &edge : edges) { for (const int2 &edge : edges) {
neighbor_count[edge.first]++; neighbor_count[edge[0]]++;
neighbor_count[edge.second]++; neighbor_count[edge[1]]++;
} }
/* Compute an offset into the array of neighbor edges based on the counts. */ /* Compute an offset into the array of neighbor edges based on the counts. */
@ -108,8 +107,8 @@ static CurveFromEdgesOutput edges_to_curve_point_indices(const int verts_num,
/* Calculate the indices of each vertex's neighboring edges. */ /* Calculate the indices of each vertex's neighboring edges. */
Array<int> neighbors(edges.size() * 2); Array<int> neighbors(edges.size() * 2);
for (const int i : edges.index_range()) { for (const int i : edges.index_range()) {
const int v1 = edges[i].first; const int v1 = edges[i][0];
const int v2 = edges[i].second; const int v2 = edges[i][1];
neighbors[neighbor_offsets[v1] + used_slots[v1]] = v2; neighbors[neighbor_offsets[v1] + used_slots[v1]] = v2;
neighbors[neighbor_offsets[v2] + used_slots[v2]] = v1; neighbors[neighbor_offsets[v2] + used_slots[v2]] = v1;
used_slots[v1]++; used_slots[v1]++;
@ -199,19 +198,17 @@ static CurveFromEdgesOutput edges_to_curve_point_indices(const int verts_num,
return {std::move(vert_indices), std::move(curve_offsets), cyclic_curves}; return {std::move(vert_indices), std::move(curve_offsets), cyclic_curves};
} }
/** static bke::CurvesGeometry edges_to_curves_convert(
* Get a separate array of the indices for edges in a selection (a boolean attribute). const Mesh &mesh,
* This helps to make the above algorithm simpler by removing the need to check for selection const Span<int2> edges,
* in many places. const bke::AnonymousAttributePropagationInfo &propagation_info)
*/
static Vector<std::pair<int, int>> get_selected_edges(const Mesh &mesh, const IndexMask selection)
{ {
Vector<std::pair<int, int>> selected_edges; CurveFromEdgesOutput output = edges_to_curve_point_indices(mesh.totvert, edges);
const Span<int2> edges = mesh.edges(); return create_curve_from_vert_indices(mesh.attributes(),
for (const int i : selection) { output.vert_indices,
selected_edges.append({edges[i][0], edges[i][1]}); output.curve_offsets,
} output.cyclic_curves,
return selected_edges; propagation_info);
} }
bke::CurvesGeometry mesh_to_curve_convert( bke::CurvesGeometry mesh_to_curve_convert(
@ -219,11 +216,13 @@ bke::CurvesGeometry mesh_to_curve_convert(
const IndexMask selection, const IndexMask selection,
const bke::AnonymousAttributePropagationInfo &propagation_info) const bke::AnonymousAttributePropagationInfo &propagation_info)
{ {
Vector<std::pair<int, int>> selected_edges = get_selected_edges(mesh, selection); const Span<int2> edges = mesh.edges();
CurveFromEdgesOutput output = edges_to_curve_point_indices(mesh.totvert, selected_edges); if (selection.size() == edges.size()) {
return edges_to_curves_convert(mesh, edges, propagation_info);
return create_curve_from_vert_indices( }
mesh, output.vert_indices, output.curve_offsets, output.cyclic_curves, propagation_info); Array<int2> selected_edges(selection.size());
array_utils::gather(edges, selection, selected_edges.as_mutable_span());
return edges_to_curves_convert(mesh, selected_edges, propagation_info);
} }
} // namespace blender::geometry } // namespace blender::geometry