Skip to content

Commit

Permalink
fixed some issues with transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Williamson committed Sep 23, 2023
1 parent b505ccf commit 11ab525
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 110 deletions.
37 changes: 17 additions & 20 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: clang++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/${relativeFileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ build active file",
"postDebugTask": "Open image"
}
]
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: clang++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/${relativeFileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ build active file",
"postDebugTask": "Open image"
}
]
}
74 changes: 37 additions & 37 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-I",
".",
"-o",
"${workspaceFolder}/build/${relativeFileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
},
{
"type": "shell",
"label": "Open image",
"command": "open",
"args": [
"/tmp/out.png"
]
}
]
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-I",
".",
"-o",
"${workspaceFolder}/build/${relativeFileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
},
{
"type": "shell",
"label": "Open image",
"command": "open",
"args": [
"/tmp/out.png"
]
}
]
}
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
cmake_minimum_required(VERSION 3.12)

project(examples C)

set(CMAKE_C_STANDARD 17)

project(examples C)

include_directories(
.
)

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")

add_subdirectory(examples)
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include(character.cmake)
include(geometry.cmake)
include(logo.cmake)
57 changes: 57 additions & 0 deletions examples/geometry.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#define PP_DEBUG

#include "pretty-poly.h"

const int WIDTH = 1024;
const int HEIGHT = 1024;
uint32_t buffer[WIDTH][HEIGHT];


void callback(const pp_tile_t tile) {
for(int32_t y = 0; y < tile.bounds.h; y++) {
for(int32_t x = 0; x < tile.bounds.w; x++) {
uint8_t alpha = pp_tile_get_value(&tile, x, y);
uint8_t r = 234;
uint8_t g = 78;
uint8_t b = 63;
buffer[y + tile.bounds.y][x + tile.bounds.x] = alpha << 24 | b << 16 | g << 8 | r;
}
}
}

int main() {
//(callback, PP_AA_X4, {0, 0, WIDTH, HEIGHT});

pp_tile_callback(callback);
pp_antialias(PP_AA_X4);
pp_clip((pp_rect_t){0, 0, WIDTH, HEIGHT});

for(int y = 0; y < HEIGHT; y++) {
for(int x = 0; x < WIDTH; x++) {
buffer[x][y] = ((x / 8) + (y / 8)) % 2 == 0 ? 0xff302010 : 0xff000000;
}
}

pp_point_t square_outline[] = { // outline contour
{-256, -256}, {256, -256}, {256, 256}, {-256, 256}
};

pp_contour_t square_contours[] = {
(pp_contour_t){.points = square_outline, .point_count = sizeof(square_outline) / sizeof(pp_point_t)}
};

pp_polygon_t square = (pp_polygon_t){.contours = square_contours, .contour_count = sizeof(square_contours) / sizeof(pp_contour_t)};

pp_mat3_t transform = pp_mat3_identity();
transform = pp_mat3_mul(transform, pp_mat3_translation(512, 512));
transform = pp_mat3_mul(transform, pp_mat3_rotation(45));
pp_transform(&transform);
draw_polygon(square);

stbi_write_png("/tmp/out.png", WIDTH, HEIGHT, 4, (void *)buffer, WIDTH * sizeof(uint32_t));

return 0;
}
3 changes: 3 additions & 0 deletions examples/geometry.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(
character
character.c)
46 changes: 9 additions & 37 deletions examples/logo.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include "ctype.h"
#include "pretty-poly.h"

const int WIDTH = 256;
const int HEIGHT = 256;
uint32_t buffer[256][256];
const int WIDTH = 1024;
const int HEIGHT = 1024;
uint32_t buffer[1024][1024];

