Volume 多边形裁剪测试

2023-05-16

vtk自带了一个Volume剪切的例子, 可以用隐函数确定的剪切面裁剪vtkImageData.

本人对该例子代码略加修改,剪切口为一个封闭的梯形。其代码和配置文件如下。

目录

ClipVolume.cxx

CMakeLists.txt

运行结果


ClipVolume.cxx

// This program draws a checkerboard and clips it with two planes.

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellData.h>
#include <vtkClipVolume.h>
#include <vtkColor.h>
#include <vtkDataSetMapper.h>
#include <vtkDoubleArray.h>
#include <vtkExecutive.h>
#include <vtkImageData.h>
#include <vtkImageMapToColors.h>
#include <vtkInteractorStyleSwitch.h>
#include <vtkLookupTable.h>
#include <vtkMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlanes.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkProperty.h>
#include <vtkRenderStepsPass.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

namespace {

constexpr auto IMAGESIZE{64};  // number of checkerboard squares on a side
constexpr auto CUBESIZE{20.0}; // physical linear dimension of entire system

// Color for the checkerboard image
constexpr auto DIM{0.5}; // amount to dim the dark squares by

// Offsets for clipping planes with normals in the X and Y directions
constexpr auto XOFFSET{8};
constexpr auto YOFFSET{8};

///

// Make the image data. A checkerboard pattern is used for
// simplicity.
vtkSmartPointer<vtkImageData> makeImage(int n, vtkColor3d fillColor,
                                        vtkColor3d checkerColor);
} // namespace

int main(int, char*[])
{

  vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();

  // Define colors
  vtkNew<vtkNamedColors> colors;
  vtkColor3d backgroundColor = colors->GetColor3d("Wheat");
  vtkColor3d checkerColor = colors->GetColor3d("Tomato");
  vtkColor3d fillColor = colors->GetColor3d("Banana");

  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(backgroundColor.GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetSize(640, 480);

  vtkNew<vtkRenderWindowInteractor> interactor;
  vtkNew<vtkInteractorStyleSwitch> style;
  interactor->SetInteractorStyle(style);
  interactor->SetRenderWindow(renderWindow);

  auto image = makeImage(IMAGESIZE, fillColor, checkerColor);

  // Clipping planes in the X and Y direction.
  vtkNew<vtkDoubleArray> normals;
  vtkNew<vtkPoints> clipPts;
  normals->SetNumberOfComponents(3);
  double xnorm[3] = {-1., -0.3, 0.};
  double lnorm[3] = { -0.5, -0.5, 0 };
  double ynorm[3] = {-0.3, -1., 0.};
  double snorm[3] = { 0.5, 0.5, 0 };
  double xpt[3] = {XOFFSET, 0., 0.};
  double lpt[3] = {XOFFSET + 5, 0, 0 };
  double ypt[3] = {0., YOFFSET, 0.};
  double spt[3] = {XOFFSET + 12, 0, 0 };
  normals->InsertNextTuple(xnorm);
  normals->InsertNextTuple(lnorm);
  normals->InsertNextTuple(ynorm);
  normals->InsertNextTuple(snorm);
  clipPts->InsertNextPoint(xpt);
  clipPts->InsertNextPoint(lpt);
  clipPts->InsertNextPoint(ypt);
  clipPts->InsertNextPoint(spt);
  vtkNew<vtkPlanes> clipPlanes;
  clipPlanes->SetNormals(normals);
  clipPlanes->SetPoints(clipPts);

  vtkNew<vtkClipVolume> clipper;
  clipper->SetClipFunction(clipPlanes);
  clipper->SetInputData(image);

  vtkNew<vtkDataSetMapper> imageMapper;
  vtkNew<vtkActor> imageActor;
  imageActor->SetMapper(imageMapper);
  renderer->AddViewProp(imageActor);
  imageMapper->SetInputConnection(clipper->GetOutputPort());

  renderer->ResetCamera();
  renderer->GetActiveCamera()->Azimuth(120);
  renderer->GetActiveCamera()->Elevation(30);
  renderer->ResetCameraClippingRange();
  renderWindow->SetWindowName("ClipVolume");
  renderWindow->Render();

  interactor->Start();

  return EXIT_SUCCESS;
  ;
}

namespace {
// Make the image data. A checkerboard pattern is used for
// simplicity.
vtkSmartPointer<vtkImageData> makeImage(int n, vtkColor3d fillColor,
                                        vtkColor3d checkerColor)
{
  vtkNew<vtkImageData> image0;
  image0->SetDimensions(n, n, n);
  image0->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
  image0->SetSpacing(CUBESIZE / n, CUBESIZE / n, CUBESIZE / n);
  int checkerSize = n / 8;
  for (int z = 0; z < n; z++)
  {
    for (int y = 0; y < n; y++)
    {
      for (int x = 0; x < n; x++)
      {
        unsigned char* ptr = (unsigned char*)image0->GetScalarPointer(x, y, z);
        *ptr = (x / checkerSize + y / checkerSize + z / checkerSize) %
            2; // checkerboard
      }
    }
  }

  vtkNew<vtkLookupTable> lut;
  lut->SetNumberOfTableValues(2);
  lut->SetTableRange(0, 1);
  lut->SetTableValue(0, fillColor.GetRed(), fillColor.GetGreen(),
                     fillColor.GetBlue(), 1.0);
  lut->SetTableValue(1, checkerColor.GetRed(), checkerColor.GetGreen(),
                     checkerColor.GetBlue(), 1.0);

  auto map = vtkSmartPointer<vtkImageMapToColors>::New();
  map->SetLookupTable(lut);
  map->SetOutputFormatToRGBA();
  map->SetInputData(image0);
  map->GetExecutive()->Update();

  return map->GetOutput();
}

} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ClipVolume)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkCommonExecutionModel
  vtkFiltersGeneral
  vtkImagingCore
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping ClipVolume: ${VTK_NOT_FOUND_MESSAGE}")
  return()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(ClipVolume MACOSX_BUNDLE ClipVolume.cxx )
  target_link_libraries(ClipVolume PRIVATE ${VTK_LIBRARIES})
