Skip to content

Commit

Permalink
[examples] GeoZones improvements, ImageUI fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Apr 18, 2024
1 parent 491c63a commit a067a4d
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 29 deletions.
14 changes: 10 additions & 4 deletions examples/Advanced/GeoZones/GeoZones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
#include <QDebug>
namespace co
{
static GeoZones::result
pip(const GeoZones::zone& z, double latitude, double longitude, double blur)
static result pip(const zone& z, double latitude, double longitude, double blur)
{
auto pt = GeoZones::geom_point{latitude, longitude};
auto pt = geom_point{latitude, longitude};
bool within = boost::geometry::within(pt, z.polygon);
double distance_side = std::abs(boost::geometry::distance(pt, z.polygon));
double distance_center = std::abs(boost::geometry::distance(pt, z.center));
Expand All @@ -22,7 +21,7 @@ pip(const GeoZones::zone& z, double latitude, double longitude, double blur)
: std::clamp(
std::pow(1. / (1.0 + distance_center), 1e5 * (1. - blur)), 0., 1.);

return GeoZones::result{
return result{
.to_side = (float)distance_side,
.to_center = (float)distance_center,
.influence = (float)influence};
Expand Down Expand Up @@ -106,6 +105,7 @@ void GeoZones::operator()()
}

outputs.zones.value = oscr::to_ossia_value(m_outputs);
send_message({pos_message{inputs.latitude, inputs.longitude}});
}

void GeoZones::loadZones()
Expand Down Expand Up @@ -179,6 +179,12 @@ void GeoZones::loadZones()
boost::geometry::envelope(polys, box);
m_bounding0 = box.min_corner();
m_bounding1 = box.max_corner();

std::vector<std::vector<geom_point>> ptx;
ptx.reserve(m_zones.size());
for(auto& zz : m_zones)
ptx.push_back(zz.positions);
send_message({shape_message{m_bounding0, m_bounding1, std::move(ptx)}});
}
catch(...)
{
Expand Down
152 changes: 128 additions & 24 deletions examples/Advanced/GeoZones/GeoZones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,68 @@
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <halp/controls.hpp>
#include <halp/layout.hpp>
#include <halp/meta.hpp>
#include <ossia/detail/flat_set.hpp>

namespace co
{

using geom_point = boost::geometry::model::d2::point_xy<double>;
using geom_polygon = boost::geometry::model::polygon<geom_point>;
struct zone
{
std::vector<geom_point> positions;
geom_polygon polygon;
geom_point center{};
boost::container::flat_map<std::string, float> attributes;
};

struct result
{
float to_side;
float to_center;
float influence;
};

struct output
{
halp_field_names(inside, closest, per_zone, attributes);
result inside;
result closest;
std::vector<result> per_zone;
boost::container::flat_map<std::string, float> attributes;
};

struct zone_display
{
static constexpr double width() { return 100.; }
static constexpr double height() { return 100.; }

void paint(avnd::painter auto ctx)
{
ctx.set_fill_color({255, 255, 255, 255});
for(const auto& zone : zones)
{
ctx.begin_path();
const double* pos_xy = reinterpret_cast<const double*>(zone.data());
ctx.draw_polygon(pos_xy, zone.size());
ctx.fill();
}

ctx.set_stroke_color({255, 255, 255, 255});
ctx.begin_path();
ctx.draw_line(0, lat * height(), width(), lat * height());
ctx.draw_line(lon * width(), 0, lon * width(), height());
ctx.stroke();
}

std::function<void()> update;

float lat{};
float lon{};
std::vector<std::vector<geom_point>> zones;
};

struct GeoZones
{
halp_meta(name, "Geo Zones")
Expand All @@ -15,7 +72,7 @@ struct GeoZones
halp_meta(author, "Jean-Michaël Celerier, Brice Ammar-Khodja")
halp_meta(uuid, "b5690418-5832-4038-9549-5cc69b77008c")

struct
struct ins
{
struct : halp::lineedit<"Program", "">
{
Expand All @@ -37,35 +94,82 @@ struct GeoZones
void operator()();
void loadZones();

using geom_point = boost::geometry::model::d2::point_xy<double>;
using geom_polygon = boost::geometry::model::polygon<geom_point>;
struct zone
geom_point m_bounding0, m_bounding1;
ossia::flat_set<std::string> m_attributes;
std::vector<zone> m_zones;
output m_outputs;

struct pos_message
{
std::vector<geom_point> positions;
geom_polygon polygon;
geom_point center{};
boost::container::flat_map<std::string, float> attributes;
halp_flag(relocatable);
float lat;
float lon;
};

struct result
struct shape_message
{
float to_side;
float to_center;
float influence;
halp_flag(relocatable);
geom_point b0;
geom_point b1;
std::vector<std::vector<geom_point>> z;
};

struct output
struct ui_message
{
halp_field_names(inside, closest, per_zone, attributes);
result inside;
result closest;
std::vector<result> per_zone;
boost::container::flat_map<std::string, float> attributes;
halp_flag(relocatable);
boost::variant2::variant<pos_message, shape_message> msg;
};

geom_point m_bounding0, m_bounding1;
ossia::flat_set<std::string> m_attributes;
std::vector<zone> m_zones;
output m_outputs;
std::function<void(ui_message)> send_message;

struct ui
{
halp_meta(name, "Main")
halp_meta(layout, halp::layouts::vbox)
halp_meta(width, 100)
halp_meta(height, 100)

halp::item<&ins::zones> z;
halp::item<&ins::latitude> lat;
halp::item<&ins::longitude> lon;
halp::item<&ins::blur> bl;
halp::custom_actions_item<zone_display> anim;

struct bus
{
void init(ui& ui) { }

// 4. Receive a message on the UI thread from the processing thread
static void process(ui& self, pos_message&& msg)
{
self.anim.lat = msg.lat;
self.anim.lon = msg.lon;
self.anim.update();
}

static void process(ui& self, shape_message&& msg)
{
self.anim.zones = msg.z;

auto& b0 = msg.b0;
auto& b1 = msg.b1;

for(auto& zone : self.anim.zones)
{
for(geom_point& pos : zone)
{
pos.x(100. * (pos.x() - b0.x()) / (b1.x() - b0.x()));
pos.y(100. * (pos.y() - b0.y()) / (b1.y() - b0.y()));
}
}
self.anim.update();
}

static void process_message(ui& self, ui_message&& msg)
{
boost::variant2::visit(
[&self](auto&& msg) { process(self, std::move(msg)); }, std::move(msg.msg));
}
};
};
};
}
2 changes: 1 addition & 1 deletion examples/Helpers/ImageUi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ struct AdvancedUi
halp_meta(height, 200)
halp_meta(font, "Inconsolata")

halp::custom_item<custom_slider, &ins::float_ctl> widget{.x = 190, .y = 170};
halp::custom_control<custom_slider, &ins::float_ctl> widget{.x = 190, .y = 170};
halp::custom_actions_item<custom_anim> anim{.x = 90, .y = -50};
};
};
Expand Down
30 changes: 30 additions & 0 deletions include/halp/device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include <halp/static_string.hpp>

#include <string_view>
namespace halp
{

template <halp::static_string lit, typename T>
struct addr_port
{
static clang_buggy_consteval auto name() { return std::string_view{lit.value}; }
static clang_buggy_consteval auto path() { return std::string_view{lit.value}; }
operator T&() noexcept { return value; }
operator const T&() const noexcept { return value; }
auto& operator=(const T& t) noexcept
{
value = t;
return *this;
}
auto& operator=(T&& t) noexcept
{
value = std::move(t);
return *this;
}

// Running value (last value before the tick started)
T value{};
};

}

0 comments on commit a067a4d

Please sign in to comment.