ActiViz学习点滴(九)——显示直线(两种方式)、矩形、网格

在之前的程序中加入直线显示程序段

            vtkLineSource lineSource = vtkLineSource.New();
            //vtkLineSource-创建由两个端点定义的线
            //vtkLineSource是创建由两个端点定义的多段线的源对象。
            //组成多段线的线段数通过设置对象分辨率来控制。

            lineSource.SetPoint1(0, 4, 0);
            lineSource.SetPoint2(0, 0, 3);
            //设置两个端点

            vtkPolyDataMapper lineMapper = vtkPolyDataMapper.New();
            lineMapper.SetInputConnection(lineSource.GetOutputPort());

            vtkActor lineActor = vtkActor.New();
            lineActor.SetMapper(lineMapper);
            lineActor.GetProperty().SetColor(0.1, 0.2, 0.3);

            VTKrenderer.AddActor(lineActor);

程序运行结果如下:

更新日期:2021-10-22

第二种方式

还可以通过vtkcellArray类的使用,以点建立线。程序如下:

            vtkPoints points = vtkPoints.New();
            points.InsertPoint(0, 4, 0, 0);
            points.InsertPoint(1, 0, 4, 0);

            //直线的两个端点

            vtkCellArray lines = vtkCellArray.New();
            //vtkCellArray-表示单元连接的对象
            //vtkCellArray是一个明确表示单元连接的支持对象。
            //单元数组结构是以下形式的原始整数列表:

            //(n,id1,id2,…,idn,n,id1,id2,…,idn,…),
            //其中n是单元中的点数,id是关联点列表的索引。
            //这种数据结构的优点是紧凑、简单,并且易于与外部数据接口。
            //然而,它完全不适合随机访问。

            //此功能(必要时)通过使用vtkCellTypes和vtkCellLinks对象

            //来扩展数据结构的定义来实现。
            lines.InsertNextCell(2);
            lines.InsertCellPoint(0);
            lines.InsertCellPoint(1);

            vtkPolyData polyData = vtkPolyData.New();

            polyData.SetPoints(points);
            //指定点阵列以定义点坐标。
            polyData.SetLines(lines);
            //设置单元格数组以定义行。

            vtkPolyDataMapper polyMapper = vtkPolyDataMapper.New();
            polyMapper.SetInput(polyData);

            vtkActor polyActor = new vtkActor();
            polyActor.SetMapper(polyMapper);
            polyActor.GetProperty().SetLineWidth(2);
            polyActor.GetProperty().SetColor(0, 0, 0);

            VTKrenderer.AddActor(polyActor);

程序运行效果如图

 使用vtkcellArray类建立矩形

首先添加矩形的4个顶点

            vtkPoints points = vtkPoints.New();
            points.InsertPoint(0, 4, 0, 0);
            points.InsertPoint(1, 12, 5, 0);
            points.InsertPoint(2, 0, 4, 0);
            points.InsertPoint(3, 8, 9, 0);

添加矩形的两个长边

            vtkCellArray lines = vtkCellArray.New();           
            lines.InsertNextCell(2);
            lines.InsertCellPoint(0);
            lines.InsertCellPoint(1);
            lines.InsertNextCell(2);
            lines.InsertCellPoint(2);
            lines.InsertCellPoint(3);

添加矩形的两个短边

            lines.InsertNextCell(2);
            lines.InsertCellPoint(0);
            lines.InsertCellPoint(2);
            lines.InsertNextCell(2);
            lines.InsertCellPoint(1);
            lines.InsertCellPoint(3);

程序运行效果如图

使用vtkcellArray类建立网格

网格的中心点位于(0,0,-10),网格平面垂直于Z轴,网格为正方形,边长40,格边长为10,程序如下

            vtkPoints points = vtkPoints.New();

            int j=0;
            for (int i=20;i>=-20;i =i -10)
            {
                points.InsertPoint(j, -20, i, -10);
                j++;
                points.InsertPoint(j, 20, i, -10);
                j++;
            }

首先添加竖直方向的10个点,ID如下图

 接着添加横向的10个点,ID如下图:

            for (int i=-20;i<=20;i=i+10)
            {
                points.InsertPoint(j, i, 20, -10);
                j++;
                points.InsertPoint(j, i, - 20, -10);
                j++;
            }

            vtkCellArray lines = vtkCellArray.New();
            for (int i =0;i<10;i ++)
            {
                lines.InsertNextCell(2);
                lines.InsertCellPoint(i * 2);
                lines.InsertCellPoint(i * 2 + 1);
            }

画线,程序运行效果如下图:

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值