ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vtk编辑点

2021-10-06 11:33:25  阅读:136  来源: 互联网

标签:point vtk contourWidget 编辑 path line pts


import vtk
from vtkplotter import *
from vtk.util.numpy_support import vtk_to_numpy
import numpy as np
import os


def get_gum_line_pts(gum_line_path):
    """
    读取牙龈线文件,pts格式
    """
    f = open(gum_line_path)
    pts = []

    is_generate_by_streamflow = False  # 是否由前台界面生成

    for num, line in enumerate(f):
        if 0 == num and line.strip() == "BEGIN_0":
            is_generate_by_streamflow = True
            continue
        if line.strip() == "BEGIN" or line.strip() == "END":
            continue
        if is_generate_by_streamflow:
            line = line.strip()
            if line == "END_0":
                pts.append(pts[0])
            else:
                splitted_line = line.split()
                point = [float(i) for i in splitted_line][:3]  # 只取点坐标,去掉法向量
                assert len(point) == 3, "点的坐标为x,y,z"
                pts.append(point)
        else:
            line = line.strip()
            splitted_line = line.split()
            point = [float(i) for i in splitted_line]
            assert len(point) == 3, "点的坐标为x,y,z"
            pts.append(point)

    f.close()
    pts = np.asarray(pts)
    return pts


path = r'E:\code\Test_Hgapi\AI_HTTP_gumline_simplify\B14059_U1@.stl'
pts_path = r'E:\code\Test_Hgapi\AI_HTTP_gumline_simplify\B14059_U1@_node.pts'
save_pts_path = os.path.splitext(pts_path)[0] + "_node.pts"
pts = get_gum_line_pts(pts_path)
with open(save_pts_path, "w") as f:
    f.write("BEGIN\n")
    for i in range(0, len(pts), 10):
        pt = pts[i]
        f.write(str(pt[0]) + " " + str(pt[1]) + " " + str(pt[2]) + "\n")
    f.write("END\n")

r = vtk.vtkSTLReader()
r.SetFileName(path)
r.Update()

arr = []
f1 = open(save_pts_path)

for i1 in f1.readlines()[1:-1]:

    b1 = i1.strip('\n')
    s1 = b1.split(' ')
    brr = []
    for j1 in s1:
        l1 = float(j1)
        brr.append(l1)
    arr.append(brr)
print(len(arr))


vtk_Points = vtk.vtkPoints()
vertices = vtk.vtkCellArray()

for v in arr:
    ids = vtk_Points.InsertNextPoint(v)
    vertices.InsertNextCell(1)
    vertices.InsertCellPoint(ids)

TemplateF = vtk.vtkPolyData()
TemplateF.SetPoints(vtk_Points)
TemplateF.SetVerts(vertices)
TemplateF.Modified()

ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(r.GetOutput())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetOpacity(0.5)

contourRep = vtk.vtkOrientedGlyphContourRepresentation()
contourRep.GetLinesProperty().SetColor(0, 0, 1)
contourRep.GetLinesProperty().SetLineWidth(5)
contourRep.GetProperty().SetPointSize(5)

contourWidget = vtk.vtkContourWidget()
contourWidget.SetInteractor(iren)
contourWidget.SetRepresentation(contourRep)
contourWidget.On()
contourWidget.Initialize(TemplateF, 1)
contourWidget.CloseLoop()

pointPlacer = vtk.vtkPolygonalSurfacePointPlacer()
pointPlacer.AddProp(actor)
pointPlacer.GetPolys().AddItem(r.GetOutput())
pointPlacer.SnapToClosestPointOn()
contourRep.SetPointPlacer(pointPlacer)

ren.AddActor(actor)
ren.ResetCamera()
renWin.Render()

iren.Initialize()
iren.Start()

pro_model = load(path)

line_point = contourWidget.GetRepresentation().GetContourRepresentationAsPolyData()
points = vtk_to_numpy(line_point.GetPoints().GetData())

with open(path[0:-4] + '_1.pts', 'w') as fr:
    b = 'BEGIN_0' + '\n'
    e = 'END_0'

    fr.write(b)
    for i in points:
        ii = pro_model.closestPoint(i)
        for j in range(len(ii)):
            if j <= 1:
                fr.write(str(ii[j]) + ' ')

            else:
                # i[j] -= 0.1
                fr.write(str(ii[j]) + '\n')
    fr.write(e)
    fr.close()

    print('PTS File has saved....')

标签:point,vtk,contourWidget,编辑,path,line,pts
来源: https://www.cnblogs.com/xiaxuexiaoab/p/15074843.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有