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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 = calloc(1, 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. #if MUST_ABORT
  117. abort();
  118. #endif
  119. return -1;
  120. }
  121. memcpy(&where->bbs[where->fidx], what, sizeof(Bubulle));
  122. where->fidx++;
  123. return 0;
  124. }
  125. /* --------------------------------------------------------------------- */
  126. int poke_bubulle(BBList *where, Bubulle *what, int idx)
  127. {
  128. if ((idx < 0) || (idx > where->size)) {
  129. #if DEBUG_LEVEL
  130. fprintf(stderr, "%s : idx %d out of range\n", __func__, idx);
  131. #endif
  132. return -1;
  133. }
  134. #if DEBUG_LEVEL
  135. fprintf(stderr, "%s : %p --> %p+%d\n", __func__, what, where, idx);
  136. fprintf(stderr, "src XYZ %f %f %f\n", what->p.x, what->p.y, what->p.z);
  137. #endif
  138. memcpy(&where->bbs[idx], what, sizeof(Bubulle));
  139. return 0;
  140. }
  141. /* --------------------------------------------------------------------- */
  142. int peek_bubulle(BBList *from, Bubulle *to, int idx)
  143. {
  144. if (NULL==from) {
  145. fprintf(stderr, "in %s, *from is null\n", __func__);
  146. #ifdef MUST_ABORT
  147. abort();
  148. #endif
  149. return -5;
  150. }
  151. if ((idx < 0) || (idx > from->size)) {
  152. #if DEBUG_LEVEL
  153. fprintf(stderr, "%s : idx %d out of range\n", __func__, idx);
  154. #endif
  155. return -1;
  156. }
  157. memcpy(to, &from->bbs[idx], sizeof(Bubulle));
  158. return 0;
  159. }
  160. /* --------------------------------------------------------------------- */
  161. /* flags :
  162. * 0x0001 print diameter
  163. * 0x0002 print graylevel
  164. * 0x0004 print RGB values
  165. */
  166. int fprint_bubulles(FILE *fp, char *title, BBList *bbl, int opts)
  167. {
  168. int idx;
  169. Bubulle *ar;
  170. if (NULL == bbl) {
  171. fprintf(stderr, "in %s, *bbl is NULL\n", __func__);
  172. #ifdef MUST_ABORT
  173. abort();
  174. #endif
  175. return -5;
  176. }
  177. ar = bbl->bbs;
  178. #if DEBUG_LEVEL > 1
  179. fprintf(stderr, "*** %s : array at %p, sz %d\n", __func__, ar, bbl->size);
  180. #endif
  181. for (idx=0; idx<bbl->fidx; idx++) {
  182. fprintf(fp, "%12.6f %12.6f %12.6f",
  183. ar[idx].p.x, ar[idx].p.y, ar[idx].p.z);
  184. if (opts & 0x01) fprintf(fp, " %12.6f", ar[idx].d);
  185. if (opts & 0x02) fprintf(fp, " %6d", ar[idx].gray);
  186. fputs("\n", fp);
  187. }
  188. fflush(fp);
  189. return 0;
  190. }
  191. /* --------------------------------------------------------------------- */
  192. int niceprint_bubulle(Bubulle *what, int unused)
  193. {
  194. printf("----------------------- @ %p -----------------\n", what);
  195. printf("xyzd %11.6f %11.6f %11.6f %11.6f\n",
  196. what->p.x, what->p.y, what->p.z, what->d);
  197. printf("diam %11.6f gray %5d\n", what->d, what->gray);
  198. printf("rgba %11.6f %11.6f %11.6f %11.6f\n",
  199. what->col.r, what->col.g, what->col.b, what->col.a);
  200. puts("----------------------------------------------------------");
  201. return 0;
  202. }
  203. /* --------------------------------------------------------------------- */
  204. int cleanfill_my_bublist(BBList *what, int k)
  205. {
  206. Bubulle *ar;
  207. int idx;
  208. if (NULL == what) {
  209. fprintf(stderr, "SHIT HAPPEN IN %s\n", __func__);
  210. #ifdef MUST_ABORT
  211. abort();
  212. #endif
  213. return -5;
  214. }
  215. ar = what->bbs; /* get the bubble array addr */
  216. #if DEBUG_LEVEL > 2
  217. fprintf(stderr, "*O* %s array at %p, sz %d\n",
  218. __func__, ar, what->size);
  219. #endif
  220. for (idx=0; idx<what->size; idx++) {
  221. memset(&ar[idx], 0, sizeof(Bubulle));
  222. ar[idx].kvalue = 1.0;
  223. }
  224. what->fidx = what->size - 1;
  225. return 0;
  226. }
  227. /* --------------------------------------------------------------------- */
  228. /*
  229. * this is the WTF function * see tbb.c
  230. */
  231. int bubulles_to_data(char *fname, char *title, BBList *bbl, int k)
  232. {
  233. FILE *fp;
  234. int retval;
  235. if (NULL==(fp=fopen(fname, "w"))) {
  236. perror(fname);
  237. return -1;
  238. }
  239. retval = fprint_bubulles(fp, title, bbl, k);
  240. if (retval) {
  241. fprintf(stderr, "something strange (%d) happen in %s\n",
  242. retval, __func__);
  243. }
  244. fclose(fp);
  245. return retval;
  246. }
  247. /* --------------------------------------------------------------------- */
  248. /******* BOUNDING BOXES *******/
  249. int bounding_box(Bubulle *boubs, int nbre, BBox *bbox)
  250. {
  251. int idx;
  252. bbox->minX = bbox->minY = bbox->minZ = 9e99;
  253. bbox->maxX = bbox->maxY = bbox->maxZ = -9e99;
  254. for (idx=0; idx<nbre; idx++) {
  255. if (boubs[idx].p.x > bbox->maxX) bbox->maxX = boubs[idx].p.x;
  256. else if (boubs[idx].p.x < bbox->minX) bbox->minX = boubs[idx].p.x;
  257. if (boubs[idx].p.y > bbox->maxY) bbox->maxY = boubs[idx].p.y;
  258. else if (boubs[idx].p.y < bbox->minY) bbox->minY = boubs[idx].p.y;
  259. if (boubs[idx].p.z > bbox->maxZ) bbox->maxZ = boubs[idx].p.z;
  260. else if (boubs[idx].p.z < bbox->minZ) bbox->minZ = boubs[idx].p.z;
  261. }
  262. return 0;
  263. }
  264. /* --------------------------------------------------------------------- */
  265. int print_bbox(BBox *bbox, int k)
  266. {
  267. if (NULL==bbox) {
  268. fprintf(stderr, "in %s, *bbox is NULL\n", __func__);
  269. #ifdef MUST_ABORT
  270. abort();
  271. #endif
  272. return -5;
  273. }
  274. printf("%9.6f %9.6f %9.6f %9.6f %9.6f %9.6f\n",
  275. bbox->minX, bbox->minY, bbox->minZ,
  276. bbox->maxX, bbox->maxY, bbox->maxZ);
  277. return 0;
  278. }
  279. /* --------------------------------------------------------------------- */
  280. /* --------------------------------------------------------------------- */
  281. /* --------------------------------------------------------------------- */