From 22266479a99ff82946ee7eb698166a548fcf77c9 Mon Sep 17 00:00:00 2001
From: Philip Trettner <Philip.Trettner@rwth-aachen.de>
Date: Sun, 26 May 2019 11:47:12 +0200
Subject: [PATCH] working on 2D api

---
 vector/glow-extras/vector/graphics2D.hh | 64 ++++++++++++++++++++++++-
 vector/glow-extras/vector/image2D.hh    | 19 ++++++++
 2 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/vector/glow-extras/vector/graphics2D.hh b/vector/glow-extras/vector/graphics2D.hh
index 4a282338..824e5ce8 100644
--- a/vector/glow-extras/vector/graphics2D.hh
+++ b/vector/glow-extras/vector/graphics2D.hh
@@ -2,7 +2,7 @@
 
 #include "image2D.hh"
 
-#include <typed-geometry/tg-lean.hh>
+#include <typed-geometry/tg.hh>
 
 namespace glow
 {
@@ -13,19 +13,81 @@ struct stroke2D
 {
     tg::color4 color;
     float width = 1.0f;
+
+    stroke2D() = default;
+    stroke2D(tg::color3 const& c) : color(c, 1.0f), width(1.0f) {}
+    stroke2D(tg::color4 const& c, float w = 1.0f) : color(c), width(w) {}
 };
 struct fill2D
 {
     tg::color4 color;
+
+    fill2D() = default;
+    fill2D(tg::color4 const& c) : color(c) {}
+    fill2D(tg::color3 const& c) : color(c) {}
 };
 
 /// High-level API for writing to images
 /// See Vector2DSample in glow-samples
+///
+/// Usage:
+///   auto g = graphics(img);
+///   g.draw(<tg-obj>, <stroke>);
+///   g.fill(<tg-obj>, <fill>);
+///
+/// Example:
+///   g.draw(tg::pos3{...}, {1, 0, 0}); // 1px red dot
+///   g.draw(tg::segment3{...}, {tg::color3::blue, 5.0f}); // 5px blue segment
 struct graphics2D
 {
     explicit graphics2D(image2D& img) : img(img) {}
 
+    // drawing
+public:
+    void draw(tg::pos2 const& p, stroke2D const& stroke)
+    {
+        img.begin_path();
+        img.rect({p - .5f, p + .5f});
+        perform_draw(stroke);
+    }
+
+    void draw(tg::segment2 const& s, stroke2D const& stroke)
+    {
+        img.begin_path();
+        img.move_to(s.pos0);
+        img.line_to(s.pos1);
+        perform_draw(stroke);
+    }
+
+    void draw(tg::aabb2 const& r, stroke2D const& stroke)
+    {
+        img.begin_path();
+        img.rect(r);
+        perform_draw(stroke);
+    }
+
+    // filling
+public:
+    void fill(tg::aabb2 const& r, fill2D const& fill)
+    {
+        img.begin_path();
+        img.rect(r);
+        perform_fill(fill);
+    }
+
 private:
+    void perform_draw(stroke2D const& s)
+    {
+        img.set_stroke_color(s.color);
+        img.set_stroke_width(s.width);
+        img.stroke();
+    }
+    void perform_fill(fill2D const& f)
+    {
+        img.set_fill_color(f.color);
+        img.fill();
+    }
+
     image2D_low_level_api img;
 };
 
diff --git a/vector/glow-extras/vector/image2D.hh b/vector/glow-extras/vector/image2D.hh
index 0b886416..70bc84a4 100644
--- a/vector/glow-extras/vector/image2D.hh
+++ b/vector/glow-extras/vector/image2D.hh
@@ -7,6 +7,7 @@
 #include "cmd2D.hh"
 
 #include <typed-geometry/common/assert.hh>
+#include <typed-geometry/tg-lean.hh>
 
 namespace glow
 {
@@ -83,6 +84,24 @@ struct image2D_low_level_api
     void clip_rect(float x, float y, float w, float h) { img << cmd2D::clip_rect << x << y << w << h; }
     void reset_clip() { img << cmd2D::reset_clip; }
 
+    // tg versions:
+    void move_to(tg::pos2 const& p) { move_to(p.x, p.y); }
+    void line_to(tg::pos2 const& p) { line_to(p.x, p.y); }
+    void bezier_to(tg::pos2 const& c1, tg::pos2 const& c2, tg::pos2 const& p) { bezier_to(c1.x, c1.y, c2.x, c2.y, p.x, p.y); }
+    void quad_to(tg::pos2 const& c, tg::pos2 const& p) { quad_to(c.x, c.y, p.x, p.y); }
+    void arc_to(tg::pos2 const& p1, tg::pos2 const& p2, float radius) { arc_to(p1.x, p1.y, p2.x, p2.y, radius); }
+
+    // TODO: arc, rounded_rect_varying, ellipse
+    void rect(tg::aabb2 const& r) { rect(r.min.x, r.min.y, r.max.x - r.min.x, r.max.y - r.min.y); }
+    void rounded_rect(tg::aabb2 const& r, float radius) { rounded_rect(r.min.x, r.min.y, r.max.x - r.min.x, r.max.y - r.min.y, radius); }
+    void circle(tg::pos2 const& p, float r) { circle(p.x, p.y, r); }
+
+    void set_stroke_color(tg::color4 const& c) { set_stroke_color(c.r, c.g, c.b, c.a); }
+    void set_fill_color(tg::color4 const& c) { set_fill_color(c.r, c.g, c.b, c.a); }
+
+    void clip_rect(tg::aabb2 const& r) { clip_rect(r.min.x, r.min.y, r.max.x - r.min.x, r.max.y - r.min.y); }
+
+
 private:
     image2D& img;
 };
-- 
GitLab