Skip to content

Commit 841c141

Browse files
committed
Interpolators for line segment, and faster transforms
1 parent 41eb2b5 commit 841c141

File tree

5 files changed

+74
-30
lines changed

5 files changed

+74
-30
lines changed

libraries/pico_vector/pico_vector.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ add_library(pico_vector
66

77
target_include_directories(pico_vector INTERFACE ${CMAKE_CURRENT_LIST_DIR})
88

9-
target_link_libraries(pico_vector pico_stdlib)
9+
target_link_libraries(pico_vector pico_stdlib hardware_interp)

libraries/pico_vector/pico_vector.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,42 @@ namespace pimoroni {
1010
}
1111

1212
void PicoVector::rotate(std::vector<pretty_poly::contour_t<picovector_point_type>> &contours, Point origin, float angle) {
13-
pretty_poly::mat3_t t2 = pretty_poly::mat3_t::translation(origin.x, origin.y);
14-
pretty_poly::mat3_t t1 = pretty_poly::mat3_t::translation(-origin.x, -origin.y);
15-
angle = 2 * M_PI * (angle / 360.0f);
16-
pretty_poly::mat3_t r = pretty_poly::mat3_t::rotation(angle);
13+
pretty_poly::point_t<picovector_point_type> t{(picovector_point_type)origin.x, (picovector_point_type)origin.y};
14+
angle = (2 * (float)M_PI / 360.f) * angle;
15+
pretty_poly::mat2_t r = pretty_poly::mat2_t::rotation(angle);
1716
for(auto &contour : contours) {
1817
for(auto i = 0u; i < contour.count; i++) {
19-
contour.points[i] *= t1;
18+
contour.points[i] -= t;
2019
contour.points[i] *= r;
21-
contour.points[i] *= t2;
20+
contour.points[i] += t;
2221
}
2322
}
2423
}
2524

2625
void PicoVector::translate(std::vector<pretty_poly::contour_t<picovector_point_type>> &contours, Point translation) {
27-
pretty_poly::mat3_t t = pretty_poly::mat3_t::translation(translation.x, translation.y);
26+
pretty_poly::point_t<picovector_point_type> t{(picovector_point_type)translation.x, (picovector_point_type)translation.y};
2827
for(auto &contour : contours) {
2928
for(auto i = 0u; i < contour.count; i++) {
30-
contour.points[i] *= t;
29+
contour.points[i] += t;
3130
}
3231
}
3332
}
3433

3534
void PicoVector::rotate(pretty_poly::contour_t<picovector_point_type> &contour, Point origin, float angle) {
36-
pretty_poly::mat3_t t2 = pretty_poly::mat3_t::translation(origin.x, origin.y);
37-
pretty_poly::mat3_t t1 = pretty_poly::mat3_t::translation(-origin.x, -origin.y);
38-
angle = 2 * M_PI * (angle / 360.0f);
39-
pretty_poly::mat3_t r = pretty_poly::mat3_t::rotation(angle);
35+
pretty_poly::point_t<picovector_point_type> t{(picovector_point_type)origin.x, (picovector_point_type)origin.y};
36+
angle = (2 * (float)M_PI / 360.f) * angle;
37+
pretty_poly::mat2_t r = pretty_poly::mat2_t::rotation(angle);
4038
for(auto i = 0u; i < contour.count; i++) {
41-
contour.points[i] *= t1;
39+
contour.points[i] -= t;
4240
contour.points[i] *= r;
43-
contour.points[i] *= t2;
41+
contour.points[i] += t;
4442
}
4543
}
4644

4745
void PicoVector::translate(pretty_poly::contour_t<picovector_point_type> &contour, Point translation) {
48-
pretty_poly::mat3_t t = pretty_poly::mat3_t::translation(translation.x, translation.y);
46+
pretty_poly::point_t<picovector_point_type> t{(picovector_point_type)translation.x, (picovector_point_type)translation.y};
4947
for(auto i = 0u; i < contour.count; i++) {
50-
contour.points[i] *= t;
48+
contour.points[i] += t;
5149
}
5250
}
5351

@@ -115,7 +113,7 @@ namespace pimoroni {
115113
pretty_poly::point_t<float> caret(0, 0);
116114

117115
// Prepare a transformation matrix for character and offset rotation
118-
angle = 2 * M_PI * (angle / 360.0f);
116+
angle = (2 * (float)M_PI / 360.f) * angle;
119117
pretty_poly::mat3_t transform = pretty_poly::mat3_t::rotation(angle);
120118

121119
// Align text from the bottom left

libraries/pico_vector/pretty_poly.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "pretty_poly.hpp"
1010

11+
#include "hardware/interp.h"
1112

1213
#ifdef PP_DEBUG
1314
#define debug(...) printf(__VA_ARGS__)
@@ -79,8 +80,8 @@ namespace pretty_poly {
7980
std::swap(sx, ex);
8081
}
8182

82-
// Early out if line is completely outside the tile
83-
if (ey < 0 || sy >= (int)node_buffer_size) return;
83+
// Early out if line is completely outside the tile, or has no lines
84+
if (ey < 0 || sy >= (int)node_buffer_size || sy == ey) return;
8485

8586
debug(" + line segment from %d, %d to %d, %d\n", sx, sy, ex, ey);
8687

@@ -123,13 +124,16 @@ namespace pretty_poly {
123124
x += xinc * xjump;
124125
}
125126

127+
interp1->base[1] = full_tile_width;
128+
interp1->accum[0] = x;
129+
126130
// loop over scanlines
127131
while(count--) {
128132
// consume accumulated error
129-
while(e > dy) {e -= dy; x += xinc;}
133+
while(e > dy) {e -= dy; interp1->add_raw[0] = xinc;}
130134

131135
// clamp node x value to tile bounds
132-
int nx = std::max(std::min(x, full_tile_width), 0);
136+
const int nx = interp1->peek[0];
133137
debug(" + adding node at %d, %d\n", x, y);
134138
// add node to node list
135139
nodes[y][node_counts[y]++] = nx;
@@ -270,6 +274,14 @@ namespace pretty_poly {
270274
debug(" - bounds %d, %d (%d x %d)\n", polygon_bounds.x, polygon_bounds.y, polygon_bounds.w, polygon_bounds.h);
271275
debug(" - clip %d, %d (%d x %d)\n", settings::clip.x, settings::clip.y, settings::clip.w, settings::clip.h);
272276

277+
interp_hw_save_t interp1_save;
278+
interp_save(interp1, &interp1_save);
279+
280+
interp_config cfg = interp_default_config();
281+
interp_config_set_clamp(&cfg, true);
282+
interp_config_set_signed(&cfg, true);
283+
interp_set_config(interp1, 0, &cfg);
284+
interp1->base[0] = 0;
273285

274286
//memset(nodes, 0, node_buffer_size * sizeof(unsigned) * 32);
275287

@@ -303,18 +315,21 @@ namespace pretty_poly {
303315
// render the tile
304316
rect_t bounds;
305317
render_nodes(tile, bounds);
306-
307-
tile.data += bounds.x + tile.stride * bounds.y;
308-
bounds.x += tile.bounds.x;
309-
bounds.y += tile.bounds.y;
310-
tile.bounds = bounds.intersection(tile.bounds);
311-
if (tile.bounds.empty()) {
318+
if (bounds.empty()) {
312319
continue;
313320
}
314321

322+
tile.data += bounds.x + tile.stride * bounds.y;
323+
tile.bounds.x += bounds.x;
324+
tile.bounds.y += bounds.y;
325+
tile.bounds.w = bounds.w;
326+
tile.bounds.h = bounds.h;
327+
315328
settings::callback(tile);
316329
}
317330
}
331+
332+
interp_restore(interp1, &interp1_save);
318333
}
319334
}
320335

libraries/pico_vector/pretty_poly_types.hpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace pretty_poly {
1414

1515
// 3x3 matrix for coordinate transformations
1616
struct mat3_t {
17-
float v00, v10, v20, v01, v11, v21, v02, v12, v22 = 0.0f;
17+
float v00 = 0.0f, v10 = 0.0f, v20 = 0.0f, v01 = 0.0f, v11 = 0.0f, v21 = 0.0f, v02 = 0.0f, v12 = 0.0f, v22 = 0.0f;
1818
mat3_t() = default;
1919
mat3_t(const mat3_t &m) = default;
2020
inline mat3_t& operator*= (const mat3_t &m) {
@@ -43,6 +43,29 @@ namespace pretty_poly {
4343
mat3_t r = mat3_t::identity(); r.v00 = x; r.v11 = y; return r;}
4444
};
4545

46+
// 2x2 matrix for rotations and scales
47+
struct mat2_t {
48+
float v00 = 0.0f, v10 = 0.0f, v01 = 0.0f, v11 = 0.0f;
49+
mat2_t() = default;
50+
mat2_t(const mat2_t &m) = default;
51+
inline mat2_t& operator*= (const mat2_t &m) {
52+
float r00 = this->v00 * m.v00 + this->v01 * m.v10;
53+
float r01 = this->v00 * m.v01 + this->v01 * m.v11;
54+
float r10 = this->v10 * m.v00 + this->v11 * m.v10;
55+
float r11 = this->v10 * m.v01 + this->v11 * m.v11;
56+
this->v00 = r00; this->v01 = r01;
57+
this->v10 = r10; this->v11 = r11;
58+
return *this;
59+
}
60+
61+
static mat2_t identity() {mat2_t m; m.v00 = m.v11 = 1.0f; return m;}
62+
static mat2_t rotation(float a) {
63+
float c = cosf(a), s = sinf(a); mat2_t r;
64+
r.v00 = c; r.v01 = -s; r.v10 = s; r.v11 = c; return r;}
65+
static mat2_t scale(float x, float y) {
66+
mat2_t r; r.v00 = x; r.v11 = y; return r;}
67+
};
68+
4669
// point type for contour points
4770
template<typename T = int>
4871
struct __attribute__ ((packed)) point_t {
@@ -52,6 +75,7 @@ namespace pretty_poly {
5275
inline point_t& operator-= (const point_t &a) {x -= a.x; y -= a.y; return *this;}
5376
inline point_t& operator+= (const point_t &a) {x += a.x; y += a.y; return *this;}
5477
inline point_t& operator*= (const float a) {x *= a; y *= a; return *this;}
78+
inline point_t& operator*= (const mat2_t &a) {this->transform(a); return *this;}
5579
inline point_t& operator*= (const mat3_t &a) {this->transform(a); return *this;}
5680
inline point_t& operator/= (const float a) {x /= a; y /= a; return *this;}
5781
inline point_t& operator/= (const point_t &a) {x /= a.x; y /= a.y; return *this;}
@@ -60,6 +84,11 @@ namespace pretty_poly {
6084
this->x = (m.v00 * tx + m.v01 * ty + m.v02);
6185
this->y = (m.v10 * tx + m.v11 * ty + m.v12);
6286
}
87+
void transform(const mat2_t &m) {
88+
float tx = x, ty = y;
89+
this->x = (m.v00 * tx + m.v01 * ty);
90+
this->y = (m.v10 * tx + m.v11 * ty);
91+
}
6392

6493
};
6594

@@ -78,7 +107,7 @@ namespace pretty_poly {
78107
int x, y, w, h;
79108
rect_t() : x(0), y(0), w(0), h(0) {}
80109
rect_t(int x, int y, int w, int h) : x(x), y(y), w(w), h(h) {}
81-
bool empty() const {return this->w == 0 && this->h == 0;}
110+
bool empty() const {return this->w == 0 || this->h == 0;}
82111
rect_t intersection(const rect_t &c) {
83112
return rect_t(std::max(this->x, c.x), std::max(this->y, c.y),
84113
std::max(0, std::min(this->x + this->w, c.x + c.w) - std::max(this->x, c.x)),

micropython/modules/picovector/micropython.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ target_compile_definitions(usermod_picovector INTERFACE
1818
MODULE_PICOVECTOR_ENABLED=1
1919
)
2020

21+
target_link_libraries(usermod_picovector INTERFACE hardware_interp)
22+
2123
target_link_libraries(usermod INTERFACE usermod_picovector)

0 commit comments

Comments
 (0)