Blender V5.0
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
8
10#include "BLI_task.hh"
11#include <algorithm>
12
14
15void sample_uniform(const Span<float> accumulated_segment_lengths,
16 const bool include_last_point,
17 MutableSpan<int> r_segment_indices,
18 MutableSpan<float> r_factors)
19{
20 const int count = r_segment_indices.size();
21 BLI_assert(count > 0);
22 BLI_assert(accumulated_segment_lengths.size() >= 1);
24 std::is_sorted(accumulated_segment_lengths.begin(), accumulated_segment_lengths.end()));
25
26 if (count == 1) {
27 r_segment_indices[0] = 0;
28 r_factors[0] = 0.0f;
29 return;
30 }
31 const float total_length = accumulated_segment_lengths.last();
32 const float step_length = total_length / (count - include_last_point);
33 threading::parallel_for(IndexRange(count), 512, [&](const IndexRange range) {
35 for (const int i : range) {
36 /* Use minimum to avoid issues with floating point accuracy. */
37 const float sample_length = std::min(total_length, i * step_length);
39 accumulated_segment_lengths, sample_length, r_segment_indices[i], r_factors[i], &hint);
40 }
41 });
42}
43
44void sample_uniform_reverse(const Span<float> accumulated_segment_lengths,
45 const bool include_first_point,
46 MutableSpan<int> r_segment_indices,
47 MutableSpan<float> r_factors)
48{
49 const int count = r_segment_indices.size();
50 BLI_assert(count > 0);
51 BLI_assert(accumulated_segment_lengths.size() >= 1);
53 std::is_sorted(accumulated_segment_lengths.begin(), accumulated_segment_lengths.end()));
54
55 if (count == 1) {
56 r_segment_indices[0] = accumulated_segment_lengths.size() - 1;
57 r_factors[0] = 1.0f;
58 return;
59 }
60 const float total_length = accumulated_segment_lengths.last();
61 const float step_length = total_length / (count - include_first_point);
62 threading::parallel_for(IndexRange(count), 512, [&](const IndexRange range) {
64 for (const int i : range) {
65 /* Use maximum to avoid issues with floating point accuracy. */
66 const float sample_length = std::max(0.0f, total_length - i * step_length);
68 accumulated_segment_lengths, sample_length, r_segment_indices[i], r_factors[i], &hint);
69 }
70 });
71}
72
73void sample_at_lengths(const Span<float> accumulated_segment_lengths,
74 const Span<float> sample_lengths,
75 MutableSpan<int> r_segment_indices,
76 MutableSpan<float> r_factors)
77{
79 std::is_sorted(accumulated_segment_lengths.begin(), accumulated_segment_lengths.end()));
80 BLI_assert(std::is_sorted(sample_lengths.begin(), sample_lengths.end()));
81
82 const int count = sample_lengths.size();
83 BLI_assert(count == r_segment_indices.size());
84 BLI_assert(count == r_factors.size());
85
86 threading::parallel_for(IndexRange(count), 512, [&](const IndexRange range) {
88 for (const int i : range) {
89 const float sample_length = sample_lengths[i];
91 accumulated_segment_lengths, sample_length, r_segment_indices[i], r_factors[i], &hint);
92 }
93 });
94}
95
96} // namespace blender::length_parameterize
#define BLI_assert(a)
Definition BLI_assert.h:46
constexpr int64_t size() const
Definition BLI_span.hh:493
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:325
constexpr const T * end() const
Definition BLI_span.hh:224
constexpr const T * begin() const
Definition BLI_span.hh:220
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:93
i
Definition text_draw.cc:230