File: select.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (166 lines) | stat: -rw-r--r-- 4,270 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdlib.h> 
#include <string.h>
#include <unistd.h>
#include <math.h>
#include "gis.h"
#include "raster.h"
#include "display.h"
#include "Vect.h"
#include "colors.h"
#include "proto.h"
#include "glocale.h"

#define WDTH 5

int display ( struct Map_info *Map, struct line_pnts *, int, int, int );

int path ( struct Map_info *Map, int color, int hcolor, int bgcolor )
{
    int button, ret;
    int screen_x, screen_y ;
    double x, y, nx, ny, fx, fy, tx, ty, msize;
    double x1, y1, x2, y2, maxdist;
    struct line_pnts *Points;
    int node;
    int from_disp = 0, to_disp = 0, sp_disp = 0, from_node = 0, to_node = 0;

    Points = Vect_new_line_struct();

    msize = 10 * ( D_d_to_u_col(2.0) - D_d_to_u_col(1.0) ); /* do it better */ 
    G_debug (1, "msize = %f\n", msize);
    
    fprintf (stderr, _("L: from  M: to R: quit\n"));
    
    while ( 1 ) {
	R_get_location_with_pointer(&screen_x, &screen_y, &button) ;
	
        x = D_d_to_u_col ((double)(screen_x));
        y = D_d_to_u_row ((double)(screen_y));
    	/* fprintf (stderr, "%f %f\n", x, y); */
	    
        x1 = D_d_to_u_col ((double)(screen_x-WDTH));
        y1 = D_d_to_u_row ((double)(screen_y-WDTH));
        x2 = D_d_to_u_col ((double)(screen_x+WDTH));
        y2 = D_d_to_u_row ((double)(screen_y+WDTH));
			 
        x1 = fabs ( x2 - x1 );
        y1 = fabs ( y2 - y1 );
			     
        if ( x1 > y1 ) maxdist = x1;
        else maxdist = y1;
        G_debug (1, "Maximum distance in map units = %f\n", maxdist);

	node = Vect_find_node (Map, x, y, 0.0, maxdist, 0);

	if ( node > 0 ) {
	    Vect_get_node_coor ( Map, node, &nx, &ny, NULL); 
    	    fprintf (stderr, _("Node %d: %f %f\n"), node, nx, ny);
	}

	if ( sp_disp ) { /* erase old */
	    /* delete old highlight */
	    
            display ( Map, Points, color, from_node, to_node );
		
	    R_color(bgcolor);
	    if ( !from_node ) 
	        G_plot_line(Points->x[0], Points->y[0], Points->x[1], Points->y[1]);

	    if ( !to_node )
	        G_plot_line(Points->x[Points->n_points-2], Points->y[Points->n_points-2], 
			    Points->x[Points->n_points-1], Points->y[Points->n_points-1]);
	}
	
	switch ( button )	{
	    case 1:
		if ( from_disp ) {
		    R_color(bgcolor);
		    G_plot_icon( fx, fy, G_ICON_BOX, 0.0, msize);
                }
		if ( node > 0 ) {
		    fx = nx;
		    fy = ny;
		    from_node = 1;
		} else {
		    fx = x;
		    fy = y;
		    from_node = 0;
		}
		R_color(hcolor);
		G_plot_icon( fx, fy, G_ICON_BOX, 0.0, msize);
		R_flush();
		from_disp = 1;
	        break;
	    case 2:
		if ( to_disp ) {
		    R_color(bgcolor);
		    G_plot_icon( tx, ty, G_ICON_CROSS, 0.0, msize);
                }
		if ( node > 0 ) {
		    tx = nx;
		    ty = ny;
		    to_node = 1;
		} else {
		    tx = x;
		    ty = y;
		    to_node = 0;
		}
		R_color(hcolor);
		G_plot_icon( tx, ty, G_ICON_CROSS, 0.0, msize);
		R_flush();
		to_disp = 1;
	        break;
	    case 3:
		if ( from_disp ) {
		    R_color(bgcolor);
		    G_plot_icon( fx, fy, G_ICON_BOX, 0.0, msize);
		}
		if ( to_disp ) {
		    R_color(bgcolor);
		    G_plot_icon( tx, ty, G_ICON_CROSS, 0.0, msize);
		}
	        return 1;
	        break;
	}
	if ( from_disp && to_disp ) {
	    double fdist, tdist, cost;
	    
	    G_debug (2, "find path %f %f -> %f %f", fx, fy, tx, ty);
	    
	    ret = Vect_net_shortest_path_coor ( Map, fx, fy, 0.0, tx, ty, 0.0, 5*maxdist, 5*maxdist,
				                &cost, Points, NULL, NULL, NULL, &fdist, &tdist );
	    if ( ret == 0 ) {
		fprintf (stdout, _("Destination unreachable\n") );
		sp_disp = 0;
	    } else { 
		fprintf (stdout, _("Costs on the network = %f\n"), cost);
		fprintf (stdout, _("  Distance to the network = %f, distance from the network = %f\n"),  
			          fdist, tdist);
		
	        display ( Map, Points, hcolor, 1, 1 );
		sp_disp = 1;
	    }
	    
	}
	R_flush();
    };
    
    return 1;
}

int 
display ( struct Map_info *Map, struct line_pnts *Points, int color, int first, int last )
{
    int i, from, to;

    R_color(color);

    if ( first ) from = 0; else from = 1;
    if ( last ) to = Points->n_points; else to = Points->n_points - 1;
    
    for( i = from; i < to - 1; i++ ) 
	G_plot_line(Points->x[i], Points->y[i], Points->x[i+1], Points->y[i+1]);
	
    return 0;
}