Blender V4.3
length_parameterize.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6#include "BLI_task.hh"
7#include <algorithm>
8
10
11void sample_uniform(const Span<float> accumulated_segment_lengths,
12 const bool include_last_point,
13 MutableSpan<int> r_segment_indices,
14 MutableSpan<float> r_factors)
15{
16 const int count = r_segment_indices.size();
17 BLI_assert(count > 0);
18 BLI_assert(accumulated_segment_lengths.size() >= 1);
20 std::is_sorted(accumulated_segment_lengths.begin(), accumulated_segment_lengths.end()));
21
22 if (count == 1) {
23 r_segment_indices[0] = 0;
24 r_factors[0] = 0.0f;
25 return;
26 }
27 const float total_length = accumulated_segment_lengths.last();
28 const float step_length = total_length / (count - include_last_point);
29 threading::parallel_for(IndexRange(count), 512, [&](const IndexRange range) {
31 for (const int i : range) {
32 /* Use minimum to avoid issues with floating point accuracy. */
33 const float sample_length = std::min(total_length, i * step_length);
35 accumulated_segment_lengths, sample_length, r_segment_indices[i], r_factors[i], &hint);
36 }
37 });
38}
39
40void sample_uniform_reverse(const Span<float> accumulated_segment_lengths,
41 const bool include_first_point,
42 MutableSpan<int> r_segment_indices,
43 MutableSpan<float> r_factors)
44{
45 const int count = r_segment_indices.size();
46 BLI_assert(count > 0);
47 BLI_assert(accumulated_segment_lengths.size() >= 1);
49 std::is_sorted(accumulated_segment_lengths.begin(), accumulated_segment_lengths.end()));
50
51 if (count == 1) {
52 r_segment_indices[0] = accumulated_segment_lengths.size() - 1;
53 r_factors[0] = 1.0f;
54 return;
55 }
56 const float total_length = accumulated_segment_lengths.last();
57 const float step_length = total_length / (count - include_first_point);
58 threading::parallel_for(IndexRange(count), 512, [&](const IndexRange range) {
60 for (const int i : range) {
61 /* Use maximum to avoid issues with floating point accuracy. */
62 const float sample_length = std::max(0.0f, total_length - i * step_length);
64 accumulated_segment_lengths, sample_length, r_segment_indices[i], r_factors[i], &hint);
65 }
66 });
67}
68
69void sample_at_lengths(const Span<float> accumulated_segment_lengths,
70 const Span<float> sample_lengths,
71 MutableSpan<int> r_segment_indices,
72 MutableSpan<float> r_factors)
73{
75 std::is_sorted(accumulated_segment_lengths.begin(), accumulated_segment_lengths.end()));
76 BLI_assert(std::is_sorted(sample_lengths.begin(), sample_lengths.end()));
77
78 const int count = sample_lengths.size();
79 BLI_assert(count == r_segment_indices.size());
80 BLI_assert(count == r_factors.size());
81
82 threading::parallel_for(IndexRange(count), 512, [&](const IndexRange range) {
84 for (const int i : range) {
85 const float sample_length = sample_lengths[i];
87 accumulated_segment_lengths, sample_length, r_segment_indices[i], r_factors[i], &hint);
88 }
89 });
90}
91
92} // namespace blender::length_parameterize
#define BLI_assert(a)
Definition BLI_assert.h:50
constexpr int64_t size() const
Definition BLI_span.hh:494
constexpr int64_t size() const
Definition BLI_span.hh:253
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:326
constexpr const T * end() const
Definition BLI_span.hh:225
constexpr const T * begin() const
Definition BLI_span.hh:221
IndexRange range
int count
void sample_at_lengths(Span< float > accumulated_segment_lengths, Span< float > sample_lengths, MutableSpan< int > r_segment_indices, MutableSpan< float > r_factors)
void sample_uniform_reverse(Span< float > accumulated_segment_lengths, bool include_first_point, MutableSpan< int > r_segment_indices, MutableSpan< float > r_factors)
void sample_at_length(const Span< float > accumulated_segment_lengths, const float sample_length, int &r_segment_index, float &r_factor, SampleSegmentHint *hint=nullptr)
void sample_uniform(Span< float > accumulated_segment_lengths, bool include_last_point, MutableSpan< int > r_segment_indices, MutableSpan< float > r_factors)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:95