des bubulles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bubulles.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. ---------- bubulles.c ----------------------------
  3. some functions for managing bubulles in a 3D space.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "bubulles.h"
  9. /* --------------------------------------------------------------------- */
  10. int bubulles_version(int k)
  11. {
  12. fprintf(stderr, "###\n### LIBBB %s v %d / %s %s\n###\n",
  13. __FILE__, LIBBB_VERSION, __DATE__, __TIME__);
  14. if (k) {
  15. bubulles_sizeof(k);
  16. }
  17. return 0;
  18. }
  19. /* --------------------------------------------------------------------- */
  20. void bubulles_sizeof(int k)
  21. {
  22. if (k & 0x01) printf(" sizeof structs\n");
  23. printf("%-15s %4lu\n", "XYZ", sizeof(XYZ));
  24. printf("%-15s %4lu\n", "RGBA", sizeof(RGBA));
  25. printf("%-15s %4lu\n", "Bubulle", sizeof(Bubulle));
  26. printf("%-15s %4lu\n", "BBList", sizeof(BBList));
  27. puts("");
  28. }
  29. /* --------------------------------------------------------------------- */
  30. BBList * alloc_bubulles(char *name, int sz, int unused)
  31. {
  32. BBList *bblptr;
  33. Bubulle *array;
  34. #if DEBUG_LEVEL
  35. fprintf(stderr, "+++ %s '%s' %d 0x%X\n", __func__, name, sz, unused);
  36. #endif
  37. if (NULL==(bblptr = malloc(sizeof(BBList)))) {
  38. fprintf(stderr, "no mem available in %s\n", __func__);
  39. return NULL;
  40. }
  41. if ( (NULL != name) && (strlen(name) < SZ_BUBULLE_TEXT) )
  42. strcpy(bblptr->name, name);
  43. else
  44. strcpy(bblptr->name, "noname");
  45. bblptr->fidx = 0;
  46. bblptr->size = sz;
  47. bblptr->flags = 0;
  48. if (NULL==(array = calloc(sz, sizeof(Bubulle)))) {
  49. fprintf(stderr, "no mem available in %s\n", __func__);
  50. free(bblptr);
  51. return NULL;
  52. }
  53. bblptr->bbs = array;
  54. #if DEBUG_LEVEL
  55. fprintf(stderr, "\tbblptr is at %p\n", bblptr);
  56. #endif
  57. return bblptr;
  58. }
  59. /* --------------------------------------------------------------------- */
  60. int free_bubulles(BBList *bbl, int k)
  61. {
  62. if (NULL == bbl->bbs) {
  63. fprintf(stderr, "%s : array ptr is null\n", __func__);
  64. #ifdef MUST_ABORT
  65. abort();
  66. #endif
  67. return 1;
  68. }
  69. free(bbl->bbs);
  70. /* it's safe to erase the metadata header (nsa) */
  71. memset(bbl, 0, sizeof(BBList));
  72. /* release a clean memory */ free(bbl);
  73. return 0;
  74. }
  75. /* --------------------------------------------------------------------- */
  76. Bubulle * bubulle_getaddr(BBList *where, int idx)
  77. {
  78. #if DEBUG_LEVEL > 1
  79. fprintf(stderr, ">>> %s ( %p %d )\n", __func__, where, idx);
  80. #endif
  81. if ( (idx < 0) || (idx > where->fidx) ) {
  82. fprintf(stderr, "%s : idx %d out of range on %p\n",
  83. __func__, idx, where);
  84. #ifdef MUST_ABORT
  85. abort();
  86. #endif
  87. return NULL;
  88. }
  89. return ( &where->bbs[idx] );
  90. }
  91. /* --------------------------------------------------------------------- */
  92. int print_bublist_desc(BBList *bbl, int opts)
  93. {
  94. printf("------- bblist at %p\n", bbl);
  95. printf("\tname \t'%s'\n", bbl->name);
  96. printf("\tsize\t%6d\n\tfidx\t%6d\n", bbl->size, bbl->fidx);
  97. if (opts & 0x01) {
  98. printf("\txyz\t%f %f %f\n",
  99. bbl->position.x, bbl->position.y, bbl->position.z);
  100. }
  101. printf("\tflags\t0x%08lX\n", bbl->flags);
  102. printf("\tarray\t%p\n", bbl->bbs);
  103. puts(""); fflush(stdout);
  104. return 0;
  105. }
  106. /* --------------------------------------------------------------------- */
  107. int push_bubulle(BBList *where, Bubulle *what)
  108. {
  109. #if DEBUG_LEVEL > 1
  110. fprintf(stderr, "%s : %p --> %p\n", __func__, what, where);
  111. fprintf(stderr, "XYZ %f %f %f\n", what->p.x, what->p.y, what->p.z);
  112. #endif
  113. if (where->fidx > where->size) {
  114. /* this is a very bad fatal error */
  115. fprintf(stderr, "%s : overflow in BBList at %p\n", __func__, where);
  116. abort();
  117. }
  118. if (where->fidx > where->size) {
  119. /* array is full */
  120. #if DEBUG_LEVEL
  121. fprintf(stderr, "%s : array of %p is full\n", __func__, where);
  122. #endif
  123. return -1;
  124. }
  125. memcpy(&where->bbs[where->fidx], what, sizeof(Bubulle));
  126. where->fidx++;
  127. return 0;
  128. }
  129. /* --------------------------------------------------------------------- */
  130. int poke_bubulle(BBList *where, Bubulle *what, int idx)
  131. {
  132. if ((idx < 0) || (idx > where->size)) {
  133. #if DEBUG_LEVEL
  134. fprintf(stderr, "%s : idx %d out of range\n", __func__, idx);
  135. #endif
  136. return -1;
  137. }
  138. #if DEBUG_LEVEL
  139. fprintf(stderr, "%s : %p --> %p+%d\n", __func__, what, where, idx);
  140. fprintf(stderr, "src XYZ %f %f %f\n", what->p.x, what->p.y, what->p.z);
  141. #endif
  142. memcpy(&where->bbs[idx], what, sizeof(Bubulle));
  143. return 0;
  144. }
  145. /* --------------------------------------------------------------------- */
  146. int peek_bubulle(BBList *from, Bubulle *to, int idx)
  147. {
  148. if (NULL==from) {
  149. fprintf(stderr, "in %s, *from is null\n", __func__);
  150. #ifdef MUST_ABORT
  151. abort();
  152. #endif
  153. return -5;
  154. }
  155. if ((idx < 0) || (idx > from->size)) {
  156. #if DEBUG_LEVEL
  157. fprintf(stderr, "%s : idx %d out of range\n", __func__, idx);
  158. #endif
  159. return -1;
  160. }
  161. memcpy(to, &from->bbs[idx], sizeof(Bubulle));
  162. return 0;
  163. }
  164. /* --------------------------------------------------------------------- */
  165. /* flags :
  166. * 0x0001 print diameter
  167. * 0x0002 print graylevel
  168. * 0x0004 print RGB values
  169. */
  170. int fprint_bubulles(FILE *fp, char *title, BBList *bbl, int opts)
  171. {
  172. int idx;
  173. Bubulle *ar;
  174. if (NULL == bbl) {
  175. fprintf(stderr, "in %s, *bbl is NULL\n", __func__);
  176. #ifdef MUST_ABORT
  177. abort();
  178. #endif
  179. return -5;
  180. }
  181. ar = bbl->bbs;
  182. #if DEBUG_LEVEL > 1
  183. fprintf(stderr, "*** %s : array at %p, sz %d\n", __func__, ar, bbl->size);
  184. #endif
  185. for (idx=0; idx<bbl->fidx; idx++) {
  186. fprintf(fp, "%12.6f %12.6f %12.6f",
  187. ar[idx].p.x, ar[idx].p.y, ar[idx].p.z);
  188. if (opts & 0x01) fprintf(fp, " %12.6f", ar[idx].d);
  189. if (opts & 0x02) fprintf(fp, " %6d", ar[idx].gray);
  190. fputs("\n", fp);
  191. }
  192. fflush(fp);
  193. return 0;
  194. }
  195. /* --------------------------------------------------------------------- */
  196. int niceprint_bubulle(Bubulle *what, int unused)
  197. {
  198. printf("----------------------- @ %p -----------------\n", what);
  199. printf("xyzd %11.6f %11.6f %11.6f %11.6f\n",
  200. what->p.x, what->p.y, what->p.z, what->d);
  201. printf("diam %11.6f gray %5d\n", what->d, what->gray);
  202. printf("rgba %11.6f %11.6f %11.6f %11.6f\n",
  203. what->p.x, what->p.y, what->p.z, what->d);
  204. puts("----------------------------------------------------------");
  205. return 0;
  206. }
  207. /* --------------------------------------------------------------------- */
  208. int cleanfill_my_bublist(BBList *what, int k)
  209. {
  210. Bubulle *ar;
  211. int idx;
  212. if (NULL == what) {
  213. fprintf(stderr, "SHIT HAPPEN IN %s\n", __func__);
  214. #ifdef MUST_ABORT
  215. abort();
  216. #endif
  217. return -5;
  218. }
  219. ar = what->bbs; /* get the bubble array addr */
  220. #if DEBUG_LEVEL > 2
  221. fprintf(stderr, "*O* %s array at %p, sz %d\n",
  222. __func__, ar, what->size);
  223. #endif
  224. for (idx=0; idx<what->size; idx++) {
  225. memset(&ar[idx], 0, sizeof(Bubulle));
  226. ar[idx].kvalue = 1.0;
  227. }
  228. what->fidx = what->size - 1;
  229. return 0;
  230. }
  231. /* --------------------------------------------------------------------- */
  232. /*
  233. * this is the WTF function * see tbb.c
  234. */
  235. int bubulles_to_data(char *fname, char *title, BBList *bbl, int k)
  236. {
  237. FILE *fp;
  238. int retval;
  239. if (NULL==(fp=fopen(fname, "w"))) {
  240. perror(fname);
  241. return -1;
  242. }
  243. retval = fprint_bubulles(fp, title, bbl, k);
  244. if (retval) {
  245. fprintf(stderr, "something strange (%d) happen in %s\n",
  246. retval, __func__);
  247. }
  248. fclose(fp);
  249. return retval;
  250. }
  251. /* --------------------------------------------------------------------- */
  252. /******* BOUNDING BOXES *******/
  253. int bounding_box(Bubulle *boubs, int nbre, BBox *bbox)
  254. {
  255. int idx;
  256. bbox->minX = bbox->minY = bbox->minZ = 9e99;
  257. bbox->maxX = bbox->maxY = bbox->maxZ = -9e99;
  258. for (idx=0; idx<nbre; idx++) {
  259. if (boubs[idx].p.x > bbox->maxX) bbox->maxX = boubs[idx].p.x;
  260. else if (boubs[idx].p.x < bbox->minX) bbox->minX = boubs[idx].p.x;
  261. if (boubs[idx].p.y > bbox->maxY) bbox->maxY = boubs[idx].p.y;
  262. else if (boubs[idx].p.y < bbox->minY) bbox->minY = boubs[idx].p.y;
  263. if (boubs[idx].p.z > bbox->maxZ) bbox->maxZ = boubs[idx].p.z;
  264. else if (boubs[idx].p.z < bbox->minZ) bbox->minZ = boubs[idx].p.z;
  265. }
  266. return 0;
  267. }
  268. /* --------------------------------------------------------------------- */
  269. int print_bbox(BBox *bbox, int k)
  270. {
  271. if (NULL==bbox) {
  272. fprintf(stderr, "in %s, *bbox is NULL\n", __func__);
  273. #ifdef MUST_ABORT
  274. abort();
  275. #endif
  276. return -5;
  277. }
  278. printf("%9.6f %9.6f %9.6f %9.6f %9.6f %9.6f\n",
  279. bbox->minX, bbox->minY, bbox->minZ,
  280. bbox->maxX, bbox->maxY, bbox->maxZ);
  281. return 0;
  282. }
  283. /* --------------------------------------------------------------------- */
  284. /* --------------------------------------------------------------------- */
  285. /* --------------------------------------------------------------------- */