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

◆ path_fraction()

std::function< float(glm::vec2)> MayaFlux::Kinesis::path_fraction ( std::span< const glm::vec2 >  points)
inline

Normalized arc-length position of the closest point on a polyline.

Inverse to stroke_slider, which places the handle at a fraction of the path's total arc length. Finds the nearest point across all segments and returns its cumulative length divided by the total, so the handle follows the cursor along the path regardless of how far off the path it strays.

Cumulative lengths are computed once and captured; the returned callable allocates nothing per invocation.

Parameters
pointsOrdered polyline vertices in NDC. Copied into the closure.
Returns
Callable producing a value in [0, 1]. Returns 0 for paths with fewer than two points or zero total length.

Definition at line 143 of file Projection.hpp.

144{
145 std::vector<glm::vec2> pts(points.begin(), points.end());
146 std::vector<float> cumulative(pts.size(), 0.F);
147
148 for (size_t i = 1; i < pts.size(); ++i)
149 cumulative[i] = cumulative[i - 1] + glm::length(pts[i] - pts[i - 1]);
150
151 const float total = pts.empty() ? 0.F : cumulative.back();
152
153 return [pts = std::move(pts), cumulative = std::move(cumulative), total](
154 glm::vec2 p) -> float {
155 if (pts.size() < 2 || total <= 0.F)
156 return 0.F;
157
158 float best_d2 = std::numeric_limits<float>::max();
159 float best_s = 0.F;
160
161 for (size_t i = 0; i + 1 < pts.size(); ++i) {
162 const glm::vec2 a = pts[i];
163 const glm::vec2 ab = pts[i + 1] - a;
164 const float len2 = glm::dot(ab, ab);
165
166 const float t = len2 > 1e-12F
167 ? glm::clamp(glm::dot(p - a, ab) / len2, 0.F, 1.F)
168 : 0.F;
169
170 const glm::vec2 diff = p - (a + t * ab);
171 const float d2 = glm::dot(diff, diff);
172
173 if (d2 < best_d2) {
174 best_d2 = d2;
175 best_s = cumulative[i] + t * std::sqrt(len2);
176 }
177 }
178
179 return std::clamp(best_s / total, 0.F, 1.F);
180 };
181}
std::vector< glm::vec2 > * points
size_t a

References a, and points.

Referenced by MayaFlux::Portal::Forma::Geometry::stroke_slider().

+ Here is the caller graph for this function: