[vtk-developers] Charts and float/int conversion

David Gobbi david.gobbi at gmail.com
Tue Sep 21 17:51:05 EDT 2010


Hi Marcus,

I've noticed that "Charts" uses truncation for its float-to-int
conversion.  This can lead to rendering problems.

E.g. if float x is set to 1.0, some transformation is done to x, then
x is now equal to 3.99999 on some platforms, and equal to 4.0000001 on
other platforms.

i = static_cast<int>(x); // 3 on platform A, 4 on platform B

It might be worthwhile to add an inline float-to-int function that
does a bit of rounding to vtkContext2D, and then utilize this function
wherever int-to-float conversion is done within the Charts directory:

inline vtkContext2D::FloatToInt(float x)
{
  return static_cast<int>(x + 1.0625) - 1;
}

This provides a tolerance of 1/16 of a pixel for the float-to-int
conversion. The "1" and "- 1" ensure that float values in the range
[-1.0, 0.0] are rounded down, instead of being rounded toward zero.

  David



More information about the vtk-developers mailing list