视觉开发不可不知——ActiViz.NET

  ActiViz.NET由Kitware公司开发。ActiViz.Net可以理解为C#版本的VTK,充当了 C# (.NET) 代码与用 C++ 编写的 VTK (Visualization Toolkit) 库之间的桥梁,让你能够在 .NET 应用程序中使用 VTK 强大的三维可视化和图像处理功能。

1、github地址

  https://github.com/bitzhuwei/Kitware.VTK

2、简单Demo

using System;
using System.Windows.Forms;
using Kitware.VTK; // 引入 VTK 命名空间

namespace VtkDemo
{
    public partial class Form1 : Form
    {
        // 声明 VTK 控件
        private RenderWindowControl renderWindowControl;

        public Form1()
        {
            InitializeComponent();
            InitializeVtkPipeline();
        }

        private void InitializeVtkPipeline()
        {
            // 1. 创建 VTK 控件并添加到窗体
            renderWindowControl = new RenderWindowControl();
            renderWindowControl.Dock = DockStyle.Fill;
            this.Controls.Add(renderWindowControl);
            this.Text = "Kitware VTK 简单示例";

            // --- VTK 核心管线开始 ---

            // 2. 创建几何体源:创建一个立方体
            vtkCubeSource cubeSource = vtkCubeSource.New();
            cubeSource.SetXLength(2.0);
            cubeSource.SetYLength(2.0);
            cubeSource.SetZLength(2.0);

            // 3. 创建Mapper(映射器):将几何数据转换为图形基元
            vtkPolyDataMapper cubeMapper = vtkPolyDataMapper.New();
            cubeMapper.SetInputConnection(cubeSource.GetOutputPort());

            // 4. 创建Actor(演员):代表场景中的一个对象
            vtkActor cubeActor = vtkActor.New();
            cubeActor.SetMapper(cubeMapper);
            cubeActor.GetProperty().SetColor(1.0, 0.0, 0.0); // 设置为红色 (R, G, B)

            // 5. 创建Renderer(渲染器):管理场景中的 Actor 和光照
            vtkRenderer renderer = renderWindowControl.Renderer;
            renderer.AddActor(cubeActor);
            renderer.SetBackground(0.1, 0.1, 0.2); // 设置背景颜色 (深蓝灰色)

            // 6. 重置摄像机以包含所有对象
            renderer.ResetCamera();
            
            // 可选:旋转摄像机角度
            renderer.GetActiveCamera().Azimuth(30);
            renderer.GetActiveCamera().Elevation(30);

            // --- VTK 核心管线结束 ---
        }

        // 必须重写 Dispose 方法以正确释放 VTK 资源
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (renderWindowControl != null)
                {
                    renderWindowControl.Dispose();
                }
            }
            base.Dispose(disposing);
        }
    }
}

  

posted @ 2026-04-23 13:38  echo-efun  阅读(51)  评论(0)    收藏  举报