The article shows a few tips in VTK development. There are simple tasks for most developers but the functions can be used in many situations.

### Convert the length value in the display coordinate system to the new value in the world coordinate system.

``````double DisplayLenToWorldLen(const double &length)
{
Point p0( 0, 0, 0 );
vtkRenderer *renderer = GetVtkRenderer();
renderer->SetDisplayPoint( p0.point );
renderer->DisplayToWorld();
double tmp;
renderer->GetWorldPoint( tmp );
Point worldP0( tmp, tmp, tmp );

Point p1( length, 0, 0 );
renderer->SetDisplayPoint( p1.point );
renderer->DisplayToWorld();
renderer->GetWorldPoint( tmp );
Point worldP1( tmp, tmp, tmp );

return (worldP0 - worldP1).Length();
}
``````

### Rotate a vector to the target direction.

``````    // Rotate Y to be plane normal
PointStruct planeNormal = GetPlaneNormal();
planeNormal.Unit();
PointStruct YDir( 0, 1, 0 );
PointStruct rotateAxis = YDir ^ planeNormal;
double radians = vtkMath::AngleBetweenVectors( YDir.point, planeNormal.point );
userTransform->Translate( center.point );  // center is our rotate center.
userTransform->RotateWXYZ( angle, rotateAxis.point );
userTransform->Translate( (-center).point );
userTransform->Update();
``````

### Show a sphere at the special position. It’s often used as a debug function.

``````void ShowPoint( Point pt, vtkRenderer *renderer )
{
vtkSmartPointer<vtkSphereSource> sphere =
vtkSmartPointer<vtkSphereSource>::New();
sphere->SetThetaResolution( 100 );
sphere->SetPhiResolution( 50 );
sphere->SetCenter( pt.point );
sphere->Update();

vtkSmartPointer<vtkPolyDataMapper> sphereMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputData( sphere->GetOutput() );

vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper( sphereMapper );
actor->GetProperty()->SetColor(0, 1, 1);

}
``````
Categories: VTK

Article Rating
Subscribe
Notify of 