/* Programa que faz o casamento do modelo com os dados obtidos. O arquivo de entrada dos vertices deve estar no formato x y z para cada vertice. */ #include #include #define NUM_CASA 3 /* num minimo de vertices casados (dados com modelo transladado) */ #define SIZEX 512 #define SIZEY 512 float baseline, foco, const_cord_x, const_cord_y; float deltamax; float **mv, /* matriz de vertices do modelo */ **mu_3d, /* matriz de vertices dos dados em 3d */ **mu_iso; /* matriz de vertices dos dados isotropicos */ float **mv_homog; /* mat. para coord. homogeneas dos vertices do modelo*/ float **mv_map_hom, /* vertices do modelo mapeados pela mat final em coord. homogeneas */ **mv_map_iso; /* vertices mv_map_hom em cord. cartezianas isotropicos */ FILE *saida, *file_log; int nvertice_dados, nvertice_mod; /* num. de vertices obtidos dos dados e do modelo */ /* multiplicacao de vetor linha por matriz em R4 */ void prod_row_mat(x, m, res) float *x, m[4][4], *res; /* x[4], res[4] */ { float s; int j, k; for(j=0; j<4 ;j++) { s = 0.0; for(k=0; k<4 ;k++) { s += x[k]*m[k][j]; } res[j] = s; } } /* determinante de uma matriz 3x3 */ float det3x3(m) float m[3][3]; { float res, d0, d1, d2; d0 = m[1][1]*m[2][2] - m[1][2]*m[2][1]; d1 = m[1][0]*m[2][2] - m[1][2]*m[2][0]; d2 = m[1][0]*m[2][1] - m[1][1]*m[2][0]; res = d0 * m[0][0] - d1 * m[0][1] + d2 * m[0][2]; return(res); } /* cofator do elemento [ix][jx] de uma matriz 4x4 */ float cof(matin, ix, jx) float matin[4][4]; int ix, jx; { float t[3][3]; float res, d; int ii, jj, i, j; ii = 0; for(i=0; i<4; i++) { if(i!=ix) { jj = 0; for(j=0; j<4; j++) { if(j!=jx) { t[ii][jj] = matin[i][j]; jj++; } } ii++; } } d = det3x3(t); if(((ix+jx)%2)!=0) res = -d; else res = d; return(res); } /* determinante de uma matriz 4x4 */ float det4x4(m) float m[4][4]; { float res; res = m[0][0] * cof( m, 0, 0 ) + m[0][1] * cof( m, 0, 1 ) + m[0][2] * cof( m, 0, 2 ) + m[0][3] * cof( m, 0, 3 ); return(res); } /* inversa de uma matriz 4x4 : m * result = I * det(m)**2 */ void inversa(matin, matout) float matin[4][4], matout[4][4]; { float d; int i, j; for(i=0; i<4; i++) for(j=0; j<4; j++) matout[i][j] = 0.0; d = det4x4(matin); if(d == 0.0) return; for(i=0; i<4; i++) for(j=0; j<4; j++) matout[i][j] = cof(matin, j, i)/d; return; } void mat_prod(a, b, o) /* produto de matrizes o = ab a(4 x 4), b(4 x 4) */ float a[4][4], b[4][4], o[4][4]; { int i, j, k; float s; for(k=0; k<4; k++) for(i=0; i<4; i++) { s = 0.0; for(j=0; j<4; j++) { s += a[k][j]*b[j][i]; } o[k][i] = s; } } void crossp(a, b, o) /* produto vetorial o = a x b de vetores R3 */ float *a, *b, *o; { float d; o[0] = a[1]*b[2] - a[2]*b[1]; o[1] = a[2]*b[0] - a[0]*b[2]; o[2] = a[0]*b[1] - a[1]*b[0]; d = sqrt(o[0]*o[0] + o[1]*o[1] + o[2]*o[2]); o[0] /= d; o[1] /= d; o[2] /= d; } float normalize(a) /* calcula o modulo do vetor em R3 */ float *a; { float d; d = sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]); a[0] /= d; a[1] /= d; a[2] /= d; return(d); } /* dist() - calcula a distancia entre dois ptos no espaco R3. Retorna a distancia calculada. */ float dist(p1, p2) float *p1, /* ponto 1 ; vetor[3] */ *p2; /* ponto 2 ; vetor[3] */ { int i; float dist = 0.0; /* distancia entre p1 e p2 */ for(i=0; i<3; i++) { dist += (p1[i] - p2[i]) * (p1[i] - p2[i]); } dist = sqrt(dist); return(dist); } /* para visualizar uma matriz */ void ver2(m, nlinhas, ncol) int nlinhas, ncol; float m[nlinhas][ncol]; { int i, j; printf(stderr,"\n"); for(i = 0; i=NUM_CASA) { valor = sqrt(discrepancia/(ncasados + 1)); fprintf(file_log," Ncasados = %d\n Valor = %f\n", ncasados, valor); fprintf(file_log," Discrepancia = %f\n",discrepancia); fprintf(file_log," Vertices usados com modelo = [%d] [%d] [%d]\n",i,j,k); fprintf(file_log," Vertices usados com dados = [%d] [%d] [%d]\n",l,m,n); /* fprintf(file_log," Matriz trans\n"); ver2(trans,4,4); fprintf(file_log," Matriz Scal\n"); ver2(scal,4,4); fprintf(file_log," Matriz rot\n"); ver2(rot,4,4); fprintf(file_log," Matriz final\n"); ver2(mat_final,4,4); fprintf(file_log," Matriz mu_3d dos dados\n"); ver1(mu_3d,7,3); fprintf(file_log," Matriz mv dos vertices\n"); ver1(mv,8,3); */ fprintf(file_log," Matriz mv_map_hom[8][4] vertices mapeados\n"); ver1(mv_map_hom,nvertice_mod,4); /* ESCRITA DOS PTOS MAPEADOS EM ARQUIVO PARA PLOTAGEM EM CANVAS */ fprintf(saida,"%9.4f %9.4f %9.4f %3d %9.4f %9.4f\n",mv_map_hom[0][1], mv_map_hom[0][2],mv_map_hom[0][3],ncasados,discrepancia, valor); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[1][1], mv_map_hom[1][2],mv_map_hom[1][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[0][1], mv_map_hom[0][2],mv_map_hom[0][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[3][1], mv_map_hom[3][2],mv_map_hom[3][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[0][1], mv_map_hom[0][2],mv_map_hom[0][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[6][1], mv_map_hom[6][2],mv_map_hom[6][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[1][1], mv_map_hom[1][2],mv_map_hom[1][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[2][1], mv_map_hom[2][2],mv_map_hom[2][3]); /* */ fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[1][1], mv_map_hom[1][2],mv_map_hom[1][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[7][1], mv_map_hom[7][2],mv_map_hom[7][3]); /* */ fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[2][1], mv_map_hom[2][2],mv_map_hom[2][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[3][1], mv_map_hom[3][2],mv_map_hom[3][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[2][1], mv_map_hom[2][2],mv_map_hom[2][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[5][1], mv_map_hom[5][2],mv_map_hom[5][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[3][1], mv_map_hom[3][2],mv_map_hom[3][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[4][1], mv_map_hom[4][2],mv_map_hom[4][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[4][1], mv_map_hom[4][2],mv_map_hom[4][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[5][1], mv_map_hom[5][2],mv_map_hom[5][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[4][1], mv_map_hom[4][2],mv_map_hom[4][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[6][1], mv_map_hom[6][2],mv_map_hom[6][3]); /* */ fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[5][1], mv_map_hom[5][2],mv_map_hom[5][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[7][1], mv_map_hom[7][2],mv_map_hom[7][3]); /* */ /* */ fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[6][1], mv_map_hom[6][2],mv_map_hom[6][3]); fprintf(saida,"%9.4f %9.4f %9.4f\n",mv_map_hom[7][1], mv_map_hom[7][2],mv_map_hom[7][3]); /* */ fprintf(saida,"%f %f %f\n",-1.0,-1.0,-1.0); } } } } void main(argc, argv) int argc; char *argv[]; { int i; char aux[255], carac; char parametros[50], saida_aux[50],saida_log[50], saida_mod[50], entrada_vert[50], modelo[50]; double start, stop; /* para medida de tempo de execucao */ FILE *arq_parametros, *file_modelo, *entrada; strcpy(parametros, argv[2]); strcat(parametros, ".parms"); strcpy(saida_aux, argv[1]); strcat(saida_aux, "-"); strcat(saida_aux, argv[2]); strcpy(saida_log, saida_aux); strcat(saida_log, "_mod.log"); strcpy(saida_mod, saida_aux); strcat(saida_mod, "_3d.mod"); strcpy(entrada_vert, saida_aux); strcat(entrada_vert, "_iso.vert"); entrada = fopen(entrada_vert, "r"); saida = fopen(saida_mod, "w"); file_log = fopen(saida_log, "w"); arq_parametros = fopen(parametros, "r"); fgets(aux, 254, arq_parametros); /* leitura do cabecalho */ fgets(aux, 254, arq_parametros); /* leitura de duas linhas do arquivo de */ fgets(aux, 254, arq_parametros); /* entrada que e usada apenas em tese-seg */ fscanf(arq_parametros,"foco = %f\n",&foco); fscanf(arq_parametros,"baseline = %f\n",&baseline); const_cord_x = (SIZEX-1.0)/2.0; const_cord_y = (SIZEY-1.0)/2.0; fprintf(stderr,"\n\n Casamento do modelo com dados obtidos.\n"); fprintf(stderr,"\n\n Dados.\n"); fprintf(stderr,"\n Arquivo de vertices de entrada = %s",entrada_vert); fprintf(stderr,"\n Arquivo saida(v-mod-mapeados) = %s",saida_mod); fprintf(file_log,"\n\n Casamento do modelo com dados obtidos.\n"); fprintf(file_log,"\n\n Dados.\n"); fprintf(file_log,"\n Arquivo de vertices de entrada = %s",entrada_vert); fprintf(file_log,"\n Arquivo saida(v-mod-mapeados) = %s\n",saida_mod); fprintf(stderr,"\n Delta max = "); scanf("%f",&deltamax); nvertice_dados = 0; while((fgets(aux, 254, entrada)) != NULL) nvertice_dados++; fprintf(stderr,"nvertice_dados = %d\n",nvertice_dados); fprintf(stderr,"\n Modelo abstrato = "); scanf("%s",modelo); file_modelo = fopen(modelo, "r"); nvertice_mod = 0; carac = getc(file_modelo); while(carac == '#') { fgets(aux, 254,file_modelo); carac = getc(file_modelo); } ungetc(carac, file_modelo); while(fgets(aux, 254, file_modelo) != NULL) nvertice_mod++; fprintf(stderr,"nvertice_mod = %d\n",nvertice_mod); mv = (float **)malloc(nvertice_mod * sizeof(float *)); for(i=0; i