else()
  # Prevent a "command line is too long" failure in Windows.
  set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
  add_executable(ClipVolume MACOSX_BUNDLE ClipVolume.cxx )
  target_link_libraries(ClipVolume PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ClipVolume
    MODULES ${VTK_LIBRARIES}
    )
endif()

运行结果

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Volume 多边形裁剪测试 的相关文章

随机推荐

  • 按位异或运算和按位左移和按位右移

    按位异或运算 xff08 xff09 对应位上的数据不同返回1 xff0c 相同返回0 public class Hello public static void main String args int num1 61 12 1100 i
  • P1593 因子和(唯一分解定理,逆元)

    题目传送门 题意 xff1a 给你a b xff0c 要你求ab的因子和对9901取模的结果 xff08 1 lt 61 a lt 61 5e7 0 lt 61 b lt 61 5e7 xff09 样例输入 xff1a 2 样例输出 xff
  • 2020 牛客暑期多校 第二场 G-Greater and Greater(bitset)

    题目传送门 题意 xff1a 给你一个长度为n的数组a xff0c 一个长度为m的数组b xff0c 问你A中有多少个子数组S xff0c 满足S i gt 61 b i i 1 2 3 m 思路 xff1a 开两个bitset xff0c
  • Linux中MPICH3.2.1安装步骤及遇到的问题并解决

    一 下载Mpich并解压 Downloads MPICH 到官方网站下载Mpich xff0c 并解压到自己的文件目录下 二 安装过程及遇到的问题 正确安装步骤 xff0c 可以直接看Mpich解压文件下的README文件 xff0c 有详
  • Linux安装sonarqube(含各种错误处理)

    目录 1 下载安装 2 错误处理 2 1 JDK版本不适配 2 2 can not run elasticsearch as root 1 下载安装 下载地址 xff1a Download SonarQube Sonar xff08 下载页
  • Ubuntu更新grub

    昨天重装了系统 xff0c 需要重新引导一下Ubuntu的启动 xff0c 网上好像有很多中方法这里分享一个自己试过的 1 安装EasyBCD 下载地址 xff1a http download csdn net detail jolingo
  • 自定义控件-UIControl

    UIControl是控件的基类 xff0c 例如 xff1a 按钮 xff0c 滑块等将用户的目的传递到应用程序中 UIContol不能直接的实例化 xff0c 它只能通过继承的方式为子类提供公共的接口和动作结构 UIControl的主要角
  • 远程服务器不显示桌面,只显示一片蓝

    远程服务器不显示桌面 xff0c 只显示一片蓝 xff0c 看不到桌面 xff0c 无法进行操作 xff0c 这种情况是因为资源管理器服务没有启动 xff0c 我的解决办法如下 xff1a 1 使用快捷键 CTRL 43 SHIFT 43
  • Android 9.0 支持NTFS和Exfat 格式U盘开发

    前言 最近有个需求要求在Android 9 0上面支持NTFS和Exfat 格式的U盘 网上有很多资料都是基于Android 4 4 的系统 xff0c 系统版本太陈旧没有办法借鉴 xff0c 通过两周的摸索终于搞定了这个功能 xff0c
  • LBP特征识别

    一 LBP特征的背景介绍 LBP指局部二值模式 xff0c 英文全称 xff1a Local Binary Pattern xff0c 是一种用来描述图像局部特征的算子 xff0c LBP特征具有灰度不变性和旋转不变性等显著优点 它是由T
  • windows Update自动更新下载到哪个文件夹了?

    每次作了系统恢复时就要重新下载安装系统更新程序 xff0c 好麻烦 xff0c 哈哈 xff0c 我要是找到自动更新程序下载到那里了不就好了吗 xff1f 把它另存到备分盘里 xff0c 到时候重装或恢复完就可以不用重新等待慢长的Updat
  • 天气预报api接口

    中华万年历 请求数据方式 http wthrcdn etouch cn weather mini city 61 沈阳 通过城市名字获得天气数据 xff0c json数据 http wthrcdn etouch cn weather min
  • jetson nano 安装anaconda和pytorch 指定目录安装 完整安装方法

    前言 xff1a 由于jetson nano 是aarch64架构 xff0c Anaconda官方不支持aarch64架构 xff0c 所以有了一个叫 Archiconda xff0c 其目的就是将conda移植到aarch64平台上 资
  • Latex的学习

    因为种种原因 xff0c 要学一下latex 一个非常快速的 Latex 入门教程 哔哩哔哩 bilibili 适用 xff1a 快速切换格式和排版 即会将文档的内容和排版区分开 markdown适用于轻量文字编辑 常用工具 xff1a 1
  • VTK经典四格图

    右下角的是使用Volume绘制的三维图
  • vim常用指令类表

    最近开始Linux环境下编程 xff0c 有时需要使用vim浏览和修改代码 xff0c 特意学习了一下 xff0c 并整理了一些常用指令 xff0c 如下所示 VIM常用命令 工作模式 正常模式 Esc编辑模式 i可视选择模式 v文件 打开
  • 2021-06-23

    root 64 localhost cat usr share applications Lxg desktop Desktop Entry Name 61 Lxg Type 61 Application Exec 61 home lys
  • gcc编译过程

    1 下载相应代码库 xff0c 解压到一个大的文件夹的各子文件夹 2 执行脚本 cd gmp 5 0 0 make clean configure disable shared enable static prefix 61 usr loc
  • 【vscode】去除黄色波浪下划线

    vscode 去除黄色波浪下划线 vscode在使用过程中 xff0c 如果出现变量名下方出现黄色波浪线 xff0c 影响编程效率 可能原因是由于安装了pylint这个库 xff0c 并没有进行合适的设置 我们可以在设置中查找 34 pyt
  • Volume 多边形裁剪测试

    vtk自带了一个Volume剪切的例子 xff0c 可以用隐函数确定的剪切面裁剪vtkImageData 本人对该例子代码略加修改 xff0c 剪切口为一个封闭的梯形 其代码和配置文件如下 目录 ClipVolume cxx CMakeLi