[vtkusers] vtkImageResliceMapper problems

Ang ysa0829 at gmail.com
Thu Oct 22 22:48:13 EDT 2015


Hi David,

There is another issue. Below is my example code.

When I use move wheel to change slice plane, the image jumps a new position.


///////////////////////////////////////////////////////////////////////////////////////////////////////


class MouseInteractorStyle : public vtkInteractorStyleImage
{
public:
	vtkTypeMacro(MouseInteractorStyle, vtkInteractorStyleImage);
	static MouseInteractorStyle *New();
	vtkImageData *currentImgPtr;
	vtkImageSlice *currentImgSlicePtr;
	int sliceIndex = 0;
	double spacing[3];

	void setCurrentImageSlice(vtkImageSlice*slice)
	{
		currentImgSlicePtr = slice;
	}


	void setCurrentImageData(vtkImageData *img)
	{
		currentImgPtr = img;

		currentImgPtr->GetSpacing(spacing);
	}

	virtual void OnMouseWheelBackward()
	{
		if (sliceIndex <= currentImgPtr->GetExtent()[1]) sliceIndex++;
		double planeOrigin[3], planePt1[3], planePt2[3];

		planeOrigin[0] = currentImgPtr->GetBounds()[0] + sliceIndex * spacing[0];
		planeOrigin[1] = currentImgPtr->GetBounds()[2];
		planeOrigin[2] = currentImgPtr->GetBounds()[4];

		planePt1[0] = planeOrigin[0];
		planePt1[1] = planeOrigin[1] + currentImgPtr->GetBounds()[3];
		planePt1[2] = planeOrigin[2];

		planePt2[0] = planeOrigin[0];
		planePt2[1] = planeOrigin[1];
		planePt2[2] = planeOrigin[2] + currentImgPtr->GetBounds()[5];

		vtkSmartPointer<vtkPlaneSource> planeSource =
vtkSmartPointer<vtkPlaneSource>::New();
		
		planeSource->SetOrigin(planeOrigin);
		planeSource->SetPoint1(planePt1);
		planeSource->SetPoint2(planePt2);
		planeSource->Update();


	
reinterpret_cast<vtkImageResliceMapper*>(currentImgSlicePtr->GetMapper())->GetSlicePlane()->SetOrigin(planeSource->GetOrigin());
	
reinterpret_cast<vtkImageResliceMapper*>(currentImgSlicePtr->GetMapper())->GetSlicePlane()->SetNormal(planeSource->GetNormal());
		
		this->GetInteractor()->Render();
	}
	virtual void OnMouseWheelForward()
	{
		if (sliceIndex >=1) sliceIndex--;

		double planeOrigin[3], planePt1[3], planePt2[3];

		planeOrigin[0] = currentImgPtr->GetBounds()[0] + sliceIndex * spacing[0];
		planeOrigin[1] = currentImgPtr->GetBounds()[2];
		planeOrigin[2] = currentImgPtr->GetBounds()[4];

		planePt1[0] = planeOrigin[0];
		planePt1[1] = planeOrigin[1] + currentImgPtr->GetBounds()[3];
		planePt1[2] = planeOrigin[2];

		planePt2[0] = planeOrigin[0];
		planePt2[1] = planeOrigin[1];
		planePt2[2] = planeOrigin[2] + currentImgPtr->GetBounds()[5];

		vtkSmartPointer<vtkPlaneSource> planeSource =
vtkSmartPointer<vtkPlaneSource>::New();

		planeSource->SetOrigin(planeOrigin);
		planeSource->SetPoint1(planePt1);
		planeSource->SetPoint2(planePt2);
		planeSource->Update();


	
reinterpret_cast<vtkImageResliceMapper*>(currentImgSlicePtr->GetMapper())->GetSlicePlane()->SetOrigin(planeSource->GetOrigin());
	
reinterpret_cast<vtkImageResliceMapper*>(currentImgSlicePtr->GetMapper())->GetSlicePlane()->SetNormal(planeSource->GetNormal());
		this->GetInteractor()->Render();
	}

};
vtkStandardNewMacro(MouseInteractorStyle);

int main(int argc, char *argv[])
{

	MouseInteractorStyle *ff = MouseInteractorStyle::New();

	vtkRenderWindow *renWin = vtkRenderWindow::New();
	vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New();
	vtkRenderer *renderer = vtkRenderer::New();
	MouseInteractorStyle *styleImage = MouseInteractorStyle::New();
	styleImage->SetInteractionModeToImage2D();
	interactor->SetInteractorStyle(styleImage);
	renWin->SetInteractor(interactor);
	renWin->AddRenderer(renderer);
	renderer->SetBackground(1, 0, 0);


	vtkImageResliceMapper *resliceMapper = vtkImageResliceMapper::New();
	vtkImageSlice *imageSlice = vtkImageSlice::New();
	vtkImageData *img = vtkImageData::New();
	img->SetDimensions(50, 50, 50);
	img->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
	img->SetSpacing(1, 1, 1);

	for (int z = 0; z < img->GetDimensions()[2]; z++)
	{
		for (int x = 0; x < img->GetDimensions()[0]; x++)
		{
			for (int y = 0; y < img->GetDimensions()[1]; y++)
			{
				unsigned char *pixel = static_cast<unsigned
char*>(img->GetScalarPointer(x, y, z));

				int c0 = img->GetDimensions()[0] / 2;
				int c1 = img->GetDimensions()[1] / 2;
				int c2 = img->GetDimensions()[2] / 2;
				double pointX = (x - c0)*(x - c0);
				double pointY = (y - c1)*(y - c1);
				double pointZ = (z - c2)*(z - c2);
				if (pointX + pointY + pointZ < 15 * 15)
				{
					*pixel = 255;
				}
				else
				{
					*pixel = 0;
				}
			}
		}
	}
	resliceMapper->AutoAdjustImageQualityOff();
	imageSlice->SetMapper(resliceMapper);
	resliceMapper->SetInputData(img);
	//set reslice plane to sagittal 
	resliceMapper->GetSlicePlane()->SetNormal(1, 0, 0);
	resliceMapper->GetSlicePlane()->SetOrigin(25, 0, 0); // set plane to middle 

	//set camera to sagittal orientation 
	renderer->GetActiveCamera()->SetFocalPoint(0, 25, 25);
	renderer->GetActiveCamera()->SetPosition(700, 25, 25);
	renderer->GetActiveCamera()->SetViewUp(0, 0, 1);
	renderer->GetActiveCamera()->SetParallelScale(75);
	renderer->GetActiveCamera()->ParallelProjectionOn();

	styleImage->setCurrentImageData(img);
	styleImage->setCurrentImageSlice(imageSlice);

	
	renderer->AddActor(imageSlice);

	renWin->Render();
	interactor->Start();




	return 0;
}

















/////////////////////////////////////////////////////////////////////////////////////////////////////////



--
View this message in context: http://vtk.1045678.n5.nabble.com/vtkImageResliceMapper-problems-tp5734423p5734532.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list