Oringial scene:

The right polyData has shallowCopyed the left one.

If A had ShallowCopyed B in VTK project, A and B were both vtkPolyData objects, let’s change A’s point or change its pointData scalar, B’s point and scalar will also be changed.

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkConeSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkActor2D.h>
#include <vtkLineSource.h>
#include <vtkAppendPolyData.h>
#include <vtkPointData.h>
#include <vtkFloatArray.h>
#include <vtkQuadricDecimation.h>
#include <vtkTriangleFilter.h>
#include <vtkPlaneSource.h>

using namespace std;

int main()
{
    vtkPolyData *leftPolydata = vtkPolyData::New();
    vtkPolyData *rightPolydata = vtkPolyData::New();

    int i;
    static float x[8][3]={{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0},
                        {0,0,1}, {1,0,1}, {1,1,1}, {0,1,1}};
    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}};

    vtkPoints *points = vtkPoints::New();
    vtkCellArray *polys = vtkCellArray::New();
    vtkFloatArray *scalars = vtkFloatArray::New();

    // Load the point, cell, and data attributes.
    for (i=0; i<8; i++) points->InsertPoint(i,x[i]);
    for (i=0; i<6; i++) polys->InsertNextCell(4,pts[i]);
    for (i=0; i<8; i++) scalars->InsertTuple1(i,i);

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

    rightPolydata->ShallowCopy( leftPolydata );
    vtkFloatArray *scalars1 = vtkFloatArray::New();
    for (i=0; i<8; i++) scalars1->InsertTuple1(i,0);
    //rightPolydata->GetPointData()->SetScalars( scalars1 );
    rightPolydata->GetPointData()->GetScalars()->SetTuple1( 0, 1 );
    rightPolydata->GetPoints()->SetPoint( 0, 0, 0, 2 );

    vtkSmartPointer<vtkPolyDataMapper> leftMapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    leftMapper->SetInputData( leftPolydata );

    vtkSmartPointer<vtkActor> leftActor =
            vtkSmartPointer<vtkActor>::New();
    leftActor->SetMapper( leftMapper );

    // Define viewport ranges
    // (xmin, ymin, xmax, ymax) left-top and right-bottom point
    double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
    double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};

    // Setup renderers
    vtkSmartPointer<vtkRenderer> leftRenderer =
        vtkSmartPointer<vtkRenderer>::New();
    leftRenderer->SetViewport(leftViewport);
    leftRenderer->SetBackground(0, 0, 0);

    vtkSmartPointer<vtkRenderer> rightRenderer =
        vtkSmartPointer<vtkRenderer>::New();
    rightRenderer->SetViewport(rightViewport);
    rightRenderer->SetBackground(0, 0, 0);

    leftRenderer->AddActor( leftActor );

    vtkSmartPointer<vtkPolyDataMapper> rightMapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    rightMapper->SetInputData( rightPolydata );
    rightPolydata->FastDelete();
    vtkSmartPointer<vtkActor> rightActor =
            vtkSmartPointer<vtkActor>::New();
    rightActor->SetMapper( rightMapper );

    rightRenderer->AddActor( rightActor );

    vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
    leftRenderer->SetActiveCamera( camera );
    rightRenderer->SetActiveCamera( camera );

    leftRenderer->ResetCamera();
    rightRenderer->ResetCamera();

    vtkSmartPointer<vtkRenderWindow> renderWindow =
            vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->SetSize(600, 300);
    renderWindow->AddRenderer( leftRenderer );
    renderWindow->AddRenderer( rightRenderer );

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

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

If we use `rightPolydata->GetPointData()->SetScalars( scalars1 );` to change scalars, left polyData will not be affected. Because SetScalars, shallowCopy and DeepCopy are similar to replace original data.

Categories: VTK

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

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