First my final goal: given a point cloud, downsample it, and visualize the resultings voxels (i.e. 1 if containing any point, 0 otherwise) in an efficient way, using Python-VTK. I already succeeded in doing it with my own downsampling code, and a voxel visualization mechanism crafted from vtkUnstructuredGrid, vtkVoxel and vtkPoints. However I have the strong feeling that there must be a more efficient way of doing it, and from what I read I gather that a vtkImageData, rendered with vtkMarchingCubes might work well.<br>
<br>I tried really hard to build an understanding of those objects by triangulating the many pieces of code and information I gathered everywhere, but I just can't make it this time. The toy model I'm trying to first build is a simple 3x3x3 grid where the 3 diagonal voxels would be "on":<br>
<br><font face="'courier new', monospace">import vtk<br>n = 3<br>grid = vtk.vtkImageData()<br>grid.SetDimensions(n, n, n)<br>grid.SetOrigin(0, 0, 0)<br>grid.SetSpacing(1, 1, 1)<br></font><br>From there, I tried two things: first, to set the desired voxels explicitly:<div>
<br></div><div><div><font face="'courier new', monospace">grid.SetScalarComponentFromFloat(0,0,0,0,1)</font></div><div><font face="'courier new', monospace">grid.SetScalarComponentFromFloat(1,1,1,0,1)</font></div>
<div><font face="'courier new', monospace">grid.SetScalarComponentFromFloat(2,2,2,0,1)</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="arial, helvetica, sans-serif">The other way I tried is to set the scalar field:</font></div>
<div><font face="arial, helvetica, sans-serif"><br></font></div><div><div style="font-family:'courier new',monospace">scalars = vtk.vtkUnsignedShortArray()</div><div style="font-family:'courier new',monospace">
for i in range(n):</div><div style="font-family:'courier new',monospace"> for j in range(n):</div><div style="font-family:'courier new',monospace"> for k in range(n):</div><div style="font-family:'courier new',monospace">
scalars.InsertNextTuple1(1 if i == j == k else 0)</div><div style="font-family:'courier new',monospace">grid.GetPointData().SetScalars(scalars)</div><div style="font-family:'courier new',monospace">
<br></div><div><font face="arial, helvetica, sans-serif">But then when I try to render the resulting grid with marching cubes, it always yields some strange shape, very far from my goal:</font></div></div><div><font face="arial, helvetica, sans-serif"><br>
</font></div><div><div><font face="'courier new', monospace">surface = vtk.vtkMarchingCubes()</font></div><div><font face="'courier new', monospace">surface.SetInput(grid)</font></div><div><font face="'courier new', monospace">surface.SetValue(0, 0.5)</font></div>
<div><font face="'courier new', monospace">mapper = vtk.vtkPolyDataMapper()</font></div><div><font face="'courier new', monospace">mapper.SetInputConnection(surface.GetOutputPort())</font></div><div><font face="'courier new', monospace">actor = vtk.vtkActor()</font></div>
<div><font face="'courier new', monospace">actor.SetMapper(mapper)</font></div><div><font face="'courier new', monospace">ren = vtk.vtkRenderer()</font></div><div><font face="'courier new', monospace">renWin = vtk.vtkRenderWindow()</font></div>
<div><font face="'courier new', monospace">renWin.AddRenderer(ren)</font></div><div><font face="'courier new', monospace">iren = vtk.vtkRenderWindowInteractor()</font></div><div><font face="'courier new', monospace">iren.SetRenderWindow(renWin)</font></div>
<div><font face="'courier new', monospace">ren.AddActor(actor)</font></div><div><font face="'courier new', monospace">iren.Initialize()</font></div><div><font face="'courier new', monospace">renWin.Render()</font></div>
<div><font face="'courier new', monospace">iren.Start()</font></div><div style="font-family:arial,helvetica,sans-serif"><br></div></div><div>I'd greatly appreciate any help, thanks in advance.</div></div><div>
<br></div><div>Christian</div><div><br></div>