[vtkusers] Smoothing a meshgrid

Michka Popoff michkapopoff at gmail.com
Wed Jan 15 17:54:53 EST 2014


Hi

I am trying to smooth a meshgrid to get a nice terrain map like this :

http://i18.photobucket.com/albums/b117/666villa/RCT3/Landscape004.jpg
http://marc.jacquier.free.fr/tuts/trace_while/h_f.gif
http://research.microsoft.com/~hoppe/proj/svdlod/

So with a roundish/smoothed aspect.

I am building my terrain with triangles for the moment, from a dataset of x, y, z coordinates.

I tried the vtkSmoothPolyDataFilter but no smoothing is done. Is this the right filter to use ? What other filters could I use on my meshgrid to obtain the wanted aspect ?

I added a screenshot of the original terrain and the smoothed one (which in this case is not) at the end.

Any help would be appreciated :)

Michka




#!/usr/bin/env python

import vtk
import random
import numpy

colors = vtk.vtkUnsignedCharArray()
colors.SetNumberOfComponents(3)

size = 32

topography = numpy.zeros([size, size])
for i in range(size):
    for j in range(size):
        topography[i][j] = random.randrange(0, 100)

points = vtk.vtkPoints()

triangles = vtk.vtkCellArray()
mult = 100.0

count = 0

for i in range(size-1):
    for j in range(size-1):
        
        z1 = topography[i][j]
        z2 = topography[i][j+1]
        z3 = topography[i+1][j]
        
        # Triangle 1
        points.InsertNextPoint(i*mult, j*mult, z1)
        points.InsertNextPoint(i*mult, (j+1)*mult, z2)
        points.InsertNextPoint((i+1)*mult, j*mult, z3)
        
        triangle = vtk.vtkTriangle()
        triangle.GetPointIds().SetId ( 0, count )
        triangle.GetPointIds().SetId ( 1, count + 1 )
        triangle.GetPointIds().SetId ( 2, count + 2 )
        
        triangles.InsertNextCell ( triangle )
        
        z1 = topography[i][j+1]
        z2 = topography[i+1][j+1]
        z3 = topography[i+1][j]
        
        # Triangle 2
        points.InsertNextPoint(i*mult, (j+1)*mult, z1)
        points.InsertNextPoint((i+1)*mult, (j+1)*mult, z2)
        points.InsertNextPoint((i+1)*mult, j*mult, z3)
        
        triangle = vtk.vtkTriangle()
        triangle.GetPointIds().SetId ( 0, count + 3)
        triangle.GetPointIds().SetId ( 1, count + 4 )
        triangle.GetPointIds().SetId ( 2, count + 5 )
        
        count += 6
        
        triangles.InsertNextCell ( triangle )
        
        # Add some color
        r = [int(i/float(size)*255),int(j/float(size)*255),0]
        colors.InsertNextTupleValue(r)
        colors.InsertNextTupleValue(r)
        colors.InsertNextTupleValue(r)
        colors.InsertNextTupleValue(r)
        colors.InsertNextTupleValue(r)
        colors.InsertNextTupleValue(r)

# Create a polydata object
trianglePolyData = vtk.vtkPolyData()

# Add the geometry and topology to the polydata
trianglePolyData.SetPoints (points)
trianglePolyData.GetPointData().SetScalars(colors)
trianglePolyData.SetPolys (triangles)

# Try to smooth the meshgrid (Does nothing ?)
smooth = vtk.vtkSmoothPolyDataFilter()
smooth.SetInputData(trianglePolyData)
smooth.SetRelaxationFactor(0.001)
smooth.SetNumberOfIterations(20)

# Create a mapper and actor for normal dataset
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(trianglePolyData)
actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Create a mapper and actor for smoothed dataset
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(smooth.GetOutputPort())
actor_filtered = vtk.vtkActor()
actor_filtered.SetMapper(mapper)
actor_filtered.SetPosition(4000, 0, 0)

# Visualize
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

renderer.AddActor(actor)
renderer.AddActor(actor_filtered)
renderer.SetBackground(0,0,0) # Background color white

renderWindow.SetSize(800, 800)
renderWindow.Render()
renderWindowInteractor.Start() 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140115/16445176/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smoothing.png
Type: image/png
Size: 83557 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140115/16445176/attachment-0001.png>


More information about the vtkusers mailing list