Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_geo_curve_trim.cc « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 443f67be42170a7e2964f1166df35dc15b5003a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "BKE_curves.hh"
#include "BKE_spline.hh"
#include "BLI_task.hh"

#include "UI_interface.h"
#include "UI_resources.h"

#include "NOD_socket_search_link.hh"

#include "node_geometry_util.hh"

namespace blender::nodes::node_geo_curve_trim_cc {

using blender::attribute_math::mix2;

NODE_STORAGE_FUNCS(NodeGeometryCurveTrim)

static void node_declare(NodeDeclarationBuilder &b)
{
  b.add_input<decl::Geometry>(N_("Curve")).supported_type(GEO_COMPONENT_TYPE_CURVE);
  b.add_input<decl::Float>(N_("Start"))
      .min(0.0f)
      .max(1.0f)
      .subtype(PROP_FACTOR)
      .make_available([](bNode &node) { node_storage(node).mode = GEO_NODE_CURVE_SAMPLE_FACTOR; })
      .supports_field();
  b.add_input<decl::Float>(N_("End"))
      .min(0.0f)
      .max(1.0f)
      .default_value(1.0f)
      .subtype(PROP_FACTOR)
      .make_available([](bNode &node) { node_storage(node).mode = GEO_NODE_CURVE_SAMPLE_FACTOR; })
      .supports_field();
  b.add_input<decl::Float>(N_("Start"), "Start_001")
      .min(0.0f)
      .subtype(PROP_DISTANCE)
      .make_available([](bNode &node) { node_storage(node).mode = GEO_NODE_CURVE_SAMPLE_LENGTH; })
      .supports_field();
  b.add_input<decl::Float>(N_("End"), "End_001")
      .min(0.0f)
      .default_value(1.0f)
      .subtype(PROP_DISTANCE)
      .make_available([](bNode &node) { node_storage(node).mode = GEO_NODE_CURVE_SAMPLE_LENGTH; })
      .supports_field();
  b.add_output<decl::Geometry>(N_("Curve"));
}

static void node_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
  uiItemR(layout, ptr, "mode", UI_ITEM_R_EXPAND, nullptr, ICON_NONE);
}

static void node_init(bNodeTree *UNUSED(tree), bNode *node)
{
  NodeGeometryCurveTrim *data = MEM_cnew<NodeGeometryCurveTrim>(__func__);

  data->mode = GEO_NODE_CURVE_SAMPLE_FACTOR;
  node->storage = data;
}

static void node_update(bNodeTree *ntree, bNode *node)
{
  const NodeGeometryCurveTrim &storage = node_storage(*node);
  const GeometryNodeCurveSampleMode mode = (GeometryNodeCurveSampleMode)storage.mode;

  bNodeSocket *start_fac = ((bNodeSocket *)node->inputs.first)->next;
  bNodeSocket *end_fac = start_fac->next;
  bNodeSocket *start_len = end_fac->next;
  bNodeSocket *end_len = start_len->next;

  nodeSetSocketAvailability(ntree, start_fac, mode == GEO_NODE_CURVE_SAMPLE_FACTOR);
  nodeSetSocketAvailability(ntree, end_fac, mode == GEO_NODE_CURVE_SAMPLE_FACTOR);
  nodeSetSocketAvailability(ntree, start_len, mode == GEO_NODE_CURVE_SAMPLE_LENGTH);
  nodeSetSocketAvailability(ntree, end_len, mode == GEO_NODE_CURVE_SAMPLE_LENGTH);
}

class SocketSearchOp {
 public:
  StringRef socket_name;
  GeometryNodeCurveSampleMode mode;
  void operator()(LinkSearchOpParams &params)
  {
    bNode &node = params.add_node("GeometryNodeTrimCurve");
    node_storage(node).mode = mode;
    params.update_and_connect_available_socket(node, socket_name);
  }
};

