<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" dir="ltr" style="font-size: 12pt; color: rgb(0, 0, 0); font-family: Calibri, Helvetica, sans-serif, EmojiFont, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols;">
<p style="margin-top:0; margin-bottom:0">Hi David,</p>
<p style="margin-top:0; margin-bottom:0"><br>
</p>
<p style="margin-top:0; margin-bottom:0">thank you very much again for the hints.</p>
<p style="margin-top:0; margin-bottom:0"><br>
</p>
<p style="margin-top:0; margin-bottom:0"></p>
<p style="margin-top:0; margin-bottom:0">As you said, i removed the initialization code for the binaryLabelMap and replaced the DeepCopy with a call to
<i>imageStencilToImage->SetOutput</i> and it works perfectly!</p>
<p style="margin-top:0; margin-bottom:0"><br>
</p>
<p style="margin-top:0; margin-bottom:0">The resulting code for the whole polygon mesh voxelization from this code review iteration is now simply:</p>
<p style="margin-top:0; margin-bottom:0"><br>
</p>
<p style="margin-top:0; margin-bottom:0"><br>
</p>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">bool ConvertPolyDataToBinaryLabelMap(vtkSmartPointer<vtkPolyData> closedSurfacePolyData, </span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">                                     vtkSmartPointer<vtkImageData> binaryLabelMap)</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">{</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  <span style="color: rgb(0, 111, 201);">
<i>// Check for consistency</i></span></span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  if (closedSurfacePolyData->GetNumberOfPoints() < 2 || closedSurfacePolyData->GetNumberOfCells() < 2)</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  {</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">    std::cout <<
<span style="color: rgb(75, 165, 36);">"Convert: Cannot create binary labelmap from surface with number of points: "</span></span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">        << closedSurfacePolyData->GetNumberOfPoints() <<
<span style="color: rgb(75, 165, 36);">" and number of cells:<span style="color: rgb(75, 165, 36);">
</span></span><span style="color: rgb(75, 165, 36);">"</span> </span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">        << closedSurfacePolyData->GetNumberOfCells() << std::endl;</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">      </span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">      return false;</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  }</span></b></div>
<div><b><br>
</b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  <span style="color: rgb(0, 111, 201);">
<i>// Compute polydata normals</i></span></span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  vtkNew<vtkPolyDataNormals> normalFilter;</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  normalFilter->SetInputData(closedSurfacePolyData);</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  normalFilter->ConsistencyOn();</span></b></div>
<div><b><br>
</b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  <span style="color: rgb(0, 111, 201);">
<i>// Make sure that we have a clean triangle polydata</i></span></span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  vtkNew<vtkTriangleFilter> triangle;</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  triangle->SetInputConnection(normalFilter->GetOutputPort());</span></b></div>
<div><b><br>
</b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  <span style="color: rgb(0, 111, 201);">
<i>// Convert to triangle strip</i></span></span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  vtkNew<vtkStripper> stripper;</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  stripper->SetInputConnection(triangle->GetOutputPort());</span></b></div>
<div><b><br>
</b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  <span style="color: rgb(0, 111, 201);">
<i>// Convert polydata to stencil</i></span></span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  vtkNew<vtkPolyDataToImageStencil> polyDataToImageStencil;</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  polyDataToImageStencil->SetInputConnection(stripper->GetOutputPort());</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  polyDataToImageStencil->SetOutputSpacing(binaryLabelMap->GetSpacing());</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  polyDataToImageStencil->SetOutputOrigin(binaryLabelMap->GetOrigin());</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  polyDataToImageStencil->SetOutputWholeExtent(binaryLabelMap->GetExtent());</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  polyDataToImageStencil->Update();</span></b></div>
<div><b><br>
</b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  <span style="color: rgb(0, 111, 201);">
<i>// Convert stencil to image</i></span></span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  vtkNew<vtkImageStencilToImage> imageStencilToImage;</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  imageStencilToImage->SetInputConnection(polyDataToImageStencil->GetOutputPort());</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  imageStencilToImage->SetOutsideValue(0);</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  imageStencilToImage->SetInsideValue(1);</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  imageStencilToImage->SetOutput(binaryLabelMap);</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  imageStencilToImage->Update();</span></b></div>
<div><b><br>
</b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">  return true;</span></b></div>
<div><b><span style="font-size:9pt; font-family:"Courier New",monospace">}</span></b></div>
<div><br>
</div>
<div>As always i attached the enhanced example code and the CMakeLists.</div>
<div><br>
</div>
<div><br>
</div>
<div>The result for the voxelization with and without opacity turned on:</div>
<div><br>
</div>
<div><img size="64281" style="height: 420.473px; width: 374px; user-select: none;" id="296326_rs|3" tabindex="0" width="374" height="420" data-outlook-trace="F:1|T:1" src="cid:a98f252d-e72d-4809-a32f-404b6d8007b7"><img size="281975" style="height: 420.727px; width: 356px; user-select: none;" id="54092_rs|3" tabindex="0" width="356" height="421" data-outlook-trace="F:1|T:1" src="cid:cff0c507-5d76-4450-a3dc-3cf160cb50fd"><br>
<br>
<br>
</div>
<div>And yes you were right with the Eiffel tower mesh!</div>
<div><br>
</div>
<div>I reoriented the polygon orientation / normals (but with meshlab not vtk) and now the voxelization code above also works with the Eiffel tower:</div>
<div><br>
</div>
<div><img size="80360" contenttype="image/png" id="276076_rs|3" style="user-select: none; height: 386.526px; width: 204px;" tabindex="0" width="204" height="387" data-outlook-trace="F:1|T:1" src="cid:1fe1d519-2656-4e2d-899c-7b685f711898"></div>
<div><br>
</div>
<div>Maybe there are some small defects (broken normals, wrongly oriented polygons etc.) in the alien queen mesh as well. </div>
<div><br>
</div>
<div>Here is a closeup:</div>
<div><br>
</div>
<div><img size="65454" contenttype="image/jpeg" id="488733_rs|3" style="user-select: none; height: 413.506px; width: 751px;" tabindex="0" width="751" height="414" data-outlook-trace="F:1|T:1" src="cid:115412aa-d4c4-4377-8e33-5fa96e974b09"><br>
<br>
<img size="132745" contenttype="image/jpeg" id="img907647" style="max-width: 99.9%; user-select: none;" tabindex="0" data-outlook-trace="F:1|T:1" src="cid:0d0e1d24-29f9-4f08-8acc-12ee09a34973"><br>
<img size="28658" contenttype="image/jpeg" id="img63468" tabindex="0" style="max-width: 99.9%; user-select: none;" data-outlook-trace="F:1|T:1" src="cid:b5e05242-4131-4648-ab72-72e9f648438c"></div>
<div><br>
</div>
<div>It could be that these small dark polygons at the backside of the nails of the claw are dark because their normals are broken or wrongly oriented.</div>
<div><br>
</div>
<div>Could that lead to the strange voxelization result above?</div>
<div><br>
</div>
<div><br>
</div>
<div>Cheers,</div>
<div><br>
</div>
<div>Berti</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div>PS: </div>
<div><br>
</div>
<div>Bill if you think this example could be of any use for other vtk users, then please feel free to add / modify / enhance / rip it apart / put it in the vtk github example repository. Personally i think vtk can not have enough examples. They are always life
 savers.<br>
