/* get a voxel from all files, world coordinates */ void get_world_voxel_from_files(char **filenames, int *num_files, double *v1, double *v2, double *v3, double *voxel) { double location[3], voxel_coord_tmp[3]; misize_t voxel_coord[3]; mihandle_t hvol; int result; int i, j; location[0] = *v1; location[1] = *v2; location[2] = *v3; for(i=0; i < *num_files; i++) { /* open the volume */ result = miopen_volume(filenames[i], MI2_OPEN_READ, &hvol); if (result != MI_NOERROR) { error("Error opening input file: %s.\n", filenames[i]); } miconvert_world_to_voxel(hvol, location, voxel_coord_tmp); for (j=0; j < 3; j++) { voxel_coord[j] = (unsigned long) voxel_coord_tmp[j] + 0.5; } result = miget_real_value(hvol, voxel_coord, 3, &voxel[i]); if (result != MI_NOERROR) { error("Error getting voxel from: %s.\n", filenames[i]); } miclose_volume(hvol); } }
/* get a voxel from all files, voxel coordinates */ void get_voxel_from_files(char **filenames, int *num_files, int *v1, int *v2, int *v3, double *voxel) { misize_t location[3]; mihandle_t hvol; int result; int i; location[0] = *v1; location[1] = *v2; location[2] = *v3; for(i=0; i < *num_files; i++) { /* open the volume */ result = miopen_volume(filenames[i], MI2_OPEN_READ, &hvol); if (result != MI_NOERROR) { error("Error opening input file: %s.\n", filenames[i]); } result = miget_real_value(hvol, location, 3, &voxel[i]); if (result != MI_NOERROR) { error("Error getting voxel from: %s.\n", filenames[i]); } miclose_volume(hvol); } }
ITexture3DPtr MINCResource::GetTexture3D() { if (!loaded) return ITexture3DPtr(); if (tex) return tex; const unsigned int size = w*h*d*sizeof(float); float* data = new float[size];; tex = FloatTexture3DPtr(new FloatTexture3D(w,h,d,1,data)); string rawfile = file + string(".raw"); if (File::Exists(rawfile)) { logger.info << "MINC: Reading raw float data." << logger.end; ifstream* f = File::Open(rawfile); f->read((char*)data, size); f->close(); delete f; } else { for (unsigned int x = 0; x < w; ++x) { for (unsigned int y = 0; y < h; ++y) { for (unsigned int z = 0; z < d; ++z) { const unsigned long loc[3] = {x,y,z}; double val; miget_real_value(handle, loc, 3, &val); data[x + y*w + z*w*h] = val; } } } ofstream out(rawfile.c_str()); out.write((char*)data, size); out.close(); } return tex; }
SEXP get_vector_from_files(SEXP filenames, SEXP num_files, SEXP vec_length, SEXP v1, SEXP v2, SEXP v3) { misize_t location[4]; mihandle_t hvol; int result; int i, j, vector_length, number_files; SEXP output; double *xoutput; vector_length = *INTEGER(vec_length); number_files = *INTEGER(num_files); location[1] = *INTEGER(v1); location[2] = *INTEGER(v2); location[3] = *INTEGER(v3); Rprintf("NFILES: %d NVEC: %d\n", number_files, vector_length); PROTECT(output=allocMatrix(REALSXP, number_files, vector_length)); xoutput = REAL(output); for(i=0; i < number_files; i++) { /* open the volume */ result = miopen_volume(CHAR(STRING_ELT(filenames,i)), MI2_OPEN_READ, &hvol); if (result != MI_NOERROR) { error("Error opening input file: %s.\n", CHAR(STRING_ELT(filenames, i))); } for(j=0; j < vector_length; j++) { location[0] = j; result = miget_real_value(hvol, location, 4, &xoutput[i + number_files*j]); if (result != MI_NOERROR) { error("Error getting voxel from: %s.\n", CHAR(STRING_ELT(filenames, i))); } } miclose_volume(hvol); } UNPROTECT(1); return(output); }
ITexture2DPtr MINCResource::CreateSagitalSlice(unsigned int index) { if (!loaded) return ITexture2DPtr(); // clamp z-index if (index >= d) index = d-1; float* data = new float[w*h*sizeof(float)];; FloatTexture2DPtr slice = FloatTexture2DPtr(new FloatTexture2D(h,w,1,data)); unsigned int z = index; for (unsigned int x = 0; x < w; ++x) { for (unsigned int y = 0; y < h; ++y) { const unsigned long loc[3] = {x,y,z}; double val; miget_real_value(handle, loc, 3, &val); data[y+x*h] = val; } } return slice; }
ITexture2DPtr MINCResource::CreateCoronalSlice(unsigned int index) { if (!loaded) return ITexture2DPtr(); // clamp y-index if (index >= h) index = h-1; float* data = new float[w*d*sizeof(float)];; FloatTexture2DPtr slice = FloatTexture2DPtr(new FloatTexture2D(d,w,1,data)); unsigned int y = index; for (unsigned int x = 0; x < w; ++x) { for (unsigned int z = 0; z < d; ++z) { const unsigned long loc[3] = {x,y,z}; double val; miget_real_value(handle, loc, 3, &val); data[z+x*d] = val; } } return slice; }
ITexture2DPtr MINCResource::CreateTransverseSlice(unsigned int index) { if (!loaded) return ITexture2DPtr(); // clamp x-index if (index >= w) index = w-1; float* data = new float[h*d*sizeof(float)];; FloatTexture2DPtr slice = FloatTexture2DPtr(new FloatTexture2D(h,d,1,data)); unsigned int x = index; for (unsigned int y = 0; y < h; ++y) { for (unsigned int z = 0; z < d; ++z) { const unsigned long loc[3] = {x,y,z}; double val; miget_real_value(handle, loc, 3, &val); data[y+z*h] = val; } } return slice; }
int main(int argc, char **argv) { mihandle_t vol; midimhandle_t dim[NDIMS]; unsigned int sizes[NDIMS]; unsigned long start[NDIMS]; unsigned long count[NDIMS]; unsigned long howfar[NDIMS]; unsigned long location[NDIMS]; double *buffer,value; int r = 0; static char *dimorder[] = {"xspace", "yspace", "zspace"}; printf("Creating image with slice scaling!! \n"); create_test_file(); printf("Opening hyperslab-test2.mnc! \n"); r = miopen_volume("hyperslab-test2.mnc", MI2_OPEN_READ, &vol); if (r < 0) { TESTRPT("failed to open image", r); } #ifdef APPARENTORDER /* set the apparent dimension order to be xyz */ r = miset_apparent_dimension_order_by_name(vol, 3, dimorder); /* get the apparent dimensions and their sizes */ r = miget_volume_dimensions( vol, MI_DIMCLASS_SPATIAL, MI_DIMATTR_ALL, MI_DIMORDER_APPARENT, 3, dim); r = miget_dimension_sizes( dim, 3, sizes ); #else /* get the apparent dimensions and their sizes */ r = miget_volume_dimensions( vol, MI_DIMCLASS_SPATIAL, MI_DIMATTR_ALL, MI_DIMORDER_FILE, 3, dim); r = miget_dimension_sizes( dim, 3, sizes ); #endif if (r == MI_NOERROR) { printf("Sizes: %d, %d, %d\n", sizes[0], sizes[1], sizes[2]); } else { fprintf(stderr, "Error getting dimension sizes\n"); } /* try to play with hyperslab functions!! */ start[0] = 4; start[1] = 3; start[2] = 5; howfar[0] = 120; howfar[1] = 180; howfar[2] = 110; count[0] = howfar[0] - start[0]; count[1] = howfar[1] - start[1]; count[2] = howfar[2] - start[2]; /* Alocate memory for the hyperslab*/ buffer = (double *)malloc(count[0] * count[1] * count[2] * sizeof(double)); if (buffer == NULL) { fprintf(stderr, "Error allocation memory.\n"); exit(-1); } /* Get real value hyperslab*/ printf("\n"); printf("Getting a real value hyperslab \n"); printf("Starting at %d, %d, %d \n", start[0], start[1], start[2]); printf("Extending to %d, %d, %d \n", howfar[0], howfar[1], howfar[2]); printf("\n"); if (miget_real_value_hyperslab(vol,MI_TYPE_DOUBLE, start, count, buffer) < 0) { fprintf(stderr, "Could not get hyperslab.\n"); exit(-1); } /* set an arbitrary location to print values from */ location[0] = 70; location[1] = 100; location[2] = 104; printf("Test arbitrary location %d, %d, %d \n", location[0], location[1], location[2]); miget_real_value(vol, location, 3, &value); printf("Test from hyperslab: %f \n", *( buffer + (location[0] - start[0])*count[1]*count[2] + (location[1]- start[1]) * count[2] + (location[2]- start[2]))); printf("Test from voxel scaled: %f\n", value); miget_voxel_value(vol, location, 3, &value); printf("Test voxel value itself: %f\n", value); printf("\n"); printf("HMMMMMMMMMM! let's try something else \n"); printf("\n"); /* set another arbitrary location to print values from */ location[0] = 104; location[1] = 100; location[2] = 70; printf("Test arbitrary location %d, %d, %d \n", location[0], location[1], location[2]); miget_real_value(vol, location, 3, &value); printf("Test from hyperslab: %f \n", *( buffer + (location[0] - start[0])*count[1]*count[2] + (location[1]- start[1]) * count[2] + (location[2]- start[2]))); printf("Test from voxel scaled: %f\n", value); miget_voxel_value(vol, location, 3, &value); printf("Test voxel value itself: %f\n", value); /* close volume*/ miclose_volume(vol); if (error_cnt != 0) { fprintf(stderr, "%d error%s reported\n", error_cnt, (error_cnt == 1) ? "" : "s"); } else { fprintf(stderr, "\n No errors\n"); } return (error_cnt); }