[vtkusers] vtkImage to Cube

"André Rohde" Rohy at gmx.de
Fri Oct 16 11:03:04 EDT 2009


Hi,
i´ve got the following problem:

I´d like to make a fixed amount of 2D pictures and add them to a 3D cube.
My image data is described by a float greyscale array with the dimensions 1392*1040. The image is shown correctly in the vtkViewer but the cube structure doesn´t show the imagedata.

Yours faithfully,
Andy

My Sourcecode:

//Create vtkFloatArray and assign ImageData from the Camera
//Picture information: width:1392 - height:1040 - color: greyscale
vtkFloatArray *vtkArray = vtkFloatArray::New();
vtkArray->SetNumberOfComponents(1);
vtkArray->SetNumberOfTuples(1392*1040);
	
unsigned indexInverted = 0;
	
for(int k = 0; k < 1040; k++)
{
	for(int l = 0; l < 1392; l++)
	{
		//pcFrame contains the ImageData from the Camera
		vtkArray->SetTuple1(indexInverted,pcFrame[((1392*1040)-(1392*(k+1)))+l]);
		indexInverted++;
	}
}

//Create vtkImageData from vtkArray
vtkImageData *myImage = vtkImageData::New();
myImage->SetDimensions(1392,1040,1);
myImage->SetSpacing(1,1,1);
myImage->SetOrigin(0,0,0);
myImage->SetNumberOfScalarComponents(1);
myImage->SetScalarTypeToFloat();
myImage->AllocateScalars();
myImage->GetPointData()->SetScalars(vtkArray);

//Create vtkImageViewer, shows the correct image at program start
vtkImageViewer *viewer = vtkImageViewer::New();
viewer->SetColorLevel(0.0);
viewer->SetColorWindow(250);
viewer->SetInput(myImage);
viewer->Render();

//Define cube structure
vtkPolyData *cube = vtkPolyData::New();
vtkPoints *points = vtkPoints::New();
vtkCellArray *polys = vtkCellArray::New();
vtkFloatArray *scalars = vtkFloatArray::New();

static float x[8][3]={{0,0,0}, {1392,0,0}, {1392,1040,0}, {0,1040,0},
	{0,0,0}, {1392,0,0}, {1392,1040,0}, {0,1040,0}};
static vtkIdType pts[6][4]={{0,1,2,3}, {4,5,6,7}, {0,1,5,4},
	{1,2,6,5}, {2,3,7,6}, {3,0,4,7}};

//Create points
for(int i=0;i<8;i++)
{
	points->InsertPoint(i,x[i]);
}

//Create cells
for(int i=0;i<6;i++)
{
	polys->InsertNextCell(4,pts[i]);
}

//Create scalars
for(int i=0;i<1040;i++)
{
	for(int j=0;j<1392;j++)
	{
		scalars->InsertTuple1(i,(double)*vtkArray->GetPointer(i));
	}
}

//greyscale table
vtkLookupTable *table = vtkLookupTable::New();
table->SetHueRange(0.0,0.0);
table->SetSaturationRange(0.0,0.0);
table->SetValueRange(0.0,255.0);
table->Build();

cube->SetPoints(points);
cube->SetPolys(polys);
cube->GetPointData()->SetScalars(scalars);

vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();
mapper->SetLookupTable(table);
mapper->SetInput(cube);

//Create an actor.
vtkActor* actor = vtkActor::New();
actor->SetMapper(mapper);

//The usual rendering stuff.
vtkCamera *camera = vtkCamera::New();
camera->SetPosition(1,1,1);
camera->SetFocalPoint(0,0,0);

vtkRenderer *renderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(renderer);

vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
vtkWidget->SetRenderWindow(renWin);

renderer->AddActor(actor);

renderer->SetActiveCamera(camera);
renderer->ResetCamera();
renderer->SetBackground(0.4,0.5,0.6);

//interact with data
//renWin->SetSize(300,300);
renWin->Render();
iren->Start();
-- 
Neu: GMX DSL bis 50.000 kBit/s und 200,- Euro Startguthaben!
http://portal.gmx.net/de/go/dsl02



More information about the vtkusers mailing list