<div dir="ltr"><div><div>Great David, thanks again.<br></div>The <span style="font-size:12.8px">SetNumberOfScalar<wbr>Components(3) was indeed a residual of a not-careful-enough copy-paste from another part of the code.<br></span></div><div><span style="font-size:12.8px">Everything's working now, and thanks for the optimisation tips !<br></span></div><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><br><div class="gmail_quote">2017-07-10 14:48 GMT+02:00 David Gobbi <span dir="ltr"><<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi <span style="font-size:12.8px">Clément,</span><div><br></div><div>The "number of scalar components" should be 1 for your data:</div><div><br></div><div><span style="font-size:12.8px"> dataImporter.</span><span style="font-size:12.8px">SetNumberOfScalar<wbr>Components(1)</span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">There are a couple places where you can improve efficiency:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"> # directly create an array of the desired type</span></div><div><span style="font-size:12.8px"> npa=np.zeros((150,150,150), np.uint8)</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"> # directly pass the array to VTK (without tostring())</span></div><div><span style="font-size:12.8px"> </span><span style="font-size:12.8px">dataImporter.<wbr>CopyImportVoidPointer(npa, npa.size)</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Cheers,</span></div><div><span style="font-size:12.8px"> - David</span></div><div><div class="h5"><div><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jul 10, 2017 at 2:29 AM, Clément D. <span dir="ltr"><<a href="mailto:clement.douarre@gmail.com" target="_blank">clement.douarre@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hello all,<br><br></div><div>I am trying to use the Marching Cubes algorithm to show a 3D surface of a 3D image given by a Python (numpy array).<br></div><div>I thought I had it all mapped out, things were going great with arrays of small size (generated and rendered 3D isosurfaces), but now that I'm working with arrays of slightly bigger size, the programm crashes with Python error 11 SIGSEGV which means seg fault. What worries me a bit is that the array I'm using is not that big, 150*150*150, and I have a fairly recent computer who shouldn't have any problem handling it.<br></div><div><br>Code MWE is below (in Python)<br></div><div><br></div><div>==============================<wbr>============= Code snippet =======================<br></div><div><br>import numpy as np<br>import vtk<br><br># Creating a numpy array representing a 3D image<br>npa=np.zeros((150,150,150)).as<wbr>type(np.uint8)<br>npa[100:102,100:102,100:102]=2<wbr>55<br><br># Importing it as object vtk (based on vtk example <a href="http://www.vtk.org/Wiki/VTK/Examples/Python/vtkWithNumpy" target="_blank">http://www.vtk.org/Wiki/VTK/Ex<wbr>amples/Python/vtkWithNumpy</a>)<br>dataImporter = vtk.vtkImageImport()<br>dataString = npa.tostring()<br>[hh, ww, dd] = npa.shape<br>dataImporter.CopyImportVoidPoi<wbr>nter(dataString, len(dataString))<br>dataImporter.SetDataScalarType<wbr>ToUnsignedChar()<br>dataImporter.SetNumberOfScalar<wbr>Components(3)<br>dataImporter.SetDataExtent(0, dd - 1, 0, ww - 1, 0, hh - 1)<br>dataImporter.SetWholeExtent(0, dd - 1, 0, ww - 1, 0, hh - 1)<br>dataImporter.Update()<br>vol = dataImporter.GetOutputPort()<br></div># (print(dataImporter.GetOutput(<wbr>).GetClassName()) yields "vtk.ImageData")<br><div><div><br># Use MarchingCubes to generate iso-surface<br>surface = vtk.vtkMarchingCubes();<br>surface.SetInputConnection(vol<wbr>);<br>surface.ComputeNormalsOn();<br>surface.ComputeScalarsOn();<br>isoValue = 128<br>surface.SetValue(0, isoValue);<br><br></div><div>#This line causes segfault if npa is "too big"<br></div><div>surface.Update();<br><br></div><div># .... rest of pipeline<br># decimator = vtk.vtkDecimatePro(); ....<br># smoother = vtk.vtkSmoothPolyDataFilter(); ....<br># connectivityFilter = vtk.vtkPolyDataConnectivityFil<wbr>ter(); ....<br># mesh = vtk.vtkPolyData(); ....<br># mapper = vtk.vtkPolyDataMapper(); ....<br># actor = vtk.vtkActor(); ....<br><br><div><br></div><div>==============================<wbr>============= End of code snippet =======================<br></div><br><br><br><div>So I have a couple of questions here :<br></div><div>1/ Is this a problem of input for MarchingCubes being too big ? Or is this segfault indicating something else ?<br></div>- arguments for the former : Reducing the size of npa in this code makes it work<br></div><div>- arguments for the latter : This code I'm showing is a MWE for my code which is more complicated, and in my code, this code portion fails with a large array, but succeeds if I reduce in size *another* array which has nothing to do with MarchingCubes() (again, that other array is not that big, so I don't think I'm reaching the limits of Python memory or anything like that...)<br><br></div><div>2/ Am I doing the whole import numpy thing correctly ? I've based myself on the vtk example, but I am unsure of whether it is the recommended way for my case or if it was case specific<br><br></div><div><br></div><div>Thanks everyone,<br></div><div>Clément</div></div></div></blockquote></div></div></div></div></div>
</blockquote></div><br></div>