<HTML>
<HEAD>
<TITLE>Re: [vtk-developers] Trouble with vtkTemplateMacro</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>I think the main problem is that the declaration <BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'>void* point[3];<BR>
</SPAN></FONT></FONT></BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
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 </SPAN></FONT><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'>void</SPAN></FONT></FONT><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'> for obvious reasons.<BR>
<BR>
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 </SPAN></FONT><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'>GetVoidPointer(i*3)</SPAN></FONT></FONT><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>.  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.<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'>template<typename T><BR>
void ComputePointsCentroid(const T *data, vtkIdType numpoints, double centroid[3])<BR>
{<BR>
  centroid[0] = centroid[1] = centroid[2] = 0.0;<BR>
  for (vtkIdType i = 0; i < numpoints; i++)<BR>
    {<BR>
    centroid[0] += data[0];<BR>
    centroid[1] += data[1];<BR>
    centroid[2] += data[2];<BR>
    data += 3;<BR>
    }<BR>
  centroid[0] /= numpoints;<BR>
  centroid[1] /= numpoints;<BR>
  centroid[2] /= numpoints;<BR>
}<BR>
<BR>
</SPAN></FONT></FONT><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>...<BR>
<BR>
</SPAN></FONT><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'>double centroid[3];<BR>
switch(this->Data->GetDataType())<BR>
  {<BR>
  vtkTemplateMacro(ComputePointsCentroid(static_cast<VTK_TT*>(this->Data->GetVoidPointer(0)), this->GetNumberOfPoints(), centroid));<BR>
  }<BR>
</SPAN></FONT></FONT></BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
Hope that helps.<BR>
<BR>
-Ken<BR>
<BR>
<BR>
On 4/1/10 10:10 AM, "David Doria" <<a href="daviddoria+vtk@gmail.com">daviddoria+vtk@gmail.com</a>> wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>I am trying to write a function which efficiently gets the values of<BR>
all of the points in a vtkPoints.<BR>
<BR>
The non-efficient, virtual calls, assuming doubles version would look like this:<BR>
<BR>
    for(vtkIdType i = 0; i < this->GetNumberOfPoints(); i++)<BR>
      {<BR>
      double point[3];<BR>
      this->GetPoint(i, point);<BR>
      }<BR>
<BR>
My attempt to use vtkTemplateMacro looks like this:<BR>
<BR>
<BR>
template<class T><BR>
void vtkPoints::GetTypedPoint(T* data, vtkIdType i, T point[3])<BR>
{<BR>
  data->GetTupleValue(i, point);<BR>
}<BR>
<BR>
void vtkPoints::GetCentroidFast(double center[3])<BR>
{<BR>
  for(vtkIdType i = 0; i < this->GetNumberOfPoints(); i++)<BR>
    {<BR>
    void* point[3];<BR>
<BR>
    switch(this->Data->GetDataType())<BR>
      {<BR>
      vtkTemplateMacro(this->GetTypedPoint(static_cast<VTK_TT*>(this->Data->GetVoidPointer(0)),<BR>
i, static_cast<VTK_TT*>(point)));<BR>
      }<BR>
    }<BR>
<BR>
}<BR>
<BR>
I am trying to use the type of the vtkDataArray (named 'Data') to get<BR>
the correct type of the ith tuple and store it in 'point'.<BR>
<BR>
For example, if Data is actually a vtkFloatArray, I'd want this to achieve:<BR>
<BR>
  for(vtkIdType i = 0; i < this->GetNumberOfPoints(); i++)<BR>
    {<BR>
    float point[3];<BR>
    vtkFloatArray::SafeDownCast(Data)->GetTupleValue(i, point);<BR>
    }<BR>
<BR>
Any pointers on where this has gone astray?<BR>
<BR>
Thanks,<BR>
<BR>
David<BR>
_______________________________________________<BR>
Powered by www.kitware.com<BR>
<BR>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html">http://www.kitware.com/opensource/opensource.html</a><BR>
<BR>
Follow this link to subscribe/unsubscribe:<BR>
<a href="http://www.vtk.org/mailman/listinfo/vtk-developers">http://www.vtk.org/mailman/listinfo/vtk-developers</a><BR>
<BR>
<BR>
<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
</SPAN></FONT><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'><BR>
   ****      Kenneth Moreland<BR>
    ***      Sandia National Laboratories<BR>
***********  <BR>
*** *** ***  email: <a href="kmorel@sandia.gov">kmorel@sandia.gov</a><BR>
**  ***  **  phone: (505) 844-8919<BR>
    ***      web:   <a href="http://www.cs.unm.edu/~kmorel">http://www.cs.unm.edu/~kmorel</a><BR>
</SPAN></FONT></FONT><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
</SPAN></FONT>
</BODY>
</HTML>