[vtk-developers] Trouble with vtkTemplateMacro

Moreland, Kenneth kmorel at sandia.gov
Thu Apr 1 12:24:49 EDT 2010


I think the main problem is that the declaration

void* point[3];

is not doing what you think it is doing.  This is creating an array of arrays.  What you want is a single array of three values, but you can't do that with void for obvious reasons.

I don't think using a templated function to return a type is really what you want to do.  In fact, all you're really trying to do is equivalent to calling GetVoidPointer(i*3).  Using this template only makes sense of your are copying into another vtkDataArray of the same type or accumulated things to a known type (like double).  Based on your recent emails, I think you want something like this.

template<typename T>
void ComputePointsCentroid(const T *data, vtkIdType numpoints, double centroid[3])
{
  centroid[0] = centroid[1] = centroid[2] = 0.0;
  for (vtkIdType i = 0; i < numpoints; i++)
    {
    centroid[0] += data[0];
    centroid[1] += data[1];
    centroid[2] += data[2];
    data += 3;
    }
  centroid[0] /= numpoints;
  centroid[1] /= numpoints;
  centroid[2] /= numpoints;
}

...

double centroid[3];
switch(this->Data->GetDataType())
  {
  vtkTemplateMacro(ComputePointsCentroid(static_cast<VTK_TT*>(this->Data->GetVoidPointer(0)), this->GetNumberOfPoints(), centroid));
  }

Hope that helps.

-Ken


On 4/1/10 10:10 AM, "David Doria" <daviddoria+vtk at gmail.com> wrote:

I am trying to write a function which efficiently gets the values of
all of the points in a vtkPoints.

The non-efficient, virtual calls, assuming doubles version would look like this:

    for(vtkIdType i = 0; i < this->GetNumberOfPoints(); i++)
      {
      double point[3];
      this->GetPoint(i, point);
      }

My attempt to use vtkTemplateMacro looks like this:


template<class T>
void vtkPoints::GetTypedPoint(T* data, vtkIdType i, T point[3])
{
  data->GetTupleValue(i, point);
}

void vtkPoints::GetCentroidFast(double center[3])
{
  for(vtkIdType i = 0; i < this->GetNumberOfPoints(); i++)
    {
    void* point[3];

    switch(this->Data->GetDataType())
      {
      vtkTemplateMacro(this->GetTypedPoint(static_cast<VTK_TT*>(this->Data->GetVoidPointer(0)),
i, static_cast<VTK_TT*>(point)));
      }
    }

}

I am trying to use the type of the vtkDataArray (named 'Data') to get
the correct type of the ith tuple and store it in 'point'.

For example, if Data is actually a vtkFloatArray, I'd want this to achieve:

  for(vtkIdType i = 0; i < this->GetNumberOfPoints(); i++)
    {
    float point[3];
    vtkFloatArray::SafeDownCast(Data)->GetTupleValue(i, point);
    }

Any pointers on where this has gone astray?

Thanks,

David
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtk-developers





   ****      Kenneth Moreland
    ***      Sandia National Laboratories
***********
*** *** ***  email: kmorel at sandia.gov
**  ***  **  phone: (505) 844-8919
    ***      web:   http://www.cs.unm.edu/~kmorel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20100401/8846c7df/attachment.html>


More information about the vtk-developers mailing list