VTK – What’s difference between deep copy and shallow copy in vtkPolyData ?

VTK – What’s difference between deep copy and shallow copy in vtkPolyData ?

What’s the difference between deep copy and shallow copy in vtkPolyData?
We can find an answer after reading the two interface’s implementation.
ShallowCopy just changes the pointer pointing, two vtkPolydata objects share the individual elements.
DeepCopy makes the current polydata have own points and cells, it copies every element.

// An highlighted block
void vtkPolyData::ShallowCopy(vtkDataObject *dataObject)
{
  vtkPolyData *polyData = vtkPolyData::SafeDownCast(dataObject);
  if (this == polyData)
     return;

  if ( polyData != nullptr )
  {
    this->SetVerts(polyData->GetVerts());
    this->SetLines(polyData->GetLines());
    this->SetPolys(polyData->GetPolys());
    this->SetStrips(polyData->GetStrips());

    // I do not know if this is correct but.
    if (this->Cells)
    {
      this->Cells->UnRegister(this);
    }
    this->Cells = polyData->Cells;
    if (this->Cells)
    {
      this->Cells->Register(this);
    }

    if (this->Links)
    {
      this->Links->Delete();
    }
    this->Links = polyData->Links;
    if (this->Links)
    {
      this->Links->Register(this);
    }
  }

  // Do superclass
  this->vtkPointSet::ShallowCopy(dataObject);
}

//----------------------------------------------------------------------------
void vtkPolyData::DeepCopy(vtkDataObject *dataObject)
{
  // Do superclass
  // We have to do this BEFORE we call BuildLinks, else there are no points
  // to build the links on (the parent DeepCopy copies the points)
  this->vtkPointSet::DeepCopy(dataObject);

  vtkPolyData *polyData = vtkPolyData::SafeDownCast(dataObject);

  if ( polyData != nullptr )
  {
    vtkCellArray *ca;
    ca = vtkCellArray::New();
    ca->DeepCopy(polyData->GetVerts());
    this->SetVerts(ca);
    ca->Delete();

    ca = vtkCellArray::New();
    ca->DeepCopy(polyData->GetLines());
    this->SetLines(ca);
    ca->Delete();

    ca = vtkCellArray::New();
    ca->DeepCopy(polyData->GetPolys());
    this->SetPolys(ca);
    ca->Delete();

    ca = vtkCellArray::New();
    ca->DeepCopy(polyData->GetStrips());
    this->SetStrips(ca);
    ca->Delete();

    if ( this->Cells )
    {
      this->Cells->UnRegister(this);
      this->Cells = nullptr;
    }
    if (polyData->Cells)
    {
      this->BuildCells();
    }

    if ( this->Links )
    {
      this->Links->UnRegister(this);
      this->Links = nullptr;
    }
    if (polyData->Links)
    {
      this->BuildLinks();
    }
  }
}

main.cpp

#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 );

    scalars->Initialize();
    for (i=0; i<8; i++) scalars->InsertTuple1(i,0);
    leftPolydata->GetPointData()->SetScalars( scalars );
    leftPolydata->Modified();

    points->Delete();
    polys->Delete();
    scalars->Delete();

    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 change the scalar for left polydata, right polydata is also changed because it is created by ShallowCopy.
如果我们更改左polydata的标量,右polydata也会更改,因为它是由ShallowCopy创建的。
在这里插入图片描述
We can see the different results if the right polydata is created by DeepCopy.

//rightPolydata->ShallowCopy( leftPolydata );
rightPolydata->DeepCopy( leftPolydata );

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VTKvtkPolyDataVTK中的一个重要接口,它用于表示和操作多边形数据。vtkPolyData的数据结构包含了顶点、线段、多边形等几何元素,并且支持属性数据的附加。通过vtkPolyData接口,我们可以进行各种几何操作,如创建、修改、查询和可视化多边形数据。 引用提到了vtkPolyData的重要性,掌握了vtkPolyData的数据结构和相关方法可以更好地理解VTK。这个接口的方法包括添加几何元素、修改属性数据、进行几何操作等,可以通过官方文档或相关教程学习和了解。 如果你想将vtkUnstructuredGrid转换为vtkPolyData,可以参考引用中提供的方法。这个过程涉及重新构建输入vtkPolyData数据的Mesh数据,具体的步骤可以在文章中找到。 另外,引用提供了一篇介绍VTK的文章,可以作为参考。VTK是一个开源的免费软件系统,主要用于三维计算机图形学、图像处理和可视化。它的内核是用C构建的,支持多种编程语言的调用,如Java、Tcl/Tk和Python。 总之,VTKvtkPolyData是用于表示和操作多边形数据的接口,可以通过学习相关文档和教程来深入了解其数据结构和方法。同时,可以通过转换方法将其他数据类型转换为vtkPolyData进行处理和可视化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [VTK-vtkPolyData解读](https://blog.csdn.net/qq_40041064/article/details/127959529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [VTK基础教程(1)- vtkPolyData 介绍](https://blog.csdn.net/qq_35769071/article/details/122671756)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值