Hi all,
It turned out not to be too difficult to implement an own mesh stitching routine. Thought to share this with you. It creates mesh segment that tightly combines (at least for my cases) the two input surfaces. Happy stitching!
<pre>
def extractPoints(source):
"""
Return points from a polydata as a list of tuples.
"""
points = source.GetPoints()
indices = range(points.GetNumberOfPoints())
pointAccessor = lambda i: points.GetPoint(i)
return list(map(pointAccessor, indices))
def findClosestSurfacePoint(source, point):
source = ensurePolyData(source)
locator = vtk.vtkKdTreePointLocator()
locator.SetDataSet(source)
locator.BuildLocator()
pId = locator.FindClosestPoint(point)
return pId
# Extract points along the edge line (in correct order).
# The following further assumes that the polyline has the
# same orientation (clockwise or counterclockwise).
points1 = extractPoints(edge1)
points2 = extractPoints(edge2)
n1 = len(points1)
n2 = len(points2)
# Prepare result containers.
# Variable points concatenates points1 and points2.
# Note: all indices refer to this targert container!
points = vtk.vtkPoints()
cells = vtk.vtkCellArray()
points.SetNumberOfPoints(n1+n2)
for i, p1 in enumerate(points1):
points.SetPoint(i, p1)
for i, p2 in enumerate(points2):
points.SetPoint(i+n1, p2)
# The following code stitches the curves edge1 with (points1) and
# edge2 (with points2) together based on a simple growing scheme.
# Pick a first stitch between points1[0] and its closest neighbor
# of points2.
i1Start = 0
i2Start = findClosestSurfacePoint(edge2, points1[i1Start])
i2Start += n1 # offset to reach the points2
# Initialize
i1 = i1Start
i2 = i2Start
p1 = np.asarray(points.GetPoint(i1))
p2 = np.asarray(points.GetPoint(i2))
mask = np.zeros(n1+n2, dtype=bool)
count = 0
while not np.all(mask):
count += 1
i1Candidate = (i1+1)%n1
i2Candidate = (i2-1-n1)%n2+n1
p1Candidate = np.asarray(points.GetPoint(i1Candidate))
p2Candidate = np.asarray(points.GetPoint(i2Candidate))
diffEdge12C = np.linalg.norm(p1-p2Candidate)
diffEdge21C = np.linalg.norm(p2-p1Candidate)
mask[i1] = True
mask[i2] = True
if diffEdge12C < diffEdge21C:
triangle = vtk.vtkTriangle()
triangle.GetPointIds().SetId(0,i1)
triangle.GetPointIds().SetId(1,i2)
triangle.GetPointIds().SetId(2,i2Candidate)
cells.InsertNextCell(triangle)
i2 = i2Candidate
p2 = p2Candidate
else:
triangle = vtk.vtkTriangle()
triangle.GetPointIds().SetId(0,i1)
triangle.GetPointIds().SetId(1,i2)
triangle.GetPointIds().SetId(2,i1Candidate)
cells.InsertNextCell(triangle)
i1 = i1Candidate
p1 = p1Candidate
# Add the last triangle.
i1Candidate = (i1+1)%n1
i2Candidate = (i2-1-n1)%n2+n1
if (i1Candidate <= i1Start) or (i2Candidate <= i2Start):
if i1Candidate <= i1Start:
iC = i1Candidate
else:
iC = i2Candidate
triangle = vtk.vtkTriangle()
triangle.GetPointIds().SetId(0,i1)
triangle.GetPointIds().SetId(1,i2)
triangle.GetPointIds().SetId(2,iC)
cells.InsertNextCell(triangle)
poly = vtk.vtkPolyData()
poly.SetPoints(points)
poly.SetPolys(cells)
poly.BuildLinks()
</pre>
<img src="http://vtk.1045678.n5.nabble.com/file/t341102/Screen_Shot_2018-03-07_at_10.png" border="0" class="center"/>
<br/><hr align="left" width="300" />
Sent from the <a href="http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html">VTK - Users mailing list archive</a> at Nabble.com.<br/>