Issue
I'm trying to use the thin plates morphing function from vedo to warp a volumetric mesh. But I have not been successful. I even tried using a surface mesh for debugging but that didn't work either. Below is the original example provided by 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.0, 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)
Below are a couple of the trial adaptations I made. These were tried for both surface and volumetric meshes.
Trial 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, arrow, warp, apts, __doc__, viewup="z", axes=1)
Trial 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,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 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, Targets)
warp.c("blue",0.3).lineWidth(0)
apts = Points(Sources).c("red")
show(mesh, arrow, warp, apts, __doc__, viewup="z", axes=1)
Mainly, the kernel is freezing at the warp command. In some cases, I've also had the kernel dying and restarting. I'm suspecting I'm doing something wrong in defining the Sources and Targets, but I'm not sure.I'm using Python 3.7.3 64-bit in Spyder 4.1.5 (Windows 10).
Solution
There were two issues with my code. As expected, the problem was in the way the Sources and Targets were defined.
Both Sources and Targets had to be created using the
.tolist()
method.The Targets array that was imported from a ( .mat ) file had to be reshaped with a Fortran-like index order using
Targets = (Targets.reshape((length,3), order='F'))
Targets = Targets.tolist()
Another point is that I had to use a reduced number of Sources and Targets
for x in range(0, len(Targets), 50):
targets.append(Targets[x])
The result can be found through this link.
Answered By - Amin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.