125 lines
5.1 KiB
C
125 lines
5.1 KiB
C
/*
|
|
* curves_test.c - testa tinyblocks-curves: retorno de listas de pontos
|
|
* ([[x,y],...]) e os blocos Curves.Line / Bezier / Quadratic / Arc / Circle /
|
|
* Count / PointX / PointY.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
#include <tinyblocks/tinyblocks.h>
|
|
#include <tinyblocks/tb_json_block.h>
|
|
#include <tb_curves.h>
|
|
|
|
static int fails = 0;
|
|
#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0)
|
|
|
|
static void set_num(TinyBlocks_Block* b, uint32_t i, double v) {
|
|
TinyBlocks_Value vv = tb_value_real(v);
|
|
tb_block_set_default(b, i, vv);
|
|
tb_value_free(&vv);
|
|
}
|
|
static void set_pts(TinyBlocks_Block* b, uint32_t i, TinyBlocks_JsonValue* pts) {
|
|
TinyBlocks_Value vv = tb_value_json(pts);
|
|
tb_block_set_default(b, i, vv);
|
|
tb_value_free(&vv);
|
|
}
|
|
|
|
static bool near(double a, double b) { return fabs(a - b) < 1e-6; }
|
|
|
|
int main(void) {
|
|
/* -- API C: reta ---- */
|
|
TinyBlocks_JsonValue* ln = tb_curves_line(0, 0, 10, 20, 4);
|
|
CHECK(tb_curves_count(ln) == 5, "reta com 4 passos -> 5 pontos");
|
|
double x, y;
|
|
CHECK(tb_curves_point(ln, 2, &x, &y) && near(x, 5) && near(y, 10), "reta: ponto do meio (5,10)");
|
|
CHECK(tb_curves_point(ln, 4, &x, &y) && near(x, 10) && near(y, 20), "reta: ponto final (10,20)");
|
|
CHECK(!tb_curves_point(ln, 5, &x, &y), "reta: indice fora -> false");
|
|
|
|
/* -- Bezier cubica: extremos e simetria --- */
|
|
TinyBlocks_JsonValue* bz = tb_curves_bezier(0, 0, 0, 10, 10, 10, 10, 0, 8);
|
|
CHECK(tb_curves_count(bz) == 9, "bezier com 8 passos -> 9 pontos");
|
|
tb_curves_point(bz, 0, &x, &y);
|
|
CHECK(near(x, 0) && near(y, 0), "bezier: comeca em (0,0)");
|
|
tb_curves_point(bz, 8, &x, &y);
|
|
CHECK(near(x, 10) && near(y, 0), "bezier: termina em (10,0)");
|
|
/* t=0.5 -> (5, 7.5) para essa curva */
|
|
tb_curves_point(bz, 4, &x, &y);
|
|
CHECK(near(x, 5) && near(y, 7.5), "bezier: ponto medio (5,7.5)");
|
|
|
|
/* -- Quadratica --- */
|
|
TinyBlocks_JsonValue* qd = tb_curves_quadratic(0, 0, 5, 10, 10, 0, 2);
|
|
tb_curves_point(qd, 1, &x, &y);
|
|
CHECK(near(x, 5) && near(y, 5), "quadratica: topo (5,5)");
|
|
|
|
/* -- Arco / circulo --- */
|
|
TinyBlocks_JsonValue* ar = tb_curves_arc(0, 0, 10, 0, 90, 2);
|
|
tb_curves_point(ar, 0, &x, &y);
|
|
CHECK(near(x, 10) && near(y, 0), "arco: comeca em (10,0)");
|
|
tb_curves_point(ar, 2, &x, &y);
|
|
CHECK(near(x, 0) && near(y, 10), "arco: termina em (0,10)");
|
|
|
|
TinyBlocks_JsonValue* ci = tb_curves_circle(0, 0, 5, 4);
|
|
CHECK(tb_curves_count(ci) == 5, "circulo fechado com 4 passos -> 5 pontos");
|
|
tb_curves_point(ci, 0, &x, &y);
|
|
tb_curves_point(ci, 4, &x, &y);
|
|
CHECK(near(x, 5) && near(y, 0), "circulo: fecha no ponto inicial");
|
|
|
|
/* limites de passos */
|
|
TinyBlocks_JsonValue* z = tb_curves_line(0, 0, 1, 1, 0);
|
|
CHECK(tb_curves_count(z) == 2, "passos=0 vira 1 -> 2 pontos");
|
|
tb_json_release(z);
|
|
|
|
tb_json_release(ln); tb_json_release(bz); tb_json_release(qd);
|
|
tb_json_release(ar); tb_json_release(ci);
|
|
|
|
/* -- Blocos --- */
|
|
TinyBlocks* rt;
|
|
tinyblocks_init(&rt, NULL);
|
|
tb_library_register(rt, tb_curves_get_library());
|
|
|
|
const char* names[] = {"Curves.Line","Curves.Bezier","Curves.Quadratic",
|
|
"Curves.Arc","Curves.Circle","Curves.Count",
|
|
"Curves.PointX","Curves.PointY"};
|
|
int all = 1;
|
|
for (size_t i = 0; i < sizeof(names)/sizeof(names[0]); i++)
|
|
if (!tb_rt_find_type(rt, names[i])) { all = 0; printf(" falta %s\n", names[i]); }
|
|
CHECK(all, "todos os blocos Curves.* registrados");
|
|
|
|
TinyBlocks_TypeBlock* tl = tb_rt_find_type(rt, "Curves.Line");
|
|
const TinyBlocks_Category* cat = tb_library_category((const TinyBlocks_LibraryBlock*)tl->base.parent);
|
|
CHECK(cat && strcmp(cat->name, "Curvas") == 0, "categoria 'Curvas'");
|
|
|
|
/* Curves.Line via bloco */
|
|
TinyBlocks_Block* bl = tb_block_create(rt, tl);
|
|
set_num(bl, 0, 0); set_num(bl, 1, 0);
|
|
set_num(bl, 2, 8); set_num(bl, 3, 4);
|
|
set_num(bl, 4, 2);
|
|
tb_block_eval(bl, rt);
|
|
CHECK(bl->outputs[0].kind == TB_VALUE_JSON, "Curves.Line devolve JSON");
|
|
CHECK(tb_curves_count(bl->outputs[0].as.json) == 3, "Curves.Line devolve 3 pontos");
|
|
|
|
/* Curves.Count recebe a lista do bloco anterior */
|
|
TinyBlocks_TypeBlock* tc = tb_rt_find_type(rt, "Curves.Count");
|
|
TinyBlocks_Block* bc = tb_block_create(rt, tc);
|
|
set_pts(bc, 0, bl->outputs[0].as.json);
|
|
tb_block_eval(bc, rt);
|
|
CHECK(bc->outputs[0].as.integer == 3, "Curves.Count -> 3");
|
|
|
|
/* Curves.PointX(2) da lista */
|
|
TinyBlocks_TypeBlock* tpx = tb_rt_find_type(rt, "Curves.PointX");
|
|
TinyBlocks_Block* bpx = tb_block_create(rt, tpx);
|
|
set_pts(bpx, 0, bl->outputs[0].as.json);
|
|
TinyBlocks_Value idx = tb_value_integer(2);
|
|
tb_block_set_default(bpx, 1, idx);
|
|
tb_block_eval(bpx, rt);
|
|
CHECK(near(bpx->outputs[0].as.real, 8.0), "Curves.PointX(2) -> 8");
|
|
|
|
tb_block_destroy(bl, rt); tb_block_destroy(bc, rt); tb_block_destroy(bpx, rt);
|
|
tinyblocks_deinit(&rt);
|
|
|
|
if (fails) printf("\n%d FALHA(S)\n", fails);
|
|
else printf("\nTUDO OK\n");
|
|
return fails != 0;
|
|
}
|