Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContourSpatialObject<TDimension>::Update() LINEAR_INTERPOLATION case may need some adjustment #3222

Open
N-Dekker opened this issue Feb 23, 2022 · 7 comments
Labels
type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances

Comments

@N-Dekker
Copy link
Contributor

There is a line of code in ContourSpatialObject<TDimension>::Update() that looks suspicious to me:

newPoint = pnt[d] + i * step[d]; 

PointType newPoint;
newPoint.Fill(NumericTraits<double>::max());
for (unsigned int i = 0; i < m_InterpolationFactor; ++i)
{
for (unsigned int d = 0; d < TDimension; ++d)
{
newPoint = pnt[d] + i * step[d];
}
}

You see, newPoint is a PointType, and it gets filled by a scalar value, pnt[d] + i * step[d], over and over again, inside the inner for loop.

I think the intention was to do:

newPoint[d] = pnt[d] + i * step[d]; 

But I'm not sure, because I did not study the code well enough yet! Any feedback or possible explanation is welcome!

Note: This bug report is still in progress! More details soon, hopefully!

@N-Dekker N-Dekker added the type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances label Feb 23, 2022
@dzenanz
Copy link
Member

dzenanz commented Feb 23, 2022

@aylward might have some comment.

@aylward
Copy link
Member

aylward commented Feb 23, 2022

You're right! Great catch! Want to submit a pull request to fix it?

@N-Dekker
Copy link
Contributor Author

Great catch! Want to submit a pull request to fix it?

Thanks @aylward but I don't know how to fix it properly. Is it really a serious bug? Or is this part of the code not so important anyway?

If it is a serious bug, can you please tell me the intended behavior?

@N-Dekker
Copy link
Contributor Author

The apparent bug is part of the case InterpolationMethodEnum::LINEAR_INTERPOLATION section of a switch (m_InterpolationMethod). It appears that this specific "case" is not tested, at least not by itkContourSpatialObjectTest.cxx. If it is indeed untested, and if it is also unused, I would prefer to just put some itkExceptionMacro(<< "not yet defined") call there, just like in case InterpolationMethodEnum::BEZIER_INTERPOLATION. Then postpone actual implementation until it is really needed. Would that be OK to you?

this->SetPoints(m_ControlPoints);
break;
case InterpolationMethodEnum::EXPLICIT_INTERPOLATION:
break;
case InterpolationMethodEnum::BEZIER_INTERPOLATION:
// TODO: Implement bezier interpolation
{
itkExceptionMacro(<< "Bezier interpolation type not yet defined.");
}
break;
case InterpolationMethodEnum::LINEAR_INTERPOLATION:

@aylward
Copy link
Member

aylward commented Feb 23, 2022 via email

@N-Dekker N-Dekker changed the title WIP: BUG: ContourSpatialObject<TDimension>::Update() looks suspicious ContourSpatialObject<TDimension>::Update() LINEAR_INTERPOLATION case may need some adjustment Feb 23, 2022
N-Dekker added a commit to N-Dekker/ITK that referenced this issue Feb 23, 2022
`ContourSpatialObject::Update()` appears to have an assignment to
`newPoint`, in the `case InterpolationMethodEnum::LINEAR_INTERPOLATION`
section, which was meant to assign just a single element `newPoint[d]`.

As was confirmed by Stephen Aylward at issue InsightSoftwareConsortium#3222,
"`ContourSpatialObject<TDimension>::Update()` LINEAR_INTERPOLATION case
may need some adjustment".

Some more adjustment may still be needed, as `newPoint` now still gets
modified multiply times (rather than just once), before it eventually
gets used.
@N-Dekker
Copy link
Contributor Author

@aylward Thanks for your reply, Stephen. Please check pull request #3225 "BUG: Quick-fix ContourSpatialObject::Update(), LINEAR_INTERPOLATION case", and if possible, please approve the pull request 😃

I think it would still be useful if you could get back to it after March 8th, because I consider my pull request #3225 just a quick-fix. I guess you may be able to fix it more thoroughly 😃

hjmjohnson pushed a commit that referenced this issue Feb 24, 2022
`ContourSpatialObject::Update()` appears to have an assignment to
`newPoint`, in the `case InterpolationMethodEnum::LINEAR_INTERPOLATION`
section, which was meant to assign just a single element `newPoint[d]`.

As was confirmed by Stephen Aylward at issue #3222,
"`ContourSpatialObject<TDimension>::Update()` LINEAR_INTERPOLATION case
may need some adjustment".

Some more adjustment may still be needed, as `newPoint` now still gets
modified multiply times (rather than just once), before it eventually
gets used.
@N-Dekker
Copy link
Contributor Author

I'm glad to see the quick-fix for this issue is merged quickly, thanks @hjmjohnson! So now the code says:

 PointType newPoint; 
 newPoint.Fill(NumericTraits<double>::max()); 
 for (unsigned int i = 0; i < m_InterpolationFactor; ++i) 
 { 
   for (unsigned int d = 0; d < TDimension; ++d) 
   { 
     newPoint[d] = pnt[d] + i * step[d]; 
   } 
 } 

PointType newPoint;
newPoint.Fill(NumericTraits<double>::max());
for (unsigned int i = 0; i < m_InterpolationFactor; ++i)
{
for (unsigned int d = 0; d < TDimension; ++d)
{
newPoint[d] = pnt[d] + i * step[d];
}
}

My remaining questions are:

  • Why does it do a newPoint.Fill(...), when it appears that each coordinate of newPoint will get a new value in the following lines of code. Is that means to support m_InterpolationFactor == 0?
  • Why would the newPoint be modified multiple times, inside the for (unsigned int i = 0; i < m_InterpolationFactor; ++i) loop? It seems to me that only the result of the last iteration (i == m_InterpolationFactor - 1) is relevant, right?

Anyway, no hurry, I can wait until after after March 8th 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances
Projects
None yet
Development

No branches or pull requests

3 participants