Gv3.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*!
  2. \file Gv3.c
  3. \brief OGSF library - loading vector sets (lower level functions)
  4. GRASS OpenGL gsurf OGSF Library
  5. (C) 1999-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Bill Brown USACERL (December 1993)
  11. \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
  12. */
  13. #include <stdlib.h>
  14. #include <grass/gis.h>
  15. #include <grass/vector.h>
  16. #include <grass/glocale.h>
  17. #include <grass/gstypes.h>
  18. /*
  19. #define TRAK_MEM
  20. */
  21. #ifdef TRAK_MEM
  22. static int Tot_mem = 0;
  23. #endif
  24. /*!
  25. \brief Load vector map to memory
  26. The other alternative may be to load to a tmp file
  27. \param grassname vector map name
  28. \param[out] number of loaded features
  29. \return pointer to geoline struct
  30. \return NULL on failure
  31. */
  32. geoline *Gv_load_vect(const char *grassname, int *nlines)
  33. {
  34. struct Map_info map;
  35. struct line_pnts *points;
  36. struct line_cats *Cats = NULL;
  37. geoline *top, *gln, *prev;
  38. int np, i, n, nareas, nl = 0, area, type, is3d;
  39. struct Cell_head wind;
  40. float vect[2][3];
  41. const char *mapset;
  42. mapset = G_find_vector2(grassname, "");
  43. if (!mapset) {
  44. G_warning(_("Vector map <%s> not found"), grassname);
  45. return NULL;
  46. }
  47. Vect_set_open_level(2);
  48. if (Vect_open_old(&map, grassname, "") == -1) {
  49. G_warning(_("Unable to open vector map <%s>"),
  50. G_fully_qualified_name(grassname, mapset));
  51. return NULL;
  52. }
  53. top = gln = (geoline *) G_malloc(sizeof(geoline)); /* G_fatal_error */
  54. if (!top) {
  55. return NULL;
  56. }
  57. prev = top;
  58. #ifdef TRAK_MEM
  59. Tot_mem += sizeof(geoline);
  60. #endif
  61. points = Vect_new_line_struct();
  62. Cats = Vect_new_cats_struct();
  63. G_get_set_window(&wind);
  64. Vect_set_constraint_region(&map, wind.north, wind.south, wind.east,
  65. wind.west, PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);
  66. is3d = Vect_is_3d(&map);
  67. /* Read areas */
  68. n = Vect_get_num_areas(&map);
  69. nareas = 0;
  70. G_debug(3, "Reading vector areas (nareas = %d)", n);
  71. for (area = 1; area <= n; area++) {
  72. G_debug(3, " area %d", area);
  73. Vect_get_area_points(&map, area, points);
  74. if (points->n_points < 3)
  75. continue;
  76. gln->type = OGSF_POLYGON;
  77. gln->npts = np = points->n_points;
  78. G_debug(3, " np = %d", np);
  79. if (is3d) {
  80. gln->dims = 3;
  81. gln->p3 = (Point3 *) G_calloc(np, sizeof(Point3)); /* G_fatal_error */
  82. if (!gln->p3) {
  83. return (NULL);
  84. }
  85. #ifdef TRAK_MEM
  86. Tot_mem += (np * sizeof(Point3));
  87. #endif
  88. }
  89. else {
  90. gln->dims = 2;
  91. gln->p2 = (Point2 *) G_calloc(np, sizeof(Point2)); /* G_fatal_error */
  92. if (!gln->p2) {
  93. return (NULL);
  94. }
  95. #ifdef TRAK_MEM
  96. Tot_mem += (np * sizeof(Point2));
  97. #endif
  98. }
  99. for (i = 0; i < np; i++) {
  100. if (is3d) {
  101. gln->p3[i][X] = points->x[i];
  102. gln->p3[i][Y] = points->y[i];
  103. gln->p3[i][Z] = points->z[i];
  104. }
  105. else {
  106. gln->p2[i][X] = points->x[i];
  107. gln->p2[i][Y] = points->y[i];
  108. }
  109. }
  110. /* Calc normal (should be average) */
  111. if (is3d) {
  112. vect[0][X] = (float)(gln->p3[0][X] - gln->p3[1][X]);
  113. vect[0][Y] = (float)(gln->p3[0][Y] - gln->p3[1][Y]);
  114. vect[0][Z] = (float)(gln->p3[0][Z] - gln->p3[1][Z]);
  115. vect[1][X] = (float)(gln->p3[2][X] - gln->p3[1][X]);
  116. vect[1][Y] = (float)(gln->p3[2][Y] - gln->p3[1][Y]);
  117. vect[1][Z] = (float)(gln->p3[2][Z] - gln->p3[1][Z]);
  118. GS_v3cross(vect[1], vect[0], gln->norm);
  119. }
  120. gln->next = (geoline *) G_malloc(sizeof(geoline)); /* G_fatal_error */
  121. if (!gln->next) {
  122. return (NULL);
  123. }
  124. #ifdef TRAK_MEM
  125. Tot_mem += sizeof(geoline);
  126. #endif
  127. prev = gln;
  128. gln = gln->next;
  129. nareas++;
  130. }
  131. G_debug(3, "%d areas loaded", nareas);
  132. /* Read all lines */
  133. G_debug(3, "Reading vector lines ...");
  134. while (-1 < (type = Vect_read_next_line(&map, points, Cats))) {
  135. G_debug(3, "line type = %d", type);
  136. if (type & (GV_LINES | GV_FACE)) {
  137. if (type & (GV_LINES)) {
  138. gln->type = OGSF_LINE;
  139. }
  140. else {
  141. gln->type = OGSF_POLYGON;
  142. /* Vect_append_point ( points, points->x[0], points->y[0], points->z[0] ); */
  143. }
  144. gln->npts = np = points->n_points;
  145. G_debug(3, " np = %d", np);
  146. if (is3d) {
  147. gln->dims = 3;
  148. gln->p3 = (Point3 *) G_calloc(np, sizeof(Point3)); /* G_fatal_error */
  149. if (!gln->p3) {
  150. return (NULL);
  151. }
  152. #ifdef TRAK_MEM
  153. Tot_mem += (np * sizeof(Point3));
  154. #endif
  155. }
  156. else {
  157. gln->dims = 2;
  158. gln->p2 = (Point2 *) G_calloc(np, sizeof(Point2)); /* G_fatal_error */
  159. if (!gln->p2) {
  160. return (NULL);
  161. }
  162. #ifdef TRAK_MEM
  163. Tot_mem += (np * sizeof(Point2));
  164. #endif
  165. }
  166. for (i = 0; i < np; i++) {
  167. if (is3d) {
  168. gln->p3[i][X] = points->x[i];
  169. gln->p3[i][Y] = points->y[i];
  170. gln->p3[i][Z] = points->z[i];
  171. }
  172. else {
  173. gln->p2[i][X] = points->x[i];
  174. gln->p2[i][Y] = points->y[i];
  175. }
  176. }
  177. /* Calc normal (should be average) */
  178. if (is3d && gln->type == OGSF_POLYGON) {
  179. vect[0][X] = (float)(gln->p3[0][X] - gln->p3[1][X]);
  180. vect[0][Y] = (float)(gln->p3[0][Y] - gln->p3[1][Y]);
  181. vect[0][Z] = (float)(gln->p3[0][Z] - gln->p3[1][Z]);
  182. vect[1][X] = (float)(gln->p3[2][X] - gln->p3[1][X]);
  183. vect[1][Y] = (float)(gln->p3[2][Y] - gln->p3[1][Y]);
  184. vect[1][Z] = (float)(gln->p3[2][Z] - gln->p3[1][Z]);
  185. GS_v3cross(vect[1], vect[0], gln->norm);
  186. G_debug(3, "norm %f %f %f", gln->norm[0], gln->norm[1],
  187. gln->norm[2]);
  188. }
  189. /* Store category info for thematic display */
  190. if (Cats->n_cats > 0) {
  191. gln->cats = Cats;
  192. Cats = Vect_new_cats_struct();
  193. }
  194. else {
  195. gln->cats = NULL;
  196. Vect_reset_cats(Cats);
  197. }
  198. gln->next = (geoline *) G_malloc(sizeof(geoline)); /* G_fatal_error */
  199. if (!gln->next) {
  200. return (NULL);
  201. }
  202. #ifdef TRAK_MEM
  203. Tot_mem += sizeof(geoline);
  204. #endif
  205. prev = gln;
  206. gln = gln->next;
  207. nl++;
  208. }
  209. }
  210. G_debug(3, "%d lines loaded", nl);
  211. nl += nareas;
  212. prev->next = NULL;
  213. G_free(gln);
  214. #ifdef TRAK_MEM
  215. Tot_mem -= sizeof(geoline);
  216. #endif
  217. Vect_close(&map);
  218. if (!nl) {
  219. G_warning(_("No features from vector map <%s> fall within current region"),
  220. G_fully_qualified_name(grassname, mapset));
  221. return (NULL);
  222. }
  223. else {
  224. G_message(_("Vector map <%s> loaded (%d features)"),
  225. G_fully_qualified_name(grassname, mapset), nl);
  226. }
  227. *nlines = nl;
  228. #ifdef TRAK_MEM
  229. G_debug(3, "Total vect memory = %d Kbytes", Tot_mem / 1000);
  230. #endif
  231. return (top);
  232. }
  233. /*!
  234. \brief Tracking memory
  235. \param minus mimus number
  236. */
  237. void sub_Vectmem(int minus)
  238. {
  239. #ifdef TRAK_MEM
  240. {
  241. Tot_mem -= minus;
  242. }
  243. #endif
  244. return;
  245. }