Cleanup: Avoid adding points to splines sequentially
This should be faster because it avoids reallocating the internal vectors when the size is known beforehand, but it may also help a potential refactor to a different data structure (see T94193).
This commit is contained in:
@@ -79,44 +79,29 @@ static std::unique_ptr<CurveEval> create_bezier_segment_curve(
|
|||||||
{
|
{
|
||||||
std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
|
std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
|
||||||
std::unique_ptr<BezierSpline> spline = std::make_unique<BezierSpline>();
|
std::unique_ptr<BezierSpline> spline = std::make_unique<BezierSpline>();
|
||||||
|
spline->set_resolution(resolution);
|
||||||
|
|
||||||
|
spline->resize(2);
|
||||||
|
MutableSpan<float3> positions = spline->positions();
|
||||||
|
spline->handle_types_left().fill(BezierSpline::HandleType::Align);
|
||||||
|
spline->handle_types_right().fill(BezierSpline::HandleType::Align);
|
||||||
|
spline->radii().fill(1.0f);
|
||||||
|
spline->tilts().fill(0.0f);
|
||||||
|
|
||||||
|
positions.first() = start;
|
||||||
|
positions.last() = end;
|
||||||
|
|
||||||
if (mode == GEO_NODE_CURVE_PRIMITIVE_BEZIER_SEGMENT_POSITION) {
|
if (mode == GEO_NODE_CURVE_PRIMITIVE_BEZIER_SEGMENT_POSITION) {
|
||||||
spline->add_point(start,
|
spline->set_handle_position_right(0, start_handle_right);
|
||||||
BezierSpline::HandleType::Align,
|
spline->set_handle_position_left(1, end_handle_left);
|
||||||
2.0f * start - start_handle_right,
|
|
||||||
BezierSpline::HandleType::Align,
|
|
||||||
start_handle_right,
|
|
||||||
1.0f,
|
|
||||||
0.0f);
|
|
||||||
spline->add_point(end,
|
|
||||||
BezierSpline::HandleType::Align,
|
|
||||||
end_handle_left,
|
|
||||||
BezierSpline::HandleType::Align,
|
|
||||||
2.0f * end - end_handle_left,
|
|
||||||
1.0f,
|
|
||||||
0.0f);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
spline->add_point(start,
|
spline->set_handle_position_right(0, start + start_handle_right);
|
||||||
BezierSpline::HandleType::Align,
|
spline->set_handle_position_left(1, end + end_handle_left);
|
||||||
start - start_handle_right,
|
|
||||||
BezierSpline::HandleType::Align,
|
|
||||||
start + start_handle_right,
|
|
||||||
1.0f,
|
|
||||||
0.0f);
|
|
||||||
spline->add_point(end,
|
|
||||||
BezierSpline::HandleType::Align,
|
|
||||||
end + end_handle_left,
|
|
||||||
BezierSpline::HandleType::Align,
|
|
||||||
end - end_handle_left,
|
|
||||||
1.0f,
|
|
||||||
0.0f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
spline->set_resolution(resolution);
|
|
||||||
spline->attributes.reallocate(spline->size());
|
|
||||||
curve->add_spline(std::move(spline));
|
curve->add_spline(std::move(spline));
|
||||||
curve->attributes.reallocate(curve->splines().size());
|
curve->attributes.reallocate(1);
|
||||||
return curve;
|
return curve;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,15 +50,19 @@ static std::unique_ptr<CurveEval> create_quadratic_bezier_curve(const float3 p1,
|
|||||||
std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
|
std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
|
||||||
std::unique_ptr<PolySpline> spline = std::make_unique<PolySpline>();
|
std::unique_ptr<PolySpline> spline = std::make_unique<PolySpline>();
|
||||||
|
|
||||||
|
spline->resize(resolution + 1);
|
||||||
|
MutableSpan<float3> positions = spline->positions();
|
||||||
|
spline->radii().fill(1.0f);
|
||||||
|
spline->tilts().fill(0.0f);
|
||||||
|
|
||||||
const float step = 1.0f / resolution;
|
const float step = 1.0f / resolution;
|
||||||
for (int i : IndexRange(resolution + 1)) {
|
for (const int i : IndexRange(resolution + 1)) {
|
||||||
const float factor = step * i;
|
const float factor = step * i;
|
||||||
const float3 q1 = float3::interpolate(p1, p2, factor);
|
const float3 q1 = float3::interpolate(p1, p2, factor);
|
||||||
const float3 q2 = float3::interpolate(p2, p3, factor);
|
const float3 q2 = float3::interpolate(p2, p3, factor);
|
||||||
const float3 out = float3::interpolate(q1, q2, factor);
|
positions[i] = float3::interpolate(q1, q2, factor);
|
||||||
spline->add_point(out, 1.0f, 0.0f);
|
|
||||||
}
|
}
|
||||||
spline->attributes.reallocate(spline->size());
|
|
||||||
curve->add_spline(std::move(spline));
|
curve->add_spline(std::move(spline));
|
||||||
curve->attributes.reallocate(curve->splines().size());
|
curve->attributes.reallocate(curve->splines().size());
|
||||||
return curve;
|
return curve;
|
||||||
|
|||||||
@@ -65,6 +65,11 @@ static std::unique_ptr<CurveEval> create_spiral_curve(const float rotations,
|
|||||||
const float delta_theta = (M_PI * 2 * rotations) / (float)totalpoints *
|
const float delta_theta = (M_PI * 2 * rotations) / (float)totalpoints *
|
||||||
(direction ? 1.0f : -1.0f);
|
(direction ? 1.0f : -1.0f);
|
||||||
|
|
||||||
|
spline->resize(totalpoints + 1);
|
||||||
|
MutableSpan<float3> positions = spline->positions();
|
||||||
|
spline->radii().fill(1.0f);
|
||||||
|
spline->tilts().fill(0.0f);
|
||||||
|
|
||||||
for (const int i : IndexRange(totalpoints + 1)) {
|
for (const int i : IndexRange(totalpoints + 1)) {
|
||||||
const float theta = i * delta_theta;
|
const float theta = i * delta_theta;
|
||||||
const float radius = start_radius + i * delta_radius;
|
const float radius = start_radius + i * delta_radius;
|
||||||
@@ -72,10 +77,9 @@ static std::unique_ptr<CurveEval> create_spiral_curve(const float rotations,
|
|||||||
const float y = radius * sin(theta);
|
const float y = radius * sin(theta);
|
||||||
const float z = delta_height * i;
|
const float z = delta_height * i;
|
||||||
|
|
||||||
spline->add_point(float3(x, y, z), 1.0f, 0.0f);
|
positions[i] = {x, y, z};
|
||||||
}
|
}
|
||||||
|
|
||||||
spline->attributes.reallocate(spline->size());
|
|
||||||
curve->add_spline(std::move(spline));
|
curve->add_spline(std::move(spline));
|
||||||
curve->attributes.reallocate(curve->splines().size());
|
curve->attributes.reallocate(curve->splines().size());
|
||||||
return curve;
|
return curve;
|
||||||
|
|||||||
@@ -54,19 +54,24 @@ static std::unique_ptr<CurveEval> create_star_curve(const float inner_radius,
|
|||||||
{
|
{
|
||||||
std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
|
std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
|
||||||
std::unique_ptr<PolySpline> spline = std::make_unique<PolySpline>();
|
std::unique_ptr<PolySpline> spline = std::make_unique<PolySpline>();
|
||||||
|
spline->set_cyclic(true);
|
||||||
|
|
||||||
|
spline->resize(points * 2);
|
||||||
|
MutableSpan<float3> positions = spline->positions();
|
||||||
|
spline->radii().fill(1.0f);
|
||||||
|
spline->tilts().fill(0.0f);
|
||||||
|
|
||||||
const float theta_step = (2.0f * M_PI) / float(points);
|
const float theta_step = (2.0f * M_PI) / float(points);
|
||||||
for (int i : IndexRange(points)) {
|
for (const int i : IndexRange(points)) {
|
||||||
const float x = outer_radius * cos(theta_step * i);
|
const float x = outer_radius * cos(theta_step * i);
|
||||||
const float y = outer_radius * sin(theta_step * i);
|
const float y = outer_radius * sin(theta_step * i);
|
||||||
spline->add_point(float3(x, y, 0.0f), 1.0f, 0.0f);
|
positions[i * 2] = {x, y, 0.0f};
|
||||||
|
|
||||||
const float inner_x = inner_radius * cos(theta_step * i + theta_step * 0.5f + twist);
|
const float inner_x = inner_radius * cos(theta_step * i + theta_step * 0.5f + twist);
|
||||||
const float inner_y = inner_radius * sin(theta_step * i + theta_step * 0.5f + twist);
|
const float inner_y = inner_radius * sin(theta_step * i + theta_step * 0.5f + twist);
|
||||||
spline->add_point(float3(inner_x, inner_y, 0.0f), 1.0f, 0.0f);
|
positions[i * 2 + 1] = {inner_x, inner_y, 0.0f};
|
||||||
}
|
}
|
||||||
spline->set_cyclic(true);
|
|
||||||
spline->attributes.reallocate(spline->size());
|
|
||||||
curve->add_spline(std::move(spline));
|
curve->add_spline(std::move(spline));
|
||||||
curve->attributes.reallocate(curve->splines().size());
|
curve->attributes.reallocate(curve->splines().size());
|
||||||
|
|
||||||
|
|||||||
@@ -81,8 +81,11 @@ static SplinePtr resample_spline(const Spline &src, const int count)
|
|||||||
Spline::copy_base_settings(src, *dst);
|
Spline::copy_base_settings(src, *dst);
|
||||||
|
|
||||||
if (src.evaluated_edges_size() < 1 || count == 1) {
|
if (src.evaluated_edges_size() < 1 || count == 1) {
|
||||||
dst->add_point(src.positions().first(), src.radii().first(), src.tilts().first());
|
dst->resize(1);
|
||||||
dst->attributes.reallocate(1);
|
dst->positions().first() = src.positions().first();
|
||||||
|
dst->radii().first() = src.radii().first();
|
||||||
|
dst->tilts().first() = src.tilts().first();
|
||||||
|
|
||||||
src.attributes.foreach_attribute(
|
src.attributes.foreach_attribute(
|
||||||
[&](const AttributeIDRef &attribute_id, const AttributeMetaData &meta_data) {
|
[&](const AttributeIDRef &attribute_id, const AttributeMetaData &meta_data) {
|
||||||
std::optional<GSpan> src_attribute = src.attributes.get_for_read(attribute_id);
|
std::optional<GSpan> src_attribute = src.attributes.get_for_read(attribute_id);
|
||||||
|
|||||||
Reference in New Issue
Block a user