00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdlib.h>
00012 #include <math.h>
00013 #include <AR/ar.h>
00014 #include <AR/matrix.h>
00015
00016 #define MD_PI 3.14159265358979323846
00017
00018 double arModifyMatrix( double rot[3][3], double trans[3], double cpara[3][4],
00019 double vertex[][3], double pos2d[][2], int num )
00020 {
00021 double factor;
00022 double a, b, c;
00023 double a1, b1, c1;
00024 double a2, b2, c2;
00025 double ma = 0.0, mb = 0.0, mc = 0.0;
00026 double combo[3][4];
00027 double hx, hy, h, x, y;
00028 double err, minerr;
00029 int t1, t2, t3;
00030 int s1 = 0, s2 = 0, s3 = 0;
00031 int i, j;
00032
00033 arGetAngle( rot, &a, &b, &c );
00034
00035 a2 = a;
00036 b2 = b;
00037 c2 = c;
00038 factor = 10.0*MD_PI/180.0;
00039 for( j = 0; j < 10; j++ ) {
00040 minerr = 1000000000.0;
00041 for(t1=-1;t1<=1;t1++) {
00042 for(t2=-1;t2<=1;t2++) {
00043 for(t3=-1;t3<=1;t3++) {
00044 a1 = a2 + factor*t1;
00045 b1 = b2 + factor*t2;
00046 c1 = c2 + factor*t3;
00047 arGetNewMatrix( a1, b1, c1, trans, NULL, cpara, combo );
00048
00049 err = 0.0;
00050 for( i = 0; i < num; i++ ) {
00051 hx = combo[0][0] * vertex[i][0]
00052 + combo[0][1] * vertex[i][1]
00053 + combo[0][2] * vertex[i][2]
00054 + combo[0][3];
00055 hy = combo[1][0] * vertex[i][0]
00056 + combo[1][1] * vertex[i][1]
00057 + combo[1][2] * vertex[i][2]
00058 + combo[1][3];
00059 h = combo[2][0] * vertex[i][0]
00060 + combo[2][1] * vertex[i][1]
00061 + combo[2][2] * vertex[i][2]
00062 + combo[2][3];
00063 x = hx / h;
00064 y = hy / h;
00065
00066 err += (pos2d[i][0] - x) * (pos2d[i][0] - x)
00067 + (pos2d[i][1] - y) * (pos2d[i][1] - y);
00068 }
00069
00070 if( err < minerr ) {
00071 minerr = err;
00072 ma = a1;
00073 mb = b1;
00074 mc = c1;
00075 s1 = t1; s2 = t2; s3 = t3;
00076 }
00077 }
00078 }
00079 }
00080
00081 if( s1 == 0 && s2 == 0 && s3 == 0 ) factor *= 0.5;
00082 a2 = ma;
00083 b2 = mb;
00084 c2 = mc;
00085 }
00086
00087 arGetRot( ma, mb, mc, rot );
00088
00089
00090
00091 return minerr/num;
00092 }
00093
00094 double arsModifyMatrix( double rot[3][3], double trans[3], ARSParam *arsParam,
00095 double pos3dL[][3], double pos2dL[][2], int numL,
00096 double pos3dR[][3], double pos2dR[][2], int numR )
00097 {
00098 double factor;
00099 double a, b, c;
00100 double a1, b1, c1;
00101 double a2, b2, c2;
00102 double ma = 0.0, mb = 0.0, mc = 0.0;
00103 double combo[3][4];
00104 double hx, hy, h, x, y;
00105 double err, minerr;
00106 int t1, t2, t3;
00107 int s1 = 0, s2 = 0, s3 = 0;
00108 int i, j;
00109
00110 arGetAngle( rot, &a, &b, &c );
00111
00112 a2 = a;
00113 b2 = b;
00114 c2 = c;
00115 factor = 10.0*MD_PI/180.0;
00116 for( j = 0; j < 10; j++ ) {
00117 minerr = 1000000000.0;
00118 for(t1=-1;t1<=1;t1++) {
00119 for(t2=-1;t2<=1;t2++) {
00120 for(t3=-1;t3<=1;t3++) {
00121 a1 = a2 + factor*t1;
00122 b1 = b2 + factor*t2;
00123 c1 = c2 + factor*t3;
00124 err = 0.0;
00125
00126 arGetNewMatrix( a1, b1, c1, trans, NULL, arsParam->matL, combo );
00127 for( i = 0; i < numL; i++ ) {
00128 hx = combo[0][0] * pos3dL[i][0]
00129 + combo[0][1] * pos3dL[i][1]
00130 + combo[0][2] * pos3dL[i][2]
00131 + combo[0][3];
00132 hy = combo[1][0] * pos3dL[i][0]
00133 + combo[1][1] * pos3dL[i][1]
00134 + combo[1][2] * pos3dL[i][2]
00135 + combo[1][3];
00136 h = combo[2][0] * pos3dL[i][0]
00137 + combo[2][1] * pos3dL[i][1]
00138 + combo[2][2] * pos3dL[i][2]
00139 + combo[2][3];
00140 x = hx / h;
00141 y = hy / h;
00142 err += (pos2dL[i][0] - x) * (pos2dL[i][0] - x)
00143 + (pos2dL[i][1] - y) * (pos2dL[i][1] - y);
00144 }
00145
00146 arGetNewMatrix( a1, b1, c1, trans, arsParam->matL2R, arsParam->matR, combo );
00147 for( i = 0; i < numR; i++ ) {
00148 hx = combo[0][0] * pos3dR[i][0]
00149 + combo[0][1] * pos3dR[i][1]
00150 + combo[0][2] * pos3dR[i][2]
00151 + combo[0][3];
00152 hy = combo[1][0] * pos3dR[i][0]
00153 + combo[1][1] * pos3dR[i][1]
00154 + combo[1][2] * pos3dR[i][2]
00155 + combo[1][3];
00156 h = combo[2][0] * pos3dR[i][0]
00157 + combo[2][1] * pos3dR[i][1]
00158 + combo[2][2] * pos3dR[i][2]
00159 + combo[2][3];
00160 x = hx / h;
00161 y = hy / h;
00162
00163 err += (pos2dR[i][0] - x) * (pos2dR[i][0] - x)
00164 + (pos2dR[i][1] - y) * (pos2dR[i][1] - y);
00165 }
00166
00167 if( err < minerr ) {
00168 minerr = err;
00169 ma = a1;
00170 mb = b1;
00171 mc = c1;
00172 s1 = t1; s2 = t2; s3 = t3;
00173 }
00174 }
00175 }
00176 }
00177
00178 if( s1 == 0 && s2 == 0 && s3 == 0 ) factor *= 0.5;
00179 a2 = ma;
00180 b2 = mb;
00181 c2 = mc;
00182 }
00183
00184 arGetRot( ma, mb, mc, rot );
00185
00186 return minerr / (numL+numR);
00187 }