Blender  V3.3
length_parameterize.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
4 #include "BLI_task.hh"
5 
7 
8 void sample_uniform(const Span<float> lengths,
9  const bool include_last_point,
10  MutableSpan<int> r_segment_indices,
11  MutableSpan<float> r_factors)
12 {
13  const int count = r_segment_indices.size();
14  BLI_assert(count > 0);
15  BLI_assert(lengths.size() >= 1);
16  BLI_assert(std::is_sorted(lengths.begin(), lengths.end()));
17 
18  if (count == 1) {
19  r_segment_indices[0] = 0;
20  r_factors[0] = 0.0f;
21  return;
22  }
23  const float total_length = lengths.last();
24  const float step_length = total_length / (count - include_last_point);
25  threading::parallel_for(IndexRange(count), 512, [&](const IndexRange range) {
26  SampleSegmentHint hint;
27  for (const int i : range) {
28  /* Use minimum to avoid issues with floating point accuracy. */
29  const float sample_length = std::min(total_length, i * step_length);
30  sample_at_length(lengths, sample_length, r_segment_indices[i], r_factors[i], &hint);
31  }
32  });
33 }
34 
35 void sample_at_lengths(const Span<float> accumulated_segment_lengths,
36  const Span<float> sample_lengths,
37  MutableSpan<int> r_segment_indices,
38  MutableSpan<float> r_factors)
39 {
40  BLI_assert(
41  std::is_sorted(accumulated_segment_lengths.begin(), accumulated_segment_lengths.end()));
42  BLI_assert(std::is_sorted(sample_lengths.begin(), sample_lengths.end()));
43 
44  const int count = sample_lengths.size();
45  BLI_assert(count == r_segment_indices.size());
46  BLI_assert(count == r_factors.size());
47 
48  threading::parallel_for(IndexRange(count), 512, [&](const IndexRange range) {
49  SampleSegmentHint hint;
50  for (const int i : range) {
51  const float sample_length = sample_lengths[i];
53  accumulated_segment_lengths, sample_length, r_segment_indices[i], r_factors[i], &hint);
54  }
55  });
56 }
57 
58 } // namespace blender::length_parameterize
#define BLI_assert(a)
Definition: BLI_assert.h:46
constexpr int64_t size() const
Definition: BLI_span.hh:511
constexpr const T & last(const int64_t n=0) const
Definition: BLI_span.hh:313
constexpr int64_t size() const
Definition: BLI_span.hh:240
constexpr const T * end() const
Definition: BLI_span.hh:212
constexpr const T * begin() const
Definition: BLI_span.hh:208
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_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(IndexRange range, int64_t grain_size, const Function &function)
Definition: BLI_task.hh:51
#define min(a, b)
Definition: sort.c:35