boat
C++23 geospatial library
Loading...
Searching...
No Matches
wx.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef BOAT_GUI_WX_HPP
4#define BOAT_GUI_WX_HPP
5
6#include <wx/graphics.h>
7#include <wx/mstream.h>
8#include <wx/wx.h>
9#include <boat/gui/detail/geometry.hpp>
10#include <boat/gui/detail/gil.hpp>
11
12namespace boat::gui {
13
14wxGraphicsPath& add(geometry::curve auto&& in, wxGraphicsPath& out)
15{
16 out.MoveToPoint(in.at(0).x(), in.at(0).y());
17 for (size_t i = 1; i < in.size(); ++i)
18 out.AddLineToPoint(in[i].x(), in[i].y());
19 return out;
20}
21
22inline auto draw_geometry(wxGraphicsContext& out)
23{
24 return overloaded{
25 [&](geometry::point auto&& v) { out.DrawEllipse(v.x(), v.y(), 1, 1); },
26 [&](geometry::linestring auto&& v) {
27 auto path = out.CreatePath();
28 out.StrokePath(add(v, path));
29 },
30 [&](geometry::polygon auto&& v) {
31 auto path = out.CreatePath();
32 add(v.outer(), path).CloseSubpath();
33 for (auto& item : v.inners())
34 add(item, path).CloseSubpath();
35 out.DrawPath(path);
36 },
37 [](this auto&& self, geometry::multi auto&& v) -> void {
38 std::ranges::for_each(v, self);
39 },
40 [](this auto&& self, geometry::dynamic auto&& v) -> void {
41 std::visit(self, v);
42 }};
43}
44
45void draw_image( //
46 execution_policy auto policy,
47 boost::gil::rgba8c_view_t in,
48 geometry::matrix const& in_affine,
49 geometry::srs_spec auto&& in_crs,
50 wxGraphicsContext& out,
51 geometry::matrix const& out_affine,
52 geometry::srs_spec auto&& out_crs)
53{
54 auto [fwd, inv] = bidirectional(in_affine, in_crs, out_affine, out_crs);
55 auto intersect = [&](geometry::box auto&& v) {
56 wxDouble w, h;
57 out.GetSize(&w, &h);
58 w = std::ceil(w), h = std::ceil(h);
59 auto a = v.min_corner(), b = v.max_corner();
60 auto x = std::floor(a.x()), y = std::floor(a.y());
61 return wxRect2DDouble{0., 0., w, h}.CreateIntersection(
62 {x, y, std::ceil(b.x()) - x, std::ceil(b.y()) - y});
63 };
64 auto mbr = fwd(multi_point(in.width(), in.height()))
65 .transform(geometry::minmax)
66 .transform(intersect);
67 if (!mbr || mbr->IsEmpty())
68 return;
69 auto img = wxImage{mbr->GetSize()};
70 img.InitAlpha();
71 auto ys = std::views::iota(0, img.GetHeight());
72 auto pixel = get_pixel(in);
73 std::for_each(policy, ys.begin(), ys.end(), [&](int y) {
74 auto d = y * img.GetWidth();
75 auto rgb = reinterpret_cast<wxImage::RGBValue*>(img.GetData()) + d;
76 auto alpha = img.GetAlpha() + d;
77 for (int x{}; x < img.GetWidth(); ++x)
78 if (auto px =
79 inv(geometry::geographic::point(x + mbr->m_x, y + mbr->m_y))
80 .and_then(pixel)) {
81 rgb[x] =
82 wxImage::RGBValue(get_color(*px, boost::gil::red_t()),
83 get_color(*px, boost::gil::green_t()),
84 get_color(*px, boost::gil::blue_t()));
85 alpha[x] = get_color(*px, boost::gil::alpha_t());
86 }
87 else
88 alpha[x] = wxIMAGE_ALPHA_TRANSPARENT;
89 });
90 out.DrawBitmap( //
91 out.CreateBitmapFromImage(img),
92 mbr->m_x,
93 mbr->m_y,
94 mbr->m_width,
95 mbr->m_height);
96}
97
98} // namespace boat::gui
99
100#endif // BOAT_GUI_WX_HPP
Definition vocabulary.hpp:40
Definition vocabulary.hpp:67
Definition vocabulary.hpp:43
Definition vocabulary.hpp:46
Definition vocabulary.hpp:49
Definition vocabulary.hpp:55
Definition vocabulary.hpp:58
Definition vocabulary.hpp:37
boost::qvm::mat< double, 3, 3 > matrix
Definition vocabulary.hpp:114
constexpr auto minmax
Definition algorithm.hpp:77
Definition cache.hpp:11
wxGraphicsPath & add(geometry::curve auto &&in, wxGraphicsPath &out)
Definition wx.hpp:14
void draw_image(execution_policy auto policy, boost::gil::rgba8c_view_t in, geometry::matrix const &in_affine, geometry::srs_spec auto &&in_crs, QPainter &out, geometry::matrix const &out_affine, geometry::srs_spec auto &&out_crs)
Definition qt.hpp:44
auto draw_geometry(QPainter &out)
Definition qt.hpp:30
model::d2::point_xy< double, boost::geometry::cs::geographic< boost::geometry::degree > > point
Definition vocabulary.hpp:75