欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python VTK計(jì)算曲面的高斯曲率和平均曲率

 更新時(shí)間:2022年04月18日 14:25:44   作者:派大大大星?  
這篇文章主要介紹了Python VTK計(jì)算曲面的高斯曲率和平均曲率,如何使用戶Python版本的VTK計(jì)算曲面的高斯曲率并映射在曲面上。本例中使用了兩個(gè)不同的表面,每個(gè)表面根據(jù)其高斯曲率和平均曲率著色,需要的朋友可以參考一下

前言:

VTK,(visualizationtoolkit)是一個(gè)開放資源的免費(fèi)軟件系統(tǒng),主要用于三維計(jì)算機(jī)圖形學(xué)、圖像處理和可視化。Vtk是在面向?qū)ο笤淼幕A(chǔ)上設(shè)計(jì)和實(shí)現(xiàn)的,它的內(nèi)核是用C++構(gòu)建的,包含有大約250,000行代碼,2000多個(gè)類,還包含有幾個(gè)轉(zhuǎn)換界面,因此也可以自由的通過Java,Tcl/Tk和Python各種語言使用vtk。

本文介紹了 如何使用戶Python版本的VTK計(jì)算曲面的高斯曲率并映射在曲面上。本例中使用了兩個(gè)不同的表面,每個(gè)表面根據(jù)其高斯曲率和平均曲率著色。

  • 第一個(gè)曲面是一個(gè)超二次曲面,這演示了如何使用額外的過濾器來獲得一個(gè)平滑的曲面。
  • 第二個(gè)曲面是參數(shù)化曲面,在這種情況下,該曲面已被三角剖分,因此無需額外處理。

為了獲得漂亮的彩色圖像,使用VTKColorTransfer函數(shù)為vtkLookupTable表生成一組顏色。我們使用了發(fā)散的顏色空間。由于為查找表選擇的范圍對(duì)稱,白色表示中點(diǎn)值,而藍(lán)色表示小于中點(diǎn)值的值,橙色表示大于中點(diǎn)值的顏色。在隨機(jī)Hills高斯曲率曲面的情況下,這種顏色非常好地顯示了曲面的性質(zhì)。藍(lán)色區(qū)域?yàn)榘包c(diǎn)(負(fù)高斯曲率),橙色區(qū)域?yàn)檎咚骨?。在平均曲率的情況下,藍(lán)色表示垂直于一個(gè)主軸的負(fù)曲率。

主要函數(shù)介紹:

vtkSuperquadricSource: vtkSuperquadricSource 創(chuàng)建以原點(diǎn)為中心的多邊形超二次曲面,可以設(shè)置尺寸??梢栽O(shè)置兩個(gè)(φ)的緯度和經(jīng)度(θ)方向的分辨率(多邊形離散化)。渾圓度參數(shù)(緯度渾圓度和經(jīng)度渾圓度)控制超二次曲面的形狀。環(huán)形布爾值控制是否產(chǎn)生環(huán)形的超二次曲面。如果是的話,厚度參數(shù)控制的厚度的環(huán)形:0是最薄的環(huán)形,和1具有最小尺寸的孔??s放尺度參數(shù)允許超二次曲面,在x,y,和z(在任何情況下,正確地生成法線向量)進(jìn)行縮放。 尺寸參數(shù)控制的超二次曲面的size。原理是基于“剛性基于物理的超二次曲面”。

基本方法:

  •   SetCenter()設(shè)置中心點(diǎn)
  •   SetThickness()厚度參數(shù)控制的厚度的環(huán)形:0是最薄的環(huán)形,和1具有最小尺寸的孔
  •   ToroidalOn()開啟環(huán)形
  •   SetPhiRoundness(),SetThetaRoundness設(shè)置經(jīng)緯度的環(huán)形度
  •   SetScale()設(shè)置在x,y,z方向的超二次曲面的拉伸系數(shù)。