void callback(const pp_tile_t tile) {
//debug_tile(tile);
Expand Down Expand Up @@ -274,7 +274,6 @@ pp_polygon_t parse_svg_path(char *path) {
int main() {

// setup polygon renderer

pp_tile_callback(callback);
pp_antialias(PP_AA_X4);
pp_clip((pp_rect_t){.x = 0, .y = 0, .w = WIDTH, .h = HEIGHT});
Expand All @@ -283,44 +282,17 @@ int main() {
char logo_svg_path[] = "M3260 3933 c-168 -179 -274 -287 -503 -520 -248 -253 -248 -253 -1442 -253 -657 0 -1195 -3 -1195 -6 0 -9 124 -189 132 -192 5 -2 8 -9 8 -15 0 -6 9 -20 19 -31 11 -12 27 -35 38 -53 10 -18 31 -50 47 -73 29 -41 29 -41 -59 -173 -49 -73 -93 -133 -97 -135 -4 -2 -8 -9 -8 -14 0 -6 -17 -34 -38 -62 -21 -28 -42 -57 -46 -64 -6 -10 154 -12 805 -12 446 0 814 -1 816 -4 2 -2 -9 -23 -25 -47 -34 -51 -104 -188 -122 -239 -7 -19 -16 -44 -20 -55 -53 -128 -67 -261 -69 -641 -1 -117 -4 -164 -13 -171 -7 -6 -31 -13 -53 -16 -22 -3 -47 -8 -56 -12 -19 -7 -32 20 -50 110 -7 33 -13 61 -15 63 -6 8 -85 -51 -115 -86 -83 -97 -98 -161 -80 -347 20 -205 30 -241 83 -294 45 -46 99 -67 205 -80 126 -15 263 -65 396 -145 35 -20 113 -100 158 -161 24 -33 49 -66 56 -72 6 -7 13 -18 15 -24 3 -9 10 -9 27 0 12 7 20 19 17 26 -2 7 1 16 9 18 7 3 28 36 46 74 30 63 32 76 32 168 0 55 -4 111 -10 125 -6 14 -10 27 -9 30 0 3 -12 27 -28 54 -28 48 -28 48 11 90 82 86 150 228 169 351 6 43 17 61 78 130 39 44 73 82 76 85 43 43 192 269 185 280 -3 6 -2 10 4 10 27 0 190 372 210 480 5 30 13 87 17 125 4 39 8 75 9 80 1 6 3 30 3 55 2 45 2 45 734 43 403 -2 729 0 723 5 -5 4 -14 15 -20 24 -5 9 -65 98 -132 197 -68 99 -123 186 -123 192 0 7 6 20 14 28 8 9 69 97 135 196 122 180 122 180 -494 183 -564 2 -640 5 -606 26 4 3 11 23 15 44 3 21 13 61 21 88 8 27 31 108 51 179 20 72 45 162 55 200 11 39 28 102 39 140 10 39 23 87 30 108 6 21 10 43 8 48 -2 6 -32 -20 -68 -58z m-2188 -993 c-3 -149 1 -152 43 -24 14 43 35 98 46 122 20 43 35 50 87 36 19 -6 22 -11 17 -35 -4 -15 -15 -46 -26 -68 -10 -22 -19 -46 -19 -54 0 -7 -4 -17 -9 -23 -5 -5 -16 -29 -24 -54 -15 -45 -15 -45 18 -82 40 -43 51 -98 41 -195 -12 -112 -50 -143 -177 -143 -43 0 -81 5 -84 10 -8 13 -14 476 -7 578 5 73 5 73 51 70 46 -3 46 -3 43 -138z m476 107 c2 -10 1 -29 -2 -43 -6 -22 -11 -24 -70 -24 -63 0 -64 0 -70 -31 -3 -17 -6 -62 -6 -100 0 -69 0 -69 56 -69 55 0 55 0 52 -42 -3 -43 -3 -43 -55 -46 -53 -3 -53 -3 -53 -93 0 -89 0 -89 70 -89 70 0 70 0 70 -39 0 -48 -4 -50 -127 -50 -69 0 -94 4 -104 15 -9 11 -10 51 -5 162 4 81 7 187 6 236 -2 135 9 234 27 238 8 2 59 1 112 -2 83 -4 96 -8 99 -23z m820 21 c8 -8 12 -53 12 -132 1 -104 12 -189 33 -246 4 -8 8 -28 11 -45 15 -90 19 -111 26 -120 5 -5 10 -30 12 -55 3 -44 3 -45 -30 -48 -44 -4 -59 10 -67 61 -3 23 -10 60 -15 82 -5 22 -12 62 -15 89 -8 59 -21 50 -34 -24 -14 -80 -39 -189 -46 -200 -8 -14 -58 -13 -72 1 -12 12 -9 56 7 94 4 11 15 52 25 90 9 39 26 106 37 150 13 48 23 122 25 185 2 58 6 111 8 118 6 15 67 16 83 0z m869 -32 c43 -46 47 -85 36 -334 -8 -192 -11 -211 -31 -240 -43 -59 -157 -65 -212 -11 -37 37 -43 100 -36 357 6 182 7 195 28 222 30 36 71 50 133 45 41 -4 56 -10 82 -39z m485 -86 c3 -75 15 -159 28 -210 12 -47 26 -105 31 -130 5 -25 14 -65 20 -90 20 -85 18 -95 -19 -98 -41 -4 -62 12 -62 48 0 15 -6 45 -13 66 -7 22 -13 44 -13 49 0 6 -4 39 -9 75 -8 65 -8 65 -25 -5 -10 -38 -23 -101 -31 -140 -7 -38 -19 -76 -25 -82 -16 -17 -59 -17 -72 0 -11 13 -8 31 48 242 23 85 34 156 40 245 10 156 12 162 59 158 36 -3 36 -3 43 -128z m-2979 78 c3 -24 4 -82 3 -129 -3 -87 -3 -87 43 -92 106 -13 134 -53 135 -195 0 -93 -16 -142 -54 -162 -28 -15 -193 -30 -205 -19 -6 6 -11 132 -13 324 -4 315 -4 315 41 315 45 0 45 0 50 -42z m1007 -238 c0 -280 0 -280 46 -280 45 0 45 0 42 -42 -3 -43 -3 -43 -121 -46 -140 -3 -157 3 -157 58 0 40 0 40 45 40 45 0 45 0 46 68 1 37 3 123 5 192 2 69 3 162 4 208 0 82 0 82 45 82 45 0 45 0 45 -280z m312 3 c3 -278 3 -278 46 -281 43 -3 43 -3 40 -45 -3 -42 -3 -42 -118 -45 -134 -3 -170 7 -170 47 0 37 17 51 62 51 43 0 40 -26 39 305 -1 77 2 164 5 193 6 52 6 52 50 52 44 0 44 0 46 -277z m698 148 c0 -128 0 -128 58 -134 50 -5 61 -10 89 -42 24 -28 33 -50 39 -92 16 -130 -14 -214 -84 -232 -40 -11 -168 -18 -175 -11 -8 8 -18 626 -11 633 4 4 25 7 46 7 38 0 38 0 38 -129z m815 84 c0 -40 0 -40 -66 -43 -66 -3 -66 -3 -72 -160 -3 -86 -6 -208 -7 -271 0 -63 -3 -118 -6 -123 -3 -4 -23 -8 -45 -8 -39 0 -39 0 -39 325 0 326 0 326 118 323 117 -3 117 -3 117 -43z";
pp_polygon_t polygon = parse_svg_path(logo_svg_path);

// determine extreme bounds
// determine extreme bounds and scaling factor to fit on canvas
pp_rect_t bounds = pp_polygon_bounds(polygon);
float scale = (float)WIDTH / (float)bounds.w;

// scale contours to same size as canvas
int scale = bounds.w > bounds.h ? bounds.w : bounds.h;
for(uint32_t i = 0; i < polygon.contour_count; i++) {
pp_contour_t *contour = &polygon.contours[i];

for(uint32_t j = 0; j < contour->point_count; j++) {
pp_point_t *point = &contour->points[j];
point->x = ((point->x - bounds.x) * WIDTH) / scale;
point->y = ((point->y - bounds.y) * WIDTH) / scale;
}
}

/*#ifdef PERFORMANCE_TEST
uint64_t min_time = 1000000000;
for (int j = 0; j < 15; ++j) {
//uint64_t start_time = get_time_ns();
for (int i = 0; i < 60; ++i) {
draw_polygon(polygon, (pp_point_t){0, 0}, 65536);
}
min_time = std::min(min_time, get_time_ns() - start_time);
}
printf("Draw time: %lldus\n", min_time / 1000);
#else*/
//draw_polygon(polygon, &transform);
//transform = pp_mat3_scale(0.5, 0.5);
pp_mat3_t transform = pp_mat3_identity();
transform = pp_mat3_mul(transform, pp_mat3_translation(-128, -117));
transform = pp_mat3_mul(transform, pp_mat3_rotation(0.5));
transform = pp_mat3_mul(transform, pp_mat3_translation(128, 117));
transform = pp_mat3_mul(transform, pp_mat3_translation(512, 512));
transform = pp_mat3_mul(transform, pp_mat3_scale(scale, scale));
transform = pp_mat3_mul(transform, pp_mat3_rotation(45));
transform = pp_mat3_mul(transform, pp_mat3_translation(-bounds.w / 2, -bounds.h / 2));
pp_transform(&transform);
draw_polygon(polygon);
//#endif

stbi_write_png("/tmp/out.png", WIDTH, HEIGHT, 4, (void *)buffer, WIDTH * sizeof(uint32_t));

Expand Down
31 changes: 19 additions & 12 deletions pretty-poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typedef struct {
} pp_mat3_t;
static pp_mat3_t pp_mat3_identity();
static pp_mat3_t pp_mat3_rotation(float a);
static pp_mat3_t pp_mat3_rotation_rad(float a);
static pp_mat3_t pp_mat3_translation(float x, float y);
static pp_mat3_t pp_mat3_scale(float x, float y);
static pp_mat3_t pp_mat3_mul(pp_mat3_t m1, pp_mat3_t m2);
Expand Down Expand Up @@ -118,13 +119,17 @@ static void pp_transform(pp_mat3_t *transform);
#endif

// helpers
int _pp_max(int32_t a, int32_t b) { return a > b ? a : b; }
int _pp_min(int32_t a, int32_t b) { return a < b ? a : b; }
int _pp_max(int32_t a, int32_t b) { return a > b ? a : b; }
int _pp_min(int32_t a, int32_t b) { return a < b ? a : b; }
int32_t _pp_sign(int32_t v) {return ((uint32_t)-v >> 31) - ((uint32_t)v >> 31);}
void _pp_swap(int32_t *a, int32_t *b) {int32_t t = *a; *a = *b; *b = t;}

// pp_mat3_t implementation
static pp_mat3_t pp_mat3_identity() {
pp_mat3_t m; memset(&m, 0, sizeof(pp_mat3_t)); m.v00 = m.v11 = m.v22 = 1.0f; return m;}
static pp_mat3_t pp_mat3_rotation(float a) {
return pp_mat3_rotation_rad(a * M_PI / 180.0f);}
static pp_mat3_t pp_mat3_rotation_rad(float a) {
float c = cosf(a), s = sinf(a); pp_mat3_t r = pp_mat3_identity();
r.v00 = c; r.v01 = s; r.v10 = -s; r.v11 = c; return r;}
static pp_mat3_t pp_mat3_translation(float x, float y) {
Expand Down Expand Up @@ -264,11 +269,7 @@ void pp_transform(pp_mat3_t *transform) {
}


// dy step (returns 1, 0, or -1 if the supplied value is > 0, == 0, < 0)
int32_t sign(int32_t v) {
// assumes 32-bit int/unsigned
return ((uint32_t)-v >> 31) - ((uint32_t)v >> 31);
}


// write out the tile bits
void debug_tile(const pp_tile_t *tile) {
Expand Down Expand Up @@ -325,7 +326,7 @@ void add_line_segment_to_nodes(const pp_point_t start, const pp_point_t end) {
int x = sx;
int e = 0;

const int xinc = sign(ex - sx);
const int xinc = _pp_sign(ex - sx);
const int einc = abs(ex - sx) + 1;
const int dy = ey - sy;

Expand Down Expand Up @@ -386,26 +387,32 @@ void build_nodes(const pp_contour_t contour, const pp_tile_t tile) {

// start with the last point to close the loop
pp_point_t last = {
.x = (contour.points[contour.point_count - 1].x) * aa_scale,
.y = (contour.points[contour.point_count - 1].y) * aa_scale
.x = (contour.points[contour.point_count - 1].x),
.y = (contour.points[contour.point_count - 1].y)
};

if(_pp_transform) {
last = pp_point_transform(last, _pp_transform);
}

last.x *= aa_scale;
last.y *= aa_scale;

last = pp_point_sub(last, tile_origin);

for(uint32_t i = 0; i < contour.point_count; i++) {
pp_point_t point = {
.x = (contour.points[i].x) * aa_scale,
.y = (contour.points[i].y) * aa_scale
.x = (contour.points[i].x),
.y = (contour.points[i].y)
};

if(_pp_transform) {
point = pp_point_transform(point, _pp_transform);
}

point.x *= aa_scale;
point.y *= aa_scale;

point = pp_point_sub(point, tile_origin);

add_line_segment_to_nodes(last, point);
Expand Down

0 comments on commit 11ab525

Please sign in to comment.