[vtkusers] Problems updating filters on the fly using the vtkCommand::KeyPressEvent
Arne Hansen
bsd.diverse at gmail.com
Mon Jan 15 13:55:30 EST 2007
Hello again. I hope it is okay i send all these questions...this program is
causing me some problems. I have read on google and in the userguide, but i
was not able to find any explanation to how it is possible to put now data
into the contourfilter, and rerender this every time a certain key is
pressed. I managed to create a vtkCallbackcommand and us this. This works
fine, and i can write to the std::cout whenever a key is pressed and do some
action. I made a callbackfunction.
void callbackMethod(vtkObject *caller, unsigned long eventID, void*
clientData,void* data){...}
which is called everytime a key is pressed. Wonderful.
Now, what i want to do is to take my data, manipulate them everytime a key
is pressed, and then show these on the screen instead. But when i put the
new data into my objects, a pointer exception occurs (as always). I dont
understand why this occurs, and I hope somebody can help me. I have copied
my program, i know its quite a long code, but the reason for this is that i
have 12 different actors that i need to render, and a pipeline with
different data for each actor.
So my question is: How do i change the data of the contourfilter and
rerender it on the fly(while program is running): A very short pseudo code
of what i am doing is the following
1. load files
2. import files: binary->vnl->itk->vtk
3. find zerocrossings in data and create isosurfaces for rendering using
vtkContourFilter
4. Setup Commandobjekt for keyboardevents
5. render
6. onclick: use vnl to manipulate data from binary files (this creating
different volumes) to again import to vtk, use contourfilter and render
isosurface. <---This is the point going wrong. See function void
callbackMethod().
Thank you very much, and sorry for all the questions once again. I
appreciate your help.
Program listing:
#include <ctime>
#include<cstdio>
#include <fstream>
#include <vtkImageData.h>
#include "vtkImageImport.h" //Import images for VTK
#include "vtkActor.h"
#include "vtkOutlineFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkLight.h"
#include "vtkCamera.h"
#include "vtkContourFilter.h"
#include "vtkProperty.h"
#include "vtkMarchingCubes.h"
#include "vtkWindowedSincPolyDataFilter.h"
#include "vtkDataSetWriter.h"
#include "vtkCallbackCommand.h"
#include "vtkCommand.h"
#include "itkImportImageFilter.h"
#include "itkImage.h"
#include "itkImageRegionConstIterator.h"
#include "itkVTKImageExport.h"
#include "itkZeroCrossingImageFilter.h"
#include<map>
#include <blitz/Array.h>
#include <vnl/vnl_matrix.h>
#include <vnl/vnl_vector.h>
#include <vnl/vnl_identity.h>
typedef float SDMdatatype;
typedef unsigned short Binarytype;
typedef itk::Image<SDMdatatype,3> SDMimg;
typedef vnl_matrix<SDMdatatype> mat;
typedef itk::ImportImageFilter<SDMdatatype,3> ImportFilterType;
typedef itk::VTKImageExport<SDMimg> ImageExportType;
ImportFilterType::Pointer dataPointer2itk(SDMdatatype* data);
int D[]={110,129,97};
unsigned int N=D[0]*D[1]*D[2];
unsigned short m=12; //Shape count
unsigned short sm=7; //Significant modes chosen
mat meanShape(N*m,1);
mat prinComp(N*m,sm);
mat prinVal(sm,1);
struct commandObj{
ImportFilterType::Pointer importFilter1;
ImportFilterType::Pointer importFilter2;
ImportFilterType::Pointer importFilter3;
ImportFilterType::Pointer importFilter4;
ImportFilterType::Pointer importFilter5;
ImportFilterType::Pointer importFilter6;
ImportFilterType::Pointer importFilter7;
ImportFilterType::Pointer importFilter8;
ImportFilterType::Pointer importFilter9;
ImportFilterType::Pointer importFilter10;
ImportFilterType::Pointer importFilter11;
ImportFilterType::Pointer importFilter12;
};
void callbackMethod(vtkObject *caller, unsigned long eventID, void*
clientData,void* data){
vtkRenderWindowInteractor *iren =
reinterpret_cast<vtkRenderWindowInteractor*>(caller);
commandObj* cmdObj = reinterpret_cast<commandObj*> ( clientData ) ;
mat m1=meanShape.extract(N,1, 0*N,0);
mat m2=meanShape.extract(N,1, 1*N,0);
mat m3=meanShape.extract(N,1, 2*N,0);
mat m4=meanShape.extract(N,1, 3*N,0);
mat m5=meanShape.extract(N,1, 4*N,0);
mat m6=meanShape.extract(N,1, 5*N,0);
mat m7=meanShape.extract(N,1, 6*N,0);
mat m8=meanShape.extract(N,1, 7*N,0);
mat m9=meanShape.extract(N,1, 8*N,0);
mat m10=meanShape.extract(N,1, 9*N,0);
mat m11=meanShape.extract(N,1, 10*N,0);
mat m12=meanShape.extract(N,1, 11*N,0);
cmdObj->importFilter1->SetImportPointer(m1.data_block(),N,true);
cmdObj->importFilter2->SetImportPointer(m2.data_block(),N,true);
cmdObj->importFilter3->SetImportPointer(m3.data_block(),N,true);
cmdObj->importFilter4->SetImportPointer(m4.data_block(),N,true);
cmdObj->importFilter5->SetImportPointer(m5.data_block(),N,true);
cmdObj->importFilter6->SetImportPointer(m6.data_block(),N,true);
cmdObj->importFilter7->SetImportPointer(m7.data_block(),N,true);
cmdObj->importFilter8->SetImportPointer(m8.data_block(),N,true);
cmdObj->importFilter9->SetImportPointer(m9.data_block(),N,true);
cmdObj->importFilter10->SetImportPointer(m10.data_block(),N,true);
cmdObj->importFilter11->SetImportPointer(m11.data_block(),N,true);
cmdObj->importFilter12->SetImportPointer(m12.data_block(),N,true);
std::cout<<"rendering..."<<std::endl;
iren->Render();
std::cout<<"rendering done"<<std::endl;
}
/**
* This function will connect the given vtkImageExport filter to
* the given itk::VTKImageImport filter.
*/
//template <typename VTK_Exporter, typename ITK_Importer>
void ConnectPipelines(ImageExportType* exporter, vtkImageImport* importer)
{
importer->SetUpdateInformationCallback(exporter->GetUpdateInformationCallback());
importer->SetPipelineModifiedCallback(exporter->GetPipelineModifiedCallback());
importer->SetWholeExtentCallback(exporter->GetWholeExtentCallback());
importer->SetSpacingCallback(exporter->GetSpacingCallback());
importer->SetOriginCallback(exporter->GetOriginCallback());
importer->SetScalarTypeCallback(exporter->GetScalarTypeCallback());
importer->SetNumberOfComponentsCallback(exporter->GetNumberOfComponentsCallback());
importer->SetPropagateUpdateExtentCallback(exporter->GetPropagateUpdateExtentCallback());
importer->SetUpdateDataCallback(exporter->GetUpdateDataCallback());
importer->SetDataExtentCallback(exporter->GetDataExtentCallback());
importer->SetBufferPointerCallback(exporter->GetBufferPointerCallback());
importer->SetCallbackUserData(exporter->GetCallbackUserData());
}
SDMdatatype* readArrayFromFile(char* fn,long size){
std::ifstream file;
file.open(fn,std::ios::in|std::ios::binary);
if (!file) { printf("file was not opened, issue error msg"); }
SDMdatatype* buffer;
buffer = new SDMdatatype[size];
file.read(reinterpret_cast<char*>(buffer), sizeof (SDMdatatype) *
size);//static_cast <char *> buffer
file.close();
return buffer;
}
// Import data from a pointer into an itkImportFilter
ImportFilterType::Pointer dataPointer2itk(SDMdatatype* data){
ImportFilterType::Pointer importFilter = ImportFilterType::New();
ImportFilterType::SizeType size;
double origin[3];
double spacing[3];
size[0] = D[0]; // size along X
size[1] = D[1]; // size along Y
size[2] = D[2]; // size along Z
origin[0] = 0.0; // X coordinate
origin[1] = 0.0; // Y coordinate
origin[2] = 0.0; // Z coordinate
spacing[0] = 1.0; // along X direction
spacing[1] = 1.0; // along Y direction
spacing[2] = 1.0; // along Z direction
ImportFilterType::IndexType start;
start.Fill( 0 );
ImportFilterType::RegionType region;
region.SetIndex( start );
region.SetSize( size );
importFilter->SetRegion( region );
importFilter->SetOrigin( origin );
importFilter->SetSpacing( spacing );
importFilter->SetImportPointer(data,N,true);
return importFilter;
}
void loadPCAResults(){
SDMdatatype* b1=readArrayFromFile(
"C:\\thesisIntermediate\\meanshape.dat",
N*m);
meanShape.copy_in(b1);
delete b1;
SDMdatatype* b2=readArrayFromFile(
"C:\\thesisIntermediate\\prinComp.dat",
N*m*sm);
prinComp.copy_in(b2);
delete b2;
SDMdatatype* b3=readArrayFromFile(
"C:\\thesisIntermediate\\prinVal.dat",
sm);
prinVal.copy_in(b3);
delete b3;
}
mat ColVecElemWiseMult(mat A,SDMdatatype* B){
for(short i=0;i<A.size();i++) A(i,0)=B[i]*A(i,0);
return A;
}
int main(int argc,char* argv[])
{
double elapsedTime;
clock_t begin, end;
begin = clock();
loadPCAResults();
end = clock();
begin = clock();
//mat A(3,3);
//meanShape.set_identity();
//A*=(A*2);
//A*=(A*2);
//meanShape=meanShape.e*meanShape;
//std::cout<<meanShape<<std::endl;
//meanShape+prinComp*(prinVal*)
// Get vols from SDM
//extract(højde,bredde,top,left)
mat m1=meanShape.extract(N,1, 0*N,0);
mat m2=meanShape.extract(N,1, 1*N,0);
mat m3=meanShape.extract(N,1, 2*N,0);
mat m4=meanShape.extract(N,1, 3*N,0);
mat m5=meanShape.extract(N,1, 4*N,0);
mat m6=meanShape.extract(N,1, 5*N,0);
mat m7=meanShape.extract(N,1, 6*N,0);
mat m8=meanShape.extract(N,1, 7*N,0);
mat m9=meanShape.extract(N,1, 8*N,0);
mat m10=meanShape.extract(N,1, 9*N,0);
mat m11=meanShape.extract(N,1, 10*N,0);
mat m12=meanShape.extract(N,1, 11*N,0);
//////////////////////////////////////////////////////////////////////////////////
// Create objects for use //
//////////////////////////////////////////////////////////////////////////////////
//structure actors
vtkActor *Temporopolar_region_left_actor = vtkActor::New();
vtkActor *Temporopolar_region_right_actor = vtkActor::New();
vtkActor *Entorhinal_region_left_actor = vtkActor::New();
vtkActor *Entorhinal_region_right_actor = vtkActor::New();
vtkActor *Perirhinal_region_left_actor = vtkActor::New();
vtkActor *Perirhinal_region_right_actor = vtkActor::New();
vtkActor *Parahippocampal_region_left_actor = vtkActor::New();
vtkActor *Parahippocampal_region_right_actor = vtkActor::New();
vtkActor *Hippocampal_region_left_actor = vtkActor::New();
vtkActor *Hippocampal_region_right_actor = vtkActor::New();
vtkActor *Amygdalar_region_left_actor = vtkActor::New();
vtkActor *Amygdalar_region_right_actor = vtkActor::New();
//Bounding box
vtkOutlineFilter *outlineData = vtkOutlineFilter::New();
vtkPolyDataMapper *mapOutline = vtkPolyDataMapper::New();
vtkActor *outline = vtkActor::New();
//rendering objects
vtkRenderer *aRenderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
vtkCamera *aCamera = vtkCamera::New();
ImportFilterType::Pointer importFilter1=dataPointer2itk(m1.data_block());
ImportFilterType::Pointer importFilter2=dataPointer2itk(m2.data_block());
ImportFilterType::Pointer importFilter3=dataPointer2itk(m3.data_block());
ImportFilterType::Pointer importFilter4=dataPointer2itk(m4.data_block());
ImportFilterType::Pointer importFilter5=dataPointer2itk(m5.data_block());
ImportFilterType::Pointer importFilter6=dataPointer2itk(m6.data_block());
ImportFilterType::Pointer importFilter7=dataPointer2itk(m7.data_block());
ImportFilterType::Pointer importFilter8=dataPointer2itk(m8.data_block());
ImportFilterType::Pointer importFilter9=dataPointer2itk(m9.data_block());
ImportFilterType::Pointer importFilter10=dataPointer2itk(m10.data_block());
ImportFilterType::Pointer importFilter11=dataPointer2itk(m11.data_block());
ImportFilterType::Pointer importFilter12=dataPointer2itk(m12.data_block());
vtkImageImport* vtkImporter1 = vtkImageImport::New();
vtkImageImport* vtkImporter2 = vtkImageImport::New();
vtkImageImport* vtkImporter3 = vtkImageImport::New();
vtkImageImport* vtkImporter4 = vtkImageImport::New();
vtkImageImport* vtkImporter5 = vtkImageImport::New();
vtkImageImport* vtkImporter6 = vtkImageImport::New();
vtkImageImport* vtkImporter7 = vtkImageImport::New();
vtkImageImport* vtkImporter8 = vtkImageImport::New();
vtkImageImport* vtkImporter9 = vtkImageImport::New();
vtkImageImport* vtkImporter10 = vtkImageImport::New();
vtkImageImport* vtkImporter11 = vtkImageImport::New();
vtkImageImport* vtkImporter12 = vtkImageImport::New();
ImageExportType::Pointer itkExporter1 = ImageExportType::New();
ImageExportType::Pointer itkExporter2 = ImageExportType::New();
ImageExportType::Pointer itkExporter3 = ImageExportType::New();
ImageExportType::Pointer itkExporter4 = ImageExportType::New();
ImageExportType::Pointer itkExporter5 = ImageExportType::New();
ImageExportType::Pointer itkExporter6 = ImageExportType::New();
ImageExportType::Pointer itkExporter7 = ImageExportType::New();
ImageExportType::Pointer itkExporter8 = ImageExportType::New();
ImageExportType::Pointer itkExporter9 = ImageExportType::New();
ImageExportType::Pointer itkExporter10 = ImageExportType::New();
ImageExportType::Pointer itkExporter11 = ImageExportType::New();
ImageExportType::Pointer itkExporter12 = ImageExportType::New();
itkExporter1->SetInput(importFilter1->GetOutput());
itkExporter2->SetInput(importFilter2->GetOutput());
itkExporter3->SetInput(importFilter3->GetOutput());
itkExporter4->SetInput(importFilter4->GetOutput());
itkExporter5->SetInput(importFilter5->GetOutput());
itkExporter6->SetInput(importFilter6->GetOutput());
itkExporter7->SetInput(importFilter7->GetOutput());
itkExporter8->SetInput(importFilter8->GetOutput());
itkExporter9->SetInput(importFilter9->GetOutput());
itkExporter10->SetInput(importFilter10->GetOutput());
itkExporter11->SetInput(importFilter11->GetOutput());
itkExporter12->SetInput(importFilter12->GetOutput());
ConnectPipelines(itkExporter1, vtkImporter1);
ConnectPipelines(itkExporter2, vtkImporter2);
ConnectPipelines(itkExporter3, vtkImporter3);
ConnectPipelines(itkExporter4, vtkImporter4);
ConnectPipelines(itkExporter5, vtkImporter5);
ConnectPipelines(itkExporter6, vtkImporter6);
ConnectPipelines(itkExporter7, vtkImporter7);
ConnectPipelines(itkExporter8, vtkImporter8);
ConnectPipelines(itkExporter9, vtkImporter9);
ConnectPipelines(itkExporter10, vtkImporter10);
ConnectPipelines(itkExporter11, vtkImporter11);
ConnectPipelines(itkExporter12, vtkImporter12);
vtkPolyDataMapper *polyMapper1 = vtkPolyDataMapper::New();
vtkPolyDataMapper *polyMapper2 = vtkPolyDataMapper::New();
vtkPolyDataMapper *polyMapper3 = vtkPolyDataMapper::New();
vtkPolyDataMapper *polyMapper4 = vtkPolyDataMapper::New();
vtkPolyDataMapper *polyMapper5 = vtkPolyDataMapper::New();
vtkPolyDataMapper *polyMapper6 = vtkPolyDataMapper::New();
vtkPolyDataMapper *polyMapper7 = vtkPolyDataMapper::New();
vtkPolyDataMapper *polyMapper8 = vtkPolyDataMapper::New();
vtkPolyDataMapper *polyMapper9 = vtkPolyDataMapper::New();
vtkPolyDataMapper *polyMapper10 = vtkPolyDataMapper::New();
vtkPolyDataMapper *polyMapper11 = vtkPolyDataMapper::New();
vtkPolyDataMapper *polyMapper12 = vtkPolyDataMapper::New();
vtkContourFilter *contours1 = vtkContourFilter::New();
vtkContourFilter *contours2 = vtkContourFilter::New();
vtkContourFilter *contours3 = vtkContourFilter::New();
vtkContourFilter *contours4 = vtkContourFilter::New();
vtkContourFilter *contours5 = vtkContourFilter::New();
vtkContourFilter *contours6 = vtkContourFilter::New();
vtkContourFilter *contours7 = vtkContourFilter::New();
vtkContourFilter *contours8 = vtkContourFilter::New();
vtkContourFilter *contours9 = vtkContourFilter::New();
vtkContourFilter *contours10 = vtkContourFilter::New();
vtkContourFilter *contours11 = vtkContourFilter::New();
vtkContourFilter *contours12 = vtkContourFilter::New();
outlineData->SetInput(vtkImporter1->GetOutput());
mapOutline->SetInput(outlineData->GetOutput());
outline->SetMapper(mapOutline);
outline->GetProperty()->SetColor(0.,0.,1.);
contours1->SetInput(vtkImporter1->GetOutput());
contours2->SetInput(vtkImporter2->GetOutput());
contours3->SetInput(vtkImporter3->GetOutput());
contours4->SetInput(vtkImporter4->GetOutput());
contours5->SetInput(vtkImporter5->GetOutput());
contours6->SetInput(vtkImporter6->GetOutput());
contours7->SetInput(vtkImporter7->GetOutput());
contours8->SetInput(vtkImporter8->GetOutput());
contours9->SetInput(vtkImporter9->GetOutput());
contours10->SetInput(vtkImporter10->GetOutput());
contours11->SetInput(vtkImporter11->GetOutput());
contours12->SetInput(vtkImporter12->GetOutput());
contours1->SetValue(0,0);
contours2->SetValue(0,0);
contours3->SetValue(0,0);
contours4->SetValue(0,0);
contours5->SetValue(0,0);
contours6->SetValue(0,0);
contours7->SetValue(0,0);
contours8->SetValue(0,0);
contours9->SetValue(0,0);
contours10->SetValue(0,0);
contours11->SetValue(0,0);
contours12->SetValue(0,0);
polyMapper1->SetInput(contours1->GetOutput());
polyMapper2->SetInput(contours2->GetOutput());
polyMapper3->SetInput(contours3->GetOutput());
polyMapper4->SetInput(contours4->GetOutput());
polyMapper5->SetInput(contours5->GetOutput());
polyMapper6->SetInput(contours6->GetOutput());
polyMapper7->SetInput(contours7->GetOutput());
polyMapper8->SetInput(contours8->GetOutput());
polyMapper9->SetInput(contours9->GetOutput());
polyMapper10->SetInput(contours10->GetOutput());
polyMapper11->SetInput(contours11->GetOutput());
polyMapper12->SetInput(contours12->GetOutput());
polyMapper1->ScalarVisibilityOff();
polyMapper2->ScalarVisibilityOff();
polyMapper3->ScalarVisibilityOff();
polyMapper4->ScalarVisibilityOff();
polyMapper5->ScalarVisibilityOff();
polyMapper6->ScalarVisibilityOff();
polyMapper7->ScalarVisibilityOff();
polyMapper8->ScalarVisibilityOff();
polyMapper9->ScalarVisibilityOff();
polyMapper10->ScalarVisibilityOff();
polyMapper11->ScalarVisibilityOff();
polyMapper12->ScalarVisibilityOff();
Temporopolar_region_left_actor->SetMapper(polyMapper1);
Temporopolar_region_right_actor->SetMapper(polyMapper2);
Entorhinal_region_left_actor->SetMapper(polyMapper3);
Entorhinal_region_right_actor->SetMapper(polyMapper4);
Perirhinal_region_left_actor->SetMapper(polyMapper5);
Perirhinal_region_right_actor->SetMapper(polyMapper6);
Parahippocampal_region_left_actor->SetMapper(polyMapper7);
Parahippocampal_region_right_actor->SetMapper(polyMapper8);
Hippocampal_region_left_actor->SetMapper(polyMapper9);
Hippocampal_region_right_actor->SetMapper(polyMapper10);
Amygdalar_region_left_actor->SetMapper(polyMapper11);
Amygdalar_region_right_actor->SetMapper(polyMapper12);
Temporopolar_region_left_actor->GetProperty()-> SetColor(1,0,0);
Temporopolar_region_right_actor->GetProperty()-> SetColor(1,0,0);
Entorhinal_region_left_actor->GetProperty()-> SetColor(0,1,0);
Entorhinal_region_right_actor->GetProperty()-> SetColor(0,1,0);
Perirhinal_region_left_actor->GetProperty()-> SetColor(0,0,1);
Perirhinal_region_right_actor->GetProperty()-> SetColor(0,0,1);
Parahippocampal_region_left_actor->GetProperty()-> SetColor(1,0,1);
Parahippocampal_region_right_actor->GetProperty()-> SetColor(1,0,1);
Hippocampal_region_left_actor->GetProperty()-> SetColor(0,1,1);
Hippocampal_region_right_actor->GetProperty()-> SetColor(0,1,1);
Amygdalar_region_left_actor->GetProperty()-> SetColor(1,1,0);
Amygdalar_region_right_actor->GetProperty()-> SetColor(1,1,0);
vtkCallbackCommand *callback = vtkCallbackCommand::New();
callback->SetCallback(callbackMethod);
commandObj* cmdObj=new commandObj;
callback->SetClientData(cmdObj);
iren->AddObserver(vtkCommand::KeyPressEvent,callback);
// Actors are added to the renderer.
//aRenderer->AddActor(outline);
aRenderer->AddActor(Temporopolar_region_left_actor);
aRenderer->AddActor(Temporopolar_region_right_actor);
aRenderer->AddActor(Entorhinal_region_left_actor);
aRenderer->AddActor(Entorhinal_region_right_actor);
aRenderer->AddActor(Perirhinal_region_left_actor);
aRenderer->AddActor(Perirhinal_region_right_actor);
aRenderer->AddActor(Parahippocampal_region_left_actor);
aRenderer->AddActor(Parahippocampal_region_right_actor);
aRenderer->AddActor(Hippocampal_region_left_actor);
aRenderer->AddActor(Hippocampal_region_right_actor);
aRenderer->AddActor(Amygdalar_region_left_actor);
aRenderer->AddActor(Amygdalar_region_right_actor);
aRenderer->AddActor(outline);
aRenderer->SetBackground(0,0,0);
iren->SetRenderWindow(renWin);
renWin->AddRenderer(aRenderer);
aRenderer->SetActiveCamera(aCamera);
// Setting the color and size of the renderwindow,
// initializing the camera position
aCamera->SetViewUp(0, 0, -1);
aCamera->SetPosition(-150, -150, -150);
aCamera->SetFocalPoint(0, 0, 0);
aCamera->ComputeViewPlaneNormal();
aRenderer->ResetCamera();
aRenderer->ResetCameraClippingRange();
aRenderer->SetBackground(0.2, 0.2, 0.2);
renWin->SetSize(800, 600);
// Here we go!
iren->Initialize();
renWin->Render();
iren->Start();
end = clock();
elapsedTime = static_cast<double>(end - begin) /
CLOCKS_PER_SEC;std::cout<<elapsedTime<<std::endl;
//////////////////////////////////////////////////////////////////////////////////
// CLEAN UP //
//////////////////////////////////////////////////////////////////////////////////
aCamera->Delete();
aRenderer->Delete();
renWin->Delete();
iren->Delete();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070115/98b450dd/attachment.htm>
More information about the vtkusers
mailing list