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[4];
    renderer->GetWorldPoint( tmp );
    Point worldP0( tmp[0], tmp[1], tmp[2] );

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

    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 );
    double angle = vtkMath::DegreesFromRadians( radians );
    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->SetRadius( 1 );
    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);

    renderer->AddActor( actor );
}
Categories: VTK

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

XML To JSON
: Input your strings, the tool can convert XML to JSON for you.

X
0
Would love your thoughts, please comment.x
()
x