static void node_gather_link_searches(GatherLinkSearchOpParams &params)
{
  const NodeDeclaration &declaration = *params.node_type().fixed_declaration;

  search_link_ops_for_declarations(params, declaration.outputs());
  search_link_ops_for_declarations(params, declaration.inputs().take_front(1));

  if (params.in_out() == SOCK_IN) {
    if (params.node_tree().typeinfo->validate_link(
            static_cast<eNodeSocketDatatype>(params.other_socket().type), SOCK_FLOAT)) {
      params.add_item(IFACE_("Start (Factor)"),
                      SocketSearchOp{"Start", GEO_NODE_CURVE_SAMPLE_FACTOR});
      params.add_item(IFACE_("End (Factor)"), SocketSearchOp{"End", GEO_NODE_CURVE_SAMPLE_FACTOR});
      params.add_item(IFACE_("Start (Length)"),
                      SocketSearchOp{"Start", GEO_NODE_CURVE_SAMPLE_LENGTH});
      params.add_item(IFACE_("End (Length)"), SocketSearchOp{"End", GEO_NODE_CURVE_SAMPLE_LENGTH});
    }
  }
}

struct TrimLocation {
  /* Control point index at the start side of the trim location. */
  int left_index;
  /* Control point index at the end of the trim location's segment. */
  int right_index;
  /* The factor between the left and right indices. */
  float factor;
};

template<typename T>
static void shift_slice_to_start(MutableSpan<T> data, const int start_index, const int num)
{
  BLI_assert(start_index + num - 1 <= data.size());
  memmove(data.data(), &data[start_index], sizeof(T) * num);
}

/* Shift slice to start of span and modifies start and end data. */
template<typename T>
static void linear_trim_data(const TrimLocation &start,
                             const TrimLocation &end,
                             MutableSpan<T> data)
{
  const int num = end.right_index - start.left_index + 1;

  if (start.left_index > 0) {
    shift_slice_to_start<T>(data, start.left_index, num);
  }

  const T start_data = mix2<T>(start.factor, data.first(), data[1]);
  const T end_data = mix2<T>(end.factor, data[num - 2], data[num - 1]);

  data.first() = start_data;
  data[num - 1] = end_data;
}

/**
 * Identical operation as #linear_trim_data, but copy data to a new #MutableSpan rather than
 * modifying the original data.
 */
template<typename T>
static void linear_trim_to_output_data(const TrimLocation &start,
                                       const TrimLocation &end,
                                       Span<T> src,
                                       MutableSpan<T> dst)
{
  const int num = end.right_index - start.left_index + 1;

  const T start_data = mix2<T>(start.factor, src[start.left_index], src[start.right_index]);
  const T end_data = mix2<T>(end.factor, src[end.left_index], src[end.right_index]);

  dst.copy_from(src.slice(start.left_index, num));
  dst.first() = start_data;
  dst.last() = end_data;
}

/* Look up the control points to the left and right of factor, and get the factor between them. */
static TrimLocation lookup_control_point_position(const Spline::LookupResult &lookup,
                                                  const BezierSpline &spline)
{
  Span<int> offsets = spline.control_point_offsets();

  const int *offset = std::lower_bound(offsets.begin(), offsets.end(), lookup.evaluated_index);
  const int index = offset - offsets.begin();

  const int left = offsets[index] > lookup.evaluated_index ? index - 1 : index;
  const int right = left == (spline.size() - 1) ? 0 : left + 1;

  const float offset_in_segment = lookup.evaluated_index + lookup.factor - offsets[left];
  const int segment_eval_num = offsets[left + 1] - offsets[left];
  const float factor = std::clamp(offset_in_segment / segment_eval_num, 0.0f, 1.0f);

  return {left, right, factor};
}

static void trim_poly_spline(Spline &spline,
                             const Spline::LookupResult &start_lookup,
                             const Spline::LookupResult &end_lookup)
{
  /* Poly splines have a 1 to 1 mapping between control points and evaluated points. */
  const TrimLocation start = {
      start_lookup.evaluated_index, start_lookup.next_evaluated_index, start_lookup.factor};
  const TrimLocation end = {
      end_lookup.evaluated_index, end_lookup.next_evaluated_index, end_lookup.factor};

  const int num = end.right_index - start.left_index + 1;

  linear_trim_data<float3>(start, end, spline.positions());
  linear_trim_data<float>(start, end, spline.radii());
  linear_trim_data<float>(start, end, spline.tilts());

  spline.attributes.foreach_attribute(
      [&](const AttributeIDRef &attribute_id, const AttributeMetaData &UNUSED(meta_data)) {
        std::optional<GMutableSpan> src = spline.attributes.get_for_write(attribute_id);
        BLI_assert(src);
        attribute_math::convert_to_static_type(src->type(), [&](auto dummy) {
          using T = decltype(dummy);
          linear_trim_data<T>(start, end, src->typed<T>());
        });
        return true;
      },
      ATTR_DOMAIN_POINT);

  spline.resize(num);
}

/**
 * Trim NURB splines by converting to a poly spline.
 */
static PolySpline trim_nurbs_spline(const Spline &spline,
                                    const Spline::LookupResult &start_lookup,
                                    const Spline::LookupResult &end_lookup)
{
  /* Since this outputs a poly spline, the evaluated indices are the control point indices. */
  const TrimLocation start = {
      start_lookup.evaluated_index, start_lookup.next_evaluated_index, start_lookup.factor};
  const TrimLocation end = {
      end_lookup.evaluated_index, end_lookup.next_evaluated_index, end_lookup.factor};

  const int num = end.right_index - start.left_index + 1;

  /* Create poly spline and copy trimmed data to it. */
  PolySpline new_spline;
  new_spline.resize(num);

  /* Copy generic attribute data. */
  spline.attributes.foreach_attribute(
      [&](const AttributeIDRef &attribute_id, const AttributeMetaData &meta_data) {
        std::optional<GSpan> src = spline.attributes.get_for_read(attribute_id);
        BLI_assert(src);
        if (!new_spline.attributes.create(attribute_id, meta_data.data_type)) {
          BLI_assert_unreachable();
          return false;
        }
        std::optional<GMutableSpan> dst = new_spline.attributes.get_for_write(attribute_id);
        BLI_assert(dst);

        attribute_math::convert_to_static_type(src->type(), [&](auto dummy) {
          using T = decltype(dummy);
          VArray<T> eval_data = spline.interpolate_to_evaluated<T>(src->typed<T>());
          linear_trim_to_output_data<T>(
              start, end, eval_data.get_internal_span(), dst->typed<T>());
        });
        return true;
      },
      ATTR_DOMAIN_POINT);

  linear_trim_to_output_data<float3>(
      start, end, spline.evaluated_positions(), new_spline.positions());

  VArray<float> evaluated_radii = spline.interpolate_to_evaluated(spline.radii());
  linear_trim_to_output_data<float>(
      start, end, evaluated_radii.get_internal_span(), new_spline.radii());

  VArray<float> evaluated_tilts = spline.interpolate_to_evaluated(spline.tilts());
  linear_trim_to_output_data<float>(
      start, end, evaluated_tilts.get_internal_span(), new_spline.tilts());

  return new_spline;
}

/**
 * Trim Bezier splines by adjusting the first and last handles
 * and control points to maintain the original shape.
 */
static void trim_bezier_spline(Spline &spline,
                               const Spline::LookupResult &start_lookup,
                               const Spline::LookupResult &end_lookup)
{
  BezierSpline &bezier_spline = static_cast<BezierSpline &>(spline);

  const TrimLocation start = lookup_control_point_position(start_lookup, bezier_spline);
  TrimLocation end = lookup_control_point_position(end_lookup, bezier_spline);

  const Span<int> control_offsets = bezier_spline.control_point_offsets();

  /* The number of control points in the resulting spline. */
  const int num = end.right_index - start.left_index + 1;

  /* Trim the spline attributes. Done before end.factor recalculation as it needs
   * the original end.factor value. */
  linear_trim_data<float>(start, end, bezier_spline.radii());
  linear_trim_data<float>(start, end, bezier_spline.tilts());
  spline.attributes.foreach_attribute(
      [&](const AttributeIDRef &attribute_id, const AttributeMetaData &UNUSED(meta_data)) {
        std::optional<GMutableSpan> src = spline.attributes.get_for_write(attribute_id);
        BLI_assert(src);
        attribute_math::convert_to_static_type(src->type(), [&](auto dummy) {
          using T = decltype(dummy);
          linear_trim_data<T>(start, end, src->typed<T>());
        });
        return true;
      },
      ATTR_DOMAIN_POINT);

  /* Recalculate end.factor if the `num` is two, because the adjustment in the
   * position of the control point of the spline to the left of the new end point will change the
   * factor between them. */
  if (num == 2) {
    if (start_lookup.factor == 1.0f) {
      end.factor = 0.0f;
    }
    else {
      end.factor = (end_lookup.evaluated_index + end_lookup.factor -
                    (start_lookup.evaluated_index + start_lookup.factor)) /
                   (control_offsets[end.right_index] -
                    (start_lookup.evaluated_index + start_lookup.factor));
      end.factor = std::clamp(end.factor, 0.0f, 1.0f);
    }
  }

  BezierSpline::InsertResult start_point = bezier_spline.calculate_segment_insertion(
      start.left_index, start.right_index, start.factor);

  /* Update the start control point parameters so they are used calculating the new end point. */
  bezier_spline.positions()[start.left_index] = start_point.position;
  bezier_spline.handle_positions_right()[start.left_index] = start_point.right_handle;
  bezier_spline.handle_positions_left()[start.right_index] = start_point.handle_next;

  const BezierSpline::InsertResult end_point = bezier_spline.calculate_segment_insertion(
      end.left_index, end.right_index, end.factor);

  /* If `num` is two, then the start point right handle needs to change to reflect the end point
   * previous handle update. */
  if (num == 2) {
    start_point.right_handle = end_point.handle_prev;
  }

  /* Shift control point position data to start at beginning of array. */
  if (start.left_index > 0) {
    shift_slice_to_start(bezier_spline.positions(), start.left_index, num);
    shift_slice_to_start(bezier_spline.handle_positions_left(), start.left_index, num);
    shift_slice_to_start(bezier_spline.handle_positions_right(), start.left_index, num);
  }

  bezier_spline.positions().first() = start_point.position;
  bezier_spline.positions()[num - 1] = end_point.position;

  bezier_spline.handle_positions_left().first() = start_point.left_handle;
  bezier_spline.handle_positions_left()[num - 1] = end_point.left_handle;

  bezier_spline.handle_positions_right().first() = start_point.right_handle;
  bezier_spline.handle_positions_right()[num - 1] = end_point.right_handle;

  /* If there is at least one control point between the endpoints, update the control
   * point handle to the right of the start point and to the left of the end point. */
  if (num > 2) {
    bezier_spline.handle_positions_left()[start.right_index - start.left_index] =
        start_point.handle_next;
    bezier_spline.handle_positions_right()[end.left_index - start.left_index] =
        end_point.handle_prev;
  }

  bezier_spline.resize(num);
}

static void trim_spline(SplinePtr &spline,
                        const Spline::LookupResult start,
                        const Spline::LookupResult end)
{
  switch (spline->type()) {
    case CURVE_TYPE_BEZIER:
      trim_bezier_spline(*spline, start, end);
      break;
    case CURVE_TYPE_POLY:
      trim_poly_spline(*spline, start, end);
      break;
    case CURVE_TYPE_NURBS:
      spline = std::make_unique<PolySpline>(trim_nurbs_spline(*spline, start, end));
      break;
    case CURVE_TYPE_CATMULL_ROM:
      BLI_assert_unreachable();
      spline = {};
  }
  spline->mark_cache_invalid();
}

template<typename T>
static void to_single_point_data(const TrimLocation &trim, MutableSpan<T> data)
{
  data.first() = mix2<T>(trim.factor, data[trim.left_index], data[trim.right_index]);
}
template<typename T>
static void to_single_point_data(const TrimLocation &trim, Span<T> src, MutableSpan<T> dst)
{
  dst.first() = mix2<T>(trim.factor, src[trim.left_index], src[trim.right_index]);
}

static void to_single_point_bezier(Spline &spline, const Spline::LookupResult &lookup)
{
  BezierSpline &bezier = static_cast<BezierSpline &>(spline);

  const TrimLocation trim = lookup_control_point_position(lookup, bezier);

  const BezierSpline::InsertResult new_point = bezier.calculate_segment_insertion(
      trim.left_index, trim.right_index, trim.factor);
  bezier.positions().first() = new_point.position;
  bezier.handle_types_left().first() = BEZIER_HANDLE_FREE;
  bezier.handle_types_right().first() = BEZIER_HANDLE_FREE;
  bezier.handle_positions_left().first() = new_point.left_handle;
  bezier.handle_positions_right().first() = new_point.right_handle;

  to_single_point_data<float>(trim, bezier.radii());
  to_single_point_data<float>(trim, bezier.tilts());
  spline.attributes.foreach_attribute(
      [&](const AttributeIDRef &attribute_id, const AttributeMetaData &UNUSED(meta_data)) {
        std::optional<GMutableSpan> data = spline.attributes.get_for_write(attribute_id);
        attribute_math::convert_to_static_type(data->type(), [&](auto dummy) {
          using T = decltype(dummy);
          to_single_point_data<T>(trim, data->typed<T>());
        });
        return true;
      },
      ATTR_DOMAIN_POINT);
  spline.resize(1);
}

