16 #include <grass/vedit.h>
20 double center_easting;
21 double center_northing;
34 struct line_pnts *Points;
37 static struct robject *draw_line(
struct Map_info *,
int,
int);
38 static struct robject *draw_line_vertices();
39 static void draw_line_nodes(
struct Map_info *,
int,
int,
40 struct robject_list *);
41 static int draw_line_dir(
struct robject_list *,
int);
42 static void list_append(
struct robject_list *,
struct robject *);
43 static struct robject *robj_alloc(
int,
int);
44 static void robj_points(
struct robject *,
const struct line_pnts *);
45 static double dist_in_px(
double);
46 static void en_to_xy(
double,
double,
int *,
int *);
47 static void draw_arrow(
int,
int,
int,
int,
double,
int,
int,
48 struct robject_list *);
49 static void draw_area(
struct Map_info *,
int,
struct robject_list *);
62 struct bound_box *
box,
int draw_flag,
63 double center_easting,
64 double center_northing,
int map_width,
65 int map_height,
double map_res)
69 struct robject_list *list_obj;
73 region.center_easting = center_easting;
74 region.center_northing = center_northing;
75 region.map_width = map_width;
76 region.map_height = map_height;
77 region.map_res = map_res;
78 region.map_west = center_easting - (map_width / 2.) * map_res;
79 region.map_north = center_northing + (map_height / 2.) * map_res;
83 state.nitems_alloc = 1000;
85 list_obj = (
struct robject_list *)G_malloc(
sizeof(
struct robject_list));
88 (
struct robject **)G_malloc(state.nitems_alloc *
89 sizeof(
struct robject *));
92 if (draw_flag & DRAW_AREA) {
94 for (i = 0; i < nfeat; i++) {
96 draw_area(Map, fid, list_obj);
103 G_debug(1,
"Vedit_render_map(): region: w=%f, e=%f, s=%f, n=%f nlines=%d",
104 box->W, box->E, box->S, box->N, nfeat);
107 for (i = 0; i < list->n_values; i++) {
108 fid = list->value[i];
109 robj = draw_line(Map, fid, draw_flag);
112 list_append(list_obj, robj);
116 if (draw_flag & DRAW_VERTEX) {
117 robj = draw_line_vertices();
120 list_append(list_obj, robj);
123 if (draw_flag & (DRAW_NODEONE | DRAW_NODETWO)) {
124 draw_line_nodes(Map, fid, draw_flag, list_obj);
127 if (draw_flag & DRAW_DIRECTION) {
128 draw_line_dir(list_obj, fid);
134 (
struct robject **)G_realloc(list_obj->item,
136 sizeof(
struct robject *));
138 G_debug(1,
"Vedit_render_map(): -> nitems = %d",
149 struct robject *draw_line(
struct Map_info *
Map,
int line,
int draw_flag)
162 obj = (
struct robject *)G_malloc(
sizeof(
struct robject));
166 if (state.type == GV_LINE) {
167 obj->type = TYPE_LINE;
168 draw = draw_flag & DRAW_LINE;
170 else if (state.type == GV_BOUNDARY) {
174 if (left == 0 && right == 0) {
175 obj->type = TYPE_BOUNDARYNO;
176 draw = draw_flag & DRAW_BOUNDARYNO;
178 else if (left > 0 && right > 0) {
179 obj->type = TYPE_BOUNDARYTWO;
180 draw = draw_flag & DRAW_BOUNDARYTWO;
183 obj->type = TYPE_BOUNDARYONE;
184 draw = draw_flag & DRAW_BOUNDARYONE;
188 else if (state.type & GV_POINTS) {
189 if (state.type == GV_POINT) {
190 obj->type = TYPE_POINT;
191 draw = draw_flag & DRAW_POINT;
193 else if (state.type == GV_CENTROID) {
197 obj->type = TYPE_CENTROIDIN;
198 draw = draw_flag & DRAW_CENTROIDIN;
200 else if (cret == 0) {
201 obj->type = TYPE_CENTROIDOUT;
202 draw = draw_flag & DRAW_CENTROIDOUT;
205 obj->type = TYPE_CENTROIDDUP;
206 draw = draw_flag & DRAW_CENTROIDDUP;
210 G_debug(3,
" draw_line(): type=%d rtype=%d npoints=%d draw=%d",
211 state.type, obj->type, state.Points->n_points, draw);
216 obj->npoints = state.Points->n_points;
218 (
struct rpoint *)G_malloc(obj->npoints *
sizeof(
struct rpoint));
219 robj_points(obj, state.Points);
227 void en_to_xy(
double east,
double north,
int *x,
int *
y)
231 w = region.center_easting - (region.map_width / 2) * region.map_res;
232 n = region.center_northing + (region.map_height / 2) * region.map_res;
235 *x = (east -
w) / region.map_res;
237 *y = (n - north) / region.map_res;
245 void draw_line_nodes(
struct Map_info *Map,
int line,
int draw_flag,
246 struct robject_list *list)
252 struct robject *robj;
256 for (i = 0; i <
sizeof(nodes) /
sizeof(
int); i++) {
259 if (draw_flag & DRAW_NODEONE) {
264 if (draw_flag & DRAW_NODETWO) {
274 robj = robj_alloc(type, 1);
275 en_to_xy(east, north, &x, &y);
280 list_append(list, robj);
287 void list_append(
struct robject_list *list,
struct robject *obj)
289 if (list->nitems >= state.nitems_alloc) {
290 state.nitems_alloc += 1000;
292 (
struct robject **)G_realloc(list->item,
294 sizeof(
struct robject *));
296 list->item[list->nitems++] = obj;
302 struct robject *robj_alloc(
int type,
int npoints)
304 struct robject *robj;
306 robj = (
struct robject *)G_malloc(
sizeof(
struct robject));
308 robj->npoints = npoints;
309 robj->point = (
struct rpoint *)G_malloc(npoints *
sizeof(
struct rpoint));
317 struct robject *draw_line_vertices()
321 struct robject *robj;
323 robj = robj_alloc(TYPE_VERTEX, state.Points->n_points - 2);
325 for (i = 1; i < state.Points->n_points - 1; i++) {
326 en_to_xy(state.Points->x[i], state.Points->y[i], &x, &y);
327 robj->point[i - 1].x = x;
328 robj->point[i - 1].y =
y;
337 int draw_line_dir(
struct robject_list *list,
int line)
342 double dist, angle,
pos;
351 G_debug(5,
" draw_line_dir() line=%d", line);
353 if (dist_in_px(dist) >= limit) {
355 pos = (narrows + 1) * 8 * limit * region.map_res;
362 en_to_xy(e, n, &x0, &y0);
365 (state.Points, pos - 3 * size * region.map_res, &e, &n,
NULL,
370 en_to_xy(e, n, &x1, &y1);
372 draw_arrow(x0, y0, x1, y1, angle, size, line, list);
386 en_to_xy(e, n, &x0, &y0);
389 (state.Points, dist - 3 * size * region.map_res, &e, &n,
392 en_to_xy(e, n, &x1, &y1);
394 draw_arrow(x0, y0, x1, y1, angle, size, line, list);
406 double dist_in_px(
double dist)
410 en_to_xy(region.map_west + dist, region.map_north, &x, &y);
418 void draw_arrow(
int x0,
int y0,
int x1,
int y1,
double angle,
int size,
int line,
419 struct robject_list *list)
422 struct robject *robj;
424 robj = robj_alloc(TYPE_DIRECTION, 3);
427 angle_symb = angle - M_PI / 2.;
428 robj->point[0].x = (
int)x1 + size * cos(angle_symb);
429 robj->point[0].y = (
int)y1 - size * sin(angle_symb);
431 robj->point[1].x = x0;
432 robj->point[1].y = y0;
434 angle_symb = M_PI / 2. + angle;
435 robj->point[2].x = (
int)x1 + size * cos(angle_symb);
436 robj->point[2].y = (
int)y1 - size * sin(angle_symb);
438 list_append(list, robj);
444 void draw_area(
struct Map_info *Map,
int area,
struct robject_list *list)
446 int i, centroid, isle;
448 struct line_pnts *ipoints;
450 struct robject *robj;
466 robj = robj_alloc(TYPE_AREA, state.Points->n_points);
467 robj->fid = centroid;
468 robj_points(robj, state.Points);
469 list_append(list, robj);
473 for (i = 0; i < num_isles; i++) {
479 robj = robj_alloc(TYPE_ISLE, ipoints->n_points);
481 robj_points(robj, ipoints);
482 list_append(list, robj);
491 void robj_points(
struct robject *robj,
const struct line_pnts *points)
496 for (i = 0; i < points->n_points; i++) {
497 en_to_xy(points->x[i], points->y[i], &x, &y);
498 robj->point[i].x = x;
499 robj->point[i].y =
y;
int Vect_destroy_list(struct ilist *list)
Frees all memory associated with a struct ilist, including the struct itself.
int Vect_get_area_centroid(struct Map_info *Map, int area)
Returns centroid number of area.
int Vect_get_isle_points(struct Map_info *Map, int isle, struct line_pnts *BPoints)
Returns the polygon array of points in BPoints.
int Vect_get_line_nodes(struct Map_info *Map, int line, int *n1, int *n2)
Get line nodes.
int Vect_get_line_areas(struct Map_info *Map, int line, int *left, int *right)
Get area/isle ids on the left and right.
struct line_pnts * Vect_new_line_struct()
Creates and initializes a struct line_pnts.
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int Vect_get_area_num_isles(struct Map_info *Map, int area)
Returns number of isles for area.
int Vect_get_area_isle(struct Map_info *Map, int area, int isle)
Returns isle for area.
int Vect_isle_alive(struct Map_info *Map, int isle)
Check if isle is alive or dead.
int Vect_get_area_points(struct Map_info *Map, int area, struct line_pnts *BPoints)
Returns the polygon array of points in BPoints.
struct robject_list * Vedit_render_map(struct Map_info *Map, struct bound_box *box, int draw_flag, double center_easting, double center_northing, int map_width, int map_height, double map_res)
Render vector features into list.
int Vect_point_on_line(struct line_pnts *Points, double distance, double *x, double *y, double *z, double *angle, double *slope)
Find point on line in the specified distance.
int Vect_line_alive(struct Map_info *Map, int line)
Check if feature is alive or dead.
int Vect_select_lines_by_box(struct Map_info *Map, BOUND_BOX *Box, int type, struct ilist *list)
Select lines by box.
int Vect_select_areas_by_box(struct Map_info *Map, BOUND_BOX *Box, struct ilist *list)
Select areas by box.
double Vect_line_length(struct line_pnts *Points)
Calculate line length, 3D-length in case of 3D vector line.
int G_debug(int level, const char *msg,...)
Print debugging message.
int Vect_get_centroid_area(struct Map_info *Map, int centroid)
Get area id the centroid is within.
int Vect_get_node_coor(struct Map_info *map, int num, double *x, double *y, double *z)
Get node coordinates.
int Vect_area_alive(struct Map_info *Map, int area)
Check if area is alive or dead.
int Vect_destroy_line_struct(struct line_pnts *p)
Frees all memory associated with a struct line_pnts, including the struct itself. ...
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature.
int Vect_get_node_n_lines(struct Map_info *Map, int node)
Get number of lines for node.