render.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*!
  2. \file lib/vector/vedit/render.c
  3. \brief Vedit library - render vector features (used by wxGUI digitizer)
  4. (C) 2010-2011 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. static 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. static 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 *, int);
  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, 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 boxlist *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_boxlist(0);
  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->id[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->id[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. robj->fid = fid;
  99. if (robj)
  100. list_append(list_obj, robj);
  101. }
  102. /* nodes */
  103. if (draw_flag & (DRAW_NODEONE | DRAW_NODETWO)) {
  104. draw_line_nodes(Map, fid, draw_flag, list_obj);
  105. }
  106. /* direction */
  107. if (draw_flag & DRAW_DIRECTION) {
  108. draw_line_dir(list_obj, fid);
  109. }
  110. }
  111. }
  112. list_obj->item =
  113. (struct robject **)G_realloc(list_obj->item,
  114. list_obj->nitems *
  115. sizeof(struct robject *));
  116. G_debug(1, "Vedit_render_map(): -> nitems = %d",
  117. list_obj->nitems);
  118. Vect_destroy_boxlist(list);
  119. return list_obj;
  120. }
  121. /*!
  122. \brief Draw one feature
  123. */
  124. struct robject *draw_line(struct Map_info *Map, int line, int draw_flag)
  125. {
  126. int draw;
  127. struct robject *obj;
  128. if (!state.Points)
  129. state.Points = Vect_new_line_struct();
  130. if (!Vect_line_alive(Map, line))
  131. return NULL;
  132. state.type = Vect_read_line(Map, state.Points, NULL, line);
  133. obj = (struct robject *)G_malloc(sizeof(struct robject));
  134. obj->fid = line;
  135. draw = FALSE;
  136. if (state.type & GV_LINES) {
  137. if (state.type == GV_LINE) {
  138. obj->type = TYPE_LINE;
  139. draw = draw_flag & DRAW_LINE;
  140. }
  141. else if (state.type == GV_BOUNDARY) {
  142. int left, right;
  143. Vect_get_line_areas(Map, line, &left, &right);
  144. if (left == 0 && right == 0) {
  145. obj->type = TYPE_BOUNDARYNO;
  146. draw = draw_flag & DRAW_BOUNDARYNO;
  147. }
  148. else if (left > 0 && right > 0) {
  149. obj->type = TYPE_BOUNDARYTWO;
  150. draw = draw_flag & DRAW_BOUNDARYTWO;
  151. }
  152. else {
  153. obj->type = TYPE_BOUNDARYONE;
  154. draw = draw_flag & DRAW_BOUNDARYONE;
  155. }
  156. }
  157. }
  158. else if (state.type & GV_POINTS) {
  159. if (state.type == GV_POINT) {
  160. obj->type = TYPE_POINT;
  161. draw = draw_flag & DRAW_POINT;
  162. }
  163. else if (state.type == GV_CENTROID) {
  164. int cret = Vect_get_centroid_area(Map, line);
  165. if (cret > 0) { /* -> area */
  166. obj->type = TYPE_CENTROIDIN;
  167. draw = draw_flag & DRAW_CENTROIDIN;
  168. }
  169. else if (cret == 0) {
  170. obj->type = TYPE_CENTROIDOUT;
  171. draw = draw_flag & DRAW_CENTROIDOUT;
  172. }
  173. else {
  174. obj->type = TYPE_CENTROIDDUP;
  175. draw = draw_flag & DRAW_CENTROIDDUP;
  176. }
  177. }
  178. }
  179. G_debug(3, " draw_line(): type=%d rtype=%d npoints=%d draw=%d",
  180. state.type, obj->type, state.Points->n_points, draw);
  181. if (!draw)
  182. return NULL;
  183. obj->npoints = state.Points->n_points;
  184. obj->point =
  185. (struct rpoint *)G_malloc(obj->npoints * sizeof(struct rpoint));
  186. robj_points(obj, state.Points);
  187. return obj;
  188. }
  189. /*!
  190. \brief Convert geographic coordinates to the screen
  191. */
  192. void en_to_xy(double east, double north, int *x, int *y)
  193. {
  194. double n, w;
  195. w = region.center_easting - (region.map_width / 2) * region.map_res;
  196. n = region.center_northing + (region.map_height / 2) * region.map_res;
  197. if (x)
  198. *x = (east - w) / region.map_res;
  199. if (y)
  200. *y = (n - north) / region.map_res;
  201. return;
  202. }
  203. /*!
  204. \brief Draw line nodes
  205. */
  206. void draw_line_nodes(struct Map_info *Map, int line, int draw_flag,
  207. struct robject_list *list)
  208. {
  209. unsigned int i;
  210. int type, nodes[2];
  211. int x, y;
  212. double east, north;
  213. struct robject *robj;
  214. if (Vect_get_line_type(Map, line) & GV_POINTS)
  215. return;
  216. Vect_get_line_nodes(Map, line, &(nodes[0]), &(nodes[1]));
  217. for (i = 0; i < sizeof(nodes) / sizeof(int); i++) {
  218. type = 0;
  219. if (Vect_get_node_n_lines(Map, nodes[i]) == 1) {
  220. if (draw_flag & DRAW_NODEONE) {
  221. type = TYPE_NODEONE;
  222. }
  223. }
  224. else {
  225. if (draw_flag & DRAW_NODETWO) {
  226. type = TYPE_NODETWO;
  227. }
  228. }
  229. if (type == 0)
  230. continue;
  231. Vect_get_node_coor(Map, nodes[i], &east, &north, NULL);
  232. robj = robj_alloc(type, 1);
  233. en_to_xy(east, north, &x, &y);
  234. robj->fid = line;
  235. robj->point->x = x;
  236. robj->point->y = y;
  237. list_append(list, robj);
  238. }
  239. }
  240. /*!
  241. \brief Append object to the list
  242. */
  243. void list_append(struct robject_list *list, struct robject *obj)
  244. {
  245. if (list->nitems >= state.nitems_alloc) {
  246. state.nitems_alloc += 1000;
  247. list->item =
  248. (struct robject **)G_realloc(list->item,
  249. state.nitems_alloc *
  250. sizeof(struct robject *));
  251. }
  252. list->item[list->nitems++] = obj;
  253. }
  254. /*!
  255. \brief Allocate robject
  256. */
  257. struct robject *robj_alloc(int type, int npoints)
  258. {
  259. struct robject *robj;
  260. robj = (struct robject *)G_malloc(sizeof(struct robject));
  261. robj->type = type;
  262. robj->npoints = npoints;
  263. robj->point = (struct rpoint *)G_malloc(npoints * sizeof(struct rpoint));
  264. return robj;
  265. }
  266. /*!
  267. \brief Draw line vertices
  268. */
  269. struct robject *draw_line_vertices()
  270. {
  271. int i;
  272. int x, y;
  273. struct robject *robj;
  274. robj = robj_alloc(TYPE_VERTEX, state.Points->n_points - 2); /* ignore nodes */
  275. for (i = 1; i < state.Points->n_points - 1; i++) {
  276. en_to_xy(state.Points->x[i], state.Points->y[i], &x, &y);
  277. robj->point[i - 1].x = x;
  278. robj->point[i - 1].y = y;
  279. }
  280. return robj;
  281. }
  282. /*!
  283. \brief Draw line dirs
  284. */
  285. int draw_line_dir(struct robject_list *list, int line)
  286. {
  287. int narrows;
  288. int size; /* arrow length in pixels */
  289. int limit; /* segment length limit for drawing symbol (in pixels) */
  290. double dist, angle, pos;
  291. double e, n;
  292. int x0, y0, x1, y1;
  293. narrows = 0;
  294. size = 5;
  295. limit = 5; /* 5px for line segment */
  296. dist = Vect_line_length(state.Points);
  297. G_debug(5, " draw_line_dir() line=%d", line);
  298. if (dist_in_px(dist) >= limit) {
  299. while (1) {
  300. pos = (narrows + 1) * 8 * limit * region.map_res;
  301. if (Vect_point_on_line(state.Points, pos,
  302. &e, &n, NULL, NULL, NULL) < 1) {
  303. break;
  304. }
  305. en_to_xy(e, n, &x0, &y0);
  306. if (Vect_point_on_line
  307. (state.Points, pos - 3 * size * region.map_res, &e, &n, NULL,
  308. &angle, NULL) < 1) {
  309. break;
  310. }
  311. en_to_xy(e, n, &x1, &y1);
  312. draw_arrow(x0, y0, x1, y1, angle, size, line, list);
  313. if (narrows > 1e2) /* low resolution, break */
  314. break;
  315. narrows++;
  316. }
  317. /* draw at least one arrow in the middle of line */
  318. if (narrows < 1) {
  319. dist /= 2.;
  320. if (Vect_point_on_line(state.Points, dist,
  321. &e, &n, NULL, NULL, NULL) > 0) {
  322. en_to_xy(e, n, &x0, &y0);
  323. if (Vect_point_on_line
  324. (state.Points, dist - 3 * size * region.map_res, &e, &n,
  325. NULL, &angle, NULL) > 0) {
  326. en_to_xy(e, n, &x1, &y1);
  327. draw_arrow(x0, y0, x1, y1, angle, size, line, list);
  328. }
  329. }
  330. }
  331. }
  332. return narrows;
  333. }
  334. /*!
  335. \brief Calculate distance in pixels (on screen)
  336. */
  337. double dist_in_px(double dist)
  338. {
  339. int x, y;
  340. en_to_xy(region.map_west + dist, region.map_north, &x, &y);
  341. return sqrt(x * x);
  342. }
  343. /*!
  344. \brief Draw arrow
  345. */
  346. void draw_arrow(int x0, int y0, int x1, int y1, double angle, int size, int line,
  347. struct robject_list *list)
  348. {
  349. double angle_symb;
  350. struct robject *robj;
  351. robj = robj_alloc(TYPE_DIRECTION, 3);
  352. robj->fid = line;
  353. angle_symb = angle - M_PI / 2.;
  354. robj->point[0].x = (int)x1 + size * cos(angle_symb);
  355. robj->point[0].y = (int)y1 - size * sin(angle_symb);
  356. robj->point[1].x = x0;
  357. robj->point[1].y = y0;
  358. angle_symb = M_PI / 2. + angle;
  359. robj->point[2].x = (int)x1 + size * cos(angle_symb);
  360. robj->point[2].y = (int)y1 - size * sin(angle_symb);
  361. list_append(list, robj);
  362. }
  363. /*!
  364. \brief Draw area
  365. */
  366. void draw_area(struct Map_info *Map, int area, struct robject_list *list)
  367. {
  368. int i, centroid, isle;
  369. int num_isles;
  370. struct line_pnts *ipoints;
  371. struct robject *robj;
  372. if (!state.Points)
  373. state.Points = Vect_new_line_struct();
  374. if (!Vect_area_alive(Map, area))
  375. return;
  376. /* check for other centroids -- only area with one centroid is valid */
  377. centroid = Vect_get_area_centroid(Map, area);
  378. if (centroid <= 0)
  379. return;
  380. ipoints = Vect_new_line_struct();
  381. /* get area's boundary */
  382. Vect_get_area_points(Map, area, state.Points);
  383. robj = robj_alloc(TYPE_AREA, state.Points->n_points);
  384. robj->fid = area;
  385. robj_points(robj, state.Points);
  386. list_append(list, robj);
  387. /* check for isles */
  388. num_isles = Vect_get_area_num_isles(Map, area);
  389. for (i = 0; i < num_isles; i++) {
  390. isle = Vect_get_area_isle(Map, area, i);
  391. if (!Vect_isle_alive(Map, isle))
  392. continue;
  393. Vect_get_isle_points(Map, isle, ipoints);
  394. robj = robj_alloc(TYPE_ISLE, ipoints->n_points);
  395. robj->fid = -1;
  396. robj_points(robj, ipoints);
  397. list_append(list, robj);
  398. }
  399. Vect_destroy_line_struct(ipoints);
  400. }
  401. /*!
  402. \brief convert EN -> XY
  403. */
  404. void robj_points(struct robject *robj, const struct line_pnts *points)
  405. {
  406. int i;
  407. int x, y;
  408. for (i = 0; i < points->n_points; i++) {
  409. en_to_xy(points->x[i], points->y[i], &x, &y);
  410. robj->point[i].x = x;
  411. robj->point[i].y = y;
  412. }
  413. }