diff options
Diffstat (limited to 'source/blender/blenlib/intern/length_parameterize.cc')
-rw-r--r-- | source/blender/blenlib/intern/length_parameterize.cc | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/length_parameterize.cc b/source/blender/blenlib/intern/length_parameterize.cc index d6862b96944..7c0fc860b53 100644 --- a/source/blender/blenlib/intern/length_parameterize.cc +++ b/source/blender/blenlib/intern/length_parameterize.cc @@ -77,4 +77,68 @@ void create_uniform_samples(const Span<float> lengths, } } +void create_samples_from_sorted_lengths(const Span<float> lengths, + const Span<float> sample_lengths, + const bool cyclic, + MutableSpan<int> indices, + MutableSpan<float> factors) +{ + BLI_assert(std::is_sorted(lengths.begin(), lengths.end())); + BLI_assert(std::is_sorted(sample_lengths.begin(), sample_lengths.end())); + BLI_assert(indices.size() == sample_lengths.size()); + BLI_assert(indices.size() == factors.size()); + const int segments_num = lengths.size(); + const int points_num = cyclic ? segments_num : segments_num + 1; + + const float total_length = lengths.last(); + if (total_length == 0.0f) { + indices.fill(0); + factors.fill(0.0f); + return; + } + + int i_dst = 0; + /* Store the length at the previous point in a variable so it can start out at zero + * (the lengths array doesn't contain 0 for the first point). */ + float prev_length = 0.0f; + for (const int i_src : IndexRange(points_num - 1)) { + const float next_length = lengths[i_src]; + const float segment_length = next_length - prev_length; + if (segment_length == 0.0f) { + continue; + } + /* Add every sample that fits in this segment. It's also necessary to check if the last sample + * has been reached, since there is no upper bound on the number of samples in each segment. */ + const float segment_length_inv = 1.0f / segment_length; + while (i_dst < sample_lengths.size() && sample_lengths[i_dst] < next_length) { + const float length_in_segment = sample_lengths[i_dst] - prev_length; + const float factor = length_in_segment * segment_length_inv; + indices[i_dst] = i_src; + factors[i_dst] = factor; + i_dst++; + } + + prev_length = next_length; + } + + /* Add the samples on the last cyclic segment if necessary, and also the samples + * that weren't created in the previous loop due to floating point inaccuracy. */ + if (cyclic && lengths.size() > 1) { + const float segment_length = lengths.last() - lengths.last(1); + while (sample_lengths[i_dst] < total_length) { + const float length_in_segment = sample_lengths[i_dst] - prev_length; + const float factor = length_in_segment / segment_length; + indices[i_dst] = points_num - 1; + factors[i_dst] = factor; + i_dst++; + } + indices.drop_front(i_dst).fill(points_num - 1); + factors.drop_front(i_dst).fill(1.0f); + } + else { + indices.drop_front(i_dst).fill(points_num - 2); + factors.drop_front(i_dst).fill(1.0f); + } +} + } // namespace blender::length_parameterize |