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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #include <math.h>
00065 #include <grass/libtrans.h>
00066
00067
00068 static double A0,A1,A2,A3,A4,A5;
00069 static double B0,B1,B2,B3,B4,B5;
00070
00071 static int resid(
00072 double *,double *,double *,double *,int *,int,double *,double *,int);
00073
00074 int compute_transformation_coef(
00075 double ax[],double ay[],double bx[],double by[],
00076 int use[],int n)
00077 {
00078 int i;
00079 int j;
00080 int count;
00081 double aa[3];
00082 double aar[3];
00083 double bb[3];
00084 double bbr[3];
00085
00086 double cc[3][3];
00087 double x;
00088
00089 count = 0;
00090 for (i = 0; i < n; i++)
00091 if (use[i])
00092 count++;
00093 if (count < 4)
00094 return -2;
00095
00096 for (i = 0; i < 3; i++)
00097 {
00098 aa[i] = bb[i] = 0.0;
00099
00100 for (j = 0; j < 3; j++)
00101 cc[i][j] = 0.0;
00102 }
00103
00104 for (i = 0; i < n; i++)
00105 {
00106 if (!use[i]) continue;
00107 cc[0][0] += 1;
00108 cc[0][1] += bx[i];
00109 cc[0][2] += by[i];
00110
00111 cc[1][1] += bx[i] * bx[i];
00112 cc[1][2] += bx[i] * by[i];
00113 cc[2][2] += by[i] * by[i];
00114
00115 aa[0] += ay[i];
00116 aa[1] += ay[i] * bx[i];
00117 aa[2] += ay[i] * by[i];
00118
00119 bb[0] += ax[i];
00120 bb[1] += ax[i] * bx[i];
00121 bb[2] += ax[i] * by[i];
00122 }
00123
00124 cc[1][0] = cc[0][1];
00125 cc[2][0] = cc[0][2];
00126 cc[2][1] = cc[1][2];
00127
00128
00129
00130 if ( inverse (cc) < 0)
00131 return (-1) ;
00132 if ( m_mult ( cc, aa, aar) < 0 || m_mult ( cc, bb, bbr) < 0)
00133 return (-1) ;
00134
00135
00136
00137
00138
00139 B0 = aar[0];
00140 B1 = aar[1];
00141 B2 = aar[2];
00142
00143 B3 = bbr[0];
00144 B4 = bbr[1];
00145 B5 = bbr[2];
00146
00147
00148
00149 x = B2 * B4 - B1 * B5 ;
00150
00151 if( ! x)
00152 return (-1) ;
00153
00154 A0 = (B1 * B3 - B0 * B4) / x ;
00155 A1 = -B1 / x ;
00156 A2 = B4 / x ;
00157 A3 = (B0 * B5 - B2 * B3) / x ;
00158 A4 = B2 / x ;
00159 A5 = -B5 / x ;
00160
00161 return 1;
00162 }
00163
00164 int transform_a_into_b(
00165 double ax,double ay,
00166 double *bx,double *by)
00167 {
00168 *by = A0 + A1 * ax + A2 * ay ;
00169 *bx = A3 + A4 * ax + A5 * ay ;
00170
00171 return 0;
00172 }
00173
00174 int transform_b_into_a(
00175 double bx,double by,
00176 double *ax,double *ay)
00177 {
00178 *ay = B0 + B1 * bx + B2 * by ;
00179 *ax = B3 + B4 * bx + B5 * by ;
00180
00181 return 0;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 int residuals_a_predicts_b (
00199 double ax[],double ay[],double bx[],double by[],
00200 int use[],
00201 int n,
00202 double residuals[],
00203 double *rms)
00204 {
00205 resid (ax,ay,bx,by,use,n,residuals,rms,1);
00206
00207 return 0;
00208 }
00209
00210 int residuals_b_predicts_a(
00211 double ax[],double ay[],double bx[],double by[],
00212 int use[],
00213 int n,
00214 double residuals[],
00215 double *rms)
00216 {
00217 resid (ax,ay,bx,by,use,n,residuals,rms,0);
00218
00219 return 0;
00220 }
00221
00222 static int resid(
00223 double ax[],double ay[],double bx[],double by[],
00224 int use[],int n,
00225 double residuals[],
00226 double *rms,
00227 int atob)
00228 {
00229 double x,y;
00230 int i;
00231 int count;
00232 double sum;
00233 double delta;
00234 double dx,dy;
00235
00236 count = 0;
00237 sum = 0.0;
00238 for (i=0; i < n; i++)
00239 {
00240 if (!use[i]) continue;
00241
00242 count++;
00243 if (atob)
00244 {
00245 transform_a_into_b (ax[i], ay[i], &x, &y);
00246 dx = x - bx[i];
00247 dy = y - by[i];
00248 }
00249 else
00250 {
00251 transform_b_into_a (bx[i], by[i], &x, &y);
00252 dx = x - ax[i];
00253 dy = y - ay[i];
00254 }
00255
00256 delta = dx * dx + dy * dy;
00257 residuals[i] = sqrt (delta);
00258 sum += delta;
00259 }
00260 *rms = sqrt (sum / count);
00261
00262 return 0;
00263 }