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

◆ stroke_slider()

MAYAFLUX_API GeometryFn< float > MayaFlux::Portal::Forma::Geometry::stroke_slider ( std::span< const glm::vec2 >  path,
std::shared_ptr< Buffers::FormaBuffer handle_buf,
float  half_thickness = 0.02F,
glm::vec3  track_color = glm::vec3(0.3F),
glm::vec3  fill_color = glm::vec3(0.2F, 0.6F, 1.0F),
glm::vec3  handle_color = glm::vec3(0.95F),
float  handle_size = 10.0F 
)

Geometry function for a value scrubber along an arbitrary polyline.

Value in [0, 1] maps to arc-length position along path. The full path is rendered as a LINE_LIST in track_color; the prefix from the path start to the handle position is rendered in fill_color. The handle itself is a PointVertex submitted to handle_buf each sync.

Parameters
pathOrdered polyline vertices in NDC. Copied into closure.
handle_bufPOINT_LIST FormaBuffer for the handle point. Must be registered and have setup_rendering called.
half_thicknessHit region half-thickness in NDC units.
track_colorColor of the full path.
fill_colorColor of the prefix segment up to the handle.
handle_colorColor of the handle point.
handle_sizeHandle point size in pixels.

Definition at line 129 of file Geometry.cpp.

137{
138 std::vector<glm::vec2> pts(path.begin(), path.end());
139
140 std::vector<float> seg_lengths;
141 seg_lengths.reserve(pts.size() > 0 ? pts.size() - 1 : 0);
142 float total_len = 0.0F;
143 for (size_t i = 0; i + 1 < pts.size(); ++i) {
144 float l = glm::length(pts[i + 1] - pts[i]);
145 seg_lengths.push_back(l);
146 total_len += l;
147 }
148
149 Kinesis::AABB2D aabb { .min = pts.empty() ? glm::vec2(0.F) : pts[0],
150 .max = pts.empty() ? glm::vec2(0.F) : pts[0] };
151 for (const auto& p : pts) {
152 aabb.min = glm::min(aabb.min, p);
153 aabb.max = glm::max(aabb.max, p);
154 }
155 aabb = aabb.expanded(half_thickness);
156
157 return [pts = std::move(pts),
158 seg_lengths = std::move(seg_lengths),
159 total_len,
160 aabb,
161 handle_buf = std::move(handle_buf),
162 half_thickness,
163 track_color,
164 fill_color,
165 handle_color,
166 handle_size](float v, std::vector<uint8_t>& out, Element& el) {
167 if (pts.size() < 2) {
168 out.clear();
169 return;
170 }
171
172 const float target = std::clamp(v, 0.0F, 1.0F) * total_len;
173
174 glm::vec2 handle_pos = pts.front();
175 float accumulated = 0.0F;
176 size_t split_seg = 0;
177 float split_t = 0.0F;
178 for (size_t i = 0; i < seg_lengths.size(); ++i) {
179 if (accumulated + seg_lengths[i] >= target || i + 1 == seg_lengths.size()) {
180 split_t = seg_lengths[i] > 0.0F
181 ? (target - accumulated) / seg_lengths[i]
182 : 0.0F;
183 split_t = std::clamp(split_t, 0.0F, 1.0F);
184 handle_pos = glm::mix(pts[i], pts[i + 1], split_t);
185 split_seg = i;
186 break;
187 }
188 accumulated += seg_lengths[i];
189 }
190
191 auto verts = Kinesis::polyline(pts, track_color);
192
193 auto fill = Kinesis::polyline(
194 std::span<const glm::vec2>(pts).subspan(0, split_seg + 1),
195 fill_color);
196 verts.insert(verts.end(), fill.begin(), fill.end());
197
198 if (split_t > 0.0F) {
199 verts.push_back({ .position = { pts[split_seg].x, pts[split_seg].y, 0.0F }, .color = fill_color });
200 verts.push_back({ .position = { handle_pos.x, handle_pos.y, 0.0F }, .color = fill_color });
201 }
202
203 write_verts(out, verts);
204
205 el.bounds_hint = aabb;
206 el.contains = Kinesis::stroke_bounds(pts, half_thickness);
207
208 if (handle_buf) {
209 Kakshya::PointVertex hv {
210 .position = { handle_pos.x, handle_pos.y, 0.0F },
211 .color = handle_color,
212 .size = handle_size,
213 };
214 std::vector<uint8_t> hbytes(sizeof(hv));
215 std::memcpy(hbytes.data(), &hv, sizeof(hv));
216 handle_buf->submit(hbytes);
217 }
218 };
219}
AABB3D aabb(std::span< T > pts) noexcept
Axis-aligned bounding box of a PositionCarrying span.
void write_verts(std::vector< uint8_t > &out, const std::vector< V > &verts)
Write a vertex array into a GeometryFn output buffer.
Definition Geometry.hpp:35
Axis-aligned bounding rectangle in a 2D coordinate space.
Definition Bounds.hpp:21
bool contains(const glm::vec3 &p) const noexcept
Definition Bounds.hpp:147
AABB3D expanded(float margin) const noexcept
Definition Bounds.hpp:170

References MayaFlux::Kinesis::AABB3D::contains(), MayaFlux::Kinesis::AABB3D::expanded(), MayaFlux::Kinesis::AABB3D::max, MayaFlux::Kinesis::AABB3D::min, MayaFlux::Kinesis::polyline(), MayaFlux::Kakshya::PointVertex::position, MayaFlux::Kinesis::stroke_bounds(), and write_verts().

+ Here is the call graph for this function: