Skip to content

Commit

Permalink
Merge pull request #109 from cfhammill/develop
Browse files Browse the repository at this point in the history
miconvert_voxel_to_real: Ignore scaling for float volumes
  • Loading branch information
vfonov authored Jul 17, 2020
2 parents 5044544 + 7e59a1d commit ffb5fb2
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libsrc2/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ int miconvert_voxel_to_real(mihandle_t volume,
double voxel_range, voxel_offset;
double real_range, real_offset;

if( volume->volume_type==MI_TYPE_FLOAT || volume->volume_type==MI_TYPE_DOUBLE ||
volume->volume_type==MI_TYPE_FCOMPLEX || volume->volume_type==MI_TYPE_DCOMPLEX ){
// If floating values voxel_value is the real value
*real_value_ptr = voxel_value;
return 0;
}


/* get valid min/max, image min/max
*/
miget_volume_valid_range(volume, &valid_max, &valid_min);
Expand Down
4 changes: 4 additions & 0 deletions testdir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ ADD_EXECUTABLE(minc2-read-rgb minc2-read-rgb.c)
ADD_EXECUTABLE(minc2-read-metadata minc2-read-metadata.c)

ADD_EXECUTABLE(minc2-leak-test minc2-leak-test.c)
ADD_EXECUTABLE(minc2-float-voxel-test minc2-float-voxel-test.c)

add_minc_test(minc2-convert-test minc2-convert-test)
add_minc_test(minc2-create-test-images minc2-create-test-images
Expand Down Expand Up @@ -200,6 +201,9 @@ add_minc_test(minc2-volprops-test minc2-volprops-test)

add_minc_test(minc2-leak-test minc2-leak-test)

add_minc_test(minc2-float-voxel-test minc2-float-voxel-test
${CMAKE_CURRENT_BINARY_DIR}/test-dbl.mnc
)

set_property(TEST minc2-slice-test APPEND PROPERTY DEPENDS minc2-create-test-images)
set_property(TEST minc2-slice-test APPEND PROPERTY DEPENDS minc2-create-test-images-2)
Expand Down
145 changes: 145 additions & 0 deletions testdir/minc2-float-voxel-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "minc2.h"
#include "config.h"

#define NDIMS 3
#define CX 11
#define CY 12
#define CZ 9


#ifdef _MSC_VER
double rint(double v); /*hack: defined in m2util.c*/
#endif

#define TESTRPT(msg, val) (error_cnt++, printf(\
"Error reported on line #%d, %s: %d\n", \
__LINE__, msg, val))
#define XP 101
#define YP 17
#define ZP 1

#define VALID_MAX (((CX-1)*XP)+((CY-1)*YP)+((CZ-1)*ZP))
#define VALID_MIN (0.0)
#define REAL_MAX (1.0)
#define REAL_MIN (-1.0)
#define NORM_MAX (1.0)
#define NORM_MIN (-1.0)

static int
create_and_test_image(const char *name,
mihandle_t *hvol_ptr,
midimhandle_t hdims[],
double outval)
{
mihandle_t hvol;
int result;
misize_t start[NDIMS];
misize_t count[NDIMS];
double stmp2[CX][CY][CZ];
double dtemp;
int i,j,k;
char *dimnames[] = {"zspace", "xspace", "yspace"};
int error_cnt = 0;

result = micreate_dimension("xspace", MI_DIMCLASS_SPATIAL,
MI_DIMATTR_REGULARLY_SAMPLED, CX, &hdims[0]);
if (result < 0) {
TESTRPT("Unable to create test volume", result);
return error_cnt;
}

result = micreate_dimension("yspace", MI_DIMCLASS_SPATIAL,
MI_DIMATTR_REGULARLY_SAMPLED, CY, &hdims[1]);
if (result < 0) {
TESTRPT("Unable to create test volume", result);
return error_cnt;
}

result = micreate_dimension("zspace", MI_DIMCLASS_SPATIAL,
MI_DIMATTR_REGULARLY_SAMPLED, CZ, &hdims[2]);
if (result < 0) {
TESTRPT("Unable to create test volume", result);
return error_cnt;
}

result = micreate_volume(name, NDIMS, hdims, MI_TYPE_DOUBLE,
MI_CLASS_REAL, NULL, &hvol);
if (result < 0) {
TESTRPT("Unable to create test volume", result);
return error_cnt;
}

result = miget_volume_dimensions(hvol, MI_DIMCLASS_ANY, MI_DIMATTR_ALL,
MI_DIMORDER_FILE, NDIMS, hdims);
if (result < 0) {
TESTRPT("Unable to get volume dimensions", result);
return error_cnt;
}

micreate_volume_image(hvol);

*hvol_ptr = hvol;

for (i = 0; i < CX; i++) {
for (j = 0; j < CY; j++) {
for (k = 0; k < CZ; k++) {
stmp2[i][j][k] = (double)((i*XP)+(j*YP)+(k*ZP));
}
}
}

result = miset_volume_valid_range(hvol, VALID_MAX, VALID_MIN);
if (result < 0) {
TESTRPT("error setting valid range", result);
return error_cnt;
}

result = miset_volume_range(hvol, REAL_MAX, REAL_MIN);
if (result < 0) {
TESTRPT("error setting real range", result);
return error_cnt;
}

start[0] = start[1] = start[2] = 0;
count[0] = CX;
count[1] = CY;
count[2] = CZ;
result = miset_real_value_hyperslab(hvol, MI_TYPE_DOUBLE, start, count, stmp2);
if (result < 0) {
TESTRPT("unable to set hyperslab", result);
return error_cnt;
}


misize_t test_vox[] = { (misize_t) CX/2, (misize_t) CY/2, (misize_t) CZ/2 };

result = miget_real_value(hvol, test_vox, NDIMS, &outval);
if (result < 0) {
TESTRPT("unable to read value", result);
return error_cnt;
}

// printf("outval: %f \n", (double) outval);
// printf("smtp2: %f \n", stmp2[test_vox[0]][test_vox[1]][test_vox[2]]);
// printf("predicted: %f \n", (double) (test_vox[0] * XP + test_vox[1] * YP + test_vox[2] * ZP));
if (fabs(outval - stmp2[test_vox[0]][test_vox[1]][test_vox[2]]) > .000001){
printf("Voxel difference: %f \n", outval - stmp2[test_vox[0]][test_vox[1]][test_vox[2]]);
TESTRPT("scaling incorrect for miget_real_value", -1);
return error_cnt;
}

return error_cnt;
}

int
main(void){
mihandle_t hvol;
midimhandle_t hdims[NDIMS];
double outval;

double error_count = create_and_test_image("test-dbl.mnc", &hvol, hdims, outval);
return(error_count);
}

0 comments on commit ffb5fb2

Please sign in to comment.