消隐与Z-Buffer

  使用缓冲器记录物体表面在屏幕上投影所覆盖范围内的全部像素的深度值,依次访问屏幕范围内物体表面所覆盖的每一像素,用深度小(深度用z值表示,z值小表示离视点近)的像素点颜色替代深度大的像素点颜色可以实现消隐,称为深度缓冲器算法。深度缓冲器算法也称为Z-Buffer算法,在物体空间内不对物体表面的可见性进行检测,在图像空间中根据每个像素的深度值确定最终绘制到屏幕的物体表面上各个像素的颜色。

  下面的例子中从读入一个简单的8个顶点的立方体STL模型,用vtkSelectVisiblePoints类过滤可见点,并输出相关信息。

  VTK: vtkSelectVisiblePoints Class Reference: extract points that are visible (based on z-buffer calculation).  vtkSelectVisiblePoints is a filter that selects points based on whether they are visible or not. Visibility is determined by accessing the z-buffer of a rendering window. (The position of each input point is converted into display coordinates, and then the z-value at that point is obtained. If within the user-specified tolerance, the point is considered visible.) Points that are visible are passed to the output. Associated data attributes are passed to the output as well.

  鼠标左键旋转到不同视角,点击右键观察输出的信息:

#!usrbinenv python

import vtk

def loadSTL(filenameSTL):
readerSTL = vtk.vtkSTLReader()
readerSTL.SetFileName(filenameSTL)
# 'update' the reader i.e. read the .stl file
readerSTL.Update() polydata = readerSTL.GetOutput() # If there are no points in 'vtkPolyData' something went wrong
if polydata.GetNumberOfPoints() == 0:
raise ValueError("No point data could be loaded from '" + filenameSTL)
return None return polydata # Customize vtkInteractorStyleTrackballCamera
class MyInteractor(vtk.vtkInteractorStyleTrackballCamera): def __init__(self, parent=None):
self.AddObserver("RightButtonPressEvent", self.RightButtonPressEvent) def SetVisibleFilter(self, vis):
self.VisibleFilter = vis def RightButtonPressEvent(self,obj,event):
self.VisibleFilter.Update() print "number of visible points: ", self.VisibleFilter.GetOutput().GetNumberOfPoints() mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(self.VisibleFilter.GetOutput())
actor =vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetPointSize(15)
self.GetDefaultRenderer().AddActor(actor) # Forward events
self.OnRightButtonDown()
return def CreateScene():
# Create a rendering window and renderer
renWin = vtk.vtkRenderWindow()
ren = vtk.vtkRenderer()
# Set background color
ren.GradientBackgroundOn()
ren.SetBackground(.1, .1, .1)
ren.SetBackground2(0.8,0.8,0.8)
# Set window size
renWin.SetSize(600, 600)
renWin.AddRenderer(ren) # Create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin) style = MyInteractor()
style.SetDefaultRenderer(ren)
iren.SetInteractorStyle(style) # load STL file
mesh = loadSTL("cube.stl")
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(mesh) # maps polygonal data to graphics primitives
actor = vtk.vtkLODActor()
actor.SetMapper(mapper)
ren.AddActor(actor) visPts = vtk.vtkSelectVisiblePoints()
visPts.SetInputData(mesh)
visPts.SetRenderer(ren)
style.SetVisibleFilter(visPts) # Enable user interface interactor
iren.Initialize()
iren.Start() if __name__ == "__main__":
CreateScene()


  使用vtkCellPicker可以拾取模型上可见的面和点的信息(vtkCellPicker will shoot a ray into a 3D scene and return information about the first object that the ray hit),而用vtkPointPicker拾取点时会选择离射线距离最近的点,因此所选的点可能位于不可见的表面上。

#!usrbinenv python

import vtk