vtkParametricRandomHills: 生成覆蓋隨機(jī)放置的山丘的曲面。山丘的形狀和高度會(huì)有所不同,因?yàn)楦浇角鸬拇嬖跁?huì)影響給定山丘的形狀和高度。提供了一個(gè)選項(xiàng),用于將山丘放置在曲面上的規(guī)則柵格上。在這種情況下,所有山丘的形狀和高度都相同。

adjust_edge_curvatures: 此函數(shù)通過將該值替換為鄰域中點(diǎn)曲率的平均值來調(diào)整曲面邊緣的曲率。在調(diào)用此函數(shù)之前,請(qǐng)記住更新vtkCurvatures對(duì)象。

source:與vtkCurvatures對(duì)象相對(duì)應(yīng)的vtkPolyData對(duì)象。

curvature_name:曲率的名稱,“Gauss_curvature”或“Mean_curvature”。

epsilon:小于此值的絕對(duì)曲率值將設(shè)置為零。

mport numpy as np
import vtk
from vtk.util import numpy_support
from vtkmodules.numpy_interface import dataset_adapter as dsa

def main(argv):
    colors = vtk.vtkNamedColors()
    #產(chǎn)生曲面
    torus = vtk.vtkSuperquadricSource()
    torus.SetCenter(0.0, 0.0, 0.0)
    torus.SetScale(1.0, 1.0, 1.0)
    torus.SetPhiResolution(64)
    torus.SetThetaResolution(64)
    torus.SetThetaRoundness(1)
    torus.SetThickness(0.5)
    torus.SetSize(0.5)
    torus.SetToroidal(1)

    # 改變觀察視角
    toroid_transform = vtk.vtkTransform()
    toroid_transform.RotateX(55)

    toroid_transform_filter = vtk.vtkTransformFilter()
    toroid_transform_filter.SetInputConnection(torus.GetOutputPort())
    toroid_transform_filter.SetTransform(toroid_transform)

    # The quadric is made of strips, so pass it through a triangle filter as
    # the curvature filter only operates on polys
    tri = vtk.vtkTriangleFilter()
    tri.SetInputConnection(toroid_transform_filter.GetOutputPort())

    #二次曲面在生成邊的方式上存在嚴(yán)重的不連續(xù)性,因此讓我們將其通過CleanPolyDataFilter并合并
    #任何重合或非常接近的點(diǎn)

    cleaner = vtk.vtkCleanPolyData()
    cleaner.SetInputConnection(tri.GetOutputPort())
    cleaner.SetTolerance(0.005)
    cleaner.Update()

    # 生成覆蓋隨機(jī)放置的山丘的曲面
    rh = vtk.vtkParametricRandomHills()
    rh_fn_src = vtk.vtkParametricFunctionSource()
    rh_fn_src.SetParametricFunction(rh)
    rh_fn_src.Update()

    sources = list()
    for i in range(0, 4):
        cc = vtk.vtkCurvatures()
        if i < 2:
            cc.SetInputConnection(cleaner.GetOutputPort())
        else:
            cc.SetInputConnection(rh_fn_src.GetOutputPort())
        if i % 2 == 0:
            cc.SetCurvatureTypeToGaussian()
            curvature_name = 'Gauss_Curvature'
        else:
            cc.SetCurvatureTypeToMean()
            curvature_name = 'Mean_Curvature'
        cc.Update()
        adjust_edge_curvatures(cc.GetOutput(), curvature_name)
        sources.append(cc.GetOutput())

    curvatures = {
        0: 'Gauss_Curvature',
        1: 'Mean_Curvature',
        2: 'Gauss_Curvature',
        3: 'Mean_Curvature',
    }

    # lut = get_diverging_lut()
    lut = get_diverging_lut1()

    renderers = list()
    mappers = list()
    actors = list()
    text_mappers = list()
    text_actors = list()
    scalar_bars = list()

    # Create a common text property.
    text_property = vtk.vtkTextProperty()
    text_property.SetFontSize(24)
    text_property.SetJustificationToCentered()

    # RenderWindow Dimensions
    #
    renderer_size = 512
    grid_dimensions = 2
    window_width = renderer_size * grid_dimensions
    window_height = renderer_size * grid_dimensions

 
    for idx, source in enumerate(sources):
        curvature_name = curvatures[idx].replace('_', '\n')

        source.GetPointData().SetActiveScalars(curvatures[idx])
        scalar_range = source.GetPointData().GetScalars(curvatures[idx]).GetRange()

        mappers.append(vtk.vtkPolyDataMapper())
        mappers[idx].SetInputData(source)
        mappers[idx].SetScalarModeToUsePointFieldData()
        mappers[idx].SelectColorArray(curvatures[idx])
        mappers[idx].SetScalarRange(scalar_range)
        mappers[idx].SetLookupTable(lut)

        actors.append(vtk.vtkActor())
        actors[idx].SetMapper(mappers[idx])

        text_mappers.append(vtk.vtkTextMapper())
        text_mappers[idx].SetInput(curvature_name)
        text_mappers[idx].SetTextProperty(text_property)

        text_actors.append(vtk.vtkActor2D())
        text_actors[idx].SetMapper(text_mappers[idx])
        text_actors[idx].SetPosition(250, 16)

        # Create a scalar bar
        scalar_bars.append(vtk.vtkScalarBarActor())
        scalar_bars[idx].SetLookupTable(mappers[idx].GetLookupTable())
        scalar_bars[idx].SetTitle(curvature_name)
        scalar_bars[idx].UnconstrainedFontSizeOn()
        scalar_bars[idx].SetNumberOfLabels(5)
        scalar_bars[idx].SetMaximumWidthInPixels(window_width // 8)
        scalar_bars[idx].SetMaximumHeightInPixels(window_height // 3)
        scalar_bars[idx].SetBarRatio(scalar_bars[idx].GetBarRatio() * 0.5)
        scalar_bars[idx].SetPosition(0.85, 0.1)

        renderers.append(vtk.vtkRenderer())

    for idx in range(len(sources)):
        if idx < grid_dimensions * grid_dimensions:
            renderers.append(vtk.vtkRenderer)

    # Create the RenderWindow
    #
    render_window = vtk.vtkRenderWindow()
    render_window.SetSize(renderer_size * grid_dimensions, renderer_size * grid_dimensions)
    render_window.SetWindowName('CurvaturesDemo')


    viewport = list()
    for row in range(grid_dimensions):
        for col in range(grid_dimensions):
            idx = row * grid_dimensions + col

            viewport[:] = []
            viewport.append(float(col) / grid_dimensions)
            viewport.append(float(grid_dimensions - (row + 1)) / grid_dimensions)
            viewport.append(float(col + 1) / grid_dimensions)
            viewport.append(float(grid_dimensions - row) / grid_dimensions)

            if idx > (len(sources) - 1):
                continue

            renderers[idx].SetViewport(viewport)
            render_window.AddRenderer(renderers[idx])

            renderers[idx].AddActor(actors[idx])
            renderers[idx].AddActor(text_actors[idx])
            renderers[idx].AddActor(scalar_bars[idx])
            renderers[idx].SetBackground(colors.GetColor3d('SlateGray'))

    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(render_window)
    style = vtk.vtkInteractorStyleTrackballCamera()
    interactor.SetInteractorStyle(style)

    render_window.Render()

    interactor.Start()


def get_diverging_lut():

    ctf = vtk.vtkColorTransferFunction()
    ctf.SetColorSpaceToDiverging()
    # Cool to warm.
    ctf.AddRGBPoint(0.0, 0.230, 0.299, 0.754)
    ctf.AddRGBPoint(0.5, 0.865, 0.865, 0.865)
    ctf.AddRGBPoint(1.0, 0.706, 0.016, 0.150)

    table_size = 256
    lut = vtk.vtkLookupTable()
    lut.SetNumberOfTableValues(table_size)
    lut.Build()

    for i in range(0, table_size):
        rgba = list(ctf.GetColor(float(i) / table_size))
        rgba.append(1)
        lut.SetTableValue(i, rgba)

    return lut


def get_diverging_lut1():
    colors = vtk.vtkNamedColors()
    # Colour transfer function.
    ctf = vtk.vtkColorTransferFunction()
    ctf.SetColorSpaceToDiverging()
    p1 = [0.0] + list(colors.GetColor3d('MidnightBlue'))
    p2 = [0.5] + list(colors.GetColor3d('Gainsboro'))
    p3 = [1.0] + list(colors.GetColor3d('DarkOrange'))
    ctf.AddRGBPoint(*p1)
    ctf.AddRGBPoint(*p2)
    ctf.AddRGBPoint(*p3)

    table_size = 256
    lut = vtk.vtkLookupTable()
    lut.SetNumberOfTableValues(table_size)
    lut.Build()

    for i in range(0, table_size):
        rgba = list(ctf.GetColor(float(i) / table_size))
        rgba.append(1)
        lut.SetTableValue(i, rgba)

    return lut


def vtk_version_ok(major, minor, build):

    requested_version = (100 * int(major) + int(minor)) * 100000000 + int(build)
    ver = vtk.vtkVersion()
    actual_version = (100 * ver.GetVTKMajorVersion() + ver.GetVTKMinorVersion()) \
                     * 100000000 + ver.GetVTKBuildVersion()
    if actual_version >= requested_version:
        return True
    else:
        return False


def adjust_edge_curvatures(source, curvature_name, epsilon=1.0e-08):

    def point_neighbourhood(pt_id):

        cell_ids = vtk.vtkIdList()
        source.GetPointCells(pt_id, cell_ids)
        neighbour = set()
        for cell_idx in range(0, cell_ids.GetNumberOfIds()):
            cell_id = cell_ids.GetId(cell_idx)
            cell_point_ids = vtk.vtkIdList()
            source.GetCellPoints(cell_id, cell_point_ids)
            for cell_pt_idx in range(0, cell_point_ids.GetNumberOfIds()):
                neighbour.add(cell_point_ids.GetId(cell_pt_idx))
        return neighbour

    def compute_distance(pt_id_a, pt_id_b):
       
        #計(jì)算距離.


        pt_a = np.array(source.GetPoint(pt_id_a))
        pt_b = np.array(source.GetPoint(pt_id_b))
        return np.linalg.norm(pt_a - pt_b)

    # 獲取活動(dòng)標(biāo)量
    source.GetPointData().SetActiveScalars(curvature_name)
    np_source = dsa.WrapDataObject(source)
    curvatures = np_source.PointData[curvature_name]

    #  獲得邊緣點(diǎn)的ID
    array_name = 'ids'
    id_filter = vtk.vtkIdFilter()
    id_filter.SetInputData(source)
    id_filter.SetPointIds(True)
    id_filter.SetCellIds(False)
    id_filter.SetPointIdsArrayName(array_name)
    id_filter.SetCellIdsArrayName(array_name)
    id_filter.Update()

    edges = vtk.vtkFeatureEdges()
    edges.SetInputConnection(id_filter.GetOutputPort())
    edges.BoundaryEdgesOn()
    edges.ManifoldEdgesOff()
    edges.NonManifoldEdgesOff()
    edges.FeatureEdgesOff()
    edges.Update()

    edge_array = edges.GetOutput().GetPointData().GetArray(array_name)
    boundary_ids = []
    for i in range(edges.GetOutput().GetNumberOfPoints()):
        boundary_ids.append(edge_array.GetValue(i))
    # Remove duplicate Ids.
    p_ids_set = set(boundary_ids)

    #迭代邊緣點(diǎn)并計(jì)算曲率作為相鄰點(diǎn)的加權(quán)平均值。
    count_invalid = 0
    for p_id in boundary_ids:
        p_ids_neighbors = point_neighbourhood(p_id)
        # Keep only interior points.
        p_ids_neighbors -= p_ids_set
        # Compute distances and extract curvature values.
        curvs = [curvatures[p_id_n] for p_id_n in p_ids_neighbors]
        dists = [compute_distance(p_id_n, p_id) for p_id_n in p_ids_neighbors]
        curvs = np.array(curvs)
        dists = np.array(dists)
        curvs = curvs[dists > 0]
        dists = dists[dists > 0]
        if len(curvs) > 0:
            weights = 1 / np.array(dists)
            weights /= weights.sum()
            new_curv = np.dot(curvs, weights)
        else:
            # Corner case.
            count_invalid += 1
            # Assuming the curvature of the point is planar.
            new_curv = 0.0
        # Set the new curvature value.
        curvatures[p_id] = new_curv

    #  將小值設(shè)置為0
    if epsilon != 0.0:
        curvatures = np.where(abs(curvatures) < epsilon, 0, curvatures)
        # Curvatures is now an ndarray
        curv = numpy_support.numpy_to_vtk(num_array=curvatures.ravel(),
                                          deep=True,
                                          array_type=vtk.VTK_DOUBLE)
        curv.SetName(curvature_name)
        source.GetPointData().RemoveArray(curvature_name)
        source.GetPointData().AddArray(curv)
        source.GetPointData().SetActiveScalars(curvature_name)


if __name__ == '__main__':
    import sys

    main(sys.argv)

顯示效果如下:

1637485770(1).jpg

到此這篇關(guān)于Python VTK計(jì)算曲面的高斯曲率和平均曲率的文章就介紹到這了,更多相關(guān) Python-VTK計(jì)算 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python中正則表達(dá)式的用法實(shí)例匯總

    Python中正則表達(dá)式的用法實(shí)例匯總

    這篇文章主要介紹了Python中正則表達(dá)式的用法實(shí)例匯總,非常實(shí)用,需要的朋友可以參考下
    2014-08-08
  • python?列表常用方法超詳細(xì)梳理總結(jié)

    python?列表常用方法超詳細(xì)梳理總結(jié)

    這篇文章主要為大家介紹了Python中列表的幾個(gè)常用方法總結(jié),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python列表有一定幫助,需要的可以參考一下
    2022-03-03
  • Python多線程學(xué)習(xí)資料

    Python多線程學(xué)習(xí)資料

    Python中使用線程有兩種方式:函數(shù)或者用類來包裝線程對(duì)象
    2012-12-12
  • 如何解決python多種版本沖突問題

    如何解決python多種版本沖突問題

    這篇文章主要介紹了如何解決python多種版本沖突問題,幫助大家更好的進(jìn)行python開發(fā),感興趣的朋友可以了解下
    2020-10-10
  • Python入門之字典的使用教程

    Python入門之字典的使用教程

    Python字典是一種可變?nèi)萜髂P?,且可存?chǔ)任意類型對(duì)象,如字符串、數(shù)字、元組等其他容器模型。本文將為大家詳細(xì)講講字典的使用教程,需要的可以參考一下
    2022-09-09
  • python 進(jìn)制轉(zhuǎn)換 int、bin、oct、hex的原理

    python 進(jìn)制轉(zhuǎn)換 int、bin、oct、hex的原理

    這篇文章主要介紹了python 進(jìn)制轉(zhuǎn)換 int、bin、oct、hex的原理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • python中實(shí)現(xiàn)迭代器(iterator)的方法示例

    python中實(shí)現(xiàn)迭代器(iterator)的方法示例

    我們經(jīng)常需要遍歷一個(gè)對(duì)象中的元素,在Python中這種功能是通過迭代器來實(shí)現(xiàn)的。下面這篇文章主要給大家介紹了python中實(shí)現(xiàn)迭代器(iterator)的方法示例,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • 詳解python中l(wèi)ist的使用

    詳解python中l(wèi)ist的使用

    這篇文章主要介紹了python中l(wèi)ist的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • python?密碼驗(yàn)證(滑塊驗(yàn)證)

    python?密碼驗(yàn)證(滑塊驗(yàn)證)

    滑塊密碼在很多登錄界面都可以看到,本文主要介紹了python?密碼驗(yàn)證(滑塊驗(yàn)證),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Python實(shí)現(xiàn)簡單的代理服務(wù)器

    Python實(shí)現(xiàn)簡單的代理服務(wù)器

    這篇文章主要介紹了Python實(shí)現(xiàn)簡單的代理服務(wù)器,可實(shí)現(xiàn)代理服務(wù)器基本的包轉(zhuǎn)發(fā)功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07

最新評(píng)論