static void to_single_point_poly(Spline &spline, const Spline::LookupResult &lookup)
{
  const TrimLocation trim{lookup.evaluated_index, lookup.next_evaluated_index, lookup.factor};

  to_single_point_data<float3>(trim, spline.positions());
  to_single_point_data<float>(trim, spline.radii());
  to_single_point_data<float>(trim, spline.tilts());
  spline.attributes.foreach_attribute(
      [&](const AttributeIDRef &attribute_id, const AttributeMetaData &UNUSED(meta_data)) {
        std::optional<GMutableSpan> data = spline.attributes.get_for_write(attribute_id);
        attribute_math::convert_to_static_type(data->type(), [&](auto dummy) {
          using T = decltype(dummy);
          to_single_point_data<T>(trim, data->typed<T>());
        });
        return true;
      },
      ATTR_DOMAIN_POINT);
  spline.resize(1);
}

static PolySpline to_single_point_nurbs(const Spline &spline, const Spline::LookupResult &lookup)
{
  /* Since this outputs a poly spline, the evaluated indices are the control point indices. */
  const TrimLocation trim{lookup.evaluated_index, lookup.next_evaluated_index, lookup.factor};

  /* Create poly spline and copy trimmed data to it. */
  PolySpline new_spline;
  new_spline.resize(1);

  spline.attributes.foreach_attribute(
      [&](const AttributeIDRef &attribute_id, const AttributeMetaData &meta_data) {
        new_spline.attributes.create(attribute_id, meta_data.data_type);
        std::optional<GSpan> src = spline.attributes.get_for_read(attribute_id);
        std::optional<GMutableSpan> dst = new_spline.attributes.get_for_write(attribute_id);
        attribute_math::convert_to_static_type(src->type(), [&](auto dummy) {
          using T = decltype(dummy);
          VArray<T> eval_data = spline.interpolate_to_evaluated<T>(src->typed<T>());
          to_single_point_data<T>(trim, eval_data.get_internal_span(), dst->typed<T>());
        });
        return true;
      },
      ATTR_DOMAIN_POINT);

  to_single_point_data<float3>(trim, spline.evaluated_positions(), new_spline.positions());

  VArray<float> evaluated_radii = spline.interpolate_to_evaluated(spline.radii());
  to_single_point_data<float>(trim, evaluated_radii.get_internal_span(), new_spline.radii());

  VArray<float> evaluated_tilts = spline.interpolate_to_evaluated(spline.tilts());
  to_single_point_data<float>(trim, evaluated_tilts.get_internal_span(), new_spline.tilts());

  return new_spline;
}

static void to_single_point_spline(SplinePtr &spline, const Spline::LookupResult &lookup)
{
  switch (spline->type()) {
    case CURVE_TYPE_BEZIER:
      to_single_point_bezier(*spline, lookup);
      break;
    case CURVE_TYPE_POLY:
      to_single_point_poly(*spline, lookup);
      break;
    case CURVE_TYPE_NURBS:
      spline = std::make_unique<PolySpline>(to_single_point_nurbs(*spline, lookup));
      break;
    case CURVE_TYPE_CATMULL_ROM:
      BLI_assert_unreachable();
      spline = {};
  }
}

