/* * TinyBlocks - tb_curves.c * Categoria "Curvas": Curvas.Line / Bezier / Quadratic / Arc / Circle / Count / * PointX / PointY. As curvas retornam uma lista de pontos [[x,y],...] (JSON). */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include "tb_curves.h" #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #define CURVES_MAX_STEPS 100000 static int clamp_steps(int steps) { if (steps < 1) return 1; if (steps > CURVES_MAX_STEPS) return CURVES_MAX_STEPS; return steps; } static void push_number(TinyBlocks_JsonValue* arr, double v) { TinyBlocks_JsonValue* n = tb_json_number(v); if (n) { tb_json_array_push(arr, n); tb_json_release(n); } } /* Adiciona o ponto [x,y] ao array. */ static void push_point(TinyBlocks_JsonValue* arr, double x, double y) { TinyBlocks_JsonValue* pt = tb_json_array(); if (!pt) return; push_number(pt, x); push_number(pt, y); tb_json_array_push(arr, pt); tb_json_release(pt); } /* -- Curvas -------------------------------------------------------------- */ TinyBlocks_JsonValue* tb_curves_line(double x0, double y0, double x1, double y1, int steps) { steps = clamp_steps(steps); TinyBlocks_JsonValue* a = tb_json_array(); if (!a) return NULL; for (int i = 0; i <= steps; i++) { double t = (double)i / steps; push_point(a, x0 + (x1 - x0) * t, y0 + (y1 - y0) * t); } return a; } TinyBlocks_JsonValue* tb_curves_bezier(double x0, double y0, double cx1, double cy1, double cx2, double cy2, double x1, double y1, int steps) { steps = clamp_steps(steps); TinyBlocks_JsonValue* a = tb_json_array(); if (!a) return NULL; for (int i = 0; i <= steps; i++) { double t = (double)i / steps, u = 1.0 - t; double b0 = u*u*u, b1 = 3*u*u*t, b2 = 3*u*t*t, b3 = t*t*t; push_point(a, b0*x0 + b1*cx1 + b2*cx2 + b3*x1, b0*y0 + b1*cy1 + b2*cy2 + b3*y1); } return a; } TinyBlocks_JsonValue* tb_curves_quadratic(double x0, double y0, double cx, double cy, double x1, double y1, int steps) { steps = clamp_steps(steps); TinyBlocks_JsonValue* a = tb_json_array(); if (!a) return NULL; for (int i = 0; i <= steps; i++) { double t = (double)i / steps, u = 1.0 - t; push_point(a, u*u*x0 + 2*u*t*cx + t*t*x1, u*u*y0 + 2*u*t*cy + t*t*y1); } return a; } TinyBlocks_JsonValue* tb_curves_arc(double cx, double cy, double r, double a0_deg, double a1_deg, int steps) { steps = clamp_steps(steps); TinyBlocks_JsonValue* a = tb_json_array(); if (!a) return NULL; double a0 = a0_deg * M_PI / 180.0, a1 = a1_deg * M_PI / 180.0; for (int i = 0; i <= steps; i++) { double t = (double)i / steps; double ang = a0 + (a1 - a0) * t; push_point(a, cx + r*cos(ang), cy + r*sin(ang)); } return a; } TinyBlocks_JsonValue* tb_curves_circle(double cx, double cy, double r, int steps) { return tb_curves_arc(cx, cy, r, 0.0, 360.0, steps); } /* -- Leitura ------------------------------------------------------------- */ int tb_curves_count(const TinyBlocks_JsonValue* pts) { if (!pts || tb_json_kind(pts) != TB_JSON_ARRAY) return 0; return (int)tb_json_array_length(pts); } bool tb_curves_point(const TinyBlocks_JsonValue* pts, int i, double* x, double* y) { if (!pts || tb_json_kind(pts) != TB_JSON_ARRAY) return false; if (i < 0 || (uint32_t)i >= tb_json_array_length(pts)) return false; TinyBlocks_JsonValue* p = tb_json_array_get(pts, (uint32_t)i); if (!p) return false; bool ok = tb_json_kind(p) == TB_JSON_ARRAY && tb_json_array_length(p) >= 2; if (ok) { TinyBlocks_JsonValue* px = tb_json_array_get(p, 0); TinyBlocks_JsonValue* py = tb_json_array_get(p, 1); if (x) *x = tb_json_as_number(px, 0.0); if (y) *y = tb_json_as_number(py, 0.0); tb_json_release(px); tb_json_release(py); } tb_json_release(p); return ok; } /* -- Helpers de entrada -------------------------------------------------- */ static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); if (v.kind == TB_VALUE_REAL) return v.as.real; if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer; if (v.kind == TB_VALUE_TEXT && v.as.text) return atof(v.as.text); return 0.0; } static int64_t in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); if (v.kind == TB_VALUE_INTEGER) return v.as.integer; if (v.kind == TB_VALUE_REAL) return (int64_t)v.as.real; return 0; } static TinyBlocks_JsonValue* in_pts(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); return (v.kind == TB_VALUE_JSON) ? v.as.json : NULL; } static void out_pts(TinyBlocks_Block* self, TinyBlocks_JsonValue* pts) { tb_block_set_output(self, 0, tb_value_json(pts)); tb_json_release(pts); self->dirty = false; } /* -- Blocos -------------------------------------------------------------- */ static void eval_line(TinyBlocks_Block* self, TinyBlocks* rt) { out_pts(self, tb_curves_line(in_real(self,rt,0), in_real(self,rt,1), in_real(self,rt,2), in_real(self,rt,3), (int)in_int(self,rt,4))); } static void eval_bezier(TinyBlocks_Block* self, TinyBlocks* rt) { out_pts(self, tb_curves_bezier(in_real(self,rt,0), in_real(self,rt,1), in_real(self,rt,2), in_real(self,rt,3), in_real(self,rt,4), in_real(self,rt,5), in_real(self,rt,6), in_real(self,rt,7), (int)in_int(self,rt,8))); } static void eval_quadratic(TinyBlocks_Block* self, TinyBlocks* rt) { out_pts(self, tb_curves_quadratic(in_real(self,rt,0), in_real(self,rt,1), in_real(self,rt,2), in_real(self,rt,3), in_real(self,rt,4), in_real(self,rt,5), (int)in_int(self,rt,6))); } static void eval_arc(TinyBlocks_Block* self, TinyBlocks* rt) { out_pts(self, tb_curves_arc(in_real(self,rt,0), in_real(self,rt,1), in_real(self,rt,2), in_real(self,rt,3), in_real(self,rt,4), (int)in_int(self,rt,5))); } static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) { out_pts(self, tb_curves_circle(in_real(self,rt,0), in_real(self,rt,1), in_real(self,rt,2), (int)in_int(self,rt,3))); } static void eval_count(TinyBlocks_Block* self, TinyBlocks* rt) { tb_block_set_output(self, 0, tb_value_integer(tb_curves_count(in_pts(self,rt,0)))); self->dirty = false; } static void eval_pointx(TinyBlocks_Block* self, TinyBlocks* rt) { double x = 0, y = 0; tb_curves_point(in_pts(self,rt,0), (int)in_int(self,rt,1), &x, &y); tb_block_set_output(self, 0, tb_value_real(x)); self->dirty = false; } static void eval_pointy(TinyBlocks_Block* self, TinyBlocks* rt) { double x = 0, y = 0; tb_curves_point(in_pts(self,rt,0), (int)in_int(self,rt,1), &x, &y); tb_block_set_output(self, 0, tb_value_real(y)); self->dirty = false; } /* -- Registro ------------------------------------------------------------ */ static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name, const TinyBlocks_SlotDesc* in, uint32_t ni, const TinyBlocks_SlotDesc* out, uint32_t no, void (*fn)(TinyBlocks_Block*, TinyBlocks*)) { TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn); return t && tb_library_add(lib, &t->base); } static bool curves_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { static const TinyBlocks_Category cat = { "Curvas", { 233/255.f, 30/255.f, 99/255.f, 1.f}, { 194/255.f, 24/255.f, 91/255.f, 1.f}, }; tb_library_set_category(lib, &cat); static const TinyBlocks_SlotDesc pts_out[] = {{"pontos", TB_VALUE_JSON}}; static const TinyBlocks_SlotDesc num_out[] = {{"valor", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc int_out[] = {{"n", TB_VALUE_INTEGER}}; TinyBlocks_SlotDesc line_in[5], bez_in[9], quad_in[7], arc_in[6], circ_in[4]; TinyBlocks_SlotDesc cnt_in[1], pt_in[2]; const char* ln[5] = {"x0","y0","x1","y1","passos"}; const char* bn[9] = {"x0","y0","cx1","cy1","cx2","cy2","x1","y1","passos"}; const char* qn[7] = {"x0","y0","cx","cy","x1","y1","passos"}; const char* an[6] = {"cx","cy","raio","ang0","ang1","passos"}; const char* cn[4] = {"cx","cy","raio","passos"}; for (int i = 0; i < 5; i++) line_in[i] = (TinyBlocks_SlotDesc){ln[i], TB_VALUE_NONE}; for (int i = 0; i < 9; i++) bez_in[i] = (TinyBlocks_SlotDesc){bn[i], TB_VALUE_NONE}; for (int i = 0; i < 7; i++) quad_in[i] = (TinyBlocks_SlotDesc){qn[i], TB_VALUE_NONE}; for (int i = 0; i < 6; i++) arc_in[i] = (TinyBlocks_SlotDesc){an[i], TB_VALUE_NONE}; for (int i = 0; i < 4; i++) circ_in[i] = (TinyBlocks_SlotDesc){cn[i], TB_VALUE_NONE}; cnt_in[0] = (TinyBlocks_SlotDesc){"pontos", TB_VALUE_JSON}; pt_in[0] = (TinyBlocks_SlotDesc){"pontos", TB_VALUE_JSON}; pt_in[1] = (TinyBlocks_SlotDesc){"indice", TB_VALUE_INTEGER}; return reg(rt, lib, "Curves.Line", line_in, 5, pts_out, 1, eval_line) && reg(rt, lib, "Curves.Bezier", bez_in, 9, pts_out, 1, eval_bezier) && reg(rt, lib, "Curves.Quadratic", quad_in, 7, pts_out, 1, eval_quadratic) && reg(rt, lib, "Curves.Arc", arc_in, 6, pts_out, 1, eval_arc) && reg(rt, lib, "Curves.Circle", circ_in, 4, pts_out, 1, eval_circle) && reg(rt, lib, "Curves.Count", cnt_in, 1, int_out, 1, eval_count) && reg(rt, lib, "Curves.PointX", pt_in, 2, num_out, 1, eval_pointx) && reg(rt, lib, "Curves.PointY", pt_in, 2, num_out, 1, eval_pointy); } TINYBLOCKS_DEFINE_LIBRARY(curves, "tinyblocks-curves", "1.0.0", curves_init, NULL) const TinyBlocks_LibraryDescriptor* tb_curves_get_library(void) { return &_tb_lib_desc_curves; }