def loadSTL(filenameSTL):
readerSTL = vtk.vtkSTLReader()
readerSTL.SetFileName(filenameSTL)
# 'update' the reader i.e. read the .stl file
readerSTL.Update() polydata = readerSTL.GetOutput() print "Number of Cells:", polydata.GetNumberOfCells()
print "Number of Points:", polydata.GetNumberOfPoints() # If there are no points in 'vtkPolyData' something went wrong
if polydata.GetNumberOfPoints() == 0:
raise ValueError("No point data could be loaded from " + filenameSTL)
return None return polydata # Customize vtkInteractorStyleTrackballCamera
class MyInteractor(vtk.vtkInteractorStyleTrackballCamera): def __init__(self,parent=None):
self.AddObserver("RightButtonPressEvent", self.RightButtonPressEvent) def RightButtonPressEvent(self,obj,event):
clickPos = self.GetInteractor().GetEventPosition()
print "Picking pixel: " , clickPos # Pick from this location
picker = self.GetInteractor().GetPicker()
picker.Pick(clickPos[0], clickPos[1], 0, self.GetDefaultRenderer()) # If CellId = -1, nothing was picked
if(picker.GetCellId() != -1):
print "Pick position is: " , picker.GetPickPosition()
print "Cell id is:", picker.GetCellId()
print "Point id is:", picker.GetPointId() point_position = mesh.GetPoint(picker.GetPointId()) # Create a sphere
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(point_position)
#sphereSource.SetRadius(0.2)
sphereSource.SetRadius(0.02) # Create a mapper and actor
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(sphereSource.GetOutputPort()) actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(1.0, 0.0, 0.0) self.GetDefaultRenderer().AddActor(actor) # Forward events
self.OnRightButtonDown()
return def CreateScene():
# Create a rendering window and renderer
renWin = vtk.vtkRenderWindow()
# Set window size
renWin.SetSize(600, 600)
ren = vtk.vtkRenderer()
# Set background color
ren.GradientBackgroundOn()
ren.SetBackground(.1, .1, .1)
ren.SetBackground2(0.8,0.8,0.8) renWin.AddRenderer(ren) # Create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin) style = MyInteractor()
style.SetDefaultRenderer(ren)
iren.SetInteractorStyle(style) # vtkCellPicker will shoot a ray into a 3D scene and return information about
# the first object that the ray hits.
cellPicker = vtk.vtkCellPicker()
iren.SetPicker(cellPicker) # load STL file
global mesh
mesh = loadSTL("Suzanne.stl")
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(mesh) # maps polygonal data to graphics primitives
actor = vtk.vtkLODActor()
actor.SetMapper(mapper)
actor.GetProperty().EdgeVisibilityOn()
actor.GetProperty().SetLineWidth(0.3) ren.AddActor(actor) # Enable user interface interactor
iren.Initialize()
iren.Start() if __name__ == "__main__":
CreateScene()

参考:

VTK拾取相关的类

VTK - Users - point picking problem

VTK: vtkCellPicker Class Reference

VTK: vtkPointPicker Class Reference

VTK修炼之道78:交互与拾取_点拾取

VTK/Examples/Cxx/PolyData/SelectVisiblePoints

Ray Casting with Python and VTK: Intersecting lines/rays with surface meshes

