Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/BLI_length_parameterize.hh
| Show All 13 Lines | ||||||||||||
| namespace blender::length_parameterize { | namespace blender::length_parameterize { | |||||||||||
| /** | /** | |||||||||||
| * Return the size of the necessary lengths array for a group of points, taking into account the | * Return the size of the necessary lengths array for a group of points, taking into account the | |||||||||||
| * possible last cyclic segment. | * possible last cyclic segment. | |||||||||||
| * | * | |||||||||||
| * \note This is the same as #bke::curves::curve_segment_num. | * \note This is the same as #bke::curves::curve_segment_num. | |||||||||||
| */ | */ | |||||||||||
| inline int lengths_num(const int points_num, const bool cyclic) | inline int segments_num(const int points_num, const bool cyclic) | |||||||||||
| { | { | |||||||||||
| return cyclic ? points_num : points_num - 1; | return cyclic ? points_num : points_num - 1; | |||||||||||
| } | } | |||||||||||
| /** | /** | |||||||||||
| * Accumulate the length of the next segment into each point. | * Accumulate the length of the next segment into each point. | |||||||||||
| */ | */ | |||||||||||
| template<typename T> | template<typename T> | |||||||||||
| void accumulate_lengths(const Span<T> values, const bool cyclic, MutableSpan<float> lengths) | void accumulate_lengths(const Span<T> values, const bool cyclic, MutableSpan<float> lengths) | |||||||||||
| { | { | |||||||||||
| BLI_assert(lengths.size() == lengths_num(values.size(), cyclic)); | BLI_assert(lengths.size() == segments_num(values.size(), cyclic)); | |||||||||||
| float length = 0.0f; | float length = 0.0f; | |||||||||||
| for (const int i : IndexRange(values.size() - 1)) { | for (const int i : IndexRange(values.size() - 1)) { | |||||||||||
| length += math::distance(values[i], values[i + 1]); | length += math::distance(values[i], values[i + 1]); | |||||||||||
| lengths[i] = length; | lengths[i] = length; | |||||||||||
| } | } | |||||||||||
| if (cyclic) { | if (cyclic) { | |||||||||||
| lengths.last() = length + math::distance(values.last(), values.first()); | lengths.last() = length + math::distance(values.last(), values.first()); | |||||||||||
| } | } | |||||||||||
| } | } | |||||||||||
| template<typename T> | template<typename T> | |||||||||||
| void linear_interpolation(const Span<T> src, | inline void linear_interpolation(const Span<T> src, | |||||||||||
| const Span<int> indices, | const Span<int> indices, | |||||||||||
| const Span<float> factors, | const Span<float> factors, | |||||||||||
| MutableSpan<T> dst) | MutableSpan<T> dst) | |||||||||||
| { | { | |||||||||||
| BLI_assert(indices.size() == factors.size()); | BLI_assert(indices.size() == factors.size()); | |||||||||||
| BLI_assert(indices.size() == dst.size()); | BLI_assert(indices.size() == dst.size()); | |||||||||||
| const int last_src_index = src.index_range().last(); | const int last_src_index = src.size() - 1; | |||||||||||
| int cyclic_sample_count = 0; | for (const int i : dst.index_range()) { | |||||||||||
| for (int i = indices.index_range().last(); i > 0; i--) { | const int prev_index = indices[i]; | |||||||||||
| if (indices[i] != last_src_index) { | const float factor = factors[i]; | |||||||||||
| break; | const bool is_cyclic_case = prev_index == last_src_index; | |||||||||||
| if (is_cyclic_case) { | ||||||||||||
| dst[i] = math::interpolate(src.last(), src.first(), factor); | ||||||||||||
| } | ||||||||||||
| else { | ||||||||||||
| dst[i] = math::interpolate(src[prev_index], src[prev_index + 1], factor); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| /** | ||||||||||||
| * Passing this to consecutive calls of #sample_at_length can increase performance. | ||||||||||||
| */ | ||||||||||||
| struct SampleSegmentHint { | ||||||||||||
| int segment_index = -1; | ||||||||||||
| float segment_start; | ||||||||||||
| float segment_length_inv; | ||||||||||||
| }; | ||||||||||||
| /** | ||||||||||||
| * \param accumulated_segment_lengths: Lengths of individual segments added up. | ||||||||||||
| * \param sample_length: The position to sample at. | ||||||||||||
| * \param r_segment_index: Returns the index of the segment that #sample_length is in. | ||||||||||||
| * \param r_factor: Returns the position within the segment. | ||||||||||||
| * | ||||||||||||
| * \note #sample_length must not be outside of any segment. | ||||||||||||
| */ | ||||||||||||
| inline void sample_at_length(const Span<float> accumulated_segment_lengths, | ||||||||||||
| float sample_length, | ||||||||||||
| int &r_segment_index, | ||||||||||||
| float &r_factor, | ||||||||||||
| SampleSegmentHint *hint = nullptr) | ||||||||||||
| { | ||||||||||||
| /* Use a shorter variable name. */ | ||||||||||||
| const Span<float> lengths = accumulated_segment_lengths; | ||||||||||||
| BLI_assert(lengths.size() > 0); | ||||||||||||
| BLI_assert(sample_length >= 0.0f); | ||||||||||||
| BLI_assert(sample_length <= lengths.last()); | ||||||||||||
| if (hint != nullptr && hint->segment_index >= 0) { | ||||||||||||
| const float length_in_segment = sample_length - hint->segment_start; | ||||||||||||
| const float factor = length_in_segment * hint->segment_length_inv; | ||||||||||||
| if (factor >= 0.0f && factor < 1.0f) { | ||||||||||||
| r_segment_index = hint->segment_index; | ||||||||||||
| r_factor = factor; | ||||||||||||
| return; | ||||||||||||
| } | } | |||||||||||
| dst[i] = math::interpolate(src.last(), src.first(), factors[i]); | ||||||||||||
| cyclic_sample_count++; | ||||||||||||
| } | } | |||||||||||
| for (const int i : dst.index_range().drop_back(cyclic_sample_count)) { | const float total_length = lengths.last(); | |||||||||||
| dst[i] = math::interpolate(src[indices[i]], src[indices[i] + 1], factors[i]); | if (sample_length >= total_length) { | |||||||||||
| /* Return the last position on the last segment. */ | ||||||||||||
| r_segment_index = lengths.size() - 1; | ||||||||||||
| r_factor = 1.0f; | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
| const int prev_point_index = std::upper_bound(lengths.begin(), lengths.end(), sample_length) - | ||||||||||||
| lengths.begin(); | ||||||||||||
| const float segment_start = prev_point_index == 0 ? 0.0f : lengths[prev_point_index - 1]; | ||||||||||||
| const float segment_end = lengths[prev_point_index]; | ||||||||||||
| const float segment_length = segment_end - segment_start; | ||||||||||||
| const float segment_length_inv = safe_divide(1.0f, segment_length); | ||||||||||||
| const float length_in_segment = sample_length - segment_start; | ||||||||||||
| const float factor = length_in_segment * segment_length_inv; | ||||||||||||
| r_segment_index = prev_point_index; | ||||||||||||
| r_factor = factor; | ||||||||||||
| if (hint != nullptr) { | ||||||||||||
| hint->segment_index = r_segment_index; | ||||||||||||
| hint->segment_start = segment_start; | ||||||||||||
| hint->segment_length_inv = segment_length_inv; | ||||||||||||
| } | } | |||||||||||
| } | } | |||||||||||
| /** | /** | |||||||||||
| * Find the given number of points, evenly spaced along the provided length. For non-cyclic | * Find evenly spaced samples along the lengths. | |||||||||||
HooglyBooglyUnsubmitted Done Inline Actions
HooglyBoogly: | ||||||||||||
| * sequences, the first point will always be included, and last point will always be included if | ||||||||||||
| * the #count is greater than zero. For cyclic sequences, the first point will always be included. | ||||||||||||
| * | * | |||||||||||
| * \warning The #count argument must be greater than zero. | * \param accumulated_segment_lengths: The accumulated lengths of the original elements being | |||||||||||
| * sampled. Could be calculated by #accumulate_lengths. | ||||||||||||
| * \param include_last_point: Generally false for cyclic sequences and true otherwise. | ||||||||||||
Done Inline Actions
HooglyBoogly: | ||||||||||||
| * \param r_segment_indices: The index of the previous point at each sample. | ||||||||||||
| * \param r_factors: The portion of the length in each segment at each sample. | ||||||||||||
| */ | */ | |||||||||||
| void create_uniform_samples(Span<float> lengths, | void sample_uniform(Span<float> accumulated_segment_lengths, | |||||||||||
| bool cyclic, | bool include_last_point, | |||||||||||
| MutableSpan<int> indices, | MutableSpan<int> r_segment_indices, | |||||||||||
| MutableSpan<float> factors); | MutableSpan<float> r_factors); | |||||||||||
| /** | /** | |||||||||||
| * For each provided sample length, find the segment index and interpolation factor. | * For each provided sample length, find the segment index and interpolation factor. | |||||||||||
| * | * | |||||||||||
| * \param lengths: The accumulated lengths of the original elements being sampled. | * \param accumulated_segment_lengths: The accumulated lengths of the original elements being | |||||||||||
| * Could be calculated by #accumulate_lengths. | * sampled. Could be calculated by #accumulate_lengths. | |||||||||||
| * \param sample_lengths: Sampled locations in the #lengths array. Must be sorted and is expected | * \param sample_lengths: Sampled locations in the #lengths array. Must be sorted and is expected | |||||||||||
| * to be within the range of the #lengths values. | * to be within the range of the #lengths values. | |||||||||||
| * \param cyclic: Whether the points described by the #lengths input is cyclic. This is likely | * \param r_segment_indices: The index of the previous point at each sample. | |||||||||||
| * redundant information theoretically. | * \param r_factors: The portion of the length in each segment at each sample. | |||||||||||
| * \param indices: The index of the previous point at each sample. | ||||||||||||
| * \param factors: The portion of the length in each segment at each sample. | ||||||||||||
| */ | */ | |||||||||||
| void create_samples_from_sorted_lengths(Span<float> lengths, | void sample_at_lengths(Span<float> accumulated_segment_lengths, | |||||||||||
| Span<float> sample_lengths, | Span<float> sample_lengths, | |||||||||||
| bool cyclic, | MutableSpan<int> r_segment_indices, | |||||||||||
| MutableSpan<int> indices, | MutableSpan<float> r_factors); | |||||||||||
| MutableSpan<float> factors); | ||||||||||||
| } // namespace blender::length_parameterize | } // namespace blender::length_parameterize | |||||||||||