pullVolume * pullVolumeNew() { pullVolume *vol; vol = AIR_CAST(pullVolume *, calloc(1, sizeof(pullVolume))); if (vol) { vol->name = NULL; vol->kind = NULL; vol->ninSingle = NULL; vol->ninScale = NULL; vol->scaleNum = 0; vol->scalePos = NULL; vol->ksp00 = nrrdKernelSpecNew(); vol->ksp11 = nrrdKernelSpecNew(); vol->ksp22 = nrrdKernelSpecNew(); vol->kspSS = nrrdKernelSpecNew(); vol->gctx = gageContextNew(); vol->gpvl = NULL; vol->seedOnly = AIR_TRUE; } return vol; }
int _nrrdHestKernelSpecParse(void *ptr, char *str, char err[AIR_STRLEN_HUGE]) { NrrdKernelSpec **ksP; char me[]="_nrrdHestKernelSpecParse", *nerr; if (!(ptr && str)) { sprintf(err, "%s: got NULL pointer", me); return 1; } ksP = (NrrdKernelSpec **)ptr; *ksP = nrrdKernelSpecNew(); if (nrrdKernelParse(&((*ksP)->kernel), (*ksP)->parm, str)) { nerr = biffGetDone(NRRD); airStrcpy(err, AIR_STRLEN_HUGE, nerr); free(nerr); return 1; } return 0; }
static tenFiberContext * _tenFiberContextCommonNew(const Nrrd *vol, int useDwi, double thresh, double soft, double valueMin, int ten1method, int ten2method) { char me[]="_tenFiberContextCommonNew", err[BIFF_STRLEN]; tenFiberContext *tfx; gageKind *kind; if (!( tfx = (tenFiberContext *)calloc(1, sizeof(tenFiberContext)) )) { sprintf(err, "%s: couldn't allocate new context", me); biffAdd(TEN, err); return NULL; } if (useDwi) { Nrrd *ngrad=NULL, *nbmat=NULL; double bval=0; unsigned int *skip, skipNum; tfx->useDwi = AIR_TRUE; /* default fiber type */ tfx->fiberType = tenDwiFiberTypeUnknown; if (tenDWMRIKeyValueParse(&ngrad, &nbmat, &bval, &skip, &skipNum, vol)) { sprintf(err, "%s: trouble parsing DWI info", me ); biffAdd(TEN, err); return NULL; } if (skipNum) { sprintf(err, "%s: sorry, can't do DWI skipping here", me); biffAdd(TEN, err); return NULL; } kind = tenDwiGageKindNew(); if (tenDwiGageKindSet(kind, thresh, soft, bval, valueMin, ngrad, NULL, ten1method, ten2method, 42)) { sprintf(err, "%s: trouble setting DWI kind", me); biffAdd(TEN, err); return NULL; } } else { /* it should be a tensor volume */ tfx->useDwi = AIR_FALSE; /* default fiber type */ tfx->fiberType = tenFiberTypeUnknown; if (tenTensorCheck(vol, nrrdTypeUnknown, AIR_TRUE, AIR_TRUE)) { sprintf(err, "%s: didn't get a tensor volume", me); biffAdd(TEN, err); return NULL; } kind = tenGageKind; } if ( !(tfx->gtx = gageContextNew()) || !(tfx->pvl = gagePerVolumeNew(tfx->gtx, vol, kind)) || (gagePerVolumeAttach(tfx->gtx, tfx->pvl)) ) { sprintf(err, "%s: gage trouble", me); biffMove(TEN, err, GAGE); free(tfx); return NULL; } tfx->nin = vol; tfx->ksp = nrrdKernelSpecNew(); if (nrrdKernelSpecParse(tfx->ksp, tenDefFiberKernel)) { sprintf(err, "%s: couldn't parse tenDefFiberKernel \"%s\"", me, tenDefFiberKernel); biffMove(TEN, err, NRRD); return NULL; } if (tenFiberKernelSet(tfx, tfx->ksp->kernel, tfx->ksp->parm)) { sprintf(err, "%s: couldn't set default kernel", me); biffAdd(TEN, err); return NULL; } /* looks to GK like GK says that we must set some stop criterion */ tfx->intg = tenDefFiberIntg; tfx->anisoStopType = tenDefFiberAnisoStopType; tfx->anisoSpeedType = tenAnisoUnknown; tfx->stop = 0; tfx->anisoThresh = tenDefFiberAnisoThresh; /* so I'm not using the normal default mechanism, shoot me */ tfx->anisoSpeedFunc[0] = 0; tfx->anisoSpeedFunc[1] = 0; tfx->anisoSpeedFunc[2] = 0; tfx->maxNumSteps = tenDefFiberMaxNumSteps; tfx->minNumSteps = 0; tfx->useIndexSpace = tenDefFiberUseIndexSpace; tfx->verbose = 0; tfx->stepSize = tenDefFiberStepSize; tfx->maxHalfLen = tenDefFiberMaxHalfLen; tfx->minWholeLen = 0.0; tfx->confThresh = 0.5; /* why do I even bother setting these- they'll only get read if the right tenFiberStopSet has been called, in which case they'll be set... */ tfx->minRadius = 1; /* above lament applies here as well */ tfx->minFraction = 0.5; /* and here */ tfx->wPunct = tenDefFiberWPunct; GAGE_QUERY_RESET(tfx->query); tfx->mframe[0] = vol->measurementFrame[0][0]; tfx->mframe[1] = vol->measurementFrame[1][0]; tfx->mframe[2] = vol->measurementFrame[2][0]; tfx->mframe[3] = vol->measurementFrame[0][1]; tfx->mframe[4] = vol->measurementFrame[1][1]; tfx->mframe[5] = vol->measurementFrame[2][1]; tfx->mframe[6] = vol->measurementFrame[0][2]; tfx->mframe[7] = vol->measurementFrame[1][2]; tfx->mframe[8] = vol->measurementFrame[2][2]; if (ELL_3M_EXISTS(tfx->mframe)) { tfx->mframeUse = AIR_TRUE; ELL_3M_TRANSPOSE(tfx->mframeT, tfx->mframe); } else { tfx->mframeUse = AIR_FALSE; } tfx->gageAnisoStop = NULL; tfx->gageAnisoSpeed = NULL; tfx->ten2AnisoStop = AIR_NAN; /* ... don't really see the point of initializing the ten2 stuff here; its properly done in tenFiberTraceSet() ... */ tfx->radius = AIR_NAN; return tfx; }
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); }
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); }