8 #include "InitCocircWeights.hpp" 12 InitCocircWeights::InitCocircWeights(
char const *name, HyPerCol *hc) { initialize(name, hc); }
14 InitCocircWeights::InitCocircWeights() {}
16 InitCocircWeights::~InitCocircWeights() {}
18 int InitCocircWeights::initialize(
char const *name, HyPerCol *hc) {
19 int status = InitGauss2DWeights::initialize(name, hc);
25 ioParam_sigmaCocirc(ioFlag);
26 ioParam_sigmaKurve(ioFlag);
27 ioParam_cocircSelf(ioFlag);
28 ioParam_deltaRadiusCurvature(ioFlag);
33 void InitCocircWeights::ioParam_sigmaCocirc(
enum ParamsIOFlag ioFlag) {
34 parent->parameters()->ioParamValue(ioFlag, name,
"sigmaCocirc", &mSigmaCocirc, mSigmaCocirc);
37 void InitCocircWeights::ioParam_sigmaKurve(
enum ParamsIOFlag ioFlag) {
38 parent->parameters()->ioParamValue(ioFlag, name,
"sigmaKurve", &mSigmaKurve, mSigmaKurve);
41 void InitCocircWeights::ioParam_cocircSelf(
enum ParamsIOFlag ioFlag) {
42 parent->parameters()->ioParamValue(ioFlag, name,
"cocircSelf", &mCocircSelf, mCocircSelf);
45 void InitCocircWeights::ioParam_deltaRadiusCurvature(
enum ParamsIOFlag ioFlag) {
48 parent->parameters()->ioParamValue(
49 ioFlag, name,
"deltaRadiusCurvature", &mDeltaRadiusCurvature, mDeltaRadiusCurvature);
53 calcOtherParams(patchIndex);
54 mNKurvePre = mWeights->
getGeometry()->getPreLoc().nf / mNumOrientationsPre;
55 mNKurvePost = mWeights->
getGeometry()->getPostLoc().nf / mNumOrientationsPost;
57 cocircCalcWeights(dataStart);
60 void InitCocircWeights::cocircCalcWeights(
float *dataStart) {
64 int sx = mWeights->
getGeometry()->getPatchStrideX();
65 int sy = mWeights->
getGeometry()->getPatchStrideY();
66 int sf = mWeights->
getGeometry()->getPatchStrideF();
69 for (
int kfPost = 0; kfPost < nfPatch; kfPost++) {
70 float thPost = calcThPost(kfPost);
72 calcKurvePostAndSigmaKurvePost(kfPost);
74 if (checkThetaDiff(thPost)) {
78 for (
int jPost = 0; jPost < nyPatch; jPost++) {
79 float yDelta = calcYDelta(jPost);
80 for (
int iPost = 0; iPost < nxPatch; iPost++) {
81 float xDelta = calcXDelta(iPost);
83 initializeDistChordCocircKurvePreAndKurvePost();
85 if (calcDistChordCocircKurvePreNKurvePost(xDelta, yDelta, kfPost, thPost)) {
90 float weight = calculateWeight();
91 if (weight < mMinWeight) {
94 dataStart[iPost * sx + jPost * sy + kfPost * sf] = weight;
100 float InitCocircWeights::calcKurvePostAndSigmaKurvePost(
int kfPost) {
101 int iKvPost = kfPost % mNKurvePost;
102 float radKurvPost = calcKurveAndSigmaKurve(
103 iKvPost, mNKurvePost, mSigmaKurvePost, mKurvePost, mIPosKurvePost, mISaddlePost);
104 mSigmaKurvePost2 = 2 * mSigmaKurvePost * mSigmaKurvePost;
108 float InitCocircWeights::calcKurveAndSigmaKurve(
111 float &sigma_kurve_temp,
115 int iKv = kf % nKurve;
118 float radKurv = mDeltaRadiusCurvature + iKv * mDeltaRadiusCurvature;
119 sigma_kurve_temp = mSigmaKurve * radKurv;
121 kurve_tmp = (radKurv != 0.0f) ? 1 / radKurv : 1.0f;
123 int iKvPostAdj = iKv;
126 iPosKurve = iKv >= (int)(nKurve / 2);
129 iSaddle = (iKv % 2 == 0) ? 0 : 1;
130 iKvPostAdj = ((iKv % (nKurve / 2)) / 2);
133 iKvPostAdj = (iKv % (nKurve / 2));
136 radKurv = mDeltaRadiusCurvature + iKvPostAdj * mDeltaRadiusCurvature;
137 kurve_tmp = (radKurv != 0.0f) ? 1 / radKurv : 1.0f;
141 void InitCocircWeights::initializeDistChordCocircKurvePreAndKurvePost() {
148 bool InitCocircWeights::calcDistChordCocircKurvePreNKurvePost(
153 const float sigmaSquared = 2 * mSigma * mSigma;
156 float dxP = +xDelta * std::cos(mThetaPre) + yDelta * std::sin(mThetaPre);
157 float dyP = -xDelta * std::sin(mThetaPre) + yDelta * std::cos(mThetaPre);
160 float dyP_shift = dyP - mFlankShift;
161 float dyP_shift2 = dyP + mFlankShift;
162 float d2 = dxP * dxP + mAspect * dyP * mAspect * dyP;
163 float d2_shift = dxP * dxP + (mAspect * (dyP_shift)*mAspect * (dyP_shift));
164 float d2_shift2 = dxP * dxP + (mAspect * (dyP_shift2)*mAspect * (dyP_shift2));
165 if (d2_shift <= mRMaxSquared) {
166 addToGDist(std::exp(-d2_shift / sigmaSquared));
168 if (mNumFlanks > 1) {
170 if (d2_shift2 <= mRMaxSquared) {
171 addToGDist(std::exp(-d2_shift2 / sigmaSquared));
174 if (mGDist == 0.0f) {
178 if (checkSameLoc(kfPost)) {
185 float cocircKurveShift = d2_shift > 0 ? std::abs(2 * dyP_shift) / d2_shift : 0.0f;
187 updateCocircNChord(thPost, dyP_shift, dxP, cocircKurveShift, d2_shift);
189 if (checkFlags(dyP_shift, dxP)) {
194 updategKurvePreNgKurvePost(cocircKurveShift);
196 if (mNumFlanks > 1) {
198 float cocircKurve_shift2 = d2_shift2 > 0 ? fabsf(2 * dyP_shift2) / d2_shift2 : 0.0f;
200 updateCocircNChord(thPost, dyP_shift2, dxP, cocircKurve_shift2, d2_shift);
202 if (checkFlags(dyP_shift2, dxP)) {
207 updategKurvePreNgKurvePost(cocircKurve_shift2);
214 void InitCocircWeights::addToGDist(
float inc) { mGDist += inc; }
216 bool InitCocircWeights::checkSameLoc(
int kfPost) {
217 const float mSigmaCocirc2 = 2 * mSigmaCocirc * mSigmaCocirc;
218 bool sameLoc = (mFeaturePre == kfPost);
219 if ((!sameLoc) || (mCocircSelf)) {
220 mGCocirc = mSigmaCocirc > 0 ? expf(-mDeltaTheta * mDeltaTheta / mSigmaCocirc2)
221 : expf(-mDeltaTheta * mDeltaTheta / mSigmaCocirc2) - 1.0f;
222 if ((mNKurvePre > 1) && (mNKurvePost > 1)) {
224 expf(-(mKurvePre - mKurvePost) * (mKurvePre - mKurvePost)
225 / (mSigmaKurvePre2 + mSigmaKurvePost2));
235 void InitCocircWeights::updateCocircNChord(
239 float cocircKurveShift,
242 const float sigmaCocirc2 = 2 * mSigmaCocirc * mSigmaCocirc;
244 float atanx2_shift = mThetaPre + 2.0f * atan2f(dyP_shift, dxP);
245 atanx2_shift += 2.0f * PI;
246 atanx2_shift = fmodf(atanx2_shift, PI);
247 atanx2_shift = fabsf(atanx2_shift - thPost);
248 float chi_shift = atanx2_shift;
249 if (chi_shift >= PI / 2.0f) {
250 chi_shift = PI - chi_shift;
252 if (mNumOrientationsPre > 1 && mNumOrientationsPost > 1) {
253 mGCocirc = sigmaCocirc2 > 0 ? expf(-chi_shift * chi_shift / sigmaCocirc2)
254 : expf(-chi_shift * chi_shift / sigmaCocirc2) - 1.0f;
258 bool InitCocircWeights::checkFlags(
float dyP_shift,
float dxP) {
261 if ((mIPosKurvePre) && !(mISaddlePre) && (dyP_shift < 0)) {
264 if (!(mIPosKurvePre) && !(mISaddlePre) && (dyP_shift > 0)) {
267 if ((mIPosKurvePre) && (mISaddlePre)
268 && (((dyP_shift > 0) && (dxP < 0)) || ((dyP_shift > 0) && (dxP < 0)))) {
271 if (!(mIPosKurvePre) && (mISaddlePre)
272 && (((dyP_shift > 0) && (dxP > 0)) || ((dyP_shift < 0) && (dxP < 0)))) {
277 if ((mIPosKurvePre) && (dyP_shift < 0)) {
280 if (!(mIPosKurvePre) && (dyP_shift > 0)) {
288 void InitCocircWeights::updategKurvePreNgKurvePost(
float cocircKurveShift) {
289 const float sigmaCocirc2 = 2 * mSigmaCocirc * mSigmaCocirc;
294 -std::pow((cocircKurveShift - std::abs(mKurvePre)), 2.0f) / mSigmaKurvePre2)
296 mGKurvePost = ((mNKurvePre > 1) && (mNKurvePost > 1) && (sigmaCocirc2 > 0))
298 -std::pow((cocircKurveShift - std::abs(mKurvePost)), 2.0f)
303 float InitCocircWeights::calculateWeight() {
return mGDist * mGKurvePre * mGKurvePost * mGCocirc; }
int getPatchSizeX() const
virtual void calcWeights() override
float * getDataFromDataIndex(int arbor, int dataIndex)
int getPatchSizeY() const
std::shared_ptr< PatchGeometry > getGeometry() const
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
int getPatchSizeF() const