nviz.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*!
  2. \file lib/nviz/nviz.c
  3. \brief Nviz library -- Data management
  4. Based on visualization/nviz/src/
  5. (C) 2008, 2010 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Updated/modified by Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
  9. */
  10. #include <grass/colors.h>
  11. #include <grass/raster.h>
  12. #include <grass/glocale.h>
  13. #include <grass/nviz.h>
  14. /*!
  15. \brief Initialize Nviz data
  16. \param data nviz data
  17. */
  18. void Nviz_init_data(nv_data * data)
  19. {
  20. unsigned int i;
  21. /* data range */
  22. data->zrange = 0;
  23. data->xyrange = 0;
  24. /* clip planes, turn off by default */
  25. data->num_cplanes = 0;
  26. data->cur_cplane = 0;
  27. for (i = 0; i < MAX_CPLANES; i++) {
  28. Nviz_new_cplane(data, i);
  29. Nviz_off_cplane(data, i);
  30. }
  31. /* lights */
  32. GS_set_light_reset(1);
  33. for (i = 0; i < MAX_LIGHTS - 1; i++) {
  34. Nviz_new_light(data);
  35. }
  36. /* fringe */
  37. data->num_fringes = 0;
  38. data->fringe = NULL;
  39. /* north arrow */
  40. data->draw_arrow = 0;
  41. data->arrow = NULL;
  42. /* scale bar*/
  43. data->num_scalebars = 0;
  44. data->scalebar = NULL;
  45. return;
  46. }
  47. /*! \brief Free allocated space by nv_data struct
  48. \param data nviz data
  49. */
  50. void Nviz_destroy_data(nv_data *data)
  51. {
  52. int i;
  53. for (i = 0; data->num_fringes; i++) {
  54. G_free(data->fringe[i]);
  55. data->fringe[i] = NULL;
  56. }
  57. data->num_fringes = 0;
  58. data->fringe = NULL;
  59. if (data->arrow) {
  60. G_free(data->arrow);
  61. data->arrow = NULL;
  62. data->draw_arrow = 0;
  63. }
  64. for (i = 0; data->num_scalebars; i++) {
  65. G_free(data->scalebar[i]);
  66. data->scalebar[i] = NULL;
  67. }
  68. data->num_scalebars = 0;
  69. data->scalebar = NULL;
  70. }
  71. /*!
  72. \brief Set background color
  73. \param data nviz data
  74. \param color color value
  75. */
  76. void Nviz_set_bgcolor(nv_data * data, int color)
  77. {
  78. data->bgcolor = color;
  79. return;
  80. }
  81. /*!
  82. \brief Get background color
  83. \param data nviz data
  84. \return color color value
  85. */
  86. int Nviz_get_bgcolor(nv_data * data)
  87. {
  88. return data->bgcolor;
  89. }
  90. /*!
  91. \brief Get color value from color string (name or RGB triplet)
  92. \param color_str color string
  93. \return color value
  94. */
  95. int Nviz_color_from_str(const char *color_str)
  96. {
  97. int red, grn, blu;
  98. if (G_str_to_color(color_str, &red, &grn, &blu) != 1) {
  99. G_warning(_("Invalid color (%s), using \"white\" as default"),
  100. color_str);
  101. red = grn = blu = 255;
  102. }
  103. return (red & RED_MASK) + ((int)((grn) << 8) & GRN_MASK) +
  104. ((int)((blu) << 16) & BLU_MASK);
  105. }
  106. /*! Add new fringe
  107. \param data nviz data
  108. \param id surface id
  109. \param color color
  110. \param elev fringe elevation
  111. \param nw,ne,sw,se 1 (turn on) 0 (turn off)
  112. \return pointer to allocated fringe_data structure
  113. \return NULL on error
  114. */
  115. struct fringe_data *Nviz_new_fringe(nv_data *data,
  116. int id, unsigned long color,
  117. double elev, int nw, int ne, int sw, int se)
  118. {
  119. int num;
  120. int *surf;
  121. struct fringe_data *f;
  122. if (!GS_surf_exists(id)) {
  123. /* select first surface from the list */
  124. surf = GS_get_surf_list(&num);
  125. if (num < 1)
  126. return NULL;
  127. id = surf[0];
  128. G_free(surf);
  129. }
  130. f = (struct fringe_data *) G_malloc(sizeof(struct fringe_data));
  131. f->id = id;
  132. f->color = color;
  133. f->elev = elev;
  134. f->where[0] = nw;
  135. f->where[1] = ne;
  136. f->where[2] = sw;
  137. f->where[3] = se;
  138. data->fringe = (struct fringe_data **) G_realloc(data->fringe, data->num_fringes + 1 * sizeof(struct fringe_data *));
  139. data->fringe[data->num_fringes++] = f;
  140. return f;
  141. }
  142. /*! Set fringe
  143. \param data nviz data
  144. \param id surface id
  145. \param color color
  146. \param elev fringe elevation
  147. \param nw,ne,sw,se 1 (turn on) 0 (turn off)
  148. \return pointer to allocated fringe_data structure
  149. \return NULL on error
  150. */
  151. struct fringe_data *Nviz_set_fringe(nv_data *data,
  152. int id, unsigned long color,
  153. double elev, int nw, int ne, int sw, int se)
  154. {
  155. int i, num;
  156. int *surf;
  157. struct fringe_data *f;
  158. if (!GS_surf_exists(id)) {
  159. /* select first surface from the list */
  160. surf = GS_get_surf_list(&num);
  161. if (num < 1)
  162. return NULL;
  163. id = surf[0];
  164. G_free(surf);
  165. }
  166. for (i = 0; i < data->num_fringes; i++) {
  167. f = data->fringe[i];
  168. if (f->id == id) {
  169. f->color = color;
  170. f->elev = elev;
  171. f->where[0] = nw;
  172. f->where[1] = ne;
  173. f->where[2] = sw;
  174. f->where[3] = se;
  175. return f;
  176. }
  177. }
  178. f = Nviz_new_fringe(data,
  179. id, color,
  180. elev, nw, ne, sw, se);
  181. return f;
  182. }
  183. /*! Draw fringe
  184. \param data nviz data
  185. */
  186. void Nviz_draw_fringe(nv_data *data)
  187. {
  188. int i;
  189. for (i = 0; i < data->num_fringes; i++) {
  190. struct fringe_data *f = data->fringe[i];
  191. GS_draw_fringe(f->id, f->color, f->elev, f->where);
  192. }
  193. }
  194. /*!
  195. \brief Sets the North Arrow position and return world coords
  196. \param data nviz data
  197. \param sx,sy screen coordinates
  198. \param size arrow length
  199. \param color arrow/text color
  200. */
  201. int Nviz_set_arrow(nv_data *data,
  202. int sx, int sy, float size,
  203. unsigned int color)
  204. {
  205. int id, pt[2];
  206. int *surf_list, num_surfs;
  207. float coords[3];
  208. struct arrow_data *arw;
  209. if (GS_num_surfs() > 0) {
  210. surf_list = GS_get_surf_list(&num_surfs);
  211. id = surf_list[0];
  212. G_free(surf_list);
  213. pt[0] = sx;
  214. pt[1] = sy;
  215. GS_set_Narrow(pt, id, coords);
  216. if (data->arrow) {
  217. data->arrow->color = color;
  218. data->arrow->size = size;
  219. data->arrow->where[0] = coords[0];
  220. data->arrow->where[1] = coords[1];
  221. data->arrow->where[2] = coords[2];
  222. }
  223. else {
  224. arw = (struct arrow_data *) G_malloc(sizeof(struct arrow_data));
  225. arw->color = color;
  226. arw->size = size;
  227. arw->where[0] = coords[0];
  228. arw->where[1] = coords[1];
  229. arw->where[2] = coords[2];
  230. data->arrow = arw;
  231. }
  232. return 1;
  233. }
  234. return 0;
  235. }
  236. /*!
  237. \brief Draws the North Arrow
  238. \param data nviz data
  239. */
  240. int Nviz_draw_arrow(nv_data *data)
  241. {
  242. struct arrow_data *arw = data->arrow;
  243. GLuint FontBase = 0; /* don't know how to get fontbase*/
  244. data->draw_arrow = 1;
  245. gsd_north_arrow(arw->where, arw->size, FontBase, arw->color, arw->color);
  246. return 1;
  247. }
  248. /*!
  249. \brief Deletes the North Arrow
  250. \param data nviz data
  251. */
  252. void Nviz_delete_arrow(nv_data *data)
  253. {
  254. data->draw_arrow = 0;
  255. return;
  256. }
  257. /*! Add new scalebar
  258. \param data nviz data
  259. \param bar_id scale bar id
  260. \param coords real(?) coordinates
  261. \param size scale bar length
  262. \param color scalebar/text color
  263. \return pointer to allocated scalebar_data structure
  264. \return NULL on error
  265. */
  266. struct scalebar_data *Nviz_new_scalebar(nv_data *data,
  267. int bar_id, float *coords, float size,
  268. unsigned int color)
  269. {
  270. struct scalebar_data *s;
  271. s = (struct scalebar_data *) G_malloc(sizeof(struct scalebar_data));
  272. s->id = bar_id;
  273. s->color = color;
  274. s->size = size;
  275. s->where[0] = coords[0];
  276. s->where[1] = coords[1];
  277. s->where[2] = coords[2];
  278. data->scalebar = (struct scalebar_data **) G_realloc(data->scalebar,
  279. (data->num_scalebars + 1) * sizeof(struct scalebar_data *));
  280. data->scalebar[data->num_scalebars++] = s;
  281. return s;
  282. }
  283. /*!
  284. \brief Sets the scale bar position and return world coords
  285. \param data nviz data
  286. \param bar_id scale bar id
  287. \param sx,sy screen coordinates
  288. \param size scale bar length
  289. \param color scalebar/text color
  290. \return pointer to allocated scalebar_data structure
  291. \return NULL when there's no surface
  292. */
  293. struct scalebar_data *Nviz_set_scalebar(nv_data *data, int bar_id,
  294. int sx, int sy, float size,
  295. unsigned int color)
  296. {
  297. int i, id, pt[2];
  298. int *surf_list, num_surfs;
  299. float coords[3];
  300. struct scalebar_data *s;
  301. if (GS_num_surfs() > 0) {
  302. surf_list = GS_get_surf_list(&num_surfs);
  303. id = surf_list[0];
  304. G_free(surf_list);
  305. pt[0] = sx;
  306. pt[1] = sy;
  307. GS_set_Narrow(pt, id, coords); /* the same like arrow */
  308. for (i = 0; i < data->num_scalebars; i++) {
  309. if (data->scalebar[i]) {
  310. s = data->scalebar[i];
  311. if (s->id == bar_id) {
  312. s->color = color;
  313. s->size = size;
  314. s->where[0] = coords[0];
  315. s->where[1] = coords[1];
  316. s->where[2] = coords[2];
  317. return s;
  318. }
  319. }
  320. }
  321. s = Nviz_new_scalebar(data, bar_id, coords, size, color);
  322. return s;
  323. }
  324. return NULL;
  325. }
  326. /*!
  327. \brief Draws the Scale bar
  328. \param data nviz data
  329. */
  330. void Nviz_draw_scalebar(nv_data *data)
  331. {
  332. int i;
  333. GLuint FontBase = 0; /* don't know how to get fontbase*/
  334. for (i = 0; i < data->num_scalebars; i++) {
  335. if (data->scalebar[i]) {
  336. struct scalebar_data *s = data->scalebar[i];
  337. gsd_scalebar_v2(s->where, s->size, FontBase, s->color, s->color);
  338. }
  339. }
  340. }
  341. /*!
  342. \brief Deletes scale bar
  343. When scalebar is freed, array then contains NULL,
  344. which must be tested during drawing.
  345. \param data nviz data
  346. */
  347. void Nviz_delete_scalebar(nv_data *data, int bar_id)
  348. {
  349. if (bar_id < data->num_scalebars && data->scalebar[bar_id] != NULL) {
  350. G_free(data->scalebar[bar_id]);
  351. data->scalebar[bar_id] = NULL;
  352. }
  353. }