initial commit of this kluge

This commit is contained in:
Tonton 2018-11-10 11:31:31 +01:00
commit 63611af4f8
5 changed files with 528 additions and 0 deletions

15
Makefile Normal file
View File

@ -0,0 +1,15 @@
#############################################
# making the bubulles library
# maybe must be run with gmake
#############################################
OPT = -Wall -g -DDEBUG_LEVEL=1 -DMUST_ABORT
bubulles.o: bubulles.c bubulles.h Makefile
clang $(OPT) -c $<
tbb: tbb.c bubulles.h bubulles.o Makefile
clang $(OPT) $< bubulles.o -o tbb
#############################################

11
README.txt Normal file
View File

@ -0,0 +1,11 @@
+-------------------------+
| lib bubulles |
+-------------------------+
/!\ warning /!\
This kludge was mainly build on FreeBSD, so ymmv.

322
bubulles.c Normal file
View File

@ -0,0 +1,322 @@
/*
bubulles.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bubulles.h"
/* --------------------------------------------------------------------- */
int bubulles_version(int k)
{
fprintf(stderr, "###\n### LIBBB %s v %d / %s %s\n###\n",
__FILE__, LIBBB_VERSION, __DATE__, __TIME__);
if (k) {
bubulles_sizeof(k);
}
return 0;
}
/* --------------------------------------------------------------------- */
void bubulles_sizeof(int k)
{
if (k & 0x01) printf(" sizeof structs\n");
printf("%-15s %4lu\n", "XYZ", sizeof(XYZ));
printf("%-15s %4lu\n", "RGBA", sizeof(RGBA));
printf("%-15s %4lu\n", "Bubulle", sizeof(Bubulle));
printf("%-15s %4lu\n", "BBList", sizeof(BBList));
puts("");
}
/* --------------------------------------------------------------------- */
BBList * alloc_bubulles(char *name, int sz, int unused)
{
BBList *bblptr;
Bubulle *array;
#if DEBUG_LEVEL
fprintf(stderr, "+++ %s '%s' %d 0x%X\n", __func__, name, sz, unused);
#endif
if (NULL==(bblptr = malloc(sizeof(BBList)))) {
fprintf(stderr, "no mem available in %s\n", __func__);
return NULL;
}
if ( (NULL != name) && (strlen(name) < SZ_BUBULLE_TEXT) )
strcpy(bblptr->name, name);
else
strcpy(bblptr->name, "noname");
bblptr->fidx = 0;
bblptr->size = sz;
bblptr->flags = 0;
if (NULL==(array = calloc(sz, sizeof(Bubulle)))) {
fprintf(stderr, "no mem available in %s\n", __func__);
free(bblptr);
return NULL;
}
bblptr->bbs = array;
#if DEBUG_LEVEL
fprintf(stderr, "\t\tbblptr is at %p\n", bblptr);
#endif
return bblptr;
}
/* --------------------------------------------------------------------- */
int free_bubulles(BBList *bbl, int k)
{
if (NULL == bbl->bbs) {
fprintf(stderr, "%s : array ptr is null\n", __func__);
#if MUST_ABORT
abort();
#endif
return 1;
}
free(bbl->bbs);
/* it's safe to erase the metadata header (nsa) */
memset(bbl, 0, sizeof(BBList));
/* release a clean memory */ free(bbl);
return 0;
}
/* --------------------------------------------------------------------- */
Bubulle * bubulle_getaddr(BBList *where, int idx)
{
#if DEBUG_LEVEL > 1
fprintf(stderr, ">>> %s ( %p %d )\n", __func__, where, idx);
#endif
if ( (idx < 0) || (idx > where->fidx) ) {
fprintf(stderr, "%s : idx %d out of range on %p\n",
__func__, idx, where);
#if MUST_ABORT
abort();
#endif
return NULL;
}
return ( &where->bbs[idx] );
}
/* --------------------------------------------------------------------- */
int print_bublist_desc(BBList *bbl, int flags)
{
printf("------- bblist at %p\n", bbl);
printf("\tname \t'%s'\n", bbl->name);
printf("\tsize\t%6d\n\tfidx\t%6d\n", bbl->size, bbl->fidx);
if (flags & 0x01) {
printf("\txyz\t%f %f %f\n",
bbl->position.x, bbl->position.y, bbl->position.z);
}
printf("\tflags\t0x%08lX\n", bbl->flags);
printf("\tarray\t%p\n", bbl->bbs);
puts(""); fflush(stdout);
return 0;
}
/* --------------------------------------------------------------------- */
int push_bubulle(BBList *where, Bubulle *what)
{
#if DEBUG_LEVEL > 1
fprintf(stderr, "%s : %p --> %p\n", __func__, what, where);
fprintf(stderr, "XYZ %f %f %f\n", what->p.x, what->p.y, what->p.z);
#endif
if (where->fidx > where->size) {
/* this is a very bad fatal error */
fprintf(stderr, "%s : overflow in BBList at %p\n", __func__, where);
abort();
}
if (where->fidx > where->size) {
/* array is full */
#if DEBUG_LEVEL
fprintf(stderr, "%s : array of %p is full\n", __func__, where);
#endif
return -1;
}
memcpy(&where->bbs[where->fidx], what, sizeof(Bubulle));
where->fidx++;
return 0;
}
/* --------------------------------------------------------------------- */
int poke_bubulle(BBList *where, Bubulle *what, int idx)
{
if ((idx < 0) || (idx > where->size)) {
#if DEBUG_LEVEL
fprintf(stderr, "%s : idx %d out of range\n", __func__, idx);
#endif
return -1;
}
#if DEBUG_LEVEL
fprintf(stderr, "%s : %p --> %p+%d\n", __func__, what, where, idx);
fprintf(stderr, "src XYZ %f %f %f\n", what->p.x, what->p.y, what->p.z);
#endif
memcpy(&where->bbs[idx], what, sizeof(Bubulle));
return 0;
}
/* --------------------------------------------------------------------- */
int peek_bubulle(BBList *from, Bubulle *to, int idx)
{
if ((idx < 0) || (idx > from->size)) {
#if DEBUG_LEVEL
fprintf(stderr, "%s : idx %d out of range\n", __func__, idx);
#endif
return -1;
}
memcpy(to, &from->bbs[idx], sizeof(Bubulle));
return 0;
}
/* --------------------------------------------------------------------- */
/* flags :
* 0x0001 print diameter
* 0x0002 print graylevel
*/
int fprint_bubulles(FILE *fp, char *title, BBList *bbl, int flags)
{
int idx;
Bubulle *ar;
ar = bbl->bbs;
#if DEBUG_LEVEL > 1
fprintf(stderr, "*** %s : array at %p, sz %d\n", __func__, ar, bbl->size);
#endif
for (idx=0; idx<bbl->fidx; idx++) {
fprintf(fp, "%11.6f %11.6f %11.6f",
ar[idx].p.x, ar[idx].p.y, ar[idx].p.z);
if (flags & 0x01) fprintf(fp, " %11.6f", ar[idx].d); if (flags & 0x02) fprintf(fp, " %6d", ar[idx].gray);
fputs("\n", fp);
}
fflush(fp);
return 0;
}
/* --------------------------------------------------------------------- */
int niceprint_bubulle(Bubulle *what, int unused)
{
printf("----------------------- @ %p -----------------\n", what);
printf("xyzd %11.6f %11.6f %11.6f %11.6f\n",
what->p.x, what->p.y, what->p.z, what->d);
printf("diam %11.6f gray %5d\n", what->d, what->gray);
printf("rgba %11.6f %11.6f %11.6f %11.6f\n",
what->p.x, what->p.y, what->p.z, what->d);
puts("----------------------------------------------------------");
return 0;
}
/* --------------------------------------------------------------------- */
int cleanfill_my_bublist(BBList *what, int k)
{
Bubulle *ar;
int idx;
if (NULL == what) {
fprintf(stderr, "SHIT HAPPEN IN %s\n", __func__);
abort();
}
ar = what->bbs; /* get the bubble array addr */
#if DEBUG_LEVEL > 2
fprintf(stderr, "*O* %s array at %p, sz %d\n",
__func__, ar, what->size);
#endif
for (idx=0; idx<what->size; idx++) {
memset(&ar[idx], 0, sizeof(Bubulle));
ar[idx].kvalue = 1.0;
}
what->fidx = what->size - 1;
return 0;
}
/* --------------------------------------------------------------------- */
int bubulles_to_data(char *fname, char *title, BBList *bbl, int k)
{
FILE *fp;
int retval;
if (NULL==(fp=fopen(fname, "w"))) {
perror(fname);
return -1;
}
retval = fprint_bubulles(fp, title, bbl, k);
if (retval) {
fprintf(stderr, "something strange (%d) happen in %s\n",
retval, __func__);
}
fclose(fp);
return retval;
}
/* --------------------------------------------------------------------- */
/******* BOUNDING BOXES *******/
int bounding_box(Bubulle *boubs, int nbre, BBox *bbox)
{
int idx;
bbox->minX = bbox->minY = bbox->minZ = 9e99;
bbox->maxX = bbox->maxY = bbox->maxZ = -9e99;
for (idx=0; idx<nbre; idx++) {
if (boubs[idx].p.x > bbox->maxX) bbox->maxX = boubs[idx].p.x;
else if (boubs[idx].p.x < bbox->minX) bbox->minX = boubs[idx].p.x;
if (boubs[idx].p.y > bbox->maxY) bbox->maxY = boubs[idx].p.y;
else if (boubs[idx].p.y < bbox->minY) bbox->minY = boubs[idx].p.y;
if (boubs[idx].p.z > bbox->maxZ) bbox->maxZ = boubs[idx].p.z;
else if (boubs[idx].p.z < bbox->minZ) bbox->minZ = boubs[idx].p.z;
}
return 0;
}
/* --------------------------------------------------------------------- */
int print_bbox(BBox *bbox, int k)
{
printf("%9.6f %9.6f %9.6f %9.6f %9.6f %9.6f\n",
bbox->minX, bbox->minY, bbox->minZ,
bbox->maxX, bbox->maxY, bbox->maxZ);
return 0;
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */

67
bubulles.h Normal file
View File

@ -0,0 +1,67 @@
/*
bubulles.h
*/
/* --------------------------------------------------------------------- */
#define LIBBB_VERSION 40
#define SZ_BUBULLE_TEXT 51 /* arbitrary value */
typedef struct {
double x, y, z;
unsigned long reserved;
} XYZ;
typedef struct {
float r, g, b, a;
unsigned long reserved;
} RGBA;
typedef struct {
XYZ p; /* position */
double d; /* diameter */
int gray;
RGBA col;
long ttl;
double kvalue; /* wtf ? */
} Bubulle;
typedef struct {
char name[SZ_BUBULLE_TEXT+1];
int size; /* max number of bubulles */
int fidx; /* next free slot */
XYZ position;
unsigned long flags;
Bubulle *bbs;
} BBList;
BBList * alloc_bubulles(char *name, int nbre, int k);
int free_bubulles(BBList *bbl, int k);
int print_bublist_desc(BBList *bbl, int k);
int push_bubulle(BBList *where, Bubulle *what);
int poke_bubulle(BBList *where, Bubulle *what, int idx);
int peek_bubulle(BBList *from, Bubulle *to, int idx);
int cleanfill_my_bublist(BBList *what, int k);
int bubulles_version(int k);
void bubulles_sizeof(int k);
Bubulle * bubulle_getaddr(BBList *where, int idx);
int fprint_bubulles(FILE *fp, char *title, BBList *bbl, int k);
int niceprint_bubulle(Bubulle *what, int unused);
int bubulles_to_data(char *fname, char *title, BBList *bbl, int k);
/* --------------------------------------------------------------------- */
typedef struct {
double minX, minY, minZ;
double maxX, maxY, maxZ;
int flags;
} BBox;
int bounding_box(Bubulle *boubs, int nbre, BBox *bbox);
int print_bbox(BBox *bbox, int k);
/* --------------------------------------------------------------------- */

113
tbb.c Normal file
View File

@ -0,0 +1,113 @@
#include <stdio.h>
#include "bubulles.h"
/* --------------------------------------------------------------------- */
void print_sizeof(void)
{
printf("%-15s %4lu\n", "XYZ", sizeof(XYZ));
printf("%-15s %4lu\n", "RGBA", sizeof(RGBA));
printf("%-15s %4lu\n", "Bubulle", sizeof(Bubulle));
printf("%-15s %4lu\n", "BBList", sizeof(BBList));
puts("");
}
/* --------------------------------------------------------------------- */
void test_alloc_free(int nbelm)
{
BBList *bublist;
int foo;
bublist = alloc_bubulles("plop !", nbelm, 0);
print_bublist_desc(bublist, 1);
foo = free_bubulles(bublist, 0);
printf("free bubulles -> %d\n", foo);
}
/* --------------------------------------------------------------------- */
int test_push_pop(int nbelm)
{
BBList *bublist;
int foo, idx;
Bubulle bubulle;
bublist = alloc_bubulles("push & pop", nbelm, 0);
for (idx=0; idx<nbelm; idx++) {
bubulle.p.x = (double)idx;
bubulle.p.y = bubulle.p.z = 0.3;
foo = push_bubulle(bublist, &bubulle);
}
foo = bubulles_to_data("dummy-file", NULL, bublist, 0);
printf("ecriture bubulles -> %d\n", foo);
foo = free_bubulles(bublist, 0);
printf("free bubulles -> %d\n", foo);
return 0;
}
/* --------------------------------------------------------------------- */
int test_cleanfill_my_bublist(int krkrkr)
{
BBList *bublist;
int foo;
bublist = alloc_bubulles("cleanfill", 567890, 0);
foo = cleanfill_my_bublist(bublist, 0);
fprintf(stderr, "clean & fill -> %d\n", foo);
free_bubulles(bublist, 0);
return 0;
}
/* --------------------------------------------------------------------- */
int test_peek_poke(int nbre)
{
BBList *bublist;
int foo, in, out;
Bubulle bubulle;
bublist = alloc_bubulles("peek & poke", nbre, 0);
print_bublist_desc(bublist, 0);
in = nbre / 2;
out = nbre + 42;
foo = peek_bubulle(bublist, &bubulle, in);
printf("peek 1 -> %7d\n", foo);
foo = niceprint_bubulle(&bubulle, 0);
printf("niceprint -> %7d\n", foo);
foo = peek_bubulle(bublist, &bubulle, out);
printf("peek 2 -> %7d\n", foo);
bubulle.p.y = 3.14159;
foo = poke_bubulle(bublist, &bubulle, in);
printf("poke 1 -> %7d\n", foo);
foo = peek_bubulle(bublist, &bubulle, in);
printf("peek 3 -> %7d\n", foo);
foo = niceprint_bubulle(&bubulle, 0);
printf("niceprint -> %7d\n", foo);
return -42;
}
/* --------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int foo;
bubulles_version(0);
// test_alloc_free(5);
// test_push_pop(2000);
// test_cleanfill_my_bublist(999);
foo = test_peek_poke(5000);
fprintf(stderr, "test peek/poke -> %d\n", foo);
return 0;
}