title: Get depth from camera for each pixel
body: <p>I have a mesh model and, using VTK, have rendered a view of it from a given camera position (x,y,z). I can save this to an RGB image (640x480) but I also want to save a depth map where each pixel stores the value of the depth from the camera. </p>
  
  <p>I have tried using the <code>Zbuffer</code> values given by the render window by following <a href="http://vtk.org/Wiki/VTK/Examples/Cxx/Utilities/ZBuffer" rel="noreferrer">this example</a>. The problem is that the <code>Zbufer</code> only stores values in the range [0,1]. Instead I am trying to create synthetic range image, where I store the depth/distance of each pixel from the camera. Analogous to the image produced by the Kinect, I am trying to create one from a specific viewpoint of a mesh model.</p>
  
  <p><strong>EDIT</strong> - adding some code</p>
  
  <p>My current code:</p>
  
  <p><strong>Load the mesh</strong></p>
  
  <pre><code>string mesh_filename = "mesh.ply";
      vtkSmartPointer&lt;vtkPLYReader&gt; mesh_reader = read_mesh_ply(mesh_filename);
  
      vtkSmartPointer&lt;vtkPolyDataMapper&gt; mapper = vtkSmartPointer&lt;vtkPolyDataMapper&gt;::New();
      mapper-&gt;SetInputConnection(mesh_reader-&gt;GetOutputPort());
  
      vtkSmartPointer&lt;vtkActor&gt; actor = vtkSmartPointer&lt;vtkActor&gt;::New();
      actor-&gt;SetMapper(mapper);
  
      vtkSmartPointer&lt;vtkRenderer&gt; renderer =  vtkSmartPointer&lt;vtkRenderer&gt;::New();
      vtkSmartPointer&lt;vtkRenderWindow&gt; renderWindow = vtkSmartPointer&lt;vtkRenderWindow&gt;::New();
      renderWindow-&gt;AddRenderer(renderer);
      renderWindow-&gt;SetSize(640, 480);
  
      vtkSmartPointer&lt;vtkRenderWindowInteractor&gt; renderWindowInteractor = vtkSmartPointer&lt;vtkRenderWindowInteractor&gt;::New();
      renderWindowInteractor-&gt;SetRenderWindow(renderWindow);
  
      //Add the actors to the scene
      renderer-&gt;AddActor(actor);
      renderer-&gt;SetBackground(1, 1, 1);
  </code></pre>
  
  <p><strong>Create a camera and place it somewhere</strong></p>
  
  <pre><code>vtkSmartPointer&lt;vtkCamera&gt; camera = vtkSmartPointer&lt;vtkCamera&gt;::New();
  
      renderer-&gt;SetActiveCamera(camera);
      camera-&gt;SetPosition(0,0,650);
      //Render and interact
      renderWindow-&gt;Render();
  </code></pre>
  
  <p><strong>Get result from the z buffer</strong></p>
  
  <pre><code>double b = renderer-&gt;GetZ(320, 240);
  </code></pre>
  
  <p>In this example, this gives 0.999995. As the values are between [0,1] I don't know how to interpret this, as you can see I have set the camera to be 650 units away on the z-axis so I assume the z distance at this pixel (which is on the object in the rendered RGB) should be near to 650.</p>