MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
DragGeometry.cpp
Go to the documentation of this file.
1#include "DragGeometry.hpp"
2
3#include "Containment.hpp"
6
8
9std::optional<Geometry::Form<float>> scale_form(
10 const ScaleInputs& input, const RectPlacementInputs& placement, std::string& error)
11{
12 if (input.points.size() < 3) {
13 error = "points needs at least three x y pairs";
14 return std::nullopt;
15 }
16 const auto region = region_of(placement, error);
17 if (!region)
18 return std::nullopt;
19
20 const auto triangles = Kinesis::triangulate_polygon(input.points, {});
21 if (triangles.empty()) {
22 error = "the outline does not form a valid region";
23 return std::nullopt;
24 }
25
26 const size_t capacity = triangles.size() * 3 * sizeof(Kakshya::MeshVertex);
28 [unit = input.points, triangles, region = *region, color = input.color,
29 horizontal = input.horizontal](const float& value, std::vector<uint8_t>& out) {
30 const float scale = std::clamp(value, 0.F, 1.F);
31 std::vector<Kakshya::Vertex> verts;
32 verts.reserve(triangles.size() * 3);
33 for (const auto& triangle : triangles) {
34 for (const uint32_t k : triangle) {
35 glm::vec2 point = unit[k];
36 (horizontal ? point.x : point.y) *= scale;
37 const glm::vec2 ndc = region.point_at(point);
38 verts.push_back(Kakshya::Vertex {
39 .position = { ndc.x, ndc.y, 0.F },
40 .color = color,
41 });
42 }
43 }
45 },
46 rect_containment(*region));
47
48 Geometry::Form<float> form { std::move(fn), Graphics::PrimitiveTopology::TRIANGLE_LIST, capacity };
50 Kinesis::axis_fraction(*region, 0.F, input.horizontal));
51 return form;
52}
53
54std::optional<Geometry::Form<std::vector<glm::vec2>>> handle_path_form(
55 const HandlePathInputs& input, const RectPlacementInputs& placement, std::string& error)
56{
57 if (input.points.size() < 2) {
58 error = "points needs at least two x y pairs";
59 return std::nullopt;
60 }
61 for (const glm::vec2 point : input.points) {
62 if (point.x < 0.F || point.x > 1.F || point.y < 0.F || point.y > 1.F) {
63 error = "points must lie in the unit square";
64 return std::nullopt;
65 }
66 }
67 if (!(input.thickness >= 0.F) || !(input.handle_size >= 0.F)) {
68 error = "thickness and handle_size may not be negative";
69 return std::nullopt;
70 }
71 if (!(input.grab_radius > 0.F)) {
72 error = "grab_radius needs to be positive";
73 return std::nullopt;
74 }
75 const auto region = region_of(placement, error);
76 if (!region)
77 return std::nullopt;
78
79 return Geometry::handle_path(*region, input.points.size(), input.path_color,
80 input.handle_color, input.thickness, input.handle_size, input.grab_radius);
81}
82
83}
std::vector< MeshVertex > to_mesh_vertices(std::span< const Vertex > vertices, glm::vec2 weight_range)
Batch-project raw Vertex vector to MeshVertex.
std::vector< std::array< uint32_t, 3 > > triangulate_polygon(std::span< const glm::vec2 > pts, std::span< const uint32_t > hole_starts)
Ear-clipping triangulation of a polygon with optional holes, as index triples.
std::function< float(glm::vec2)> axis_fraction(AABB2D bounds, float handle_extent=0.F, bool horizontal=true, const std::function< glm::vec2()> &live_offset={}) noexcept
Fraction along one axis of bounds, inverse to fader placement.
auto drag_with(std::function< T(glm::vec2)> project, IO::MouseButtons btn=IO::MouseButtons::Left)
Produce a Form::wire that registers a drag writing a projected cursor position.
Form< std::vector< glm::vec2 > > handle_path(Kinesis::AABB2D bounds, size_t handles, glm::vec3 path_color, glm::vec3 handle_color, float thickness, float handle_size, float grab_radius, const std::function< glm::vec2()> &live_offset)
Geometry function for a path of draggable points in NDC space.
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
std::optional< Kinesis::AABB2D > region_of(const RectPlacementInputs &placement, std::string &error)
The surface region a rect placement covers.
GeometryFn< T > geometry_with_containment(std::function< void(const T &, std::vector< uint8_t > &)> draw, Containment region)
A geometry function that draws a value and claims a fixed containment.
std::optional< Geometry::Form< std::vector< glm::vec2 > > > handle_path_form(const HandlePathInputs &input, const RectPlacementInputs &placement, std::string &error)
A Form of unit square points that a press grabs and a drag moves.
Containment rect_containment(Kinesis::AABB2D region)
The rectangle itself as bounds, with no containment test.
std::optional< Geometry::Form< float > > scale_form(const ScaleInputs &input, const RectPlacementInputs &placement, std::string &error)
A float state Form: an outline scaled along one axis by the state.
Vertex type for indexed triangle mesh primitives (TRIANGLE_LIST topology).
Draggable points in the unit square, drawn as a connected stroke.
A placement by origin and size: x, y, width, height.
An outline in unit space that grows along one axis with a float state.