[vtkusers] filling a vtkFloatArray ... first element?

Richard Brown richard.j.brown at live.co.uk
Thu May 19 12:58:11 EDT 2016


Try using the InsertNextValue(), GetValue() and SetValue() methods:

int main() {

    vtkSmartPointer<vtkFloatArray> flame = vtkSmartPointer<vtkFloatArray>::New();
    flame->SetNumberOfComponents(1);

    for (int i = 0; i < 10; i++) {
        flame->InsertNextValue(i);
        vtkIdType id = i;
        cout << flame->GetValue(id) << ", " << flame->GetValue(id-1) << endl;
    }

    cout << "nTuples = " << flame->GetNumberOfTuples() << endl;

    cout << "Contents of flame: " << endl;

    for (int i = 0; i < flame->GetNumberOfTuples(); i++) {
        vtkIdType id = i;
        cout << "i = " << i << ", flame[" << i << "] = " <<
        flame->GetValue(id) << endl;
    }

    //Okay, maybe if we use SetNumberOfTuples and SetTuple1 it will work.
    // Nope!
    vtkIdType id = 0;
    flame->SetValue(id, 99);
    float retrieved = flame->GetValue(id);
    cout << "retrieved = " << retrieved << endl;
    retrieved = flame->GetValue(-1);
    cout << "retrieved = " << retrieved << endl;

    return 0;
}

> On 19 May 2016, at 18:26, jbouffar <julian.m.bouffard at erdc.dren.mil> wrote:
> 
> Hello,
> 
> I have a strange question.  It seems the first element I put into a
> vtkFloatArray is missing, so that if I construct a new floatArray element by
> element from some other arrays, they are out of sync by one element.  I have
> written some code in c++ to illustrate this (below).  Even weirder (to me)
> it seems I can access the [-1] component, and it works! What is going on
> here? I have tried also using SetNumberOfTuples and SetTuple, but this
> doesn't even work. This is VTK 6.3.
> 
> int main(int argc, char** argv){
> 
>  vtkSmartPointer<vtkFloatArray> flame =
> vtkSmartPointer<vtkFloatArray>::New();
>  flame->SetNumberOfComponents(1);
> 
>  for(int i = 0; i < 10; i++){
>    flame->InsertNextTuple1(i);
>    vtkIdType id = i;
>    cout << flame->GetComponent(id,1) << ", " << flame->GetComponent(id-1,1)
> << endl;
>  }
> 
>  cout << "nTuples = " << flame->GetNumberOfTuples() << endl;
> 
>  cout << "Contents of flame: " << endl;
>  for(int i = 0; i < flame->GetNumberOfTuples(); i++){
>    vtkIdType id = i;
>    cout << "i = " << i << ", flame[" << i << "] = " << 
> flame->GetComponent(id,1) << endl;
>  }
> 
>  //Okay, maybe if we use SetNumberOfTuples and SetTuple1 it will work.
> Nope!
>  vtkIdType id = 0;
>  flame->SetTuple1(id, 99);
>  float retrieved = flame->GetComponent(id,1);
>  cout << "retrieved = " << retrieved << endl;
>  retrieved = flame->GetComponent(-1,1);
>  cout << "retrieved = " << retrieved << endl;
> 
>  return 0;
> }
> 
> Output:
> 
> 4.58589e-41, 0
> 2.13737e+11, 1
> 4.58589e-41, 2
> 0, 3
> 0, 4
> 1.99685e-42, 5
> 0, 6
> 2.13737e+11, 7
> 4.58589e-41, 8
> 1.97443e-42, 9
> nTuples = 10
> Contents of flame:
> i = 0, flame[0] = 1
> i = 1, flame[1] = 2
> i = 2, flame[2] = 3
> i = 3, flame[3] = 4
> i = 4, flame[4] = 5
> i = 5, flame[5] = 6
> i = 6, flame[6] = 7
> i = 7, flame[7] = 8
> i = 8, flame[8] = 9
> i = 9, flame[9] = 1.97443e-42
> retrieved = 1
> retrieved = 99
> 
> 
> 
> 
> 
> 
> --
> View this message in context: http://vtk.1045678.n5.nabble.com/filling-a-vtkFloatArray-first-element-tp5738146.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> 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
> 
> Search the list archives at: http://markmail.org/search/?q=vtkusers
> 
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160519/891b2be7/attachment.html>


More information about the vtkusers mailing list