The article shows a way to enlarge a plane and keep its original position, scale it and translate it back.

#include <iostream>

#include <vtkSmartPointer.h>
#include <vtkProperty.h>
#include <vtkPolyData.h>
#include <vtkTriangleFilter.h>
#include <vtkRegularPolygonSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkImageActor.h>
#include <vtkImageViewer2.h>
#include <vtkPNGReader.h>
#include <vtkPlaneSource.h>
#include <vtkAxesActor.h>
#include <vtkTransform.h>

#include "tool.h"
using namespace std;

int main()
{
    setbuf( stdout, nullptr );
    PointStruct originPt( 0, 1, 0 );
    vtkSmartPointer<vtkPlaneSource> planeSource =
        vtkSmartPointer<vtkPlaneSource>::New();
    planeSource->SetCenter( 0, 0, 0 );
    planeSource->SetNormal( 0, 0, 1 );
    planeSource->Update();

    vtkSmartPointer<vtkPolyDataMapper> planeMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    planeMapper->SetInputData( planeSource->GetOutput() );
    planeMapper->Update();

    int scaleAccount = 5;
    vtkSmartPointer<vtkActor> planeActor =
        vtkSmartPointer<vtkActor>::New();
    planeActor->SetMapper( planeMapper );
    planeActor->SetPosition( originPt.point );

    vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New();
    trans->Translate( 0, 1, 0 );
    planeActor->SetUserTransform( trans );

    double *ptr = planeActor->GetCenter();
    printf( "ptr: (%lf, %lf, %lf)\n", ptr[0], ptr[1], ptr[2] );

    PointStruct oldPos = planeActor->GetCenter(); //PointStruct( planeActor->GetPosition() );
    //trans->TransformPoint( oldPos.point, oldPos.point );
    printf( "oldPos: (%lf, %lf, %lf)\n", oldPos[0], oldPos[1], oldPos[2] );

    trans->Scale( scaleAccount, scaleAccount, scaleAccount );
    trans->Update();

    planeActor->GetProperty()->SetColor( 0, 1, 1 );

    trans->Scale( scaleAccount, scaleAccount, scaleAccount );
    trans->Update();

    PointStruct newPos = planeActor->GetCenter(); //PointStruct( planeActor->GetPosition() );
    //trans->TransformPoint( newPos.point, newPos.point );
    printf( "newPos: (%lf, %lf, %lf)\n", newPos[0], newPos[1], newPos[2] );

    PointStruct vec( 1, 0, 0 );
    trans->TransformVector( vec.point, vec.point );
    printf( "vec: (%lf, %lf, %lf)\n", vec[0], vec[1], vec[2] );

    PointStruct tmp = (oldPos - newPos) / vec.Length(); //scaleAccount;
    trans->Translate( tmp.point );   // move in the opposite direction to keep the same center.
    trans->Update();

    ptr = planeActor->GetCenter();
    printf( "ptr: (%lf, %lf, %lf)\n", ptr[0], ptr[1], ptr[2] );

    vtkSmartPointer<vtkAxesActor> axesActor =
            vtkSmartPointer<vtkAxesActor>::New();

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor( planeActor );
    renderer->AddActor( axesActor );
    renderer->SetBackground( 0, 0, 0 );

    vtkSmartPointer<vtkRenderWindow> renderWindow =
            vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer( renderer );

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow( renderWindow );

    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();
    return 0;
}

output:

ptr: (0.000000, 2.000000, 0.000000)
oldPos: (0.000000, 2.000000, 0.000000)
newPos: (0.000000, 6.000000, 0.000000)
ptr: (0.000000, 2.000000, 0.000000)
Categories: VTK

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Content Summary
: Input your strings, the tool can get a brief summary of the content for you.

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