/* ******** nrrdSameSize() ** ** returns 1 iff given two nrrds have same dimension and axes sizes. ** This does NOT look at the type of the elements. ** ** The intended user of this is someone who really wants the nrrds to be ** the same size, so that if they aren't, some descriptive (error) message ** can be generated according to useBiff */ int /*Teem: biff?2 if (!ret) */ nrrdSameSize(const Nrrd *n1, const Nrrd *n2, int useBiff) { static const char me[]="nrrdSameSize"; unsigned int ai; char stmp[2][AIR_STRLEN_SMALL]; if (!(n1 && n2)) { biffMaybeAddf(useBiff, NRRD, "%s: got NULL pointer", me); return 0; } if (n1->dim != n2->dim) { biffMaybeAddf(useBiff, NRRD, "%s: n1->dim (%u) != n2->dim (%u)", me, n1->dim, n2->dim); return 0; } for (ai=0; ai<n1->dim; ai++) { if (n1->axis[ai].size != n2->axis[ai].size) { biffMaybeAddf(useBiff, NRRD, "%s: n1->axis[%d].size (%s) " "!= n2->axis[%d].size (%s)", me, ai, airSprintSize_t(stmp[0], n1->axis[ai].size), ai, airSprintSize_t(stmp[1], n2->axis[ai].size)); return 0; } } return 1; }
static int _nrrdFieldCheck_kinds(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_kinds"; int val[NRRD_DIM_MAX]; unsigned int wantLen, ai; nrrdAxisInfoGet_nva(nrrd, nrrdAxisInfoKind, val); for (ai=0; ai<nrrd->dim; ai++) { if (!( nrrdKindUnknown == val[ai] || !airEnumValCheck(nrrdKind, val[ai]) )) { biffMaybeAddf(useBiff, NRRD, "%s: axis %d kind %d invalid", me, ai, val[ai]); return 1; } wantLen = nrrdKindSize(val[ai]); if (wantLen && wantLen != nrrd->axis[ai].size) { char stmp[AIR_STRLEN_SMALL]; biffMaybeAddf(useBiff, NRRD, "%s: axis %d kind %s requires size %u, but have %s", me, ai, airEnumStr(nrrdKind, val[ai]), wantLen, airSprintSize_t(stmp, nrrd->axis[ai].size)); return 1; } } return 0; }
int _nrrdCheck(const Nrrd *nrrd, int checkData, int useBiff) { static const char me[]="_nrrdCheck"; int fi; if (!nrrd) { biffMaybeAddf(useBiff, NRRD, "%s: got NULL pointer", me); return 1; } if (checkData) { if (!(nrrd->data)) { biffMaybeAddf(useBiff, NRRD, "%s: nrrd %p has NULL data pointer", me, AIR_CVOIDP(nrrd)); return 1; } } for (fi=nrrdField_unknown+1; fi<nrrdField_last; fi++) { /* yes, this will call _nrrdFieldCheckSpaceInfo() many many times */ if (_nrrdFieldCheck[fi](nrrd, AIR_TRUE)) { biffMaybeAddf(useBiff, NRRD, "%s: trouble with %s field", me, airEnumStr(nrrdField, fi)); return 1; } } return 0; }
static int _nrrdFieldCheck_space_directions(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_space_directions"; if (_nrrdFieldCheckSpaceInfo(nrrd, useBiff)) { biffMaybeAddf(useBiff, NRRD, "%s: space info problem", me); return 1; } return 0; }
static int _nrrdFieldCheck_measurement_frame(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_measurement_frame"; if (_nrrdFieldCheckSpaceInfo(nrrd, useBiff)) { biffMaybeAddf(useBiff, NRRD, "%s: space info problem", me); return 1; } return 0; }
static int _nrrdFieldCheck_space_dimension(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_space_dimension"; if (_nrrdFieldCheckSpaceInfo(nrrd, useBiff)) { biffMaybeAddf(useBiff, NRRD, "%s: trouble", me); return 1; } return 0; }
static int _nrrdFieldCheck_spacings(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_spacings"; double val[NRRD_DIM_MAX]; unsigned int ai; nrrdAxisInfoGet_nva(nrrd, nrrdAxisInfoSpacing, val); for (ai=0; ai<nrrd->dim; ai++) { if (!( !airIsInf_d(val[ai]) && (airIsNaN(val[ai]) || (0 != val[ai])) )) { biffMaybeAddf(useBiff, NRRD, "%s: axis %d spacing (%g) invalid", me, ai, val[ai]); return 1; } } if (_nrrdFieldCheckSpaceInfo(nrrd, useBiff)) { biffMaybeAddf(useBiff, NRRD, "%s: trouble", me); return 1; } return 0; }
static int _nrrdFieldCheck_block_size(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_block_size"; char stmp[AIR_STRLEN_SMALL]; if (nrrdTypeBlock == nrrd->type && (!(0 < nrrd->blockSize)) ) { biffMaybeAddf(useBiff, NRRD, "%s: type is %s but nrrd->blockSize (%s) invalid", me, airEnumStr(nrrdType, nrrdTypeBlock), airSprintSize_t(stmp, nrrd->blockSize)); return 1; } if (nrrdTypeBlock != nrrd->type && (0 < nrrd->blockSize)) { biffMaybeAddf(useBiff, NRRD, "%s: type is %s (not block) but blockSize is %s", me, airEnumStr(nrrdType, nrrd->type), airSprintSize_t(stmp, nrrd->blockSize)); return 1; } return 0; }
static int _nrrdFieldCheck_type(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_type"; if (airEnumValCheck(nrrdType, nrrd->type)) { biffMaybeAddf(useBiff, NRRD, "%s: type (%d) is not valid", me, nrrd->type); return 1; } return 0; }
static int _nrrdFieldCheck_sizes(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_sizes"; size_t size[NRRD_DIM_MAX]; nrrdAxisInfoGet_nva(nrrd, nrrdAxisInfoSize, size); if (_nrrdSizeCheck(size, nrrd->dim, useBiff)) { biffMaybeAddf(useBiff, NRRD, "%s: trouble with array sizes", me); return 1; } return 0; }
static int _nrrdFieldCheck_space_units(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_space_units"; /* not sure if there's anything to specifically check for the space units themselves ... */ if (_nrrdFieldCheckSpaceInfo(nrrd, useBiff)) { biffMaybeAddf(useBiff, NRRD, "%s: space info problem", me); return 1; } return 0; }
static int _nrrdFieldCheck_dimension(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_dimension"; if (!AIR_IN_CL(1, nrrd->dim, NRRD_DIM_MAX)) { biffMaybeAddf(useBiff, NRRD, "%s: dimension %u is outside valid range [1,%d]", me, nrrd->dim, NRRD_DIM_MAX); return 1; } return 0; }
static int _nrrdFieldCheck_units(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_units"; /* as with labels- the strings themselves don't need checking themselves */ /* but per-axis units cannot be set for axes with space directions ... */ if (_nrrdFieldCheckSpaceInfo(nrrd, useBiff)) { biffMaybeAddf(useBiff, NRRD, "%s: space info problem", me); return 1; } return 0; }
static int _nrrdFieldCheck_axis_maxs(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_axis_maxs"; double val[NRRD_DIM_MAX]; unsigned int ai; int ret; nrrdAxisInfoGet_nva(nrrd, nrrdAxisInfoMax, val); for (ai=0; ai<nrrd->dim; ai++) { if ((ret=airIsInf_d(val[ai]))) { biffMaybeAddf(useBiff, NRRD, "%s: axis %d max %sinf invalid", me, ai, 1==ret ? "+" : "-"); return 1; } } if (_nrrdFieldCheckSpaceInfo(nrrd, useBiff)) { biffMaybeAddf(useBiff, NRRD, "%s: trouble", me); return 1; } /* HEY: contemplate checking min != max, but what about stub axes ... */ return 0; }
static int _nrrdFieldCheck_old_max(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_old_max"; int ret; if ((ret=airIsInf_d(nrrd->oldMax))) { biffMaybeAddf(useBiff, NRRD, "%s: old max %sinf invalid", me, 1==ret ? "+" : "-"); return 1; } /* oldMin == oldMax is perfectly valid */ return 0; }
static int _nrrdFieldCheck_space_origin(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_space_origin"; /* pre-Fri Feb 11 04:25:36 EST 2005, I thought that the spaceOrigin must be known to describe the space/orientation stuff, but that's too restrictive, which is why below says AIR_FALSE instead of AIR_TRUE */ if (_nrrdFieldCheckSpaceInfo(nrrd, useBiff)) { biffMaybeAddf(useBiff, NRRD, "%s: space info problem", me); return 1; } return 0; }
int _nrrdFormatVTK_fitsInto(const Nrrd *nrrd, const NrrdEncoding *encoding, int useBiff) { static const char me[]="_nrrdFormatVTK_fitsInto"; if (!( nrrd && encoding )) { biffMaybeAddf(useBiff, NRRD, "%s: got NULL nrrd (%p) or encoding (%p)", me, AIR_CVOIDP(nrrd), AIR_CVOIDP(encoding)); return AIR_FALSE; } if (!( nrrdEncodingRaw == encoding || nrrdEncodingAscii == encoding)) { biffMaybeAddf(useBiff, NRRD, "%s: encoding can only be %s or %s", me, nrrdEncodingRaw->name, nrrdEncodingAscii->name); return AIR_FALSE; } if (!( nrrdTypeUChar == nrrd->type || nrrdTypeChar == nrrd->type || nrrdTypeUShort == nrrd->type || nrrdTypeShort == nrrd->type || nrrdTypeUInt == nrrd->type || nrrdTypeInt == nrrd->type || nrrdTypeFloat == nrrd->type || nrrdTypeDouble == nrrd->type )) { biffMaybeAddf(useBiff, NRRD, "%s: type %s doesn't fit in VTK (as currently implemented)", me, airEnumStr(nrrdType, nrrd->type)); return AIR_FALSE; } if (!( 3 == nrrd->dim || (4 == nrrd->dim && 3 == nrrd->axis[0].size) || (4 == nrrd->dim && 9 == nrrd->axis[0].size) )) { biffMaybeAddf(useBiff, NRRD, "%s: nrrd didn't look like a volume of " "scalars, vectors, or matrices", me); return AIR_FALSE; } return AIR_TRUE; }
static int _nrrdFieldCheck_centers(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_centers"; unsigned int ai; int val[NRRD_DIM_MAX]; nrrdAxisInfoGet_nva(nrrd, nrrdAxisInfoCenter, val); for (ai=0; ai<nrrd->dim; ai++) { if (!( nrrdCenterUnknown == val[ai] || !airEnumValCheck(nrrdCenter, val[ai]) )) { biffMaybeAddf(useBiff, NRRD, "%s: axis %d center %d invalid", me, ai, val[ai]); return 1; } } return 0; }
static int _nrrdFieldCheck_thicknesses(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheck_thicknesses"; double val[NRRD_DIM_MAX]; unsigned int ai; nrrdAxisInfoGet_nva(nrrd, nrrdAxisInfoThickness, val); for (ai=0; ai<nrrd->dim; ai++) { /* note that unlike spacing, we allow zero thickness, but it makes no sense to be negative */ if (!( !airIsInf_d(val[ai]) && (airIsNaN(val[ai]) || (0 <= val[ai])) )) { biffMaybeAddf(useBiff, NRRD, "%s: axis %d thickness (%g) invalid", me, ai, val[ai]); return 1; } } return 0; }
/* ** asserts all the properties associated with orientation information ** ** The most important part of this is asserting the per-axis mutual ** exclusion of min/max/spacing/units versus using spaceDirection. */ static int _nrrdFieldCheckSpaceInfo(const Nrrd *nrrd, int useBiff) { static const char me[]="_nrrdFieldCheckSpaceInfo"; unsigned int dd, ii; int exists; if (!( !nrrd->space || !airEnumValCheck(nrrdSpace, nrrd->space) )) { biffMaybeAddf(useBiff, NRRD, "%s: space %d invalid", me, nrrd->space); return 1; } if (!( nrrd->spaceDim <= NRRD_SPACE_DIM_MAX )) { biffMaybeAddf(useBiff, NRRD, "%s: space dimension %d is outside " "valid range [0,NRRD_SPACE_DIM_MAX] = [0,%d]", me, nrrd->dim, NRRD_SPACE_DIM_MAX); return 1; } if (nrrd->spaceDim) { if (nrrd->space) { if (nrrdSpaceDimension(nrrd->space) != nrrd->spaceDim) { biffMaybeAddf(useBiff, NRRD, "%s: space %s has dimension %d but spaceDim is %d", me, airEnumStr(nrrdSpace, nrrd->space), nrrdSpaceDimension(nrrd->space), nrrd->spaceDim); return 1; } } /* check that all coeffs of spaceOrigin have consistent existance */ exists = AIR_EXISTS(nrrd->spaceOrigin[0]); for (ii=0; ii<nrrd->spaceDim; ii++) { if (exists ^ AIR_EXISTS(nrrd->spaceOrigin[ii])) { biffMaybeAddf(useBiff, NRRD, "%s: existance of space origin coefficients must " "be consistent (val[0] not like val[%d])", me, ii); return 1; } } /* check that all coeffs of measurementFrame have consistent existance */ exists = AIR_EXISTS(nrrd->measurementFrame[0][0]); for (dd=0; dd<nrrd->spaceDim; dd++) { for (ii=0; ii<nrrd->spaceDim; ii++) { if (exists ^ AIR_EXISTS(nrrd->measurementFrame[dd][ii])) { biffMaybeAddf(useBiff, NRRD, "%s: existance of measurement frame coefficients " "must be consistent: [col][row] [%d][%d] not " "like [0][0])", me, dd, ii); return 1; } } } /* check on space directions */ for (dd=0; dd<nrrd->dim; dd++) { exists = AIR_EXISTS(nrrd->axis[dd].spaceDirection[0]); for (ii=1; ii<nrrd->spaceDim; ii++) { if (exists ^ AIR_EXISTS(nrrd->axis[dd].spaceDirection[ii])) { biffMaybeAddf(useBiff, NRRD, "%s: existance of space direction %d coefficients " "must be consistent (val[0] not like val[%d])", me, dd, ii); return 1; } } if (exists) { if (AIR_EXISTS(nrrd->axis[dd].min) || AIR_EXISTS(nrrd->axis[dd].max) || AIR_EXISTS(nrrd->axis[dd].spacing) || !!airStrlen(nrrd->axis[dd].units)) { biffMaybeAddf(useBiff, NRRD, "%s: axis[%d] has a direction vector, and so can't " "have min, max, spacing, or units set", me, dd); return 1; } } } } else { /* else there's not supposed to be anything in "space" */ if (nrrd->space) { biffMaybeAddf(useBiff, NRRD, "%s: space %s can't be set with spaceDim %d", me, airEnumStr(nrrdSpace, nrrd->space), nrrd->spaceDim); return 1; } /* -------- */ exists = AIR_FALSE; for (dd=0; dd<NRRD_SPACE_DIM_MAX; dd++) { exists |= !!airStrlen(nrrd->spaceUnits[dd]); } if (exists) { biffMaybeAddf(useBiff, NRRD, "%s: spaceDim is 0, but space units is set", me); return 1; } /* -------- */ exists = AIR_FALSE; for (dd=0; dd<NRRD_SPACE_DIM_MAX; dd++) { exists |= AIR_EXISTS(nrrd->spaceOrigin[dd]); } if (exists) { biffMaybeAddf(useBiff, NRRD, "%s: spaceDim is 0, but space origin is set", me); return 1; } /* -------- */ exists = AIR_FALSE; for (dd=0; dd<NRRD_SPACE_DIM_MAX; dd++) { for (ii=0; ii<NRRD_DIM_MAX; ii++) { exists |= AIR_EXISTS(nrrd->axis[ii].spaceDirection[dd]); } } if (exists) { biffMaybeAddf(useBiff, NRRD, "%s: spaceDim is 0, but space directions are set", me); return 1; } } return 0; }