Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <stdio.h>
00026 #include <math.h>
00027 #include <grass/Vect.h>
00028
00029 static double d_atan2(double, double);
00030
00031 float dig_calc_begin_angle(struct line_pnts *points, double thresh)
00032 {
00033 double last_x;
00034 double last_y;
00035 double *xptr;
00036 double *yptr;
00037 int short_line;
00038 int i;
00039 int n_points;
00040 double *xarray;
00041 double *yarray;
00042
00043
00044 n_points = points->n_points;
00045 xarray = points->x;
00046 yarray = points->y;
00047
00048 last_x = *xarray;
00049 last_y = *yarray;
00050 xptr = xarray + 1;
00051 yptr = yarray + 1;
00052
00053
00054 if (dig_line_degenerate(points) > 0)
00055 return ((float)-9.);
00056
00057 short_line = 1;
00058 if (n_points != 2) {
00059
00060
00061
00062 for (i = 1; i < n_points - 1; i++) {
00063 if ((thresh < fabs(*xptr - last_x)) ||
00064 (thresh < fabs(*yptr - last_y))) {
00065 short_line = 0;
00066 break;
00067 }
00068 xptr++;
00069 yptr++;
00070 }
00071 }
00072
00073 if (short_line) {
00074
00075
00076 return ((float)d_atan2(yarray[1] - last_y, xarray[1] - last_x));
00077 }
00078
00079 return ((float)d_atan2(*yptr - last_y, *xptr - last_x));
00080 }
00081
00082 float dig_calc_end_angle(struct line_pnts *points, double thresh)
00083 {
00084 double last_x;
00085 double last_y;
00086 double *xptr;
00087 double *yptr;
00088 int short_line;
00089 int i;
00090 int n_points;
00091 double *xarray;
00092 double *yarray;
00093
00094 short_line = 1;
00095
00096 xarray = points->x;
00097 yarray = points->y;
00098 n_points = points->n_points;
00099
00100
00101 if (dig_line_degenerate(points) > 0)
00102 return ((float)-9.);
00103
00104 last_x = *(xarray + n_points - 1);
00105 last_y = *(yarray + n_points - 1);
00106 xptr = xarray + n_points - 2;
00107 yptr = yarray + n_points - 2;
00108
00109 if (n_points != 2) {
00110
00111
00112
00113 for (i = n_points - 2; i > 0; i--) {
00114 if ((thresh < fabs(*xptr - last_x)) ||
00115 (thresh < fabs(*yptr - last_y))) {
00116 short_line = 0;
00117 break;
00118 }
00119 xptr--;
00120 yptr--;
00121 }
00122 }
00123
00124 if (short_line) {
00125
00126
00127 return ((float)
00128 d_atan2(yarray[n_points - 2] - last_y,
00129 xarray[n_points - 2] - last_x));
00130 }
00131
00132 return ((float)d_atan2(*yptr - last_y, *xptr - last_x));
00133 }
00134
00135 int dig_is_line_degenerate(struct line_pnts *points, double thresh)
00136 {
00137 double last_x;
00138 double last_y;
00139 double *xptr;
00140 double *yptr;
00141 int short_line;
00142 int i;
00143 int n_points;
00144 double *xarray;
00145 double *yarray;
00146
00147
00148 n_points = points->n_points;
00149 xarray = points->x;
00150 yarray = points->y;
00151
00152 last_x = *xarray;
00153 last_y = *yarray;
00154 xptr = xarray + 1;
00155 yptr = yarray + 1;
00156
00157 short_line = 1;
00158 for (i = 1; i < n_points; i++) {
00159 if ((thresh < fabs(*xptr - last_x)) ||
00160 (thresh < fabs(*yptr - last_y))) {
00161 short_line = 0;
00162 break;
00163 }
00164 xptr++;
00165 yptr++;
00166 }
00167
00168 if (short_line)
00169 return (1);
00170
00171 return (0);
00172
00173 }
00174
00175
00176
00177
00178
00179
00180 int dig_line_degenerate(struct line_pnts *points)
00181 {
00182 int i, ident;
00183 int n_points;
00184
00185 G_debug(5, "dig_line_degenerate()");
00186
00187 n_points = points->n_points;
00188
00189 if (n_points == 1) {
00190 G_debug(5, " Line is degenerate (one points)");
00191 return 1;
00192 }
00193
00194
00195 ident = 1;
00196 for (i = 1; i < n_points; i++) {
00197 if (points->x[i] != points->x[i - 1] ||
00198 points->y[i] != points->y[i - 1]) {
00199 ident = 0;
00200 break;
00201 }
00202 }
00203
00204 if (ident) {
00205 G_debug(5, " Line is degenerate (more points)");
00206 return 2;
00207 }
00208
00209 return 0;
00210 }
00211
00212 static double d_atan2(double y, double x)
00213 {
00214 if (y == 0.0 && x == 0.0)
00215 return (0.0);
00216 else
00217 return (atan2(y, x));
00218 }