int main(int argc, char *argv[]) { char *me, *kernS[2], *minS, *stepS, *maxS, *outS, *err, kstr[AIR_STRLEN_LARGE]; const NrrdKernel *kern[2]; NrrdKernelSpec *ksp[2]; double parm[NRRD_KERNEL_PARMS_NUM], min, step, max, integral, *dom_d, *ran_d; float *dom_f, *ran_f, val, r_f, r_d; FILE *fout; int i, len; airArray *mop; unsigned int kii; me = argv[0]; if (!( 6 == argc || 7 == argc )) { usage(me); } kernS[0] = argv[1]; minS = argv[2]; stepS = argv[3]; maxS = argv[4]; outS = argv[5]; if (7 == argc) { kernS[1] = argv[6]; } else { kernS[1] = NULL; } if (3 != (sscanf(minS, "%lf", &min) + sscanf(stepS, "%lf", &step) + sscanf(maxS, "%lf", &max))) { fprintf(stderr, "%s: couldn't parse \"%s\", \"%s\", \"%s\" as 3 doubles\n", me, minS, stepS, maxS); exit(1); } mop = airMopNew(); for (kii=0; kii<=(kernS[1] ? 1 : 0); kii++) { if (nrrdKernelParse(&(kern[kii]), parm, kernS[kii])) { airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: (kii %u) trouble:\n%s\n", me, kii, err); airMopError(mop); exit(1); } ksp[kii] = nrrdKernelSpecNew(); airMopAdd(mop, ksp[kii], (airMopper)nrrdKernelSpecNix, airMopAlways); nrrdKernelSpecSet(ksp[kii], kern[kii], parm); if (nrrdKernelSpecSprint(kstr, ksp[kii])) { airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: trouble:\n%s\n", me, err); airMopError(mop); exit(1); } fprintf(stderr, "%s: printed kernel as \"%s\"\n", me, kstr); if (!( min <= -kern[kii]->support(parm) && max >= kern[kii]->support(parm) )) { fprintf(stderr, "%s: WARNING: support=%g => lower min (%g) or raise max (%g)\n", me, kern[kii]->support(parm), min, max); } fprintf(stderr, "%s: support(%s) = %g\n", me, kstr, kern[kii]->support(parm)); } /* see how many values are in the interval */ len = 0; for (val=min; val<=max; val+=step) { len++; } /* allocate domain and range for both float and double */ if (!( (dom_d = (double *)calloc(len, sizeof(double))) && (ran_d = (double *)calloc(len, sizeof(double))) && (dom_f = (float *)calloc(len, sizeof(float))) && (ran_f = (float *)calloc(len, sizeof(float))) )) { fprintf(stderr, "%s: PANIC: couldn't allocate buffers\n", me); exit(1); } airMopAdd(mop, dom_d, airFree, airMopAlways); airMopAdd(mop, ran_d, airFree, airMopAlways); airMopAdd(mop, dom_f, airFree, airMopAlways); airMopAdd(mop, ran_f, airFree, airMopAlways); /* set values in both domains */ i=0; for (val=min; val<=max; val+=step) { /* note that the value stored in dom_d[i] is only a single-precision float, so that it is really equal to dom_f[i] */ dom_d[i] = val; dom_f[i] = val; i++; } /* do the vector evaluations */ kern[0]->evalN_f(ran_f, dom_f, len, parm); kern[0]->evalN_d(ran_d, dom_d, len, parm); /* do the single evaluations, and make sure everything agrees */ i = 0; integral = 0; for (val=min; val<=max; val+=step) { /* compare two single evaluations */ r_f = kern[0]->eval1_f(val, parm); r_d = kern[0]->eval1_d(val, parm); if (!CLOSE(r_f,r_d, 0.00001)) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (eval1_d(%g)== %f)\n", me, val, r_f, val, r_d); } /* compare single float with vector float */ if (!CLOSE(r_f,ran_f[i], 0.00001)) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (evalN_f[%d]== %f)\n", me, val, r_f, i, ran_f[i]); } /* compare single float with vector double */ r_d = ran_d[i]; if (!CLOSE(r_f,r_d, 0.00001)) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (evalN_d[%d]== %f)\n", me, val, r_f, i, r_d); } integral += step*ran_d[i]; /* possibly check on given derivatives */ if (kern[1]) { double numd; numd = (kern[0]->eval1_d(val+step/2, parm) - kern[0]->eval1_d(val-step/2, parm))/step; if (!CLOSE(numd, kern[1]->eval1_d(val+step, parm), 0.005)) { fprintf(stderr, "%s: |numerical f'(%g) %g - true %g| = %g > 0.005\n", me, val, numd, kern[1]->eval1_d(val+step, parm), fabs(numd - kern[1]->eval1_d(val+step, parm))); /* exit(1); */ } } i++; } if (!CLOSE(integral, kern[0]->integral(parm), 0.0005)) { fprintf(stderr, "%s: HEY HEY HEY HEY HEY HEY!\n", me); fprintf(stderr, "%s: discrete integral %f != %f\n", me, integral, kern[0]->integral(parm)); /* exit(1); */ } /* it all checks out; write the file */ if (!(fout = airFopen(outS, stdout, "w"))) { fprintf(stderr, "%s: couldn't open \"%s\" for writing\n", me, outS); exit(1); } for (i=0; i<=len-1; i++) { fprintf(fout, "%g %g\n", dom_f[i], ran_f[i]); } fclose(fout); airMopOkay(mop); exit(0); }
int main(int argc, char *argv[]) { char *me, *kS, *minS, *stepS, *maxS, *outS, *err, kstr[AIR_STRLEN_LARGE]; const NrrdKernel *k; NrrdKernelSpec *ksp; double parm[NRRD_KERNEL_PARMS_NUM], min, step, max, integral, *dom_d, *ran_d; float *dom_f, *ran_f, v, r_f, r_d; FILE *fout; int i, len; airArray *mop; me = argv[0]; if (6 != argc) { usage(me); } kS = argv[1]; minS = argv[2]; stepS = argv[3]; maxS = argv[4]; outS = argv[5]; mop = airMopNew(); if (nrrdKernelParse(&k, parm, kS)) { airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: trouble:\n%s\n", me, err); airMopError(mop); exit(1); } ksp = nrrdKernelSpecNew(); airMopAdd(mop, ksp, (airMopper)nrrdKernelSpecNix, airMopAlways); nrrdKernelSpecSet(ksp, k, parm); if (nrrdKernelSpecSprint(kstr, ksp)) { airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: trouble:\n%s\n", me, err); airMopError(mop); exit(1); } printf("%s: printed kernel as \"%s\"\n", me, kstr); if (3 != (sscanf(minS, "%lf", &min) + sscanf(stepS, "%lf", &step) + sscanf(maxS, "%lf", &max))) { fprintf(stderr, "%s: couldn't parse \"%s\", \"%s\", \"%s\" as 3 doubles\n", me, minS, stepS, maxS); exit(1); } if (!( min <= -k->support(parm) && max >= k->support(parm) )) { fprintf(stderr, "%s: WARNING: support=%g => lower min (%g) or raise max (%g)\n", me, k->support(parm), min, max); } /* see how many values are in the interval */ len = 0; for (v=min; v<=max; v+=step) { len++; } /* allocate domain and range for both float and double */ if (!( (dom_d = (double *)calloc(len, sizeof(double))) && (ran_d = (double *)calloc(len, sizeof(double))) && (dom_f = (float *)calloc(len, sizeof(float))) && (ran_f = (float *)calloc(len, sizeof(float))) )) { fprintf(stderr, "%s: PANIC: couldn't allocate buffers\n", me); exit(1); } airMopAdd(mop, dom_d, airFree, airMopAlways); airMopAdd(mop, ran_d, airFree, airMopAlways); airMopAdd(mop, dom_f, airFree, airMopAlways); airMopAdd(mop, ran_f, airFree, airMopAlways); /* set values in both domains */ i=0; for (v=min; v<=max; v+=step) { /* note that the value stored in dom_d[i] is only a single-precision float, so that it is really equal to dom_f[i] */ dom_d[i] = v; dom_f[i] = v; i++; } /* do the vector evaluations */ k->evalN_f(ran_f, dom_f, len, parm); k->evalN_d(ran_d, dom_d, len, parm); /* do the single evaluations, and make sure everything agrees */ i = 0; integral = 0; for (v=min; v<=max; v+=step) { /* compare two single evaluations */ r_f = k->eval1_f(v, parm); r_d = k->eval1_d(v, parm); if (!CLOSE(r_f,r_d)) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (eval1_d(%g)== %f)\n", me, v, r_f, v, r_d); } /* compare single float with vector float */ if (!CLOSE(r_f,ran_f[i])) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (evalN_f[%d]== %f)\n", me, v, r_f, i, ran_f[i]); } /* compare single float with vector double */ r_d = ran_d[i]; if (!CLOSE(r_f,r_d)) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (evalN_d[%d]== %f)\n", me, v, r_f, i, r_d); } integral += step*ran_d[i]; i++; } if (!KINDACLOSE(integral, k->integral(parm))) { fprintf(stderr, "discrete integral %f != %f\n", integral, k->integral(parm)); /* not a fatal error */ } /* it all checks out; write the file */ if (!(fout = fopen(outS, "w"))) { fprintf(stderr, "%s: couldn't open \"%s\" for writing\n", me, outS); exit(1); } for (i=0; i<=len-1; i++) { fprintf(fout, "%g %g\n", dom_f[i], ran_f[i]); } fclose(fout); fprintf(stderr, "(for matlab:)\n"); fprintf(stderr, "x = dlmread(\'%s\', \' \'); plot(x(:,1), x(:,2));\n", outS); airMopOkay(mop); exit(0); }
/* ** little helper function to do pre-blurring of a given nrrd ** of the sort that might be useful for scale-space gage use ** ** nblur has to already be allocated for "blnum" Nrrd*s, AND, they all ** have to point to valid (possibly empty) Nrrds, so they can hold the ** results of blurring. "scale" is filled with the result of ** scaleCB(d_i), for "dom" evenly-spaced samples d_i between ** scldomMin and scldomMax */ int gageStackBlur(Nrrd *const nblur[], const double *scale, unsigned int blnum, const Nrrd *nin, unsigned int baseDim, const NrrdKernelSpec *_kspec, int boundary, int renormalize, int verbose) { char me[]="gageStackBlur", err[BIFF_STRLEN], val[AIR_STRLEN_LARGE], keyscl[]="scale", keykern[]="kernel"; unsigned int blidx, axi; NrrdResampleContext *rsmc; NrrdKernelSpec *kspec; airArray *mop; int E; if (!(nblur && scale && nin && _kspec)) { sprintf(err, "%s: got NULL pointer", me); biffAdd(GAGE, err); return 1; } if (!( blnum >= 2)) { sprintf(err, "%s: need blnum > 2, not %u", me, blnum); biffAdd(GAGE, err); return 1; } for (blidx=0; blidx<blnum; blidx++) { if (!AIR_EXISTS(scale[blidx])) { fprintf(stderr, "%s: scale[%u] = %g doesn't exist", me, blidx, scale[blidx]); biffAdd(GAGE, err); return 1; } if (blidx) { if (!( scale[blidx-1] < scale[blidx] )) { fprintf(stderr, "%s: scale[%u] = %g not < scale[%u] = %g", me, blidx, scale[blidx-1], blidx+1, scale[blidx]); biffAdd(GAGE, err); return 1; } } } if (3 + baseDim != nin->dim) { sprintf(err, "%s: need nin->dim %u (not %u) with baseDim %u", me, 3 + baseDim, nin->dim, baseDim); biffAdd(GAGE, err); return 1; } if (airEnumValCheck(nrrdBoundary, boundary)) { sprintf(err, "%s: %d not a valid %s value", me, boundary, nrrdBoundary->name); biffAdd(GAGE, err); return 1; } mop = airMopNew(); kspec = nrrdKernelSpecCopy(_kspec); if (!kspec) { sprintf(err, "%s: problem copying kernel spec", me); biffAdd(GAGE, err); airMopError(mop); return 1; } airMopAdd(mop, kspec, (airMopper)nrrdKernelSpecNix, airMopAlways); /* pre-allocate output Nrrds in case not already there */ for (blidx=0; blidx<blnum; blidx++) { if (!nblur[blidx]) { sprintf(err, "%s: got NULL nblur[%u]", me, blidx); biffAdd(GAGE, err); airMopError(mop); return 1; } } rsmc = nrrdResampleContextNew(); airMopAdd(mop, rsmc, (airMopper)nrrdResampleContextNix, airMopAlways); E = 0; if (!E) E |= nrrdResampleDefaultCenterSet(rsmc, nrrdDefaultCenter); if (!E) E |= nrrdResampleNrrdSet(rsmc, nin); if (baseDim) { unsigned int bai; for (bai=0; bai<baseDim; bai++) { if (!E) E |= nrrdResampleKernelSet(rsmc, bai, NULL, NULL); } } for (axi=0; axi<3; axi++) { if (!E) E |= nrrdResampleSamplesSet(rsmc, baseDim + axi, nin->axis[baseDim + axi].size); if (!E) E |= nrrdResampleRangeFullSet(rsmc, baseDim + axi); } if (!E) E |= nrrdResampleBoundarySet(rsmc, boundary); if (!E) E |= nrrdResampleTypeOutSet(rsmc, nrrdTypeDefault); if (!E) E |= nrrdResampleRenormalizeSet(rsmc, renormalize); if (E) { fprintf(stderr, "%s: trouble setting up resampling\n", me); biffAdd(GAGE, err); airMopError(mop); return 1; } for (blidx=0; blidx<blnum; blidx++) { kspec->parm[0] = scale[blidx]; for (axi=0; axi<3; axi++) { if (!E) E |= nrrdResampleKernelSet(rsmc, baseDim + axi, kspec->kernel, kspec->parm); } if (verbose) { fprintf(stderr, "%s: resampling %u of %u (scale %g) ... ", me, blidx, blnum, scale[blidx]); fflush(stderr); } if (!E) E |= nrrdResampleExecute(rsmc, nblur[blidx]); if (!E) nrrdKeyValueAdd(nblur[blidx], me, "true"); sprintf(val, "%g", scale[blidx]); if (!E) nrrdKeyValueAdd(nblur[blidx], keyscl, val); nrrdKernelSpecSprint(val, kspec); if (!E) nrrdKeyValueAdd(nblur[blidx], keykern, val); if (E) { if (verbose) { fprintf(stderr, "problem!\n"); } sprintf(err, "%s: trouble resampling %u of %u (scale %g)", me, blidx, blnum, scale[blidx]); biffAdd(GAGE, err); airMopError(mop); return 1; } if (verbose) { fprintf(stderr, "done.\n"); } } airMopOkay(mop); return 0; }