updating vtkimagedata in animation.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

updating vtkimagedata in animation.

shadzzz90
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


                                                                                                   



_______________________________________________
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:
https://vtk.org/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: updating vtkimagedata in animation.

David Gobbi
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 <[hidden email]> 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

_______________________________________________
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:
https://vtk.org/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: updating vtkimagedata in animation.

shadzzz90
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 <[hidden email]> 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 <[hidden email]> 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

_______________________________________________
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:
https://vtk.org/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: updating vtkimagedata in animation.

David Gobbi
On Fri, Jan 18, 2019 at 4:02 PM shadab anwar <[hidden email]> wrote:
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.

You shouldn't create a new mapper every time Execute() is called.  Just re-use the same mapper (and it's probably a good idea to call Modified() on it, as well).

  David 

_______________________________________________
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:
https://vtk.org/mailman/listinfo/vtkusers