[vtkusers] vtkDoubleArray -- you can set the number of values but you can't get the number of values?

Francois Bertel francois.bertel at kitware.com
Wed Feb 18 14:53:08 EST 2009


Wrong approach. NumberOfComponents is the number of components per
tuple. NumberOfComponents is intended to be small.

vtkDoubleArray *d=vtkDoubleArray::New();
d->SetNumberOfComponents(1);
d->SetNumberOfTuples(count); // allocation happens here.
i=0;
while(i<count)
{
d->SetValue(i,value);
++i;
}
d->Modified(); // you have too, SetValue() does not do it for you, as
InsertValue does.

or with direct access to the raw pointer:
double *ptr=d->GetPointer (0);

i=0;
while(i<count)
{
ptr[i]=value[i];
++i;
}
d->Modified();

or without copying but just using "double *value":
d->SetArray(value,count);


On Wed, Feb 18, 2009 at 2:38 PM, kent williams
<nkwmailinglists at gmail.com> wrote:
> Well as it happens, I'm storing an array in one tuple. There's no
> corresponding GetNumberOfValues method in the class. I could just
> SetNumberOfValues to 1, and then store each array value in it's own
> tuple, I guess, but that seems a little clumsy...
>
> Here's my code.  This may seem to be a dumb exercise, but there's a
> purpose to it -- I want to save metadata with the vtkPolyData when I
> write it to disk.  I posted yesterday, asking for how one would add
> vtkProperty attributes into a vtkPolyData in order to write it to disk
> with the XML writer, and no one answered, so I went ahead and added a
> data-array for each value in the vtkProperty.
>
> Here's what I'm doing, for the Double Array case...
>
>
> static void PolyDataSetDouble(vtkPolyData *p, const char *name, const
> double *value, int count)
> {
>  vtkFieldData *fieldData = p->GetFieldData();
>  vtkDoubleArray *doubleArray = vtkDoubleArray::New();
>  doubleArray->SetNumberOfComponents(count);
>  doubleArray->InsertNextTupleValue(value);
>  doubleArray->SetName(name);
>  fieldData->AddArray(doubleArray);
>  doubleArray->Delete();
> }
>
>
> static void PolyDataGetDouble(vtkPolyData *p, const char *name, double
> *value,  int count)
> {
>  vtkFieldData *fieldData = p->GetFieldData();
>  vtkDoubleArray *doubleArray =
> vtkDoubleArray::SafeDownCast(fieldData->GetArray(name));
>  if(doubleArray == 0)
>    {
>    for(unsigned i = 0; i < count; i++)
>      {
>      value[i] = 0.0;
>      }
>    }
>  else
>    { // no way to see how many elements actually in this tuple!!!
>    doubleArray->GetTupleValue(0,value);
>    }
> }
>
>
> On Wed, Feb 18, 2009 at 12:49 PM, Francois Bertel
> <francois.bertel at kitware.com> wrote:
>> It is not the number of tuples. It is the number of tuples times the
>> number of components, at least according to this implementation:
>>
>> VTK/Common/vtkDataArrayTemplate.txx:
>>
>> //----------------------------------------------------------------------------
>> // Set the number of n-tuples in the array.
>> template <class T>
>> void vtkDataArrayTemplate<T>::SetNumberOfTuples(vtkIdType number)
>> {
>>  this->SetNumberOfValues(number*this->NumberOfComponents);
>>  this->DataChanged();
>> }
>>
>> On Wed, Feb 18, 2009 at 1:44 PM, Michael Jackson
>> <mike.jackson at bluequartz.net> wrote:
>>> You can also use
>>> vtkIdType GetNumberOfTuples ();
>>>  since vtkArrays are based off the "Tuple" concept. Look at the
>>> vtkAbstractArray class which vtkDoubleArray descends from.
>>>
>>> _________________________________________________________
>>> Mike Jackson                  mike.jackson at bluequartz.net
>>> BlueQuartz Software                    www.bluequartz.net
>>> Principal Software Engineer                  Dayton, Ohio
>>>
>>>
>>>
>>> On Feb 18, 2009, at 1:34 PM, kent williams wrote:
>>>
>>>> I've been staring for some time at the Doxygen man pages for
>>>> vtkDoubleArray and vtkDataArray.  It seems that there is a member
>>>> function vtkDoubleArray::SetNumberOfValues(), but no corresponding
>>>> vtkDoubleArray::GetNumberOfValues().
>>>>
>>>> This seems to indicate that you have to make sure that you never try
>>>> to read out more values than you've put in to the array, because
>>>> there's no way to check this.
>>>>
>>>> Or am I missing something?
>>>> _______________________________________________
>>>> Powered by www.kitware.com
>>>>
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>>
>>>> Please keep messages on-topic and check the VTK FAQ at:
>>>> http://www.vtk.org/Wiki/VTK_FAQ
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>
>>> _______________________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Please keep messages on-topic and check the VTK FAQ at:
>>> http://www.vtk.org/Wiki/VTK_FAQ
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>
>>
>>
>>
>> --
>> François Bertel, PhD  | Kitware Inc. Suite 204
>> 1 (518) 371 3971 x113 | 28 Corporate Drive
>>                      | Clifton Park NY 12065, USA
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
>>
>



-- 
François Bertel, PhD  | Kitware Inc. Suite 204
1 (518) 371 3971 x113 | 28 Corporate Drive
                      | Clifton Park NY 12065, USA



More information about the vtkusers mailing list