<br>
</div>
<div><br>
</div>
<div><br>
<div style="color:rgb(0,0,0)">
<hr style="display:inline-block; width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>Von:</b> David Gobbi <david.gobbi@gmail.com><br>
<b>Gesendet:</b> Montag, 24. September 2018 04:09<br>
<b>An:</b> Berti Krüger<br>
<b>Cc:</b> Csaba Pinter; VTK Users; Bill Lorensen<br>
<b>Betreff:</b> Re: [vtkusers] Solid Voxelization with VTK</font>
<div> </div>
</div>
<meta content="text/html; charset=utf-8">
<div>
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">Hi Berti,
<div><br>
</div>
<div>You don't even have to initialize binaryLabelMap, since you don't use it as an input for anything until the DeepCopy.  Really the DeepCopy isn't necessary either, you could call imageStencilToImage->SetOutput(binaryLabelMap) before calling imageStencilToImage->Update().</div>
<div><br>
</div>
<div>Thanks for the samples, those results are very interesting.  Because of the way the algorithm works, the Z axis is immune to those bridges in the result, so you are correct that majority voting or a logical "and" might clean it up.  For the Eiffel tower,
 the polygons might be wound the wrong way (i.e. the shape is inside-out).  You could try applying vtkReverseSense.</div>
<div><br>
</div>
<div> - David</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>