static void geometry_set_curve_trim(GeometrySet &geometry_set,
                                    const GeometryNodeCurveSampleMode mode,
                                    Field<float> &start_field,
                                    Field<float> &end_field)
{
  if (!geometry_set.has_curves()) {
    return;
  }
  const Curves &src_curves_id = *geometry_set.get_curves_for_read();
  const bke::CurvesGeometry &curves = bke::CurvesGeometry::wrap(src_curves_id.geometry);

  bke::CurvesFieldContext field_context{curves, ATTR_DOMAIN_CURVE};
  fn::FieldEvaluator evaluator{field_context, curves.curves_num()};
  evaluator.add(start_field);
  evaluator.add(end_field);
  evaluator.evaluate();
  const VArray<float> starts = evaluator.get_evaluated<float>(0);
  const VArray<float> ends = evaluator.get_evaluated<float>(1);

  std::unique_ptr<CurveEval> curve = curves_to_curve_eval(src_curves_id);
  MutableSpan<SplinePtr> splines = curve->splines();

  threading::parallel_for(splines.index_range(), 128, [&](IndexRange range) {
    for (const int i : range) {
      SplinePtr &spline = splines[i];

      /* Currently trimming cyclic splines is not supported. It could be in the future though. */
      if (spline->is_cyclic()) {
        continue;
      }

      if (spline->evaluated_edges_num() == 0) {
        continue;
      }

      const float length = spline->length();
      if (length == 0.0f) {
        continue;
      }

      const float start = starts[i];
      const float end = ends[i];

      /* When the start and end samples are reversed, instead of implicitly reversing the spline
       * or switching the parameters, create a single point spline with the end sample point. */
      if (end <= start) {
        if (mode == GEO_NODE_CURVE_SAMPLE_LENGTH) {
          to_single_point_spline(spline,
                                 spline->lookup_evaluated_length(std::clamp(start, 0.0f, length)));
        }
        else {
          to_single_point_spline(spline,
                                 spline->lookup_evaluated_factor(std::clamp(start, 0.0f, 1.0f)));
        }
        continue;
      }

      if (mode == GEO_NODE_CURVE_SAMPLE_LENGTH) {
        trim_spline(spline,
                    spline->lookup_evaluated_length(std::clamp(start, 0.0f, length)),
                    spline->lookup_evaluated_length(std::clamp(end, 0.0f, length)));
      }
      else {
        trim_spline(spline,
                    spline->lookup_evaluated_factor(std::clamp(start, 0.0f, 1.0f)),
                    spline->lookup_evaluated_factor(std::clamp(end, 0.0f, 1.0f)));
      }
    }
  });

  Curves *dst_curves_id = curve_eval_to_curves(*curve);
  bke::curves_copy_parameters(src_curves_id, *dst_curves_id);
  geometry_set.replace_curves(dst_curves_id);
}

static void node_geo_exec(GeoNodeExecParams params)
{
  const NodeGeometryCurveTrim &storage = node_storage(params.node());
  const GeometryNodeCurveSampleMode mode = (GeometryNodeCurveSampleMode)storage.mode;

  GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
  GeometryComponentEditData::remember_deformed_curve_positions_if_necessary(geometry_set);

  if (mode == GEO_NODE_CURVE_SAMPLE_FACTOR) {
    Field<float> start_field = params.extract_input<Field<float>>("Start");
    Field<float> end_field = params.extract_input<Field<float>>("End");
    geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
      geometry_set_curve_trim(geometry_set, mode, start_field, end_field);
    });
  }
  else if (mode == GEO_NODE_CURVE_SAMPLE_LENGTH) {
    Field<float> start_field = params.extract_input<Field<float>>("Start_001");
    Field<float> end_field = params.extract_input<Field<float>>("End_001");
    geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
      geometry_set_curve_trim(geometry_set, mode, start_field, end_field);
    });
  }

  params.set_output("Curve", std::move(geometry_set));
}

}  // namespace blender::nodes::node_geo_curve_trim_cc

void register_node_type_geo_curve_trim()
{
  namespace file_ns = blender::nodes::node_geo_curve_trim_cc;

  static bNodeType ntype;
  geo_node_type_base(&ntype, GEO_NODE_TRIM_CURVE, "Trim Curve", NODE_CLASS_GEOMETRY);
  ntype.geometry_node_execute = file_ns::node_geo_exec;
  ntype.draw_buttons = file_ns::node_layout;
  ntype.declare = file_ns::node_declare;
  node_type_storage(
      &ntype, "NodeGeometryCurveTrim", node_free_standard_storage, node_copy_standard_storage);
  node_type_init(&ntype, file_ns::node_init);
  node_type_update(&ntype, file_ns::node_update);
  ntype.gather_link_search_ops = file_ns::node_gather_link_searches;
  nodeRegisterType(&ntype);
}