[vtkusers] updating vtkimagedata in animation.

shadab anwar sk.shadabanwar at gmail.com
Fri Jan 18 18:02:28 EST 2019


Hi David;

I am still not getting the output.
This is what I am doing.
I don't know if it makes sense.But I thought it'll better to share the
code.Please let me know for any clarifications you need.


class vtkTimerCallback2 : public vtkCommand

{

public:

    int timerId;

    static vtkTimerCallback2 *New()

    {

        vtkTimerCallback2 *cb = new vtkTimerCallback2;

        cb->TimerCount = 0;

        return cb;

    }


    virtual void Execute(vtkObject *caller, unsigned long eventId,

        void * vtkNotUsed(callData))

    {

        if (vtkCommand::TimerEvent == eventId)

        {

            ++this->TimerCount;

        }

//        std::cout << this->TimerCount << std::endl;

        vtkRenderWindowInteractor *iren =
vtkRenderWindowInteractor::SafeDownCast(caller);

      //  vtkRenderer *ren = vtkRenderer::SafeDownCast(caller);


        if(TimerCount<=toolLocations->GetNumberOfPoints())

         {

            double* location = toolLocations->GetPoint(TimerCount-1);

            int a = location[0];

            int b = location[1];

            int c = location[2];



            for (unsigned int k =0; k < height; k++){

                for (unsigned int j = 0; j < roughingToolWidth; j++) {

                    for (unsigned int i = 0; i < roughingToolWidth; i++) {

                        unsigned char* voxel = static_cast<unsigned
char*>(imageData->GetScalarPointer(i+a, j+b, k+c));


                        //std::cout<<"voxel value:  "<<voxel[0]<<"
tool value: "<<roughingToolDepthMap[i +
j*roughingToolWidth]<<std::endl;


                        if (roughingToolDepthMap[i +
j*roughingToolWidth]==0 && voxel[0]!=0){

                            voxel[0] = 0;

                        }

                    }

                }

            }


            imageData->GetPointData()->GetScalars()->Modified();

            imageData->Modified();

            actorTool->SetPosition(location[0], location[1], location[2]);

            vtkSmartPointer<vtkSmartVolumeMapper> mapper =
vtkSmartPointer<vtkSmartVolumeMapper>::New();

            mapper->SetInputData(imageData);

            volume->SetMapper(mapper);


            //ren->AddViewProp(volume);

            //iren->GetRenderWindow()->AddRenderer(ren);

            iren->GetRenderWindow()->Render();

         }

         else

        {

            double* location = toolLocations->GetPoint(0);

            actorTool->SetPosition(location[0], location[1], location[2]);

            iren->DestroyTimer(this->timerId);

//            cout << "Timer Destroyed: "
<<iren->DestroyTimer(this->timerId) << endl;

         }

    }


private:

    int TimerCount;

public:

    vtkActor* actorTool;

    vtkSmartPointer<vtkPoints> toolLocations;

    vtkSmartPointer<vtkImageData> imageData;

    unsigned int roughingToolWidth;

      int * roughingToolDepthMap;

     unsigned int height;

     vtkSmartPointer<vtkVolume> volume;

};



void MainWindow::on_toolPathSimulation_clicked()

{


    vtkSmartPointer <vtkSTLReader>reader = vtkSmartPointer
<vtkSTLReader>::New();

    reader->SetFileName("C://Users//Shaikh Shadab Anwar//Desktop//cutter.stl");

    reader->Update();


    //Render Tool to VTK Widget Window

    vtkSmartPointer <vtkPolyDataMapper> mapper = vtkSmartPointer
<vtkPolyDataMapper>::New();

    mapper->SetInputConnection(reader->GetOutputPort());

    actorTool = vtkSmartPointer <vtkActor>::New();

    actorTool->SetMapper(mapper);

    actorTool->GetProperty()->SetColor(0.752941, 0.752941, 0.752941); //(R,G,B)




    vtkRenderWindowInteractor* renderWindowInteractor =
ui->qvtkWidget->GetRenderWindow()->GetInteractor();

    renderWindowInteractor->SetRenderWindow(ui->qvtkWidget->GetRenderWindow());


    // Add the actor to the scene

    renderer->AddActor(actorTool);


    ui->qvtkWidget->GetRenderWindow()->Render();



    // Initialize must be called prior to creating timer events.

    renderWindowInteractor->Initialize();


   // ...doing something

              // inserting few test values


    for(int i =0;i!=240;i++)

    {

        toolPath.push_back({i,128,232});

    }



    // Create a vtkPoints object and store the points in it

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();

    for (size_t i =0; i < toolPath.size(); i++){

         double p[3] = {toolPath[i].x, toolPath[i].y, toolPath[i].z};

         points->InsertNextPoint(p);

         }


    // Sign up to receive TimerEvent

    vtkSmartPointer<vtkTimerCallback2> cb =

        vtkSmartPointer<vtkTimerCallback2>::New();

    cb->actorTool = actorTool;

    cb->imageData = imageData;

    cb->roughingToolDepthMap=roughingToolDepthMap;

    cb->roughingToolWidth=roughingToolWidth;

    cb->height=height;

    cb->volume = volume;

    cb->toolLocations = points;

    renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, cb);

    renderWindowInteractor->CreateRepeatingTimer(1);


}


On Fri, Jan 18, 2019 at 2:48 PM David Gobbi <david.gobbi at gmail.com> wrote:

> Hi Shadab,
>
> When you directly modify data in VTK, it is necessary to call Modified()
> on the data container or else the pipeline will not update.
>
> For vtkImageData, the voxel values are stored in an array that can be
> retrieved as follows:
>
>     data->GetPointData()->GetScalars()
>
> So when you modify voxels, you should also do the following:
>
>     data->GetPointData()->GetScalars()->Modified();
>     data->Modified();
>
> It's been a while since I've done anything similar myself, so there might
> be other Modifed() calls that are necessary.
>
> Cheers,
>    David
>
>
> On Fri, Jan 18, 2019 at 3:40 PM shadab anwar <sk.shadabanwar at gmail.com>
> wrote:
>
>> Hi everyone,
>>
>> I am working on creating an animation of a tool and a workpiece
>> (vtkimagedata). Where the tool should move to a  location and turnoff the
>> voxels (create a hole) at that location. I want to animate a dynamic scene.
>>
>> I am turning off voxels of workpiece (vtkimagedata) in " virtual void
>> Execute () "  function of  " class vtkTimerCallback"   but I am not able
>> to render the updated vtkimagedata.
>>
>> basically I am not able to see the hole at those positions.
>>
>> Any suggestions? directions? on how its done will be a great help.
>>
>> Thanks,
>> Shadab
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://vtk.org/pipermail/vtkusers/attachments/20190118/4f15d276/attachment-0001.html>


More information about the vtkusers mailing list