微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 vedo 中的薄板变形来扭曲体积网格

如何解决使用 vedo 中的薄板变形来扭曲体积网格

我正在尝试使用来自 vedo 的薄板变形功能来扭曲体积网格。但我没有成功。我什至尝试使用表面网格进行调试,但这也不起作用。以下是 vedo 提供的原始示例。

"""Warp the tip of a mesh using Thin Plate Splines.
Red points stay fixed while a single point in space
moves as the arrow indicates. """
from vedo import *


mesh = Mesh(dataurl+"man.vtk").color('w').linewidth(0.1)

# a heavily decimated copy
meshdec = mesh.clone().triangulate().decimate(N=200)

sources = [[0.9,0.0,0.2]]  # this point moves
targets = [[1.2,0.4]]  # to this.
arrow = Arrow(sources[0],targets[0])

for pt in meshdec.points():
    if pt[0] < 0.3:          # these pts don't move
        sources.append(pt)   # source = target
        targets.append(pt)   #

warp = mesh.clone().thinPlateSpline(sources,targets)
warp.c("blue",0.3).linewidth(0)

apts = Points(sources).c("red")

show(mesh,arrow,warp,apts,__doc__,viewup="z",axes=1)

以下是我所做的一些试验改编。对表面和体积网格都尝试了这些。

试用 1 using decimated mesh

from vedo import *
import numpy as np
import scipy.io
import os
import sys
import csv

meshfile = "C:\\..\\MyVirtMean.vtk";
sourcefile = "C:\\..\\MyVirtMean_meanSurfNodes.csv";
targetfile = "C:\\..\\virtShapeGeneration.mat";
matvariable = "newShape";
Sources = []
Targets = []

mesh = Mesh(meshfile).color('w').linewidth(0.1) # This is the mean volumetric mesh

# a heavily decimated copy
meshdec = mesh.clone().triangulate().decimate(N=200)

# Collecting mean surface point data from csv file
with open(sourcefile) as csvDataFile:
    csvReader = csv.reader(csvDataFile)
    for row in csvReader:
        Sources.append(row)
Sources = np.array(Sources)
Sources = Sources.astype(np.float)
length = int(np.size(Sources)/3)
Sources = list(Sources.reshape(length,3)) # ?x3 array

# Collecting virtual subjects point data from .mat file
Targets = scipy.io.loadmat(targetfile)[matvariable][0]
length = int(np.size(Targets)/3)
Targets = list(Targets.reshape(length,3)) # ?x3 array
    
# 
arrow = Arrow(Sources[0],Targets[0])

for pt in meshdec.points():
    if pt[0] < 0.3:          # these pts don't move
        Sources.append(pt)   # source = target
        Targets.append(pt)   #

warp = mesh.clone().thinPlateSpline(Sources,Targets) 
warp.c("blue",0.3).linewidth(0)

apts = Points(Sources).c("red")

show(mesh,axes=1)

试用 2 using the full mesh

from vedo import *
import numpy as np
import scipy.io
import os
import sys
import csv

meshfile = "C:\\..\\MyVirtMean.vtk"
sourcefile = "C:\\..\\MyVirtMean_meanSurfNodes.csv"
targetfile = "C:\\..\\virtShapeGeneration.mat"
matvariable = "newShape";
Sources = []
Targets = []

mesh = Mesh(meshfile).color('w').linewidth(0.1)
with open(sourcefile) as csvDataFile:
    csvReader = csv.reader(csvDataFile)
    for row in csvReader:
        Sources.append(row)
Sources = np.array(Sources)
Sources = Sources.astype(np.float)
length = int(np.size(Sources)/3)
Sources = list(Sources.reshape(length,Targets[0])

for pt in mesh.points():
    if pt[0] < 0.3:          # these pts don't move
        Sources.append(pt)   # source = target
        Targets.append(pt)   #

warp = mesh.clone().thinPlateSpline(Sources,axes=1)

主要是内核在 warp 命令处冻结。在某些情况下,我也有内核死掉并重新启动。我怀疑我在定义来源和目标时做错了什么,但我不确定。
I'm using Python 3.7.3 64-bit in Spyder 4.1.5 (Windows 10).

解决方法

我的代码有两个问题。正如预期的那样,问题在于源和目标的定义方式。

  1. 必须使用 .tolist() 方法创建源和目标。

  2. 从 ( .mat ) 文件导入的 Targets 数组必须使用

    以类似 Fortran 的索引顺序重新整形

    Targets = (Targets.reshape((length,3),order='F'))
    Targets = Targets.tolist()

另一点是我不得不使用更少数量的源和目标
for x in range(0,len(Targets),50):
targets.append(Targets[x])

结果可以找到through this link

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。