2023-11-26 15:56:00 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "calculator.h"
|
|
|
|
#include "heap.h"
|
2023-11-27 15:14:06 +01:00
|
|
|
#include "wasm.h"
|
2023-11-26 15:56:00 +01:00
|
|
|
extern "C" void EXPORT(init) (const int memlen);
|
|
|
|
extern "C" int EXPORT(cAlloc) (int len);
|
|
|
|
extern "C" void EXPORT(compute) (char * ptr, int len);
|
|
|
|
extern "C" void EXPORT(resizeCanvas)(int x, int y);
|
|
|
|
extern "C" void IMPORT(drawPoints) (float * x, float * y, int len, char * json, int jl);
|
|
|
|
extern "C" void __wasm_call_ctors ();
|
|
|
|
extern "C++" {
|
|
|
|
typedef struct yy_buffer_state * YY_BUFFER_STATE;
|
|
|
|
extern int yyparse();
|
|
|
|
extern YY_BUFFER_STATE yy_scan_string(const char * str);
|
|
|
|
extern void yy_delete_buffer(YY_BUFFER_STATE buffer);
|
|
|
|
};
|
|
|
|
extern "C" int yywrap () {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
void init (const int memlen) {
|
|
|
|
_HEAP_MAX = reinterpret_cast<char*>(memlen);
|
|
|
|
__wasm_call_ctors();
|
|
|
|
printf("Module initialized\n\n");
|
|
|
|
}
|
|
|
|
int cAlloc (int len) {
|
|
|
|
return int (malloc(len + 1));
|
|
|
|
}
|
|
|
|
void compute (char * ptr, int len) {
|
|
|
|
ptr [len] = '\0';
|
|
|
|
|
|
|
|
initData();
|
|
|
|
YY_BUFFER_STATE result = yy_scan_string(ptr);
|
|
|
|
yyparse();
|
|
|
|
yy_delete_buffer(result);
|
|
|
|
free (ptr);
|
|
|
|
finiData();
|
|
|
|
}
|
|
|
|
static Canvas canvas;
|
|
|
|
void resizeCanvas (int x, int y) {
|
|
|
|
printf("resizeCanvas: x=%d, y=%d\n", x, y);
|
|
|
|
canvas.width = x;
|
|
|
|
canvas.height = y;
|
|
|
|
}
|
2023-11-27 15:14:06 +01:00
|
|
|
static int CreateJSON (char * json, const int json_max, const Canvas & canvas,
|
|
|
|
Stepping & sx, Stepping & sy) {
|
2023-11-26 15:56:00 +01:00
|
|
|
int n = 0;
|
|
|
|
const uint32_t xColor = 0xffff00;
|
|
|
|
const uint32_t yColor = 0x00ffff;
|
|
|
|
const double wd = 4.0; // šířka čárky na osách v px
|
|
|
|
const double pos0x = canvas.xt(0.0) > 0 and canvas.xt(0.0) < canvas.width ? canvas.xt(0.0) : 0.0;
|
|
|
|
const double pos0y = canvas.yt(0.0) > 0 and canvas.yt(0.0) < canvas.height ? canvas.yt(0.0) : canvas.height;
|
|
|
|
n += snprintf(json + n, json_max - n, "{ \"name\":\"axes\", ");
|
|
|
|
n += snprintf(json + n, json_max - n, "\"x\":{ \"color\":\"#%06X\", \"w\":%d, ", xColor, 2);
|
|
|
|
n += snprintf(json + n, json_max - n, "\"b\": [%g,%g], ", 0.0, pos0y);
|
|
|
|
n += snprintf(json + n, json_max - n, "\"e\": [%g,%g] }, ", canvas.width, pos0y);
|
|
|
|
n += snprintf(json + n, json_max - n, "\"y\": { \"color\":\"#%06X\", \"w\":%d, ", yColor, 2);
|
|
|
|
n += snprintf(json + n, json_max - n, "\"b\": [%g,%g], ", pos0x, 0.0);
|
|
|
|
n += snprintf(json + n, json_max - n, "\"e\": [%g,%g] }, \"xdots\": [", pos0x, canvas.height);
|
|
|
|
for (double dx=sx.b; dx<sx.e; dx+=sx.s) {
|
|
|
|
n += snprintf(json + n, json_max - n, "{ \"color\":\"#%06X\", \"w\":%d, \"lbl\":\"%s\", ", xColor, 2, sx.ing(dx));
|
|
|
|
n += snprintf(json + n, json_max - n, "\"b\": [%g,%g], " , canvas.xt(dx), pos0y - wd);
|
|
|
|
n += snprintf(json + n, json_max - n, "\"e\": [%g,%g] }, ", canvas.xt(dx), pos0y + wd);
|
|
|
|
}
|
|
|
|
json [n-2] = ' '; // přepiš poslední čárku, jinak json nefunguje
|
|
|
|
n += snprintf(json + n, json_max - n, "], \"ydots\": [\n");
|
|
|
|
for (double dy=sy.b; dy<sy.e; dy+=sy.s) {
|
|
|
|
n += snprintf(json + n, json_max - n, "{ \"color\":\"#%06X\", \"w\":%d, \"lbl\":\"%s\", ", yColor, 2, sy.ing(dy));
|
|
|
|
n += snprintf(json + n, json_max - n, "\"b\": [%g,%g], " , pos0x - wd, canvas.yt(dy));
|
|
|
|
n += snprintf(json + n, json_max - n, "\"e\": [%g,%g] }, ", pos0x + wd, canvas.yt(dy));
|
|
|
|
}
|
|
|
|
json [n-2] = ' ';
|
|
|
|
n += snprintf(json + n, json_max - n, "] }\n");
|
|
|
|
// printf("%d\n", n);
|
2023-11-27 15:14:06 +01:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
void emitData (double * x, double * y, const int len, const bool first) {
|
|
|
|
int n = 0;
|
|
|
|
const int json_max = 0x8000; // string bude poměrně dlouhý
|
|
|
|
char * json = new char [json_max];
|
|
|
|
if (first) {
|
|
|
|
double ymin = 1.0e100, ymax = -1.0e100;
|
|
|
|
for (int n=0; n<len; n++) {
|
|
|
|
if (y[n] < ymin) ymin = y[n];
|
|
|
|
if (y[n] > ymax) ymax = y[n];
|
|
|
|
// printf("f(%g) = %g\n", x[n], y[n]);
|
|
|
|
}
|
|
|
|
const double xmin = x[0];
|
|
|
|
const double xmax = x[len - 1];
|
|
|
|
printf("xmin = %g, xmax = %g, ymin = %g, ymax = %g\n", xmin, xmax, ymin, ymax);
|
|
|
|
canvas.xofset = xmin;
|
|
|
|
canvas.yofset = ymin;
|
|
|
|
canvas.xscale = canvas.width / (xmax - xmin);
|
|
|
|
canvas.yscale = canvas.height / (ymax - ymin);
|
|
|
|
Stepping sx (xmin, xmax), sy (ymin, ymax);
|
|
|
|
// sx.f(); sy.f();
|
|
|
|
|
|
|
|
n += CreateJSON (json, json_max, canvas, sx, sy);
|
|
|
|
} else {
|
|
|
|
n += snprintf(json + n, json_max - n, "{ \"name\":\"unknown\" }");
|
|
|
|
}
|
2023-11-26 15:56:00 +01:00
|
|
|
/* Pro kreslení na canvas se používají 2 metody:
|
|
|
|
* 1. Přímé předání dat pomocí ukazatelů.
|
|
|
|
* Ukázalo se, že typ int nestačí, zaokrouhlení je kostrbaté, double je zbytečně velký,
|
|
|
|
* takže stačí float (32.bit), je to normováno na velikost canvas, takže tam nejsou skoky v řádech
|
|
|
|
* 2. Z dat uděláme JSON a ten zpracuje javascript. Není to moc efektivní, ale funguje.
|
|
|
|
* (3. zde nepoužito) Vytvoříme přímo javascript zdroják, který to celé vykreslí
|
|
|
|
* a ten pak spustíme v javascriptu pomocí eval(). Funguje také, ale je to divné.
|
|
|
|
* */
|
2023-11-27 15:14:06 +01:00
|
|
|
float * ix = new float [len]; // float32 stačí
|
|
|
|
float * iy = new float [len];
|
|
|
|
for (int k=0; k<len; k++) {
|
|
|
|
ix[k] = canvas.xt (x[k]); // přepočteno na velikost canvasu
|
|
|
|
iy[k] = canvas.yt (y[k]);
|
|
|
|
// printf("f(%d) = %d\n", ix[n], iy[n]);
|
|
|
|
}
|
2023-11-26 15:56:00 +01:00
|
|
|
drawPoints (ix, iy, len, json, n);
|
|
|
|
delete [] ix; delete [] iy;
|
|
|
|
delete [] json;
|
|
|
|
}
|