MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ interpolate_cubic() [1/2]

template<OperationReadyData DataType>
DataType MayaFlux::Yantra::interpolate_cubic ( DataType &  input,
size_t  target_size 
)

Cubic interpolation between data points using C++20 ranges (IN-PLACE)

Template Parameters
DataTypeOperationReadyData type
Parameters
inputInput data - WILL BE MODIFIED/RESIZED
target_sizeTarget size after interpolation
Returns
Interpolated data

Definition at line 679 of file MathematicalHelper.hpp.

680{
681 auto [target_data, structure_info] = OperationHelper::extract_structured_double(input);
682
683 bool needs_resize = false;
684 for (const auto& span : target_data) {
685 if (target_size != span.size()) {
686 needs_resize = true;
687 break;
688 }
689 }
690
691 if (!needs_resize) {
692 return input;
693 }
694
695 std::vector<std::vector<double>> result(target_data.size());
696
697 for (size_t ch = 0; ch < target_data.size(); ++ch) {
698 const auto& data_span = target_data[ch];
699 auto& interpolated = result[ch];
700 interpolated.resize(target_size);
701
702 if (target_size <= 1 || data_span.size() <= 1) {
703 std::fill(interpolated.begin(), interpolated.end(),
704 data_span.empty() ? 0.0 : data_span[0]);
705 continue;
706 }
707
708 auto indices = std::views::iota(size_t { 0 }, target_size);
709 std::ranges::transform(indices, interpolated.begin(),
710 [&data_span, target_size](size_t i) {
711 double pos = static_cast<double>(i) * (data_span.size() - 1) / (target_size - 1);
712 int idx = static_cast<int>(pos);
713 double frac = pos - idx;
714
715 auto get_sample = [&data_span](int i) {
716 return data_span[std::clamp(i, 0, static_cast<int>(data_span.size() - 1))];
717 };
718
719 double y0 = get_sample(idx - 1);
720 double y1 = get_sample(idx);
721 double y2 = get_sample(idx + 1);
722 double y3 = get_sample(idx + 2);
723
724 double a = -0.5 * y0 + 1.5 * y1 - 1.5 * y2 + 0.5 * y3;
725 double b = y0 - 2.5 * y1 + 2.0 * y2 - 0.5 * y3;
726 double c = -0.5 * y0 + 0.5 * y2;
727 double d = y1;
728
729 return ((a * frac + b) * frac + c) * frac + d;
730 });
731 }
732
733 return OperationHelper::reconstruct_from_double<DataType>(result, structure_info);
734}

References interpolate_cubic().

Referenced by interpolate_cubic(), interpolate_cubic(), and MayaFlux::Yantra::TemporalTransformer< InputType, OutputType >::transform_implementation().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: