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 *));
146 struct robject *draw_line(
struct Map_info *
Map,
int line,
int draw_flag)
159 obj = (
struct robject *)G_malloc(
sizeof(
struct robject));
163 if (state.type == GV_LINE) {
164 obj->type = TYPE_LINE;
165 draw = draw_flag & DRAW_LINE;
167 else if (state.type == GV_BOUNDARY) {
171 if (left == 0 && right == 0) {
172 obj->type = TYPE_BOUNDARYNO;
173 draw = draw_flag & DRAW_BOUNDARYNO;
175 else if (left > 0 && right > 0) {
176 obj->type = TYPE_BOUNDARYTWO;
177 draw = draw_flag & DRAW_BOUNDARYTWO;
180 obj->type = TYPE_BOUNDARYONE;
181 draw = draw_flag & DRAW_BOUNDARYONE;
185 else if (state.type & GV_POINTS) {
186 if (state.type == GV_POINT) {
187 obj->type = TYPE_POINT;
188 draw = draw_flag & DRAW_POINT;
190 else if (state.type == GV_CENTROID) {
194 obj->type = TYPE_CENTROIDIN;
195 draw = draw_flag & DRAW_CENTROIDIN;
197 else if (cret == 0) {
198 obj->type = TYPE_CENTROIDOUT;
199 draw = draw_flag & DRAW_CENTROIDOUT;
202 obj->type = TYPE_CENTROIDDUP;
203 draw = draw_flag & DRAW_CENTROIDDUP;
207 G_debug(3,
" draw_line(): type=%d rtype=%d npoints=%d draw=%d",
208 state.type, obj->type, state.Points->n_points, draw);
213 obj->npoints = state.Points->n_points;
215 (
struct rpoint *)G_malloc(obj->npoints *
sizeof(
struct rpoint));
216 robj_points(obj, state.Points);
224 void en_to_xy(
double east,
double north,
int *x,
int *
y)
228 w = region.center_easting - (region.map_width / 2) * region.map_res;
229 n = region.center_northing + (region.map_height / 2) * region.map_res;
232 *x = (east -
w) / region.map_res;
234 *y = (n - north) / region.map_res;
242 void draw_line_nodes(
struct Map_info *Map,
int line,
int draw_flag,
243 struct robject_list *list)
249 struct robject *robj;
253 for (i = 0; i <
sizeof(nodes) /
sizeof(
int); i++) {
256 if (draw_flag & DRAW_NODEONE) {
261 if (draw_flag & DRAW_NODETWO) {
271 robj = robj_alloc(type, 1);
272 en_to_xy(east, north, &x, &y);
277 list_append(list, robj);
284 void list_append(
struct robject_list *list,
struct robject *obj)
286 if (list->nitems >= state.nitems_alloc) {
287 state.nitems_alloc += 1000;
289 (
struct robject **)G_realloc(list->item,
291 sizeof(
struct robject *));
293 list->item[list->nitems++] = obj;
299 struct robject *robj_alloc(
int type,
int npoints)
301 struct robject *robj;
303 robj = (
struct robject *)G_malloc(
sizeof(
struct robject));
305 robj->npoints = npoints;
306 robj->point = (
struct rpoint *)G_malloc(npoints *
sizeof(
struct rpoint));
314 struct robject *draw_line_vertices()
318 struct robject *robj;
320 robj = robj_alloc(TYPE_VERTEX, state.Points->n_points - 2);
322 for (i = 1; i < state.Points->n_points - 1; i++) {
323 en_to_xy(state.Points->x[i], state.Points->y[i], &x, &y);
324 robj->point[i - 1].x = x;
325 robj->point[i - 1].y =
y;
334 int draw_line_dir(
struct robject_list *list,
int line)
339 double dist, angle,
pos;
348 G_debug(5,
" draw_line_dir() line=%d", line);
350 if (dist_in_px(dist) >= limit) {
352 pos = (narrows + 1) * 8 * limit * region.map_res;
359 en_to_xy(e, n, &x0, &y0);
362 (state.Points, pos - 3 * size * region.map_res, &e, &n,
NULL,
367 en_to_xy(e, n, &x1, &y1);
369 draw_arrow(x0, y0, x1, y1, angle, size, line, list);
383 en_to_xy(e, n, &x0, &y0);
386 (state.Points, dist - 3 * size * region.map_res, &e, &n,
389 en_to_xy(e, n, &x1, &y1);
391 draw_arrow(x0, y0, x1, y1, angle, size, line, list);
403 double dist_in_px(
double dist)
407 en_to_xy(region.map_west + dist, region.map_north, &x, &y);
415 void draw_arrow(
int x0,
int y0,
int x1,
int y1,
double angle,
int size,
int line,
416 struct robject_list *list)
419 struct robject *robj;
421 robj = robj_alloc(TYPE_DIRECTION, 3);
424 angle_symb = angle - M_PI / 2.;
425 robj->point[0].x = (
int)x1 + size * cos(angle_symb);
426 robj->point[0].y = (
int)y1 - size * sin(angle_symb);
428 robj->point[1].x = x0;
429 robj->point[1].y = y0;
431 angle_symb = M_PI / 2. + angle;
432 robj->point[2].x = (
int)x1 + size * cos(angle_symb);
433 robj->point[2].y = (
int)y1 - size * sin(angle_symb);
435 list_append(list, robj);
441 void draw_area(
struct Map_info *Map,
int area,
struct robject_list *list)
443 int i, centroid, isle;
445 struct line_pnts *ipoints;
447 struct robject *robj;
463 robj = robj_alloc(TYPE_AREA, state.Points->n_points);
464 robj_points(robj, state.Points);
465 list_append(list, robj);
469 for (i = 0; i < num_isles; i++) {
475 robj = robj_alloc(TYPE_ISLE, ipoints->n_points);
476 robj_points(robj, ipoints);
477 list_append(list, robj);
486 void robj_points(
struct robject *robj,
const struct line_pnts *points)
491 for (i = 0; i < points->n_points; i++) {
492 en_to_xy(points->x[i], points->y[i], &x, &y);
493 robj->point[i].x = x;
494 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.