int vtk_write_scalar_grid(char *filename, char *dataset_name, long int ni, long int nj, long int nk, double oi, double oj, double ok, double di, double dj, double dk, double *scalars) { FILE *fp; long int i, j, k; double d; if(filename == NULL || scalars == NULL) { printf("error: passed null arguments to vtk_write_scalar_grid\n"); return 1; } vtk_remove(filename); fp = fopen(filename, "w"); if(fp == NULL) { printf("error: vtk_write_scalar_grid cannot open %s to write\n", filename); return 1; } fprintf(fp, "# vtk DataFile Version 2.0\n"); fprintf(fp, "%s\n", dataset_name); #ifdef VTK_BINARY fprintf(fp, "BINARY\n"); #else fprintf(fp, "ASCII\n"); #endif fprintf(fp, "DATASET STRUCTURED_POINTS\n"); fprintf(fp, "DIMENSIONS %ld %ld %ld\n", ni, nj, nk); fprintf(fp, "ORIGIN %lf %lf %lf\n", oi, oj, ok); fprintf(fp, "SPACING %lf %lf %lf\n", di, dj, dk); fprintf(fp, "POINT_DATA %ld\n", ni*nj*nk); fprintf(fp, "SCALARS %s_data double 1\n", dataset_name); fprintf(fp, "LOOKUP_TABLE default\n"); for(k=0; k<nk; k++) { for(j=0; j<nj; j++) { for(i=0; i<ni; i++) { #ifdef VTK_BINARY d = double_swap(scalars[CELL_INDEX(i,j,k)]); fwrite(&d, sizeof(double), 1, fp) #else fprintf(fp, "%4.6lf\n", scalars[CELL_INDEX(i,j,k)]); #endif } } } fclose(fp); return 0; }
/* sorts an array into ascending order using insertion sort */ void sort_double_array(double A[], int n) { int i, j; for(i=0; i<n; i++) { for(j=i-1; j>=0; j--) { if (A[j+1]<A[j]) { double_swap(&A[j], &A[j+1]); } } } }
int vtk_write_vector_grid(char *filename, char *dataset_name, long int ni, long int nj, long int nk, double oi, double oj, double ok, double di, double dj, double dk, double *v1, double *v2, double *v3) { FILE *fp; long int i, j, k; const double emf = 0.000001, d; if(filename == NULL || v1 == NULL || v2 == NULL || v3 == NULL) { printf("error: passed null arguments to vtk_write_vector_grid\n"); return 1; } vtk_remove(filename); fp = fopen(filename, "w"); if(fp == NULL) { printf("error: vtk_write_vector_grid cannot open %s to write\n", filename); return 1; } fprintf(fp, "# vtk DataFile Version 2.0\n"); fprintf(fp, "%s\n", dataset_name); #ifdef VTK_BINARY fprintf(fp, "BINARY\n"); #else fprintf(fp, "ASCII\n"); #endif fprintf(fp, "DATASET STRUCTURED_POINTS\n"); fprintf(fp, "DIMENSIONS %ld %ld %ld\n", ni-1, nj-1, nk-1); fprintf(fp, "ORIGIN %lf %lf %lf\n", oi, oj, ok); fprintf(fp, "SPACING %lf %lf %lf\n", di, dj, dk); fprintf(fp, "POINT_DATA %ld\n", (ni-1)*(nj-1)*(nk-1)); fprintf(fp, "VECTORS %s_data double\n", dataset_name); for(k=0; k<nk-1; k++) { for(j=0; j<nj-1; j++) { for(i=0; i<ni-1; i++) { #ifdef VTK_BINARY d=double_swap(v1[CELL_INDEX(i,j,k)]); fwrite(&d, sizeof(double), 1, fp) d=double_swap(v2[CELL_INDEX(i,j,k)]); fwrite(&d, sizeof(double), 1, fp) d=double_swap(v3[CELL_INDEX(i,j,k)]); fwrite(&d, sizeof(double), 1, fp) #else if( (fabs(v1[CELL_INDEX(i,j,k)]) > emf && fabs(v1[CELL_INDEX(i+1,j,k)]) > emf) || (fabs(v2[CELL_INDEX(i,j,k)]) > emf && fabs(v2[CELL_INDEX(i,j+1,k)]) > emf) || (fabs(v3[CELL_INDEX(i,j,k)]) > emf && fabs(v3[CELL_INDEX(i,j,k+1)]) > emf) ) fprintf(fp, "%4.6lf %4.6lf %4.6lf\n", (v1[CELL_INDEX(i,j,k)] + v1[CELL_INDEX(i+1,j,k)])/2, (v2[CELL_INDEX(i,j,k)] + v2[CELL_INDEX(i,j+1,k)])/2, (v3[CELL_INDEX(i,j,k)] + v3[CELL_INDEX(i,j,k+1)])/2); else fprintf(fp, "0.0 0.0 0.0\n"); #endif } } } fclose(fp); return 0; }
Bool triangle_triangle_intersection( float A1[3],float A2[3], float A3[3], float B1[3],float B2[3], float B3[3], float E1[3],float E2[3] ) { float M[3][3], NA[3], NB[3], B_1[3], B_2[3], B_3[3], X1[3], X2[3], A_1[3], A_2[3], A_3[3], Y1[3], Y2[3]; double det1,det2, det3, t1, t2; triangle_normal_vector(A1, A2, A3, NA); if(scalar_product(NA,NA)==0) { return False; } triangle_normal_vector(B1, B2, B3, NB); if(scalar_product(NB,NB)==0) { return False; } vector_sub(A2,A1, M[0]); vector_sub(A3,A1, M[1]); vector_sub(B1,A1, M[2]); det1=matrix3_det(M); vector_sub(B2,A1, M[2]); det2=matrix3_det(M); vector_sub(B3,A1, M[2]); det3=matrix3_det(M); if( (det1<=0 && det2<=0 && det3<=0) || (det1>=0 && det2>=0 && det3>=0) ) { /* printf("all vertices of B are on the same side of plane A1,A2,A3\n"); */ return False; } if( (det1<0 && det2>=0 && det3>=0) || (det1>0 && det2<=0 && det3<=0) ) { vectorcpy(B_1,B1); vectorcpy(B_2,B2); vectorcpy(B_3,B3); } else if( (det2<0 && det1>=0 && det3>=0) || (det2>0 && det1<=0 && det3<=0) ) { vectorcpy(B_1,B2); vectorcpy(B_2,B1); vectorcpy(B_3,B3); } else if( (det3<0 && det2>=0 && det1>=0) || (det3>0 && det2<=0 && det1<=0) ) { vectorcpy(B_1,B3); vectorcpy(B_2,B2); vectorcpy(B_3,B1); } /* B_1 is on the other side of the plane A1, A2, A3 than B_2, B_3. */ line_triangle_intersection(B_1,B_2, A1,A2,A3, X1); line_triangle_intersection(B_1,B_3, A1,A2,A3, X2); vector_sub(B2,B1, M[0]); vector_sub(B3,B1, M[1]); vector_sub(A1,B1, M[2]); det1=matrix3_det(M); vector_sub(A2,B1, M[2]); det2=matrix3_det(M); vector_sub(A3,B1, M[2]); det3=matrix3_det(M); if( (det1<=0 && det2<=0 && det3<=0) || (det1>=0 && det2>=0 && det3>=0) ) { /* printf("all vertices of A are on the same side of plane B1,B2,B3\n"); */ return False; } if( (det1<0 && det2>=0 && det3>=0) || (det1>0 && det2<=0 && det3<=0) ) { vectorcpy(A_1,A1); vectorcpy(A_2,A2); vectorcpy(A_3,A3); } else if( (det2<0 && det1>=0 && det3>=0) || (det2>0 && det1<=0 && det3<=0) ) { vectorcpy(A_1,A2); vectorcpy(A_2,A1); vectorcpy(A_3,A3); } else if( (det3<0 && det2>=0 && det1>=0) || (det3>0 && det2<=0 && det1<=0) ) { vectorcpy(A_1,A3); vectorcpy(A_2,A2); vectorcpy(A_3,A1); } /* A_1 is on the other side of the plane B1, B2, B3 than A_2, A_3. */ vector_add(A_1, NA, NA); line_triangle_solve(X1,X2, A_1,A_2,NA, Y1, &t1); line_triangle_solve(X1,X2, A_1,A_3,NA, Y2, &t2); if(t2<t1) { double_swap(&t1, &t2); vector_swap(Y1, Y2); } if( t2<=0 || t1>=1 ) { /* printf("t2==%lf<=0 || t1==%lf>=1\n", t2, t1); */ return False; } if(t1>0) vectorcpy(E1, Y1); else vectorcpy(E1, X1); if(t2<1) vectorcpy(E2, Y2); else vectorcpy(E2, X2); return True; /* / DOKONCZ ... */ }