[vtkusers] Why is the image red?
David Gobbi
david.gobbi at gmail.com
Mon Dec 14 20:47:51 EST 2009
On Mon, Dec 14, 2009 at 6:02 PM, David Doria <daviddoria+vtk at gmail.com> wrote:
>
> ptr = static_cast<T *>(image->GetScalarPointer(p0, p1, z));
>
> pf = drawColor;
> // Assign color to pixel.
> for (idxV = 0; idxV <= maxV; ++idxV)
> {
> *ptr = static_cast<T>(*pf++);
> ptr++;
> }
>
> Couldn't this be simply:
>
> // Assign color to pixel.
> for (component = 0; component <= maxV; ++component)
> {
> image->SetScalarComponentFromDouble(p0, p1, z, component, drawColor[i]);
> }
>
> This function is a template function which is called with
> vtkTemplateMacro. I don't understand what that is doing - isn't the
> whole point of templates that you don't have to declare all of these
> different types explicitly?
>
> Thanks,
>
> David
All of the VTK imaging filters work by doing pointer arithmetic and
direct access to memory. It's the VTK convention. It's possibly not
the best way for things to be done, but after you're familiar with the
common pointer arithmetic coding patters it's easy to read this code
and see what it's doing. Really, if the main problem that I see is
that the variables weren't named very well.
The SetScalarComponentFromDouble() method isn't 100% inlined and it
involves a virtual function call, so it's far too inefficient to use
inside an image filter. Not to mention that it does a few
multiplications and additions internally. Yikes.
The big "switch" in the vtkTemplateMacro allows the execute methods to
be templated while the classes themselves aren't. Because the filter
classes themselves aren't templated, an instance of a vtk filter class
can actually operate on any data type that you feed it. In contrast
with e.g. ITK where filter instances are type specific.
David
More information about the vtkusers
mailing list