VTK拾取网格模型上的可见点的更多相关文章

  1. VTK计算网格模型上的最短路径

    Dijkstra algorithm to compute the graph geodesic.Takes as input a polygonal mesh and performs a sing ...

  2. pcl曲面网格模型的三种显示方式

    pcl网格模型有三种可选的显示模式,分别是面片模式(surface)显示,线框图模式(wireframe)显示,点模式(point)显示.默认为面片模式进行显示.设置函数分别为: void pcl:: ...

  3. 不同材质怎么通过ZBrush赋予同一个模型上

    ZBrush 作为最专业的数字雕刻与绘画软件,能够制作出高质量的3D模型,包括模型的颜色贴图和材质属性.不同材质可以改变照明在表面上的反应,以便模型表现出光泽.凹凸.反射.金属性或透明效果.ZBrus ...

  4. 使用k-means对3D网格模型进行分割

    使用k-means对3D网格模型进行分割 由于一些原因,最近在做网格分割的相关工作.网格分割的方法有很多,如Easy mesh cutting.K-means.谱分割.基于SDF的分割等.根据对分割要 ...

  5. Linux内核(7) - 设备模型(上)

    对于驱动开发来说,设备模型的理解是根本,毫不夸张得说,理解了设备模型,再去看那些五花八门的驱动程序,你会发现自己站在了另一个高度,从而有了一种俯视的感觉,就像凤姐俯视知音和故事会,韩峰同志俯视女下属. ...

  6. ZBrush如何把不同材质赋予同一个模型上

    ZBrush 作为最专业的数字雕刻与绘画软件,能够制作出高质量的3D模型,包括模型的颜色贴图和材质属性.不同材质可以改变照明在表面上的反应,以便模型表现出光泽.凹凸.反射.金属性或透明效果.ZBrus ...

  7. 3ds Max学习日记(十一)——如何给模型上贴图

    参考链接:https://jingyan.baidu.com/article/e4511cf38a810b2b845eaf1f.html   之前一直都不知道怎么在3dsMax里给模型上材质和贴图,被 ...

  8. 在skyline中将井盖、雨水箅子等部件放到地面模型上

    公司三维建模组遇到这样的一个问题,怎样将井盖.雨水盖子恰好放在做好的地面模型上.传统的方法是在skyline中逐个调整井盖的对地高度,就是调整为恰好能放在地面上.或者选择很粗糙的一个方法,在“高度”属 ...

  9. 第 16 章 CSS 盒模型[上]

    学习要点: 1.元素尺寸 2.元素内边距 3.元素外边距 4.处理溢出 主讲教师:李炎恢 本章主要探讨 HTML5 中 CSS 盒模型,学习怎样了解元素的外观配置以及文档的整体布局. 一.元素尺寸 C ...

随机推荐

  1. checkbox简单例子

    写个简单的例子,如下:html页面部分:<input type="checkbox" value="1" name="check"/& ...

  2. ***腾讯云直播(含微信小程序直播)研究资料汇总-原创

    这段时间抽空研究了下直播技术,综合比较了下腾讯云直播的技术和文档方面最齐全,现把一些技术资料和文档归集如下: 1.微信小程序移动直播入门导读 https://cloud.tencent.com/doc ...

  3. 用两个int值实现读写锁

    private int readcount = 0; private int writecount = 0; public void lockread() throws InterruptedExce ...

  4. JavaScript中为什么string可以拥有方法?

    所有文章搬运自我的个人主页:sheilasun.me 引子 我们都知道,JavaScript数据类型分两大类,基本类型(或者称原始类型)和引用类型. 基本类型的值是保存在栈内存中的简单数据段,它们是按 ...

  5. ctsc2017

    就看了几道题目.. day1t1 良心题啊.. 经过一波转化就变成了求某一个数后面有几个比它大的 并且是有长度的(固定的) 然后这样暴力是nlogn的 再写个后面的部分分大概就有70了 其实100也很 ...

  6. BZOJ3211 花神游历各国 并查集 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3211 题意概括 有n个数形成一个序列. m次操作. 有两种,分别是: 1. 区间开根(取整) 2. ...

  7. 【目录】LeetCode Java实现

    这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...

  8. vue2.0以上版本安装sass(scss)

    一.首先说明sass和scss的区别. 1.异同:1)简言之可以理解scss是sass的一个升级版本,完全兼容sass之前的功能,又有了些新增能力.语法形式上有些许不同,最主要的就是sass是靠缩进表 ...

  9. Flag之2019年立

    今天是2019年1月12日,这是我第一次在一个公众的平台上立flag. 至于为何想立一个flag,应该是因为自己年龄渐长,从儿时读书时代家人对自己的要求就不高,考试可以及格即可,导致了自己养成了比较安 ...

  10. SpringMvc 文件下载 详解

    最近SSM 需要用到文件下载,以前没用过,在百度上找了好久发现没有一篇博客,对于此段代码进行详细讲解, 这里是本人的个人总结,跟大家分享一下!!!不谢 /** * 文件下载 * ResponseEnt ...