00001 #include <stdio.h> 00002 #include <stdlib.h> 00003 #include <AR/ar.h> 00004 #include "paddle.h" 00005 00006 static char *get_buff( char *buf, int n, FILE *fp ); 00007 00008 static int get_marker( ARMarkerInfo *markerInfo, int *markerFlag, 00009 int marker_num, int id, 00010 ARMarkerInfo *prevInfo, int *pcount ); 00011 00012 00013 ARPaddleInfo *paddleInit( char *name ) 00014 { 00015 FILE *fp; 00016 ARPaddleInfo *paddleInfo; 00017 char buf[256], buf1[256]; 00018 00019 if( (fp=fopen(name, "r")) == NULL ) return(0); 00020 00021 arMalloc( paddleInfo, ARPaddleInfo, 1 ); 00022 00023 get_buff(buf, 256, fp); 00024 if( sscanf(buf, "%s", paddleInfo->name) != 1 ) { 00025 fclose(fp); free(paddleInfo); return 0; 00026 } 00027 00028 get_buff(buf, 256, fp); 00029 if( sscanf(buf, "%s", buf1) != 1 ) { 00030 fclose(fp); free(paddleInfo); return 0; 00031 } 00032 if( (paddleInfo->marker_id = arLoadPatt(buf1)) < 0 ) { 00033 fclose(fp); free(paddleInfo); return 0; 00034 } 00035 00036 get_buff(buf, 256, fp); 00037 if( sscanf(buf, "%lf", &paddleInfo->width) != 1 ) { 00038 fclose(fp); free(paddleInfo); return 0; 00039 } 00040 00041 paddleInfo->center[0] = 0.0; 00042 paddleInfo->center[1] = 0.0; 00043 paddleInfo->active = 0; 00044 paddleInfo->pcount = 0; 00045 paddleInfo->pcountL = 0; 00046 paddleInfo->pcountR = 0; 00047 00048 fclose(fp); 00049 00050 return paddleInfo; 00051 } 00052 00053 static char *get_buff( char *buf, int n, FILE *fp ) 00054 { 00055 char *ret; 00056 00057 for(;;) { 00058 ret = fgets( buf, n, fp ); 00059 if( ret == NULL ) return(NULL); 00060 if( buf[0] != '\n' && buf[0] != '#' ) return(ret); 00061 } 00062 } 00063 00064 int paddleGetTrans( ARPaddleInfo *paddleInfo, ARMarkerInfo *markerInfo, int *markerFlag, int marker_num, ARParam *cparam ) 00065 { 00066 int id; 00067 00068 id = get_marker( markerInfo, markerFlag, marker_num, paddleInfo->marker_id, 00069 &(paddleInfo->prevInfo), &(paddleInfo->pcount) ); 00070 00071 if( id == -1 ) { paddleInfo->active = 0; return 0; } 00072 00073 arGetTransMat(&markerInfo[id], 00074 paddleInfo->center, paddleInfo->width, paddleInfo->trans); 00075 markerFlag[id] = 1; 00076 00077 paddleInfo->active = 1; 00078 00079 return 0; 00080 } 00081 00082 static int get_marker( ARMarkerInfo *markerInfo, int *markerFlag, int marker_num, int id, 00083 ARMarkerInfo *prevInfo, int *pcount ) 00084 { 00085 double rlen, rlenmin, rarea, diff, diffmin; 00086 int cid, cdir; 00087 int i, j; 00088 00089 cid = -1; 00090 for( i = 0; i < marker_num; i++ ) { 00091 if( markerInfo[i].id == id && markerFlag[i] == 0 ) { 00092 if( cid == -1 ) cid = i; 00093 else { 00094 if( markerInfo[cid].cf < markerInfo[i].cf ) cid = i; 00095 } 00096 } 00097 } 00098 if( cid == -1 ) { 00099 if( *pcount == 0 ) return -1; 00100 00101 rlenmin = 10.0; 00102 for( i = 0; i < marker_num; i++ ) { 00103 if( markerFlag[i] ) continue; 00104 rarea = (double)prevInfo->area / (double)markerInfo[i].area; 00105 if( rarea < 0.7 || rarea > 1.43 ) continue; 00106 rlen = ( (markerInfo[i].pos[0] - prevInfo->pos[0]) 00107 * (markerInfo[i].pos[0] - prevInfo->pos[0]) 00108 + (markerInfo[i].pos[1] - prevInfo->pos[1]) 00109 * (markerInfo[i].pos[1] - prevInfo->pos[1]) ) / markerInfo[i].area; 00110 if( rlen < 0.5 && rlen < rlenmin ) { 00111 rlenmin = rlen; 00112 cid = i; 00113 } 00114 } 00115 if( cid >= 0 && markerInfo[cid].cf < prevInfo->cf ) { 00116 markerInfo[cid].cf = prevInfo->cf; 00117 markerInfo[cid].id = prevInfo->id; 00118 diffmin = 10000.0 * 10000.0; 00119 cdir = -1; 00120 for( i = 0; i < 4; i++ ) { 00121 diff = 0.0; 00122 for( j = 0; j < 4; j++ ) { 00123 diff += (prevInfo->vertex[j][0] - markerInfo[cid].vertex[(i+j)%4][0]) 00124 * (prevInfo->vertex[j][0] - markerInfo[cid].vertex[(i+j)%4][0]) 00125 + (prevInfo->vertex[j][1] - markerInfo[cid].vertex[(i+j)%4][1]) 00126 * (prevInfo->vertex[j][1] - markerInfo[cid].vertex[(i+j)%4][1]); 00127 } 00128 if( diff < diffmin ) { 00129 diffmin = diff; 00130 cdir = (prevInfo->dir - i + 4) % 4; 00131 } 00132 } 00133 markerInfo[cid].dir = cdir; 00134 00135 *prevInfo = markerInfo[cid]; 00136 *pcount = 1; 00137 } 00138 else { 00139 *pcount = 0; 00140 return -1; 00141 } 00142 } 00143 else { 00144 *prevInfo = markerInfo[cid]; 00145 *pcount = 1; 00146 } 00147 00148 return cid; 00149 }