render.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*!
  2. \file lib/vector/vedit/render.c
  3. \brief Vedit library - render vector features (used by wxGUI digitizer)
  4. (C) 2010 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Martin Landa <landa.martin gmail.com>
  8. */
  9. #include <math.h>
  10. #include <grass/vedit.h>
  11. struct _region
  12. {
  13. double center_easting;
  14. double center_northing;
  15. double map_west;
  16. double map_north;
  17. int map_width;
  18. int map_height;
  19. double map_res;
  20. } region;
  21. struct _state
  22. {
  23. int nitems_alloc;
  24. int type;
  25. struct line_pnts *Points;
  26. } state;
  27. static struct robject *draw_line(struct Map_info *, int, int);
  28. static struct robject *draw_line_vertices();
  29. static void draw_line_nodes(struct Map_info *, int, int,
  30. struct robject_list *);
  31. static int draw_line_dir(struct robject_list *);
  32. static void list_append(struct robject_list *, struct robject *);
  33. static struct robject *robj_alloc(int, int);
  34. static void robj_points(struct robject *, const struct line_pnts *);
  35. static double dist_in_px(double);
  36. static void en_to_xy(double, double, int *, int *);
  37. static void draw_arrow(int, int, int, int, double, int,
  38. struct robject_list *);
  39. static void draw_area(struct Map_info *, int, struct robject_list *);
  40. /*!
  41. \brief Render vector features into list
  42. \param Map pointer to Map_info structure
  43. \param box bounding box of region to be rendered
  44. \param draw_flag types of objects to be rendered (see vedit.h)
  45. \param center_easing, center_northing, map_width, map_height, map_res values used for conversion en->xy
  46. \return pointer to robject_list structure
  47. */
  48. struct robject_list *Vedit_render_map(struct Map_info *Map,
  49. struct bound_box *box, int draw_flag,
  50. double center_easting,
  51. double center_northing, int map_width,
  52. int map_height, double map_res)
  53. {
  54. int i, nfeat, fid;
  55. struct ilist *list;
  56. struct robject_list *list_obj;
  57. struct robject *robj;
  58. /* define region */
  59. region.center_easting = center_easting;
  60. region.center_northing = center_northing;
  61. region.map_width = map_width;
  62. region.map_height = map_height;
  63. region.map_res = map_res;
  64. region.map_west = center_easting - (map_width / 2.) * map_res;
  65. region.map_north = center_northing + (map_height / 2.) * map_res;
  66. list = Vect_new_list();
  67. list_obj = NULL;
  68. state.nitems_alloc = 1000;
  69. list_obj = (struct robject_list *)G_malloc(sizeof(struct robject_list));
  70. list_obj->nitems = 0;
  71. list_obj->item =
  72. (struct robject **)G_malloc(state.nitems_alloc *
  73. sizeof(struct robject *));
  74. /* area */
  75. if (draw_flag & DRAW_AREA) {
  76. nfeat = Vect_select_areas_by_box(Map, box, list);
  77. for (i = 0; i < nfeat; i++) {
  78. fid = list->value[i];
  79. draw_area(Map, fid, list_obj);
  80. }
  81. }
  82. /* draw lines inside of current display region */
  83. nfeat = Vect_select_lines_by_box(Map, box, GV_POINTS | GV_LINES, // fixme
  84. list);
  85. G_debug(1, "Vedit_render_map(): region: w=%f, e=%f, s=%f, n=%f nlines=%d",
  86. box->W, box->E, box->S, box->N, nfeat);
  87. /* features */
  88. for (i = 0; i < list->n_values; i++) {
  89. fid = list->value[i];
  90. robj = draw_line(Map, fid, draw_flag);
  91. if (!robj)
  92. continue;
  93. list_append(list_obj, robj);
  94. if (state.type & GV_LINES) {
  95. /* vertices */
  96. if (draw_flag & DRAW_VERTEX) {
  97. robj = draw_line_vertices();
  98. if (robj)
  99. list_append(list_obj, robj);
  100. }
  101. /* nodes */
  102. if (draw_flag & (DRAW_NODEONE | DRAW_NODETWO)) {
  103. draw_line_nodes(Map, fid, draw_flag, list_obj);
  104. }
  105. /* direction */
  106. if (draw_flag & DRAW_DIRECTION) {
  107. draw_line_dir(list_obj);
  108. }
  109. }
  110. }
  111. list_obj->item =
  112. (struct robject **)G_realloc(list_obj->item,
  113. list_obj->nitems *
  114. sizeof(struct robject *));
  115. Vect_destroy_list(list);
  116. return list_obj;
  117. }
  118. /*!
  119. \brief Draw one feature
  120. */
  121. struct robject *draw_line(struct Map_info *Map, int line, int draw_flag)
  122. {
  123. int draw;
  124. struct robject *obj;
  125. if (!state.Points)
  126. state.Points = Vect_new_line_struct();
  127. if (!Vect_line_alive(Map, line))
  128. return NULL;
  129. state.type = Vect_read_line(Map, state.Points, NULL, line);
  130. obj = (struct robject *)G_malloc(sizeof(struct robject));
  131. draw = FALSE;
  132. if (state.type & GV_LINES) {
  133. if (state.type == GV_LINE) {
  134. obj->type = TYPE_LINE;
  135. draw = draw_flag & DRAW_LINE;
  136. }
  137. else if (state.type == GV_BOUNDARY) {
  138. int left, right;
  139. Vect_get_line_areas(Map, line, &left, &right);
  140. if (left == 0 && right == 0) {
  141. obj->type = TYPE_BOUNDARYNO;
  142. draw = draw_flag & DRAW_BOUNDARYNO;
  143. }
  144. else if (left > 0 && right > 0) {
  145. obj->type = TYPE_BOUNDARYTWO;
  146. draw = draw_flag & DRAW_BOUNDARYTWO;
  147. }
  148. else {
  149. obj->type = TYPE_BOUNDARYONE;
  150. draw = draw_flag & DRAW_BOUNDARYONE;
  151. }
  152. }
  153. }
  154. else if (state.type & GV_POINTS) {
  155. if (state.type == GV_POINT) {
  156. obj->type = TYPE_POINT;
  157. draw = draw_flag & DRAW_POINT;
  158. }
  159. else if (state.type == GV_CENTROID) {
  160. int cret = Vect_get_centroid_area(Map, line);
  161. if (cret > 0) { // -> area
  162. obj->type = TYPE_CENTROIDIN;
  163. draw = draw_flag & DRAW_CENTROIDIN;
  164. }
  165. else if (cret == 0) {
  166. obj->type = TYPE_CENTROIDOUT;
  167. draw = draw_flag & DRAW_CENTROIDOUT;
  168. }
  169. else {
  170. obj->type = TYPE_CENTROIDDUP;
  171. draw = draw_flag & DRAW_CENTROIDDUP;
  172. }
  173. }
  174. }
  175. G_debug(3, " draw_line(): type=%d rtype=%d npoints=%d draw=%d",
  176. state.type, obj->type, state.Points->n_points, draw);
  177. if (!draw)
  178. return NULL;
  179. obj->npoints = state.Points->n_points;
  180. obj->point =
  181. (struct rpoint *)G_malloc(obj->npoints * sizeof(struct rpoint));
  182. robj_points(obj, state.Points);
  183. return obj;
  184. }
  185. /*!
  186. \brief Convert geographic coordinates to the screen
  187. */
  188. void en_to_xy(double east, double north, int *x, int *y)
  189. {
  190. double n, w;
  191. w = region.center_easting - (region.map_width / 2) * region.map_res;
  192. n = region.center_northing + (region.map_height / 2) * region.map_res;
  193. if (x)
  194. *x = (east - w) / region.map_res;
  195. if (y)
  196. *y = (n - north) / region.map_res;
  197. return;
  198. }
  199. /*!
  200. \brief Draw line nodes
  201. */
  202. void draw_line_nodes(struct Map_info *Map, int line, int draw_flag,
  203. struct robject_list *list)
  204. {
  205. unsigned int i;
  206. int type, nodes[2];
  207. int x, y;
  208. double east, north;
  209. struct robject *robj;
  210. Vect_get_line_nodes(Map, line, &(nodes[0]), &(nodes[1]));
  211. for (i = 0; i < sizeof(nodes) / sizeof(int); i++) {
  212. type = 0;
  213. if (Vect_get_node_n_lines(Map, nodes[i]) == 1) {
  214. if (draw_flag & DRAW_NODEONE) {
  215. type = TYPE_NODEONE;
  216. }
  217. }
  218. else {
  219. if (draw_flag & DRAW_NODETWO) {
  220. type = TYPE_NODETWO;
  221. }
  222. }
  223. if (type == 0)
  224. continue;
  225. Vect_get_node_coor(Map, nodes[i], &east, &north, NULL);
  226. robj = robj_alloc(type, 1);
  227. en_to_xy(east, north, &x, &y);
  228. robj->point->x = x;
  229. robj->point->y = y;
  230. list_append(list, robj);
  231. }
  232. }
  233. /*!
  234. \brief Append object to the list
  235. */
  236. void list_append(struct robject_list *list, struct robject *obj)
  237. {
  238. if (list->nitems >= state.nitems_alloc) {
  239. state.nitems_alloc += 1000;
  240. list->item =
  241. (struct robject **)G_realloc(list->item,
  242. state.nitems_alloc *
  243. sizeof(struct robject *));
  244. }
  245. list->item[list->nitems++] = obj;
  246. }
  247. /*!
  248. \brief Allocate robject
  249. */
  250. struct robject *robj_alloc(int type, int npoints)
  251. {
  252. struct robject *robj;
  253. robj = (struct robject *)G_malloc(sizeof(struct robject));
  254. robj->type = type;
  255. robj->npoints = npoints;
  256. robj->point = (struct rpoint *)G_malloc(npoints * sizeof(struct rpoint));
  257. return robj;
  258. }
  259. /*!
  260. \brief Draw line vertices
  261. */
  262. struct robject *draw_line_vertices()
  263. {
  264. int i;
  265. int x, y;
  266. struct robject *robj;
  267. robj = robj_alloc(TYPE_VERTEX, state.Points->n_points - 2); /* ignore nodes */
  268. for (i = 1; i < state.Points->n_points - 1; i++) {
  269. en_to_xy(state.Points->x[i], state.Points->y[i], &x, &y);
  270. robj->point[i - 1].x = x;
  271. robj->point[i - 1].y = y;
  272. }
  273. return robj;
  274. }
  275. /*!
  276. \brief Draw line dirs
  277. */
  278. int draw_line_dir(struct robject_list *list)
  279. {
  280. int narrows;
  281. int size; /* arrow length in pixels */
  282. int limit; /* segment length limit for drawing symbol (in pixels) */
  283. double dist, angle, pos;
  284. double e, n;
  285. int x0, y0, x1, y1;
  286. narrows = 0;
  287. size = 5;
  288. limit = 5; // 5px for line segment
  289. dist = Vect_line_length(state.Points);
  290. if (dist_in_px(dist) >= limit) {
  291. while (1) {
  292. pos = (narrows + 1) * 8 * limit * region.map_res;
  293. if (Vect_point_on_line(state.Points, pos,
  294. &e, &n, NULL, NULL, NULL) < 1) {
  295. break;
  296. }
  297. en_to_xy(e, n, &x0, &y0);
  298. if (Vect_point_on_line
  299. (state.Points, pos - 3 * size * region.map_res, &e, &n, NULL,
  300. &angle, NULL) < 1) {
  301. break;
  302. }
  303. en_to_xy(e, n, &x1, &y1);
  304. draw_arrow(x0, y0, x1, y1, angle, size, list);
  305. if (narrows > 1e2) // low resolution, break
  306. break;
  307. narrows++;
  308. }
  309. // draw at least one arrow in the middle of line
  310. if (narrows < 1) {
  311. dist /= 2.;
  312. if (Vect_point_on_line(state.Points, dist,
  313. &e, &n, NULL, NULL, NULL) > 0) {
  314. en_to_xy(e, n, &x0, &y0);
  315. if (Vect_point_on_line
  316. (state.Points, dist - 3 * size * region.map_res, &e, &n,
  317. NULL, &angle, NULL) > 0) {
  318. en_to_xy(e, n, &x1, &y1);
  319. draw_arrow(x0, y0, x1, y1, angle, size, list);
  320. }
  321. }
  322. }
  323. }
  324. return narrows;
  325. }
  326. /*!
  327. \brief Calculate distance in pixels (on screen)
  328. */
  329. double dist_in_px(double dist)
  330. {
  331. int x, y;
  332. en_to_xy(region.map_west + dist, region.map_north, &x, &y);
  333. return sqrt(x * x);
  334. }
  335. /*!
  336. \brief Draw arrow
  337. */
  338. void draw_arrow(int x0, int y0, int x1, int y1, double angle, int size,
  339. struct robject_list *list)
  340. {
  341. double angle_symb;
  342. struct robject *robj;
  343. robj = robj_alloc(TYPE_DIRECTION, 3);
  344. angle_symb = angle - M_PI / 2.;
  345. robj->point[0].x = (int)x1 + size * cos(angle_symb);
  346. robj->point[0].y = (int)y1 - size * sin(angle_symb);
  347. robj->point[1].x = x0;
  348. robj->point[1].y = y0;
  349. angle_symb = M_PI / 2. + angle;
  350. robj->point[2].x = (int)x1 + size * cos(angle_symb);
  351. robj->point[2].y = (int)y1 - size * sin(angle_symb);
  352. list_append(list, robj);
  353. }
  354. /*!
  355. \brief Draw area
  356. */
  357. void draw_area(struct Map_info *Map, int area, struct robject_list *list)
  358. {
  359. int i, centroid, isle;
  360. int num_isles;
  361. struct line_pnts *ipoints;
  362. struct robject *robj;
  363. if (!state.Points)
  364. state.Points = Vect_new_line_struct();
  365. if (!Vect_area_alive(Map, area))
  366. return;
  367. /* check for other centroids -- only area with one centroid is valid */
  368. centroid = Vect_get_area_centroid(Map, area);
  369. if (centroid <= 0)
  370. return;
  371. ipoints = Vect_new_line_struct();
  372. /* get area's boundary */
  373. Vect_get_area_points(Map, area, state.Points);
  374. robj = robj_alloc(TYPE_AREA, state.Points->n_points);
  375. robj_points(robj, state.Points);
  376. list_append(list, robj);
  377. /* check for isles */
  378. num_isles = Vect_get_area_num_isles(Map, area);
  379. for (i = 0; i < num_isles; i++) {
  380. isle = Vect_get_area_isle(Map, area, i);
  381. if (!Vect_isle_alive(Map, isle))
  382. continue;
  383. Vect_get_isle_points(Map, isle, ipoints);
  384. robj = robj_alloc(TYPE_ISLE, ipoints->n_points);
  385. robj_points(robj, ipoints);
  386. list_append(list, robj);
  387. }
  388. Vect_destroy_line_struct(ipoints);
  389. }
  390. /*!
  391. \brief convert EN -> XY
  392. */
  393. void robj_points(struct robject *robj, const struct line_pnts *points)
  394. {
  395. int i;
  396. int x, y;
  397. for (i = 0; i < points->n_points; i++) {
  398. en_to_xy(points->x[i], points->y[i], &x, &y);
  399. robj->point[i].x = x;
  400. robj->point[i].y = y;
  401. }
  402. }