00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <stdlib.h>
00013 #include <math.h>
00014 #include <AR/ar.h>
00015 #include <AR/matrix.h>
00016
00017 #define P_MAX 500
00018
00019 static double pos2d[P_MAX][2];
00020 static double pos3d[P_MAX][3];
00021
00022 static double arGetTransMatSub( double rot[3][3], double ppos2d[][2],
00023 double pos3d[][3], int num, double conv[3][4],
00024 double *dist_factor, double cpara[3][4] );
00025
00026 double arGetTransMat( ARMarkerInfo *marker_info,
00027 double center[2], double width, double conv[3][4] )
00028 {
00029 double rot[3][3];
00030 double ppos2d[4][2];
00031 double ppos3d[4][2];
00032 int dir;
00033 double err;
00034 int i;
00035
00036 if( arGetInitRot( marker_info, arParam.mat, rot ) < 0 ) return -1;
00037
00038 dir = marker_info->dir;
00039 ppos2d[0][0] = marker_info->vertex[(4-dir)%4][0];
00040 ppos2d[0][1] = marker_info->vertex[(4-dir)%4][1];
00041 ppos2d[1][0] = marker_info->vertex[(5-dir)%4][0];
00042 ppos2d[1][1] = marker_info->vertex[(5-dir)%4][1];
00043 ppos2d[2][0] = marker_info->vertex[(6-dir)%4][0];
00044 ppos2d[2][1] = marker_info->vertex[(6-dir)%4][1];
00045 ppos2d[3][0] = marker_info->vertex[(7-dir)%4][0];
00046 ppos2d[3][1] = marker_info->vertex[(7-dir)%4][1];
00047 ppos3d[0][0] = center[0] - width/2.0;
00048 ppos3d[0][1] = center[1] + width/2.0;
00049 ppos3d[1][0] = center[0] + width/2.0;
00050 ppos3d[1][1] = center[1] + width/2.0;
00051 ppos3d[2][0] = center[0] + width/2.0;
00052 ppos3d[2][1] = center[1] - width/2.0;
00053 ppos3d[3][0] = center[0] - width/2.0;
00054 ppos3d[3][1] = center[1] - width/2.0;
00055
00056 for( i = 0; i < AR_GET_TRANS_MAT_MAX_LOOP_COUNT; i++ ) {
00057 err = arGetTransMat3( rot, ppos2d, ppos3d, 4, conv,
00058 arParam.dist_factor, arParam.mat );
00059 if( err < AR_GET_TRANS_MAT_MAX_FIT_ERROR ) break;
00060 }
00061 return err;
00062 }
00063
00064 double arGetTransMat2( double rot[3][3], double ppos2d[][2],
00065 double ppos3d[][2], int num, double conv[3][4] )
00066 {
00067 return arGetTransMat3( rot, ppos2d, ppos3d, num, conv,
00068 arParam.dist_factor, arParam.mat );
00069 }
00070
00071 double arGetTransMat3( double rot[3][3], double ppos2d[][2],
00072 double ppos3d[][2], int num, double conv[3][4],
00073 double *dist_factor, double cpara[3][4] )
00074 {
00075 double off[3], pmax[3], pmin[3];
00076 double ret;
00077 int i;
00078
00079 pmax[0]=pmax[1]=pmax[2] = -10000000000.0;
00080 pmin[0]=pmin[1]=pmin[2] = 10000000000.0;
00081 for( i = 0; i < num; i++ ) {
00082 if( ppos3d[i][0] > pmax[0] ) pmax[0] = ppos3d[i][0];
00083 if( ppos3d[i][0] < pmin[0] ) pmin[0] = ppos3d[i][0];
00084 if( ppos3d[i][1] > pmax[1] ) pmax[1] = ppos3d[i][1];
00085 if( ppos3d[i][1] < pmin[1] ) pmin[1] = ppos3d[i][1];
00086
00087
00088
00089
00090 }
00091 off[0] = -(pmax[0] + pmin[0]) / 2.0;
00092 off[1] = -(pmax[1] + pmin[1]) / 2.0;
00093 off[2] = -(pmax[2] + pmin[2]) / 2.0;
00094 for( i = 0; i < num; i++ ) {
00095 pos3d[i][0] = ppos3d[i][0] + off[0];
00096 pos3d[i][1] = ppos3d[i][1] + off[1];
00097
00098
00099
00100 pos3d[i][2] = 0.0;
00101 }
00102
00103 ret = arGetTransMatSub( rot, ppos2d, pos3d, num, conv,
00104 dist_factor, cpara );
00105
00106 conv[0][3] = conv[0][0]*off[0] + conv[0][1]*off[1] + conv[0][2]*off[2] + conv[0][3];
00107 conv[1][3] = conv[1][0]*off[0] + conv[1][1]*off[1] + conv[1][2]*off[2] + conv[1][3];
00108 conv[2][3] = conv[2][0]*off[0] + conv[2][1]*off[1] + conv[2][2]*off[2] + conv[2][3];
00109
00110 return ret;
00111 }
00112
00113 double arGetTransMat4( double rot[3][3], double ppos2d[][2],
00114 double ppos3d[][3], int num, double conv[3][4] )
00115 {
00116 return arGetTransMat5( rot, ppos2d, ppos3d, num, conv,
00117 arParam.dist_factor, arParam.mat );
00118 }
00119
00120 double arGetTransMat5( double rot[3][3], double ppos2d[][2],
00121 double ppos3d[][3], int num, double conv[3][4],
00122 double *dist_factor, double cpara[3][4] )
00123 {
00124 double off[3], pmax[3], pmin[3];
00125 double ret;
00126 int i;
00127
00128 pmax[0]=pmax[1]=pmax[2] = -10000000000.0;
00129 pmin[0]=pmin[1]=pmin[2] = 10000000000.0;
00130 for( i = 0; i < num; i++ ) {
00131 if( ppos3d[i][0] > pmax[0] ) pmax[0] = ppos3d[i][0];
00132 if( ppos3d[i][0] < pmin[0] ) pmin[0] = ppos3d[i][0];
00133 if( ppos3d[i][1] > pmax[1] ) pmax[1] = ppos3d[i][1];
00134 if( ppos3d[i][1] < pmin[1] ) pmin[1] = ppos3d[i][1];
00135 if( ppos3d[i][2] > pmax[2] ) pmax[2] = ppos3d[i][2];
00136 if( ppos3d[i][2] < pmin[2] ) pmin[2] = ppos3d[i][2];
00137 }
00138 off[0] = -(pmax[0] + pmin[0]) / 2.0;
00139 off[1] = -(pmax[1] + pmin[1]) / 2.0;
00140 off[2] = -(pmax[2] + pmin[2]) / 2.0;
00141 for( i = 0; i < num; i++ ) {
00142 pos3d[i][0] = ppos3d[i][0] + off[0];
00143 pos3d[i][1] = ppos3d[i][1] + off[1];
00144 pos3d[i][2] = ppos3d[i][2] + off[2];
00145 }
00146
00147 ret = arGetTransMatSub( rot, ppos2d, pos3d, num, conv,
00148 dist_factor, cpara );
00149
00150 conv[0][3] = conv[0][0]*off[0] + conv[0][1]*off[1] + conv[0][2]*off[2] + conv[0][3];
00151 conv[1][3] = conv[1][0]*off[0] + conv[1][1]*off[1] + conv[1][2]*off[2] + conv[1][3];
00152 conv[2][3] = conv[2][0]*off[0] + conv[2][1]*off[1] + conv[2][2]*off[2] + conv[2][3];
00153
00154 return ret;
00155 }
00156
00157 static double arGetTransMatSub( double rot[3][3], double ppos2d[][2],
00158 double pos3d[][3], int num, double conv[3][4],
00159 double *dist_factor, double cpara[3][4] )
00160 {
00161 ARMat *mat_a, *mat_b, *mat_c, *mat_d, *mat_e, *mat_f;
00162 double trans[3];
00163 double wx, wy, wz;
00164 double ret;
00165 int i, j;
00166
00167 mat_a = arMatrixAlloc( num*2, 3 );
00168 mat_b = arMatrixAlloc( 3, num*2 );
00169 mat_c = arMatrixAlloc( num*2, 1 );
00170 mat_d = arMatrixAlloc( 3, 3 );
00171 mat_e = arMatrixAlloc( 3, 1 );
00172 mat_f = arMatrixAlloc( 3, 1 );
00173
00174 if( arFittingMode == AR_FITTING_TO_INPUT ) {
00175 for( i = 0; i < num; i++ ) {
00176 arParamIdeal2Observ(dist_factor, ppos2d[i][0], ppos2d[i][1],
00177 &pos2d[i][0], &pos2d[i][1]);
00178 }
00179 }
00180 else {
00181 for( i = 0; i < num; i++ ) {
00182 pos2d[i][0] = ppos2d[i][0];
00183 pos2d[i][1] = ppos2d[i][1];
00184 }
00185 }
00186
00187 for( j = 0; j < num; j++ ) {
00188 wx = rot[0][0] * pos3d[j][0]
00189 + rot[0][1] * pos3d[j][1]
00190 + rot[0][2] * pos3d[j][2];
00191 wy = rot[1][0] * pos3d[j][0]
00192 + rot[1][1] * pos3d[j][1]
00193 + rot[1][2] * pos3d[j][2];
00194 wz = rot[2][0] * pos3d[j][0]
00195 + rot[2][1] * pos3d[j][1]
00196 + rot[2][2] * pos3d[j][2];
00197 mat_a->m[j*6+0] = mat_b->m[num*0+j*2] = cpara[0][0];
00198 mat_a->m[j*6+1] = mat_b->m[num*2+j*2] = cpara[0][1];
00199 mat_a->m[j*6+2] = mat_b->m[num*4+j*2] = cpara[0][2] - pos2d[j][0];
00200 mat_c->m[j*2+0] = wz * pos2d[j][0]
00201 - cpara[0][0]*wx - cpara[0][1]*wy - cpara[0][2]*wz;
00202 mat_a->m[j*6+3] = mat_b->m[num*0+j*2+1] = 0.0;
00203 mat_a->m[j*6+4] = mat_b->m[num*2+j*2+1] = cpara[1][1];
00204 mat_a->m[j*6+5] = mat_b->m[num*4+j*2+1] = cpara[1][2] - pos2d[j][1];
00205 mat_c->m[j*2+1] = wz * pos2d[j][1]
00206 - cpara[1][1]*wy - cpara[1][2]*wz;
00207 }
00208 arMatrixMul( mat_d, mat_b, mat_a );
00209 arMatrixMul( mat_e, mat_b, mat_c );
00210 arMatrixSelfInv( mat_d );
00211 arMatrixMul( mat_f, mat_d, mat_e );
00212 trans[0] = mat_f->m[0];
00213 trans[1] = mat_f->m[1];
00214 trans[2] = mat_f->m[2];
00215
00216 ret = arModifyMatrix( rot, trans, cpara, pos3d, pos2d, num );
00217
00218 for( j = 0; j < num; j++ ) {
00219 wx = rot[0][0] * pos3d[j][0]
00220 + rot[0][1] * pos3d[j][1]
00221 + rot[0][2] * pos3d[j][2];
00222 wy = rot[1][0] * pos3d[j][0]
00223 + rot[1][1] * pos3d[j][1]
00224 + rot[1][2] * pos3d[j][2];
00225 wz = rot[2][0] * pos3d[j][0]
00226 + rot[2][1] * pos3d[j][1]
00227 + rot[2][2] * pos3d[j][2];
00228 mat_a->m[j*6+0] = mat_b->m[num*0+j*2] = cpara[0][0];
00229 mat_a->m[j*6+1] = mat_b->m[num*2+j*2] = cpara[0][1];
00230 mat_a->m[j*6+2] = mat_b->m[num*4+j*2] = cpara[0][2] - pos2d[j][0];
00231 mat_c->m[j*2+0] = wz * pos2d[j][0]
00232 - cpara[0][0]*wx - cpara[0][1]*wy - cpara[0][2]*wz;
00233 mat_a->m[j*6+3] = mat_b->m[num*0+j*2+1] = 0.0;
00234 mat_a->m[j*6+4] = mat_b->m[num*2+j*2+1] = cpara[1][1];
00235 mat_a->m[j*6+5] = mat_b->m[num*4+j*2+1] = cpara[1][2] - pos2d[j][1];
00236 mat_c->m[j*2+1] = wz * pos2d[j][1]
00237 - cpara[1][1]*wy - cpara[1][2]*wz;
00238 }
00239 arMatrixMul( mat_d, mat_b, mat_a );
00240 arMatrixMul( mat_e, mat_b, mat_c );
00241 arMatrixSelfInv( mat_d );
00242 arMatrixMul( mat_f, mat_d, mat_e );
00243 trans[0] = mat_f->m[0];
00244 trans[1] = mat_f->m[1];
00245 trans[2] = mat_f->m[2];
00246
00247 ret = arModifyMatrix( rot, trans, cpara, pos3d, pos2d, num );
00248
00249 arMatrixFree( mat_a );
00250 arMatrixFree( mat_b );
00251 arMatrixFree( mat_c );
00252 arMatrixFree( mat_d );
00253 arMatrixFree( mat_e );
00254 arMatrixFree( mat_f );
00255
00256 for( j = 0; j < 3; j++ ) {
00257 for( i = 0; i < 3; i++ ) conv[j][i] = rot[j][i];
00258 conv[j][3] = trans[j];
00259 }
00260
00261 return ret;
00262 }