PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/brlcad/tags/rel-7-14-2/src/adrt/slave/load_MySQL.c

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
C | 221 lines | 130 code | 41 blank | 50 comment | 14 complexity | 96a04789fb9a226f0e473a24c157a37b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, Apache-2.0, AGPL-3.0, LGPL-3.0, GPL-3.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, 0BSD, BSD-3-Clause
  1. /* L O A D . C
  2. * BRL-CAD / ADRT
  3. *
  4. * Copyright (c) 2007-2009 United States Government as represented by
  5. * the U.S. Army Research Laboratory.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * version 2.1 as published by the Free Software Foundation.
  10. *
  11. * This library is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this file; see the file named COPYING for more
  18. * information.
  19. */
  20. /** @file load.c
  21. *
  22. */
  23. #ifndef TIE_PRECISION
  24. # define TIE_PRECISION 0
  25. #endif
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "bu.h"
  30. #include "tienet.h"
  31. #include "load.h"
  32. #ifdef HAVE_MYSQL
  33. # include <mysql.h>
  34. # define ADRT_MYSQL_USER "adrt"
  35. # define ADRT_MYSQL_PASS "adrt"
  36. # define ADRT_MYSQL_DB "adrt"
  37. MYSQL slave_load_mysql_db;
  38. int
  39. slave_load_MySQL (uint32_t pid, tie_t *tie, const char *hostname)
  40. {
  41. MYSQL_RES *res;
  42. MYSQL_ROW row;
  43. TIE_3 *vlist, **tlist;
  44. char query[256];
  45. uint32_t gid, gsize, gind, mnum, tnum, vnum, f32num, *f32list, i, mind;
  46. uint16_t f16num, *f16list;
  47. uint8_t c, ftype;
  48. void *gdata;
  49. /* establish mysql connection */
  50. mysql_init (&slave_load_mysql_db);
  51. /* establish connection to database */
  52. if ( mysql_real_connect (&slave_load_mysql_db, hostname, ADRT_MYSQL_USER, ADRT_MYSQL_PASS, ADRT_MYSQL_DB, 0, 0, 0) == NULL ) {
  53. printf("Unable to connect to db: %s\n", mysql_error(&slave_load_mysql_db));
  54. return -1;
  55. }
  56. /* Obtain the geometry id for this project id */
  57. sprintf (query, "select gid from project where pid = '%d'", pid);
  58. mysql_query (&slave_load_mysql_db, query);
  59. res = mysql_use_result (&slave_load_mysql_db);
  60. if (res == NULL) {
  61. printf("Unable to get gid... \"%s\"\n", query);
  62. return -1;
  63. }
  64. row = mysql_fetch_row (res);
  65. gid = atoi (row[0]);
  66. mysql_free_result (res);
  67. /*
  68. * Now that a gid has been obtained, query for the size of the binary data,
  69. * allocate memory for it, extract it, and process it.
  70. */
  71. snprintf (query, 256, "select trinum,meshnum,gsize,gdata from geometry where gid = '%d'", gid);
  72. mysql_query (&slave_load_mysql_db, query);
  73. res = mysql_use_result (&slave_load_mysql_db);
  74. row = mysql_fetch_row (res);
  75. tnum = atoi (row[0]);
  76. mnum = atoi (row[1]);
  77. gsize = atoi (row[2]);
  78. gdata = row[3];
  79. /* initialize tie */
  80. tie_init (tie, tnum, TIE_KDTREE_FAST);
  81. /*
  82. * Process geometry data
  83. */
  84. slave_load_mesh_num = mnum;
  85. slave_load_mesh_list = (adrt_mesh_t *) bu_realloc (slave_load_mesh_list, slave_load_mesh_num * sizeof(adrt_mesh_t), "mesh list");
  86. mind = 0;
  87. /* skip over endian */
  88. gind = sizeof(uint16_t);
  89. /* For each mesh */
  90. while (gind < gsize) {
  91. slave_load_mesh_list[mind].texture = NULL;
  92. slave_load_mesh_list[mind].flags = 0;
  93. slave_load_mesh_list[mind].attributes = (adrt_mesh_attributes_t *)bu_malloc(sizeof(adrt_mesh_attributes_t), "mesh attributes");
  94. /* length of name string */
  95. memcpy(&c, &((char *)gdata)[gind], 1);
  96. gind += 1;
  97. /* name */
  98. memcpy(slave_load_mesh_list[mind].name, &((char *)gdata)[gind], c);
  99. gind += c;
  100. /* Assign default attributes */
  101. VSET(slave_load_mesh_list[mind].attributes->color.v, 0.8, 0.8, 0.8);
  102. /* vertice num */
  103. memcpy(&vnum, &((char *)gdata)[gind], sizeof(uint32_t));
  104. gind += sizeof(uint32_t);
  105. /* vertice list */
  106. vlist = (TIE_3 *)&((char *)gdata)[gind];
  107. gind += vnum * sizeof(TIE_3);
  108. /* face type */
  109. memcpy(&ftype, &((char *)gdata)[gind], 1);
  110. gind += 1;
  111. if (ftype) {
  112. /* face num 32-bit */
  113. memcpy(&f32num, &((char *)gdata)[gind], sizeof(uint32_t));
  114. gind += sizeof(uint32_t);
  115. f32list = (uint32_t *)&((char *)gdata)[gind];
  116. gind += 3 * f32num * sizeof(uint32_t);
  117. tlist = (TIE_3 **)bu_malloc(3 * f32num * sizeof(TIE_3 *), "tlist32");
  118. for (i = 0; i < 3*f32num; i++)
  119. tlist[i] = &vlist[f32list[i]];
  120. /* assign the current mesh to group of triangles */
  121. tie_push (tie, tlist, f32num, &slave_load_mesh_list[mind], 0);
  122. } else {
  123. /* face num 16-bit */
  124. memcpy(&f16num, &((char *)gdata)[gind], sizeof(uint16_t));
  125. gind += sizeof(uint16_t);
  126. f16list = (uint16_t *)&((char *)gdata)[gind];
  127. gind += 3 * f16num * sizeof(uint16_t);
  128. tlist = (TIE_3 **)bu_malloc(3 * f16num * sizeof(TIE_3 *), "tlist16");
  129. for (i = 0; i < 3*f16num; i++)
  130. tlist[i] = &vlist[f16list[i]];
  131. /* assign the current mesh to group of triangles */
  132. tie_push (tie, tlist, f16num, &slave_load_mesh_list[mind], 0);
  133. }
  134. bu_free (tlist, "tlist");
  135. mind++;
  136. }
  137. /* free mysql result */
  138. mysql_free_result (res);
  139. /* Query the mesh attributes map for the attribute id */
  140. for (i = 0; i < slave_load_mesh_num; i++) {
  141. snprintf (query, 256, "select attr from meshattrmap where mesh='%s' and gid=%d", slave_load_mesh_list[i].name, gid);
  142. if (!mysql_query (&slave_load_mysql_db, query)) {
  143. char attr[48];
  144. res = mysql_use_result (&slave_load_mysql_db);
  145. row = mysql_fetch_row (res);
  146. strncpy (attr, row[0], 48-1);
  147. attr[48-1] = '\0'; /* sanity */
  148. /* printf("attr: %s\n", attr); */
  149. mysql_free_result (res);
  150. snprintf (query, 256, "select data from attribute where gid='%d' and name='%s' and type='color'", gid, attr);
  151. /* printf("query[%d]: %s\n", i, query); */
  152. if (!mysql_query (&slave_load_mysql_db, query)) {
  153. res = mysql_use_result (&slave_load_mysql_db);
  154. while ((row = mysql_fetch_row (res)))
  155. sscanf(row[0], "%f %f %f", &slave_load_mesh_list[i].attributes->color.v[0], &slave_load_mesh_list[i].attributes->color.v[1], &slave_load_mesh_list[i].attributes->color.v[2]);
  156. mysql_free_result (res);
  157. }
  158. }
  159. }
  160. /* Query to see if there is acceleration data for this geometry. */
  161. snprintf (query, 256, "select asize,adata from geometry where gid='%d'", gid);
  162. mysql_query (&slave_load_mysql_db, query);
  163. res = mysql_use_result (&slave_load_mysql_db);
  164. row = mysql_fetch_row (res);
  165. if (atoi(row[0]) > 0) {
  166. printf ("acceleration structure found\n");
  167. tie_kdtree_cache_load (tie, row[1], atoi(row[0]));
  168. }
  169. mysql_free_result (res);
  170. mysql_close (&slave_load_mysql_db);
  171. return 0;
  172. }
  173. #else
  174. int
  175. slave_load_MySQL (uint32_t pid, tie_t *tie, const char *hostname)
  176. {
  177. bu_log("MySQL not supported in this build.\n");
  178. return -1;
  179. }
  180. #endif