00001 #ifdef _WIN32
00002 #include <windows.h>
00003 #endif
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <math.h>
00008 #include <AR/param.h>
00009 #include <AR/matrix.h>
00010 #include <AR/ar.h>
00011 #include "paddle.h"
00012 #include "command_sub.h"
00013 #include "util.h"
00014
00015
00016 #define SHAKE_BUF_SIZE 10
00017 #define PUNCH_BUF_SIZE 5
00018 #define PUSH_BUF_SIZE 2
00019
00020 typedef struct {
00021 double mat[3][4];
00022 int f;
00023 } SHAKE_BUF_T;
00024
00025 typedef struct {
00026 double x, y, z;
00027 int f;
00028 } PUNCH_BUF_T;
00029
00030 typedef struct {
00031 double x, y, z;
00032 int f;
00033 } PUSH_BUF_T;
00034
00035 static SHAKE_BUF_T shake_buf[SHAKE_BUF_SIZE];
00036 static int shake_buf_num = 0;
00037
00038
00039
00040
00041
00042
00043 int check_shake( double card_trans[3][4], int f )
00044 {
00045 ARMat *mat_a, *mat_b, *mat_c;
00046 double lxy, lz;
00047 int i, j, k;
00048
00049 if( shake_buf_num < SHAKE_BUF_SIZE ) {
00050 if( f ) {
00051 for( j = 0; j < 3; j++ ) {
00052 for( i = 0; i < 4; i++ ) {
00053 shake_buf[shake_buf_num].mat[j][i] = card_trans[j][i];
00054 }
00055 }
00056 shake_buf[shake_buf_num].f = 1;
00057 }
00058 else {
00059 shake_buf[shake_buf_num].f = 0;
00060 }
00061 shake_buf_num++;
00062
00063 return 0;
00064 }
00065 for( i = 1; i < shake_buf_num; i++ ) {
00066 shake_buf[i-1] = shake_buf[i];
00067 }
00068 if( f ) {
00069 for( j = 0; j < 3; j++ ) {
00070 for( i = 0; i < 4; i++ ) {
00071 shake_buf[shake_buf_num-1].mat[j][i] = card_trans[j][i];
00072 }
00073 }
00074 shake_buf[shake_buf_num-1].f = 1;
00075 }
00076 else {
00077 shake_buf[shake_buf_num-1].f = 0;
00078
00079 return 0;
00080 }
00081
00082 if( shake_buf[SHAKE_BUF_SIZE-3].f == 0
00083 || shake_buf[0].f == 0 ) return 0;
00084
00085
00086 mat_a = arMatrixAlloc( 4, 4 );
00087 mat_b = arMatrixAlloc( 4, 4 );
00088 mat_c = arMatrixAlloc( 4, 4 );
00089 for( j = 0; j < 3; j++ ) {
00090 for( i = 0; i < 4; i++ ) {
00091 mat_a->m[j*4+i] = card_trans[j][i];
00092 }
00093 }
00094 mat_a->m[3*4+0] = 0.0;
00095 mat_a->m[3*4+1] = 0.0;
00096 mat_a->m[3*4+2] = 0.0;
00097 mat_a->m[3*4+3] = 1.0;
00098 arMatrixSelfInv( mat_a );
00099
00100 for( k = 0 ; k < SHAKE_BUF_SIZE-3; k++ ) {
00101 if( shake_buf[k].f == 0 ) continue;
00102
00103 for( j = 0; j < 3; j++ ) {
00104 for( i = 0; i < 4; i++ ) {
00105 mat_b->m[j*4+i] = shake_buf[k].mat[j][i];
00106 }
00107 }
00108 mat_b->m[3*4+0] = 0.0;
00109 mat_b->m[3*4+1] = 0.0;
00110 mat_b->m[3*4+2] = 0.0;
00111 mat_b->m[3*4+3] = 1.0;
00112 arMatrixMul( mat_c, mat_a, mat_b );
00113
00114 lxy = sqrt( (mat_c->m[0*4+3])*(mat_c->m[0*4+3])
00115 +(mat_c->m[1*4+3])*(mat_c->m[1*4+3]));
00116 lz = mat_c->m[2*4+3];
00117
00118 if( lxy < 20.0 && lz < 20.0 ) break;
00119 }
00120 if( k == SHAKE_BUF_SIZE-3 ) {
00121 arMatrixFree( mat_a );
00122 arMatrixFree( mat_b );
00123 arMatrixFree( mat_c );
00124 return 0;
00125 }
00126
00127 for( ; k < SHAKE_BUF_SIZE-1; k++ ) {
00128 if( shake_buf[k].f == 0 ) continue;
00129
00130 for( j = 0; j < 3; j++ ) {
00131 for( i = 0; i < 4; i++ ) {
00132 mat_b->m[j*4+i] = shake_buf[k].mat[j][i];
00133 }
00134 }
00135 mat_b->m[3*4+0] = 0.0;
00136 mat_b->m[3*4+1] = 0.0;
00137 mat_b->m[3*4+2] = 0.0;
00138 mat_b->m[3*4+3] = 1.0;
00139 arMatrixMul( mat_c, mat_a, mat_b );
00140
00141 lxy = sqrt( (mat_c->m[0*4+3])*(mat_c->m[0*4+3])
00142 +(mat_c->m[1*4+3])*(mat_c->m[1*4+3]));
00143 lz = mat_c->m[2*4+3];
00144
00145 if( lxy > 60.0 && lz < 20.0 ) break;
00146 }
00147 arMatrixFree( mat_a );
00148 arMatrixFree( mat_b );
00149 arMatrixFree( mat_c );
00150
00151 if( k < SHAKE_BUF_SIZE-1 ) {
00152 shake_buf_num = 0;
00153 return 1;
00154 }
00155
00156 return 0;
00157 }
00158
00159
00160 int check_incline( double card_trans[3][4], double base_trans[3][4], double *angle )
00161 {
00162 ARMat *mat_a, *mat_b, *mat_c;
00163 double a, b, c;
00164 int i, j;
00165
00166 mat_a = arMatrixAlloc( 4, 4 );
00167 mat_b = arMatrixAlloc( 4, 4 );
00168 mat_c = arMatrixAlloc( 4, 4 );
00169 for( j = 0; j < 3; j++ ) {
00170 for( i = 0; i < 4; i++ ) {
00171 mat_b->m[j*4+i] = base_trans[j][i];
00172 }
00173 }
00174 mat_b->m[3*4+0] = 0.0;
00175 mat_b->m[3*4+1] = 0.0;
00176 mat_b->m[3*4+2] = 0.0;
00177 mat_b->m[3*4+3] = 1.0;
00178
00179 for( j = 0; j < 3; j++ ) {
00180 for( i = 0; i < 4; i++ ) {
00181 mat_a->m[j*4+i] = card_trans[j][i];
00182 }
00183 }
00184 mat_a->m[3*4+0] = 0.0;
00185 mat_a->m[3*4+1] = 0.0;
00186 mat_a->m[3*4+2] = 0.0;
00187 mat_a->m[3*4+3] = 1.0;
00188 arMatrixSelfInv( mat_a );
00189 arMatrixMul( mat_c, mat_a, mat_b );
00190
00191 get_angle( (double (*)[4])(mat_c->m), &a, &b, &c );
00192
00193 arMatrixFree( mat_a );
00194 arMatrixFree( mat_b );
00195 arMatrixFree( mat_c );
00196
00197 if( b > 0.4 ) {
00198 *angle = a + 3.141592;
00199 return 1;
00200 }
00201
00202 return 0;
00203 }
00204
00205 int check_pickup(double card_trans[3][4], double base_trans[3][4], ItemList* itlist, double* angle)
00206 {
00207 ARMat *t1, *t2, *t3;
00208 double x, y, z;
00209 double lx, ly;
00210 double a, b, c;
00211 int ret;
00212 int i, j;
00213
00214
00215
00216 t1 = arMatrixAlloc( 4, 4 );
00217 for( j = 0; j < 3; j++ ) {
00218 for( i = 0; i < 4; i++ ) {
00219 t1->m[j*4+i] = base_trans[j][i];
00220 }
00221 }
00222 t1->m[12] = t1->m[13] = t1->m[14] = 0.0;
00223 t1->m[15] = 1.0;
00224
00225 t2 = arMatrixAlloc( 4, 4 );
00226 for( j = 0; j < 3; j++ ) {
00227 for( i = 0; i < 4; i++ ) {
00228 t2->m[j*4+i] = card_trans[j][i];
00229 }
00230 }
00231 t2->m[12] = t2->m[13] = t2->m[14] = 0.0;
00232 t2->m[15] = 1.0;
00233
00234 if( arMatrixSelfInv(t1) != 0 ) {
00235 arMatrixFree( t1 );
00236 arMatrixFree( t2 );
00237 return -1;
00238 }
00239
00240
00241 t3 = arMatrixAllocMul(t1, t2);
00242 if( t3 == NULL ) {
00243 arMatrixFree( t1 );
00244 arMatrixFree( t2 );
00245 return -1;
00246 }
00247
00248
00249
00250 x = t3->m[0*4+3];
00251 y = t3->m[1*4+3];
00252 z = t3->m[2*4+3];
00253
00254
00255
00256 ret = -1;
00257 for( i = 0; i < itlist->itemnum; i ++ ){
00258 lx = x - itlist->item[i].pos[0];
00259 ly = y - itlist->item[i].pos[1];
00260
00261 if( lx*lx + ly*ly < 1000.0 && z < 20.0 ) {
00262 ret = i;
00263 }
00264 }
00265
00266 if( ret >= 0 ) {
00267 get_angle( (double (*)[4])(t3->m), &a, &b, &c );
00268 *angle = -c;
00269 }
00270
00271 arMatrixFree( t1 );
00272 arMatrixFree( t2 );
00273 arMatrixFree( t3 );
00274
00275 return ret;
00276 }